diff --git a/Core/HLE/ErrorCodes.h b/Core/HLE/ErrorCodes.h index 35e18fc256..9af26b28df 100644 --- a/Core/HLE/ErrorCodes.h +++ b/Core/HLE/ErrorCodes.h @@ -4,6 +4,7 @@ enum PSPErrorCode : u32 { SCE_KERNEL_ERROR_OK = 0, + SCE_KERNEL_ERROR_NOT_INITIALIZED = 0x80000001, // Name guessed, from sceAudioInput before init. SCE_KERNEL_ERROR_BAD_ARGUMENT = 0x80000004, // Maybe SCE_KERNEL_ERROR_ALREADY = 0x80000020, SCE_KERNEL_ERROR_BUSY = 0x80000021, @@ -373,6 +374,7 @@ enum PSPErrorCode : u32 { SCE_ERROR_AUDIO_INVALID_FREQUENCY = 0x8026000A, SCE_ERROR_AUDIO_INVALID_VOLUME = 0x8026000B, SCE_ERROR_AUDIO_CHANNEL_ALREADY_RESERVED = 0x80268002, + SCE_ERROR_AUDIO_ROUTING_INVALID_MODE = 0x802C01FE, SCE_ERROR_USBMIC_INVALID_MAX_SAMPLES = 0x80243806, SCE_ERROR_USBMIC_INVALID_SAMPLERATE = 0x8024380A, diff --git a/Core/HLE/__sceAudio.cpp b/Core/HLE/__sceAudio.cpp index 1f35e600b9..e5a0b95566 100644 --- a/Core/HLE/__sceAudio.cpp +++ b/Core/HLE/__sceAudio.cpp @@ -112,6 +112,8 @@ void __AudioInit() { g_audioChans[i].clear(); } g_audioSRC.clear(); + __AudioRoutingInit(); + audioInputInited = false; mixBuffer = new s32[hwBlockSize * 2]; clampedMixBuffer = new s16[hwBlockSize * 2]; @@ -121,7 +123,7 @@ void __AudioInit() { } void __AudioDoState(PointerWrap &p) { - auto s = p.Section("sceAudio", 1, 4); + auto s = p.Section("sceAudio", 1, 5); if (!s) return; @@ -197,6 +199,12 @@ void __AudioDoState(PointerWrap &p) { } else { mixerDMABusy = false; } + if (s >= 5) { + Do(p, audioInputInited); + } else { + // A game that was already capturing had initialized it. + audioInputInited = true; + } // For older states the routing modes were read back once per channel, above. __AudioCPUMHzChange(); diff --git a/Core/HLE/sceAudio.cpp b/Core/HLE/sceAudio.cpp index 5a02ca265b..81b3b0b8c7 100644 --- a/Core/HLE/sceAudio.cpp +++ b/Core/HLE/sceAudio.cpp @@ -33,10 +33,11 @@ const u32 PSP_AUDIO_SAMPLE_MAX = 65536 - 64; const int PSP_AUDIO_ERROR_SRC_FORMAT_4 = 0x80000003; -const int AUDIO_ROUTING_SPEAKER_OFF = 0; -const int AUDIO_ROUTING_SPEAKER_ON = 1; -int defaultRoutingMode = AUDIO_ROUTING_SPEAKER_ON; -int defaultRoutingVolMode = AUDIO_ROUTING_SPEAKER_ON; +// Both read 0 at boot on a PSP. +int defaultRoutingMode = 0; +int defaultRoutingVolMode = 0; +// sceAudioInput fails until sceAudioInputInit. +bool audioInputInited = false; // Only still here to read savestates written before the channels held buffer pointers. struct AudioChannelWaitInfo { @@ -157,6 +158,11 @@ void AudioSRCChannel::DoState(PointerWrap &p) { Do(p, waitingThreads); } +void __AudioRoutingInit() { + defaultRoutingMode = 0; + defaultRoutingVolMode = 0; +} + void __AudioRoutingDoState(PointerWrap &p) { auto s = p.Section("sceAudioRouting", 1); if (!s) @@ -543,7 +549,25 @@ static u32 sceAudioSRCOutputBlocking(u32 vol, u32 buf) { return hleLogDebug(Log::sceAudio, __AudioSRCEnqueueBlocking(g_audioSRC, buf, vol)); } +// From pspautotests audio/sceaudio/input. The size is checked first. +static int AudioInputCheck(u32 maxSamples, u32 sampleRate) { + if (!audioInputInited) { + return SCE_KERNEL_ERROR_NOT_INITIALIZED; + } + if (maxSamples == 0 || (maxSamples & 63) != 0 || maxSamples >= 0x80000000) { + return SCE_ERROR_AUDIO_OUTPUT_SAMPLE_DATA_SIZE_NOT_ALIGNED; + } + if (sampleRate != 44100 && sampleRate != 22050 && sampleRate != 11025) { + return SCE_ERROR_AUDIO_INVALID_FREQUENCY; + } + return 0; +} + static int sceAudioInputBlocking(u32 maxSamples, u32 sampleRate, u32 bufAddr) { + int error = AudioInputCheck(maxSamples, sampleRate); + if (error != 0) { + return hleLogError(Log::HLE, error); + } if (!Memory::IsValidAddress(bufAddr)) { return hleLogError(Log::HLE, -1, "invalid address"); } @@ -551,6 +575,13 @@ static int sceAudioInputBlocking(u32 maxSamples, u32 sampleRate, u32 bufAddr) { } static int sceAudioInput(u32 maxSamples, u32 sampleRate, u32 bufAddr) { + int error = AudioInputCheck(maxSamples, sampleRate); + if (error != 0) { + return hleLogError(Log::HLE, error); + } + if (bufAddr == 0) { + return hleLogWarning(Log::HLE, 0, "NULL buffer"); + } if (!Memory::IsValidAddress(bufAddr)) { return hleLogError(Log::HLE, -1, "invalid address"); } @@ -561,11 +592,14 @@ static int sceAudioInput(u32 maxSamples, u32 sampleRate, u32 bufAddr) { static int sceAudioInputInit(int unknown1, int gain, int unknown2) { ERROR_LOG(Log::HLE, "UNIMPL sceAudioInputInit: unknown1: %d, gain: %d, unknown2: %d", unknown1, gain, unknown2); - return 0; + audioInputInited = true; + // Takes about 3ms on a PSP. + return hleDelayResult(0, "audio input init", 3000); } static int sceAudioInputInitEx(u32 paramAddr) { ERROR_LOG(Log::HLE, "UNIMPL sceAudioInputInitEx: paramAddr: %08x", paramAddr); + audioInputInited = true; return 0; } @@ -583,28 +617,31 @@ static int sceAudioGetInputLength() { return ret; } -static u32 sceAudioRoutingSetMode(u32 mode) { - ERROR_LOG(Log::sceAudio, "sceAudioRoutingSetMode(%08x)", mode); +// Ranges and return values from pspautotests audio/sceaudio/routing. Also exported as sceAudioRouting. +int sceAudioRoutingSetMode(int mode) { + if (mode < 0 || mode > 3) { + return hleLogError(Log::sceAudio, SCE_ERROR_AUDIO_ROUTING_INVALID_MODE, "invalid mode"); + } int previousMode = defaultRoutingMode; defaultRoutingMode = mode; - return previousMode; + return hleLogInfo(Log::sceAudio, previousMode); } -static u32 sceAudioRoutingGetMode() { - ERROR_LOG(Log::sceAudio, "sceAudioRoutingGetMode()"); - return defaultRoutingMode; +int sceAudioRoutingGetMode() { + return hleLogDebug(Log::sceAudio, defaultRoutingMode); } -static u32 sceAudioRoutingSetVolumeMode(u32 mode) { - ERROR_LOG(Log::sceAudio, "sceAudioRoutingSetVolumeMode(%08x)", mode); - int previousMode = defaultRoutingVolMode; +// Unlike SetMode, this returns 0 rather than the previous mode. +int sceAudioRoutingSetVolumeMode(int mode) { + if (mode < 0 || mode > 1) { + return hleLogError(Log::sceAudio, SCE_ERROR_AUDIO_ROUTING_INVALID_MODE, "invalid mode"); + } defaultRoutingVolMode = mode; - return previousMode; + return hleLogInfo(Log::sceAudio, 0); } -static u32 sceAudioRoutingGetVolumeMode() { - ERROR_LOG(Log::sceAudio, "sceAudioRoutingGetVolumeMode()"); - return defaultRoutingVolMode; +int sceAudioRoutingGetVolumeMode() { + return hleLogDebug(Log::sceAudio, defaultRoutingVolMode); } const HLEFunction sceAudio[] = @@ -653,10 +690,10 @@ const HLEFunction sceAudio[] = {0XA633048E, &WrapI_V, "sceAudioPollInputEnd", 'i', "" }, {0X87B2E651, &WrapI_V, "sceAudioWaitInputEnd", 'i', "" }, - {0X36FD8AA9, &WrapU_U, "sceAudioRoutingSetMode", 'x', "x" }, - {0X39240E7D, &WrapU_V, "sceAudioRoutingGetMode", 'x', "" }, - {0XBB548475, &WrapU_U, "sceAudioRoutingSetVolumeMode", 'x', "x" }, - {0X28235C56, &WrapU_V, "sceAudioRoutingGetVolumeMode", 'x', "" }, + {0X36FD8AA9, &WrapI_I, "sceAudioRoutingSetMode", 'i', "i" }, + {0X39240E7D, &WrapI_V, "sceAudioRoutingGetMode", 'i', "" }, + {0XBB548475, &WrapI_I, "sceAudioRoutingSetVolumeMode", 'i', "i" }, + {0X28235C56, &WrapI_V, "sceAudioRoutingGetVolumeMode", 'i', "" }, }; diff --git a/Core/HLE/sceAudio.h b/Core/HLE/sceAudio.h index 097b91ffee..eb53988e3d 100644 --- a/Core/HLE/sceAudio.h +++ b/Core/HLE/sceAudio.h @@ -114,7 +114,16 @@ extern AudioSRCChannel g_audioSRC; bool SRCFrequencyAllowed(int freq); // The two routing modes are globals rather than per-channel; they get their own little block. +void __AudioRoutingInit(); + +extern bool audioInputInited; void __AudioRoutingDoState(PointerWrap &p); +// sceAudioRouting exports the same four functions. +int sceAudioRoutingSetMode(int mode); +int sceAudioRoutingGetMode(); +int sceAudioRoutingSetVolumeMode(int mode); +int sceAudioRoutingGetVolumeMode(); + void Register_sceAudio(); diff --git a/Core/HLE/sceAudioRouting.cpp b/Core/HLE/sceAudioRouting.cpp index c7c91f3f2f..d941b25aa0 100644 --- a/Core/HLE/sceAudioRouting.cpp +++ b/Core/HLE/sceAudioRouting.cpp @@ -17,39 +17,7 @@ #include "Core/HLE/HLE.h" #include "Core/HLE/FunctionWrappers.h" -#include "Core/HLE/sceAudiocodec.h" - -enum { - AUDIO_ROUTING_SPEAKER_OFF = 0, - AUDIO_ROUTING_SPEAKER_ON = 1, -}; - -u32 audioRoutingMode = AUDIO_ROUTING_SPEAKER_ON; -u32 audioRoutineVolumeMode = AUDIO_ROUTING_SPEAKER_ON; - -static int sceAudioRoutingGetMode() { - INFO_LOG(Log::sceAudio, "sceAudioRoutingGetMode"); - return 0; -} - -static int sceAudioRoutingSetVolumeMode(int mode) { - INFO_LOG(Log::sceAudio, "sceAudioRoutingSetVolumeMode %d", mode); - int previousMode = audioRoutineVolumeMode; - audioRoutineVolumeMode = mode; - return previousMode; -} - -static int sceAudioRoutingGetVolumeMode() { - INFO_LOG(Log::sceAudio, "sceAudioRoutingGetMode"); - return 0; -} - -static int sceAudioRoutingSetMode(int mode) { - INFO_LOG(Log::sceAudio, "sceAudioRoutingSetMode %d", mode); - int previousMode = audioRoutingMode; - audioRoutingMode = mode; - return previousMode; -} +#include "Core/HLE/sceAudio.h" const HLEFunction sceAudioRouting[] = { diff --git a/Core/HLE/sceUsbMic.cpp b/Core/HLE/sceUsbMic.cpp index 8e465d81e7..499596fd26 100644 --- a/Core/HLE/sceUsbMic.cpp +++ b/Core/HLE/sceUsbMic.cpp @@ -437,8 +437,8 @@ void Microphone::onMicDeviceChange() { } u32 __MicInput(u32 maxSamples, u32 sampleRate, u32 bufAddr, MICTYPE type, bool block) { - // sceAudioInput and the camera mic pass these straight through. The wait below divides by the - // rate, and the size sizes a host buffer. Not tested on the PSP. + // The camera mic passes these straight through (sceAudioInput checks them first). The wait below + // divides by the rate, and the size sizes a host buffer. if (sampleRate == 0) { return SCE_ERROR_USBMIC_INVALID_SAMPLERATE; } @@ -484,9 +484,11 @@ u32 __MicInput(u32 maxSamples, u32 sampleRate, u32 bufAddr, MICTYPE type, bool b MicWaitInfo waitInfo = { __KernelGetCurThread(), bufAddr, size, sampleRate }; waitingThreads.push_back(waitInfo); DEBUG_LOG(Log::HLE, "MicInputBlocking: blocking thread(%d)", (int)__KernelGetCurThread()); - __KernelWaitCurThread(WAITTYPE_MICINPUT, 1, size, 0, false, "blocking microphone"); + // The wait value is what the thread gets back when it's woken. + const u32 result = type == CAMERAMIC ? size : maxSamples; + __KernelWaitCurThread(WAITTYPE_MICINPUT, 1, result, 0, false, "blocking microphone"); - return type == CAMERAMIC ? size : maxSamples; + return result; } // A non-blocking input keeps filling its buffer from addAudioData() after it returns. diff --git a/pspautotests b/pspautotests index fab33ea469..e2d098fcf3 160000 --- a/pspautotests +++ b/pspautotests @@ -1 +1 @@ -Subproject commit fab33ea469ae6d82980064f3e29299e6920bf2e4 +Subproject commit e2d098fcf3c3396729a915094195b15a8ce8171e diff --git a/test.py b/test.py index f8cc384c5e..1bd99b35e5 100755 --- a/test.py +++ b/test.py @@ -173,6 +173,8 @@ tests_good = [ "audio/blocking/vaudio", "audio/sceaudio/reserve", "audio/sceaudio/datalen", + "audio/sceaudio/input", + "audio/sceaudio/routing", "audio/output2/changelength", "audio/output2/release", "audio/output2/reserve",