diff --git a/Source/Core/AudioCommon/Src/Mixer.cpp b/Source/Core/AudioCommon/Src/Mixer.cpp index 69bc4699fb..2e9f54d327 100644 --- a/Source/Core/AudioCommon/Src/Mixer.cpp +++ b/Source/Core/AudioCommon/Src/Mixer.cpp @@ -25,18 +25,18 @@ #include "FixedSizeQueue.h" #include "AudioCommon.h" -void CMixer::Mix(short *samples, int numSamples) +int CMixer::Mix(short *samples, int numSamples) { if (! samples) { Premix(NULL, 0); - return; + return 0; } // silence memset(samples, 0, numSamples * 2 * sizeof(short)); if (g_dspInitialize.pEmulatorState) { if (*g_dspInitialize.pEmulatorState != 0) - return; + return 0; } // first get the DTK Music @@ -46,9 +46,11 @@ void CMixer::Mix(short *samples, int numSamples) Premix(samples, numSamples); - push_sync.Enter(); int count = 0; - while (m_queueSize > queue_minlength && count < numSamples * 2) { + + push_sync.Enter(); + while (m_queueSize > queue_minlength && count < numSamples * 2) + { int x = samples[count]; x += sample_queue.front(); if (x > 32767) x = 32767; @@ -64,6 +66,8 @@ void CMixer::Mix(short *samples, int numSamples) m_queueSize-=2; } push_sync.Leave(); + + return count; } diff --git a/Source/Core/AudioCommon/Src/Mixer.h b/Source/Core/AudioCommon/Src/Mixer.h index e2394da893..131a620a1e 100644 --- a/Source/Core/AudioCommon/Src/Mixer.h +++ b/Source/Core/AudioCommon/Src/Mixer.h @@ -31,7 +31,7 @@ public: CMixer() : m_sampleRate(48000),m_bits(16),m_channels(2), m_mode(2), m_HLEready(false),m_queueSize(0) {} // Called from audio threads - virtual void Mix(short *sample, int numSamples); + virtual int Mix(short *sample, int numSamples); // Called from main thread virtual void PushSamples(short* samples, int num_stereo_samples, int core_sample_rate); @@ -39,7 +39,9 @@ public: virtual void Premix(short *samples, int numSamples) {} int GetSampleRate() {return m_sampleRate;} - + + int GetDataSize() {return m_queueSize;} + void SetThrottle(bool use) { m_throttle = use;} void SetDTKMusic(bool use) { m_EnableDTKMusic = use;} diff --git a/Source/Core/AudioCommon/Src/NullSoundStream.h b/Source/Core/AudioCommon/Src/NullSoundStream.h index 76f62b9f2f..e65678d9d1 100644 --- a/Source/Core/AudioCommon/Src/NullSoundStream.h +++ b/Source/Core/AudioCommon/Src/NullSoundStream.h @@ -23,7 +23,7 @@ class NullMixer : public CMixer { public: - virtual void Mix(short *sample, int numSamples) {} + virtual int Mix(short *sample, int numSamples) {return 0;} virtual void PushSamples(short* samples, int num_stereo_samples, int core_sample_rate) {} }; diff --git a/Source/Core/AudioCommon/Src/OpenALStream.cpp b/Source/Core/AudioCommon/Src/OpenALStream.cpp index 3d21b9c7b7..672d581151 100644 --- a/Source/Core/AudioCommon/Src/OpenALStream.cpp +++ b/Source/Core/AudioCommon/Src/OpenALStream.cpp @@ -86,7 +86,10 @@ void OpenALStream::Stop() void OpenALStream::Update() { - soundSyncEvent.Set(); + //if (m_mixer->GetDataSize()) //here need debug + { + soundSyncEvent.Set(); + } } THREAD_RETURN OpenALStream::ThreadFunc(void* args) @@ -120,28 +123,37 @@ void OpenALStream::SoundLoop() while (!threadData) { - ALint iBuffersProcessed = 0; - alGetSourcei(uiSource, AL_BUFFERS_PROCESSED, &iBuffersProcessed); + soundCriticalSection.Enter(); + int numBytesToRender = 32768; //ya, this is a hack, we need real data count + int numBytesRender = m_mixer->Mix(realtimeBuffer, numBytesToRender >> 2); + soundCriticalSection.Leave(); - if (iBuffersProcessed) + //if (numBytesRender) //here need debug { - // Remove the Buffer from the Queue. (uiBuffer contains the Buffer ID for the unqueued Buffer) - ALuint uiTempBuffer = 0; - alSourceUnqueueBuffers(uiSource, 1, &uiTempBuffer); + ALint iBuffersProcessed = 0; + alGetSourcei(uiSource, AL_BUFFERS_PROCESSED, &iBuffersProcessed); - soundCriticalSection.Enter(); - int numBytesToRender = 32768; //ya, this is a hack, we need real data count - m_mixer->Mix(realtimeBuffer, numBytesToRender >> 2); - soundCriticalSection.Leave(); - - unsigned long ulBytesWritten = 0; - //if (ulBytesWritten) + if (iBuffersProcessed) { - //alBufferData(uiTempBuffer, ulFormat, pDecodeBuffer, ulBytesWritten, ulFrequency); - alBufferData(uiTempBuffer, AL_FORMAT_STEREO16, realtimeBuffer, numBytesToRender, ulFrequency); + // Remove the Buffer from the Queue. (uiBuffer contains the Buffer ID for the unqueued Buffer) + ALuint uiTempBuffer = 0; + alSourceUnqueueBuffers(uiSource, 1, &uiTempBuffer); +/* + soundCriticalSection.Enter(); + int numBytesToRender = 32768; //ya, this is a hack, we need real data count + m_mixer->Mix(realtimeBuffer, numBytesToRender >> 2); + soundCriticalSection.Leave(); + + unsigned long ulBytesWritten = 0; +*/ + //if (numBytesRender) + { + alBufferData(uiTempBuffer, AL_FORMAT_STEREO16, realtimeBuffer, numBytesToRender, ulFrequency); + } alSourceQueueBuffers(uiSource, 1, &uiTempBuffer); } } + if (!threadData) soundSyncEvent.Wait(); }