From f2bd0b9f6f3f9ad2a843cd562b74acba750cb630 Mon Sep 17 00:00:00 2001 From: shenweip <1037567878@qq.com> Date: Mon, 23 Nov 2020 14:07:11 +0800 Subject: [PATCH] Implements sceUsbCamGetMicDataLength. --- Core/HLE/sceUsbCam.cpp | 6 +++++- Core/HLE/sceUsbMic.cpp | 26 +++++++++++++++++--------- Core/HLE/sceUsbMic.h | 7 +++++-- 3 files changed, 27 insertions(+), 12 deletions(-) diff --git a/Core/HLE/sceUsbCam.cpp b/Core/HLE/sceUsbCam.cpp index e391f52457..d24b6ed2eb 100644 --- a/Core/HLE/sceUsbCam.cpp +++ b/Core/HLE/sceUsbCam.cpp @@ -142,6 +142,10 @@ static int sceUsbCamReadMic(u32 bufAddr, u32 size) { return __MicInput(size >> 1, config->micParam.frequency, bufAddr, false); } +static int sceUsbCamGetMicDataLength() { + return Microphone::getReadMicDataLength(); +} + static int sceUsbCamSetupVideo(u32 paramAddr, u32 workareaAddr, int wasize) { if (Memory::IsValidRange(paramAddr, sizeof(PspUsbCamSetupVideoParam))) { Memory::ReadStruct(paramAddr, &config->videoParam); @@ -251,7 +255,7 @@ const HLEFunction sceUsbCam[] = { 0X3DC0088E, &WrapI_UU, "sceUsbCamReadMic", 'i', "xx" }, { 0XB048A67D, nullptr, "sceUsbCamWaitReadMicEnd", '?', "" }, { 0XF8847F60, nullptr, "sceUsbCamPollReadMicEnd", '?', "" }, - { 0X5778B452, nullptr, "sceUsbCamGetMicDataLength", '?', "" }, + { 0X5778B452, &WrapI_V, "sceUsbCamGetMicDataLength", 'i', "" }, { 0X08AEE98A, nullptr, "sceUsbCamSetMicGain", '?', "" }, { 0X17F7B2FB, &WrapI_UUI, "sceUsbCamSetupVideo", 'i', "xxi" }, diff --git a/Core/HLE/sceUsbMic.cpp b/Core/HLE/sceUsbMic.cpp index 3248a70225..131edfe40a 100644 --- a/Core/HLE/sceUsbMic.cpp +++ b/Core/HLE/sceUsbMic.cpp @@ -50,6 +50,7 @@ std::mutex wtMutex; bool isNeedInput; u32 curSampleRate; u32 curChannels; +u32 readMicDataLength; int micState; // 0 means stopped, 1 means started, for save state. static void __UsbMicAudioUpdate(u64 userdata, int cyclesLate) { @@ -72,6 +73,7 @@ static void __UsbMicAudioUpdate(u64 userdata, int cyclesLate) { DEBUG_LOG(HLE, "sceUsbMic: Waking up thread(%d)", (int)waitingThread.threadID); __KernelResumeThreadFromWait(threadID, ret); waitingThreads.erase(waitingThreads.begin() + count); + readMicDataLength += waitingThread.needSize; } else { u64 waitTimeus = (waitingThread.needSize - Microphone::availableAudioBufSize()) * 1000000 / 2 / waitingThread.sampleRate; if(eventUsbMicAudioUpdate == -1) @@ -88,6 +90,7 @@ static void __UsbMicAudioUpdate(u64 userdata, int cyclesLate) { DEBUG_LOG(HLE, "sceUsbMic: Waking up thread(%d)", (int)waitingThread.threadID); __KernelResumeThreadFromWait(threadID, ret); waitingThreads.erase(waitingThreads.begin() + count); + readMicDataLength += waitingThread.needSize; } } ++count; @@ -183,6 +186,8 @@ QueueBuf& QueueBuf::operator=(const QueueBuf &buf) { u32 QueueBuf::push(u8 *buf, u32 size) { u32 addedSize = 0; + if (size > capacity) + resize(size); // This will overwrite the old data if the size prepare to add more than remaining size. std::unique_lock lock(mutex); while (end + size > capacity) { @@ -331,13 +336,7 @@ bool Microphone::isHaveDevice() { } bool Microphone::isMicStarted() { -#ifdef HAVE_WIN32_MICROPHONE - if(winMic) - return winMic->isStarted(); -#elif PPSSPP_PLATFORM(ANDROID) - return audioRecording_State(); -#endif - return false; + return micState == 1; } // Deprecated. @@ -353,6 +352,10 @@ u32 Microphone::availableAudioBufSize() { return audioBuf->getAvailableSize(); } +u32 Microphone::getReadMicDataLength() { + return ::readMicDataLength; +} + int Microphone::addAudioData(u8 *buf, u32 size) { if (audioBuf) audioBuf->push(buf, size); @@ -400,21 +403,26 @@ u32 __MicInput(u32 maxSamples, u32 sampleRate, u32 bufAddr, bool block) { if (!audioBuf) return 0; + readMicDataLength = 0; numNeedSamples = maxSamples; + if (!Microphone::isMicStarted()) { std::vector *param = new std::vector({ sampleRate, 1 }); Microphone::startMic(param); } + if (!block) { - size = Microphone::availableAudioBufSize(); if (size > 0) { u8 *tempbuf8 = new u8[size]; - Microphone::getAudioData(tempbuf8, Microphone::availableAudioBufSize()); + memset(tempbuf8, 0, size); + Microphone::getAudioData(tempbuf8, size); Memory::Memcpy(bufAddr, tempbuf8, size); delete[] tempbuf8; } + readMicDataLength += size; return size; } + u64 waitTimeus = 0; if (Microphone::availableAudioBufSize() < size) { waitTimeus = (size - Microphone::availableAudioBufSize()) * 1000000 / 2 / sampleRate; diff --git a/Core/HLE/sceUsbMic.h b/Core/HLE/sceUsbMic.h index 42d4d6ec53..b17087caa3 100644 --- a/Core/HLE/sceUsbMic.h +++ b/Core/HLE/sceUsbMic.h @@ -65,10 +65,10 @@ namespace Microphone { int stopMic(); bool isHaveDevice(); bool isMicStarted(); - // Deprecated. - bool isNeedInput(); u32 numNeedSamples(); u32 availableAudioBufSize(); + u32 getReadMicDataLength(); + int addAudioData(u8 *buf, u32 size); u32 getAudioData(u8 *buf, u32 size); @@ -76,6 +76,9 @@ namespace Microphone { std::vector getDeviceList(); void onMicDeviceChange(); + + // Deprecated. + bool isNeedInput(); } u32 __MicInput(u32 maxSamples, u32 sampleRate, u32 bufAddr, bool block = true);