From 6d26bd6539b474e18c70f3c4d11f00d08e2c8e60 Mon Sep 17 00:00:00 2001 From: shenweip <1037567878@qq.com> Date: Fri, 31 Jul 2020 15:52:03 +0800 Subject: [PATCH] Do state. --- Core/HLE/sceKernel.cpp | 1 + Core/HLE/sceUsbMic.cpp | 70 +++++++++++++++++++++++++++++++++++++++--- Core/HLE/sceUsbMic.h | 5 +++ 3 files changed, 71 insertions(+), 5 deletions(-) diff --git a/Core/HLE/sceKernel.cpp b/Core/HLE/sceKernel.cpp index cedef16ed6..d84aad032a 100644 --- a/Core/HLE/sceKernel.cpp +++ b/Core/HLE/sceKernel.cpp @@ -272,6 +272,7 @@ void __KernelDoState(PointerWrap &p) __UmdDoState(p); __UtilityDoState(p); __UsbDoState(p); + __UsbMicDoState(p); __VaudioDoState(p); __HeapDoState(p); diff --git a/Core/HLE/sceUsbMic.cpp b/Core/HLE/sceUsbMic.cpp index e9f0b8f5c7..8041aed186 100644 --- a/Core/HLE/sceUsbMic.cpp +++ b/Core/HLE/sceUsbMic.cpp @@ -39,11 +39,13 @@ enum { int eventUsbMicAudioUpdate = -1; QueueBuf *audioBuf = nullptr; -u32 audioBufSize = 0; u32 numNeedSamples = 0; static std::vector waitingThreads; std::mutex wtMutex; bool isNeedInput = false; +u32 curSampleRate = 44100; +u32 curChannels = 1; +int micState = 0; // 0 means stopped, 1 means started, for save state. static void __UsbMicAudioUpdate(u64 userdata, int cyclesLate) { SceUID threadID = (SceUID)userdata; @@ -107,6 +109,35 @@ void __UsbMicShutdown() { Microphone::stopMic(); } +void __UsbMicDoState(PointerWrap &p) { + auto s = p.Section("sceUsbMic", 1, 1); + if (!s) { + return; + } + p.Do(numNeedSamples); + p.Do(waitingThreads); + p.Do(isNeedInput); + p.Do(curSampleRate); + p.Do(curChannels); + p.Do(micState); + // Maybe also need to save the state of audioBuf. + if (waitingThreads.size() != 0 && p.mode == PointerWrap::MODE_READ) { + u64 waitTimeus = (waitingThreads[0].needSize - Microphone::availableAudioBufSize()) * 1000000 / 2 / waitingThreads[0].sampleRate; + CoreTiming::ScheduleEvent(usToCycles(waitTimeus), eventUsbMicAudioUpdate, waitingThreads[0].threadID); + } + if (micState == 0) { + if (Microphone::isMicStarted()) + Microphone::stopMic(); + } else if (micState == 1) { + if (Microphone::isMicStarted()) { + Microphone::stopMic(); + Microphone::startMic(new std::vector({ curSampleRate, curChannels })); + } else { + Microphone::startMic(new std::vector({ curSampleRate, curChannels })); + } + } +} + QueueBuf::QueueBuf(u32 size) : start(0), end(0), capacity(size) { buf_ = new u8[size]; } @@ -115,6 +146,26 @@ QueueBuf::~QueueBuf() { delete[] buf_; } +QueueBuf::QueueBuf(const QueueBuf &buf) { + buf_ = new u8[buf.capacity]; + memcpy(buf_, buf.buf_, buf.capacity); + start = buf.start; + end = buf.end; + capacity = buf.capacity; +} + +QueueBuf& QueueBuf::operator=(const QueueBuf &buf) { + if (capacity < buf.capacity) { + resize(buf.capacity); + } + std::unique_lock lock(mutex); + memcpy(buf_, buf.buf_, buf.capacity); + start = buf.start; + end = buf.end; + lock.unlock(); + return *this; +} + void QueueBuf::push(u8 *buf, u32 size) { if (getRemainingSize() < size) { resize((capacity + size - getRemainingSize()) * 3 / 2); @@ -171,6 +222,13 @@ void QueueBuf::resize(u32 newSize) { lock.unlock(); } +void QueueBuf::flush() { + std::unique_lock lock(mutex); + start = 0; + end = 0; + lock.unlock(); +} + u32 QueueBuf::getAvailableSize() { u32 availableSize = 0; if (end >= start) { @@ -199,6 +257,8 @@ static int sceUsbMicInputBlocking(u32 maxSamples, u32 sampleRate, u32 bufAddr) { if (sampleRate != 44100 && sampleRate != 22050 && sampleRate != 11025) { return SCE_USBMIC_ERROR_INVALID_SAMPLERATE; } + curSampleRate = sampleRate; + curChannels = 1; return __MicInputBlocking(maxSamples, sampleRate, bufAddr); } @@ -232,7 +292,7 @@ int Microphone::startMic(void *param) { if (winMic) winMic->sendMessage({ CAPTUREDEVIDE_COMMAND::START, param }); #endif - + micState = 1; return 0; } @@ -241,6 +301,7 @@ int Microphone::stopMic() { if (winMic) winMic->sendMessage({ CAPTUREDEVIDE_COMMAND::STOP, nullptr }); #endif + micState = 0; return 0; } @@ -306,14 +367,13 @@ void Microphone::onMicDeviceChange() { u32 __MicInputBlocking(u32 maxSamples, u32 sampleRate, u32 bufAddr) { u32 size = maxSamples << 1; - numNeedSamples = maxSamples; - if (size != audioBufSize) { + if (size != numNeedSamples << 1) { if (audioBuf) { delete audioBuf; } audioBuf = new QueueBuf(size); - audioBufSize = size; } + numNeedSamples = maxSamples; if (!Microphone::isMicStarted()) { std::vector *param = new std::vector({ sampleRate, 1 }); Microphone::startMic(param); diff --git a/Core/HLE/sceUsbMic.h b/Core/HLE/sceUsbMic.h index 09d6a661c4..e8ffea37c6 100644 --- a/Core/HLE/sceUsbMic.h +++ b/Core/HLE/sceUsbMic.h @@ -24,6 +24,7 @@ void Register_sceUsbMic(); void __UsbMicInit(); void __UsbMicShutdown(); +void __UsbMicDoState(PointerWrap &p); struct MicWaitInfo { SceUID threadID; @@ -37,9 +38,13 @@ public: QueueBuf(u32 size); ~QueueBuf(); + QueueBuf(const QueueBuf &buf); + QueueBuf& operator=(const QueueBuf &buf); + void push(u8 *buf, u32 size); u32 pop(u8 *buf, u32 size); void resize(u32 newSize); + void flush(); u32 getAvailableSize(); u32 getRemainingSize();