Merge pull request #1854 from unknownbrackets/audio-minor

Improve audio blocking and some sceAudio functions
This commit is contained in:
Henrik Rydgård committed 2013-05-19 13:50:44 -07:00
commit 1b3079142b
6 files changed
+143 -96

No files matched your search

+5
View File
@@ -276,6 +276,11 @@ template<u32 func(u32, int, int)> void WrapU_UII() {
RETURN(retval);
}
template<u32 func(u32, int, int, u32)> void WrapU_UIIU() {
u32 retval = func(PARAM(0), PARAM(1), PARAM(2), PARAM(3));
RETURN(retval);
}
template<int func(u32, int, int, u32, u32)> void WrapI_UIIUU() {
u32 retval = func(PARAM(0), PARAM(1), PARAM(2), PARAM(3), PARAM(4));
RETURN(retval);
+61 -36
View File
@@ -29,6 +29,7 @@
#include "FixedSizeQueue.h"
#include "Common/Thread.h"
// Should be used to lock anything related to the outAudioQueue.
std::recursive_mutex section;
int eventAudioUpdate = -1;
@@ -48,7 +49,7 @@ const int hostAttemptBlockSize = 512;
const int audioIntervalUs = (int)(1000000ULL * hwBlockSize / hwSampleRate);
const int audioHostIntervalUs = (int)(1000000ULL * hostAttemptBlockSize / hwSampleRate);
// High and low watermarks, basically.
// High and low watermarks, basically. For perfect emulation, the correct values are 0 and 1, respectively.
// TODO: Tweak
#ifdef ANDROID
const int chanQueueMaxSizeFactor = 4;
@@ -89,28 +90,27 @@ void __AudioInit()
void __AudioDoState(PointerWrap &p)
{
section.lock();
p.Do(eventAudioUpdate);
CoreTiming::RestoreRegisterEvent(eventAudioUpdate, "AudioUpdate", &hleAudioUpdate);
p.Do(eventHostAudioUpdate);
CoreTiming::RestoreRegisterEvent(eventHostAudioUpdate, "AudioUpdateHost", &hleHostAudioUpdate);
p.Do(mixFrequency);
section.lock();
outAudioQueue.DoState(p);
section.unlock();
int chanCount = ARRAY_SIZE(chans);
p.Do(chanCount);
if (chanCount != ARRAY_SIZE(chans))
{
ERROR_LOG(HLE, "Savestate failure: different number of audio channels.");
section.unlock();
return;
}
for (int i = 0; i < chanCount; ++i)
chans[i].DoState(p);
section.unlock();
p.DoMarker("sceAudio");
}
@@ -122,25 +122,38 @@ void __AudioShutdown()
u32 __AudioEnqueue(AudioChannel &chan, int chanNum, bool blocking)
{
u32 ret = 0;
section.lock();
if (chan.sampleAddress == 0)
return SCE_ERROR_AUDIO_NOT_OUTPUT;
if (chan.sampleQueue.size() > chan.sampleCount*2*chanQueueMaxSizeFactor) {
// Block!
if (blocking) {
chan.waitingThread = __KernelGetCurThread();
// WARNING: This changes currentThread so must grab waitingThread before (line above).
__KernelWaitCurThread(WAITTYPE_AUDIOCHANNEL, (SceUID)chanNum, 0, 0, false, "blocking audio waited");
// Fall through to the sample queueing, don't want to lose the samples even though
// we're getting full.
u32 ret = chan.sampleCount;
if (chan.sampleAddress == 0) {
// For some reason, multichannel audio lies and returns the sample count here.
if (chanNum == PSP_AUDIO_CHANNEL_SRC || chanNum == PSP_AUDIO_CHANNEL_OUTPUT2) {
ret = 0;
}
else
{
chan.waitingThread = 0;
}
// If there's anything on the queue at all, it should be busy, but we try to be a bit lax.
if (chan.sampleQueue.size() > chan.sampleCount * 2 * chanQueueMaxSizeFactor || chan.sampleAddress == 0) {
if (blocking) {
// TODO: Regular multichannel audio seems to block for 64 samples less? Or enqueue the first 64 sync?
int blockSamples = chan.sampleQueue.size() / 2 / chanQueueMinSizeFactor;
AudioChannelWaitInfo waitInfo = {__KernelGetCurThread(), blockSamples};
chan.waitingThreads.push_back(waitInfo);
// Also remember the value to return in the waitValue.
__KernelWaitCurThread(WAITTYPE_AUDIOCHANNEL, (SceUID)chanNum + 1, ret, 0, false, "blocking audio waited");
// Fall through to the sample queueing, don't want to lose the samples even though
// we're getting full. The PSP would enqueue after blocking.
} else {
// Non-blocking doesn't even enqueue, but it's not commonly used.
return SCE_ERROR_AUDIO_CHANNEL_BUSY;
}
}
if (chan.sampleAddress == 0) {
return ret;
}
if (chan.format == PSP_AUDIO_FORMAT_STEREO)
{
const u32 totalSamples = chan.sampleCount * 2;
@@ -161,8 +174,6 @@ u32 __AudioEnqueue(AudioChannel &chan, int chanNum, bool blocking)
for (u32 i = 0; i < totalSamples; i++)
chan.sampleQueue.push((s16)Memory::Read_U16(chan.sampleAddress + sizeof(s16) * i));
}
ret = chan.sampleCount;
}
else if (chan.format == PSP_AUDIO_FORMAT_MONO)
{
@@ -173,10 +184,7 @@ u32 __AudioEnqueue(AudioChannel &chan, int chanNum, bool blocking)
chan.sampleQueue.push(sample);
chan.sampleQueue.push(sample);
}
ret = chan.sampleCount;
}
section.unlock();
return ret;
}
@@ -188,6 +196,31 @@ static inline s16 clamp_s16(int i) {
return i;
}
inline void __AudioWakeThreads(AudioChannel &chan, int step)
{
u32 error;
for (size_t w = 0; w < chan.waitingThreads.size(); ++w)
{
AudioChannelWaitInfo &waitInfo = chan.waitingThreads[w];
waitInfo.numSamples -= hwBlockSize;
// If it's done (there will still be samples on queue) and actually still waiting, wake it up.
if (waitInfo.numSamples <= 0 && __KernelGetWaitID(waitInfo.threadID, WAITTYPE_AUDIOCHANNEL, error) != 0)
{
// DEBUG_LOG(HLE, "Woke thread %i for some buffer filling", waitingThread);
u32 ret = __KernelGetWaitValue(waitInfo.threadID, error);
__KernelResumeThreadFromWait(waitInfo.threadID, ret);
chan.waitingThreads.erase(chan.waitingThreads.begin() + w--);
}
}
}
void __AudioWakeThreads(AudioChannel &chan)
{
__AudioWakeThreads(chan, 0x7FFFFFFF);
}
// Mix samples from the various audio channels into a single sample queue.
// This single sample queue is where __AudioMix should read from. If the sample queue is full, we should
// just sleep the main emulator thread a little.
@@ -204,6 +237,8 @@ void __AudioUpdate()
{
if (!chans[i].reserved)
continue;
__AudioWakeThreads(chans[i], hwBlockSize);
if (!chans[i].sampleQueue.size()) {
// ERROR_LOG(HLE, "No queued samples, skipping channel %i", i);
continue;
@@ -225,17 +260,6 @@ void __AudioUpdate()
break;
}
}
if (chans[i].sampleQueue.size() < chans[i].sampleCount * 2 * chanQueueMinSizeFactor)
{
// Ask the thread to send more samples until next time, queue is being drained.
if (chans[i].waitingThread) {
SceUID waitingThread = chans[i].waitingThread;
chans[i].waitingThread = 0;
// DEBUG_LOG(HLE, "Woke thread %i for some buffer filling", waitingThread);
__KernelResumeThreadFromWait(waitingThread, chans[i].sampleCount);
}
}
}
if (g_Config.bEnableSound) {
@@ -266,6 +290,7 @@ void __AudioSetOutputFrequency(int freq)
}
// numFrames is number of stereo frames.
// This is called from *outside* the emulator thread.
int __AudioMix(short *outstereo, int numFrames)
{
// TODO: if mixFrequency != the actual output frequency, resample!
+2
View File
@@ -29,5 +29,7 @@ void __AudioSetOutputFrequency(int freq);
// May return SCE_ERROR_AUDIO_CHANNEL_BUSY if buffer too large
u32 __AudioEnqueue(AudioChannel &chan, int chanNum, bool blocking);
void __AudioWakeThreads(AudioChannel &chan, int step);
void __AudioWakeThreads(AudioChannel &chan);
int __AudioMix(short *outstereo, int numSamples);
+65 -47
View File
@@ -15,14 +15,14 @@
// Official git repository and contact information can be found at
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
#include "../MIPS/MIPS.h"
#include "../Host.h"
#include "Common/ChunkFile.h"
#include "Core/MIPS/MIPS.h"
#include "Core/Host.h"
#include "Core/CoreTiming.h"
#include "ChunkFile.h"
#include "sceAudio.h"
#include "__sceAudio.h"
#include "HLE.h"
#include "Core/HLE/HLE.h"
#include "Core/HLE/sceKernelThread.h"
#include "Core/HLE/sceAudio.h"
#include "Core/HLE/__sceAudio.h"
const int PSP_AUDIO_SAMPLE_MAX = 65536 - 64;
const int PSP_AUDIO_ERROR_SRC_FORMAT_4 = 0x80000003;
@@ -35,11 +35,24 @@ void AudioChannel::DoState(PointerWrap &p)
p.Do(leftVolume);
p.Do(rightVolume);
p.Do(format);
p.Do(waitingThread);
p.Do(waitingThreads);
sampleQueue.DoState(p);
p.DoMarker("AudioChannel");
}
void AudioChannel::clear()
{
reserved = false;
leftVolume = 0;
rightVolume = 0;
format = 0;
sampleAddress = 0;
sampleCount = 0;
sampleQueue.clear();
__AudioWakeThreads(*this);
}
// There's a second Audio api called Audio2 that only has one channel, I guess the 8 channel api was overkill.
// We simply map it to an extra channel after the 8 channels, since they can be used concurrently.
@@ -52,13 +65,10 @@ AudioChannel chans[PSP_AUDIO_CHANNEL_MAX + 1];
// Not sure about the range of volume, I often see 0x800 so that might be either
// max or 50%?
u32 sceAudioOutputBlocking(u32 chan, u32 vol, u32 samplePtr) {
u32 sceAudioOutputBlocking(u32 chan, int vol, u32 samplePtr) {
if (vol > 0xFFFF) {
ERROR_LOG(HLE, "sceAudioOutputBlocking() - invalid volume");
return SCE_ERROR_AUDIO_INVALID_VOLUME;
} else if (samplePtr == 0) {
ERROR_LOG(HLE, "sceAudioOutputBlocking() - Sample pointer null");
return 0;
} else if (chan >= PSP_AUDIO_CHANNEL_MAX) {
ERROR_LOG(HLE,"sceAudioOutputBlocking() - bad channel");
return SCE_ERROR_AUDIO_INVALID_CHANNEL;
@@ -67,20 +77,19 @@ u32 sceAudioOutputBlocking(u32 chan, u32 vol, u32 samplePtr) {
return SCE_ERROR_AUDIO_CHANNEL_NOT_RESERVED;
} else {
DEBUG_LOG(HLE, "sceAudioOutputBlocking(%08x, %08x, %08x)", chan, vol, samplePtr);
chans[chan].leftVolume = vol;
chans[chan].rightVolume = vol;
if (vol >= 0) {
chans[chan].leftVolume = vol;
chans[chan].rightVolume = vol;
}
chans[chan].sampleAddress = samplePtr;
return __AudioEnqueue(chans[chan], chan, true);
}
}
u32 sceAudioOutputPannedBlocking(u32 chan, u32 leftvol, u32 rightvol, u32 samplePtr) {
u32 sceAudioOutputPannedBlocking(u32 chan, int leftvol, int rightvol, u32 samplePtr) {
if (leftvol > 0xFFFF || rightvol > 0xFFFF) {
ERROR_LOG(HLE, "sceAudioOutputPannedBlocking() - invalid volume");
return SCE_ERROR_AUDIO_INVALID_VOLUME;
} else if (samplePtr == 0) {
ERROR_LOG(HLE, "sceAudioOutputPannedBlocking() - Sample pointer null");
return 0;
} else if (chan >= PSP_AUDIO_CHANNEL_MAX) {
ERROR_LOG(HLE,"sceAudioOutputPannedBlocking() - bad channel");
return SCE_ERROR_AUDIO_INVALID_CHANNEL;
@@ -89,20 +98,21 @@ u32 sceAudioOutputPannedBlocking(u32 chan, u32 leftvol, u32 rightvol, u32 sample
return SCE_ERROR_AUDIO_CHANNEL_NOT_RESERVED;
} else {
DEBUG_LOG(HLE, "sceAudioOutputPannedBlocking(%08x, %08x, %08x, %08x)", chan, leftvol, rightvol, samplePtr);
chans[chan].leftVolume = leftvol;
chans[chan].rightVolume = rightvol;
if (leftvol >= 0) {
chans[chan].leftVolume = leftvol;
}
if (rightvol >= 0) {
chans[chan].rightVolume = rightvol;
}
chans[chan].sampleAddress = samplePtr;
return __AudioEnqueue(chans[chan], chan, true);
}
}
u32 sceAudioOutput(u32 chan, u32 vol, u32 samplePtr) {
u32 sceAudioOutput(u32 chan, int vol, u32 samplePtr) {
if (vol > 0xFFFF) {
ERROR_LOG(HLE, "sceAudioOutput() - invalid volume");
return SCE_ERROR_AUDIO_INVALID_VOLUME;
} else if (samplePtr == 0) {
ERROR_LOG(HLE, "sceAudioOutput() - Sample pointer null");
return 0;
} else if (chan >= PSP_AUDIO_CHANNEL_MAX) {
ERROR_LOG(HLE,"sceAudioOutput() - bad channel");
return SCE_ERROR_AUDIO_INVALID_CHANNEL;
@@ -111,20 +121,19 @@ u32 sceAudioOutput(u32 chan, u32 vol, u32 samplePtr) {
return SCE_ERROR_AUDIO_CHANNEL_NOT_RESERVED;
} else {
DEBUG_LOG(HLE, "sceAudioOutputPanned(%08x, %08x, %08x)", chan, vol, samplePtr);
chans[chan].leftVolume = vol;
chans[chan].rightVolume = vol;
if (vol >= 0) {
chans[chan].leftVolume = vol;
chans[chan].rightVolume = vol;
}
chans[chan].sampleAddress = samplePtr;
return __AudioEnqueue(chans[chan], chan, false);
}
}
u32 sceAudioOutputPanned(u32 chan, u32 leftvol, u32 rightvol, u32 samplePtr) {
u32 sceAudioOutputPanned(u32 chan, int leftvol, int rightvol, u32 samplePtr) {
if (leftvol > 0xFFFF || rightvol > 0xFFFF) {
ERROR_LOG(HLE, "sceAudioOutputPannedBlocking() - invalid volume");
return SCE_ERROR_AUDIO_INVALID_VOLUME;
} else if (samplePtr == 0) {
ERROR_LOG(HLE, "sceAudioOutputPannedBlocking() - Sample pointer null");
return 0;
} else if (chan >= PSP_AUDIO_CHANNEL_MAX) {
ERROR_LOG(HLE,"sceAudioOutputPanned() - bad channel");
return SCE_ERROR_AUDIO_INVALID_CHANNEL;
@@ -133,8 +142,12 @@ u32 sceAudioOutputPanned(u32 chan, u32 leftvol, u32 rightvol, u32 samplePtr) {
return SCE_ERROR_AUDIO_CHANNEL_NOT_RESERVED;
} else {
DEBUG_LOG(HLE,"sceAudioOutputPanned(%08x, %08x, %08x, %08x)", chan, leftvol, rightvol, samplePtr);
chans[chan].leftVolume = leftvol;
chans[chan].rightVolume = rightvol;
if (leftvol >= 0) {
chans[chan].leftVolume = leftvol;
}
if (rightvol >= 0) {
chans[chan].rightVolume = rightvol;
}
chans[chan].sampleAddress = samplePtr;
return __AudioEnqueue(chans[chan], chan, false);
}
@@ -166,10 +179,10 @@ static u32 GetFreeChannel() {
return -1;
}
u32 sceAudioChReserve(u32 chan, u32 sampleCount, u32 format) {
if ((int)chan < 0) {
u32 sceAudioChReserve(int chan, u32 sampleCount, u32 format) {
if (chan < 0) {
chan = GetFreeChannel();
if ((int)chan < 0) {
if (chan < 0) {
ERROR_LOG(HLE, "sceAudioChReserve - no channels remaining");
return SCE_ERROR_AUDIO_NO_CHANNELS_AVAILABLE;
}
@@ -195,6 +208,8 @@ u32 sceAudioChReserve(u32 chan, u32 sampleCount, u32 format) {
chans[chan].sampleCount = sampleCount;
chans[chan].format = format;
chans[chan].reserved = true;
chans[chan].leftVolume = 0;
chans[chan].rightVolume = 0;
return chan;
}
@@ -216,11 +231,14 @@ u32 sceAudioChRelease(u32 chan) {
u32 sceAudioSetChannelDataLen(u32 chan, u32 len) {
if (chan >= PSP_AUDIO_CHANNEL_MAX) {
ERROR_LOG(HLE,"sceAudioSetChannelDataLen(%08x, %08x) - bad channel", chan, len);
ERROR_LOG(HLE, "sceAudioSetChannelDataLen(%08x, %08x) - bad channel", chan, len);
return SCE_ERROR_AUDIO_INVALID_CHANNEL;
} else if (!chans[chan].reserved) {
ERROR_LOG(HLE,"sceAudioSetChannelDataLen(%08x, %08x) - channel not reserved", chan, len);
return SCE_ERROR_AUDIO_CHANNEL_NOT_RESERVED;
ERROR_LOG(HLE, "sceAudioSetChannelDataLen(%08x, %08x) - channel not reserved", chan, len);
return SCE_ERROR_AUDIO_CHANNEL_NOT_INIT;
} else if ((len & 63) != 0 || len == 0 || len > PSP_AUDIO_SAMPLE_MAX) {
ERROR_LOG(HLE, "sceAudioSetChannelDataLen(%08x, %08x) - invalid sample count", chan, len);
return SCE_ERROR_AUDIO_OUTPUT_SAMPLE_DATA_SIZE_NOT_ALIGNED;
} else {
DEBUG_LOG(HLE, "sceAudioSetChannelDataLen(%08x, %08x)", chan, len);
chans[chan].sampleCount = len;
@@ -396,21 +414,21 @@ const HLEFunction sceAudio[] =
{0x01562ba3, WrapU_U<sceAudioOutput2Reserve>, "sceAudioOutput2Reserve"},
{0x2d53f36e, WrapU_UU<sceAudioOutput2OutputBlocking>, "sceAudioOutput2OutputBlocking"},
{0x63f2889c, WrapU_U<sceAudioOutput2ChangeLength>, "sceAudioOutput2ChangeLength"},
{0x647cef33, WrapU_V<sceAudioOutput2GetRestSample>, "sceAudioOutput2GetRestSample"},
{0x647cef33, WrapU_V<sceAudioOutput2GetRestSample>, "sceAudioOutput2GetRestSample"},
{0x43196845, WrapU_V<sceAudioOutput2Release>, "sceAudioOutput2Release"},
{0x80F1F7E0, WrapU_V<sceAudioInit>, "sceAudioInit"},
{0x210567F7, WrapU_V<sceAudioEnd>, "sceAudioEnd"},
{0xA2BEAA6C, WrapU_U<sceAudioSetFrequency>, "sceAudioSetFrequency"},
{0x927AC32B, WrapU_V<sceAudioSetVolumeOffset>, "sceAudioSetVolumeOffset"},
{0x8c1009b2, WrapU_UUU<sceAudioOutput>, "sceAudioOutput"},
{0x136CAF51, WrapU_UUU<sceAudioOutputBlocking>, "sceAudioOutputBlocking"},
{0xE2D56B2D, WrapU_UUUU<sceAudioOutputPanned>, "sceAudioOutputPanned"},
{0x13F592BC, WrapU_UUUU<sceAudioOutputPannedBlocking>, "sceAudioOutputPannedBlocking"},
{0x5EC81C55, WrapU_UUU<sceAudioChReserve>, "sceAudioChReserve"},
{0x6FC46853, WrapU_U<sceAudioChRelease>, "sceAudioChRelease"},
{0x8c1009b2, WrapU_UIU<sceAudioOutput>, "sceAudioOutput"},
{0x136CAF51, WrapU_UIU<sceAudioOutputBlocking>, "sceAudioOutputBlocking"},
{0xE2D56B2D, WrapU_UIIU<sceAudioOutputPanned>, "sceAudioOutputPanned"},
{0x13F592BC, WrapU_UIIU<sceAudioOutputPannedBlocking>, "sceAudioOutputPannedBlocking"},
{0x5EC81C55, WrapU_IUU<sceAudioChReserve>, "sceAudioChReserve"},
{0x6FC46853, WrapU_U<sceAudioChRelease>, "sceAudioChRelease"},
{0xE9D97901, WrapI_U<sceAudioGetChannelRestLen>, "sceAudioGetChannelRestLen"},
{0xB011922F, WrapI_U<sceAudioGetChannelRestLen>, "sceAudioGetChannelRestLength"},
{0xCB2E439E, WrapU_UU<sceAudioSetChannelDataLen>, "sceAudioSetChannelDataLen"},
{0xB011922F, WrapI_U<sceAudioGetChannelRestLen>, "sceAudioGetChannelRestLength"},
{0xCB2E439E, WrapU_UU<sceAudioSetChannelDataLen>, "sceAudioSetChannelDataLen"},
{0x95FD0C2D, WrapU_UU<sceAudioChangeChannelConfig>, "sceAudioChangeChannelConfig"},
{0xB7E1D8E7, WrapU_UUU<sceAudioChangeChannelVolume>, "sceAudioChangeChannelVolume"},
{0x38553111, WrapU_UUU<sceAudioSRCChReserve>, "sceAudioSRCChReserve"},
+8 -11
View File
@@ -47,6 +47,12 @@ const int PSP_AUDIO_CHANNEL_MAX = 8;
const int PSP_AUDIO_CHANNEL_SRC = 8;
const int PSP_AUDIO_CHANNEL_OUTPUT2 = 8;
struct AudioChannelWaitInfo
{
SceUID threadID;
int numSamples;
};
struct AudioChannel
{
AudioChannel() {
@@ -64,7 +70,7 @@ struct AudioChannel
u32 rightVolume;
u32 format;
SceUID waitingThread;
std::vector<AudioChannelWaitInfo> waitingThreads;
// PC side - should probably split out
@@ -74,16 +80,7 @@ struct AudioChannel
void DoState(PointerWrap &p);
void clear() {
reserved = false;
waitingThread = 0;
leftVolume = 0;
rightVolume = 0;
format = 0;
sampleAddress = 0;
sampleCount = 0;
sampleQueue.clear();
}
void clear();
};
// The extra channel is for SRC/Output2.
+2 -2
View File
@@ -786,12 +786,12 @@ int scePsmfPlayerGetAudioData(u32 psmfPlayer, u32 audioDataAddr)
int scePsmfPlayerGetCurrentStatus(u32 psmfPlayer)
{
ERROR_LOG(HLE, "scePsmfPlayerGetCurrentStatus(%08x)", psmfPlayer);
PsmfPlayer *psmfplayer = getPsmfPlayer(psmfPlayer);
if (!psmfplayer) {
ERROR_LOG(HLE, "scePsmfPlayerUpdate - invalid psmf");
ERROR_LOG(HLE, "scePsmfPlayerGetCurrentStatus(%08x) - invalid psmf", psmfPlayer);
return ERROR_PSMF_NOT_FOUND;
}
ERROR_LOG(HLE, "%d=scePsmfPlayerGetCurrentStatus(%08x)", psmfplayer->status, psmfPlayer);
return psmfplayer->status;
}