diff --git a/BizHawk.Client.EmuHawk/Sound/Output/DirectSoundSoundOutput.cs b/BizHawk.Client.EmuHawk/Sound/Output/DirectSoundSoundOutput.cs index 9bf155b9e7..ec1a19b549 100644 --- a/BizHawk.Client.EmuHawk/Sound/Output/DirectSoundSoundOutput.cs +++ b/BizHawk.Client.EmuHawk/Sound/Output/DirectSoundSoundOutput.cs @@ -129,11 +129,7 @@ namespace BizHawk.Client.EmuHawk int cursorDelta = CircularDistance(_lastWriteCursor, writeCursor, BufferSizeBytes); cursorDelta += BufferSizeBytes * (int)Math.Round((elapsedSeconds - (cursorDelta / (double)(Sound.SampleRate * Sound.BlockAlign))) / bufferSizeSeconds); _filledBufferSizeBytes -= cursorDelta; - if (_filledBufferSizeBytes < 0) - { - _sound.OnUnderrun(); - detectedUnderrun = true; - } + detectedUnderrun = _filledBufferSizeBytes < 0; } if (isInitializing || detectedUnderrun) { diff --git a/BizHawk.Client.EmuHawk/Sound/Output/DummySoundOutput.cs b/BizHawk.Client.EmuHawk/Sound/Output/DummySoundOutput.cs index fad2c2f618..d1632ced87 100644 --- a/BizHawk.Client.EmuHawk/Sound/Output/DummySoundOutput.cs +++ b/BizHawk.Client.EmuHawk/Sound/Output/DummySoundOutput.cs @@ -56,7 +56,6 @@ namespace BizHawk.Client.EmuHawk if (_remainingSamples < 0) { _remainingSamples = 0; - _sound.OnUnderrun(); detectedUnderrun = true; } _lastWriteTime = currentWriteTime; diff --git a/BizHawk.Client.EmuHawk/Sound/Output/OpenALSoundOutput.cs b/BizHawk.Client.EmuHawk/Sound/Output/OpenALSoundOutput.cs index 6fb3b98ea8..4a24bb4d82 100644 --- a/BizHawk.Client.EmuHawk/Sound/Output/OpenALSoundOutput.cs +++ b/BizHawk.Client.EmuHawk/Sound/Output/OpenALSoundOutput.cs @@ -81,7 +81,6 @@ namespace BizHawk.Client.EmuHawk bool detectedUnderrun = sourceState == ALSourceState.Stopped; if (detectedUnderrun) { - _sound.OnUnderrun(); // SampleOffset should reset to 0 when stopped; update the queued sample count to match UnqueueProcessedBuffers(); currentSamplesPlayed = 0; diff --git a/BizHawk.Client.EmuHawk/Sound/Output/XAudio2SoundOutput.cs b/BizHawk.Client.EmuHawk/Sound/Output/XAudio2SoundOutput.cs index dab844f113..b5e81f73e2 100644 --- a/BizHawk.Client.EmuHawk/Sound/Output/XAudio2SoundOutput.cs +++ b/BizHawk.Client.EmuHawk/Sound/Output/XAudio2SoundOutput.cs @@ -102,10 +102,6 @@ namespace BizHawk.Client.EmuHawk { bool isInitializing = _runningSamplesQueued == 0; bool detectedUnderrun = !isInitializing && _sourceVoice.State.BuffersQueued == 0; - if (detectedUnderrun) - { - _sound.OnUnderrun(); - } long samplesAwaitingPlayback = _runningSamplesQueued - _sourceVoice.State.SamplesPlayed; int samplesNeeded = (int)Math.Max(BufferSizeSamples - samplesAwaitingPlayback, 0); if (isInitializing || detectedUnderrun) diff --git a/BizHawk.Client.EmuHawk/Sound/Sound.cs b/BizHawk.Client.EmuHawk/Sound/Sound.cs index 0a3c3eef94..31174fb69b 100644 --- a/BizHawk.Client.EmuHawk/Sound/Sound.cs +++ b/BizHawk.Client.EmuHawk/Sound/Sound.cs @@ -109,40 +109,21 @@ namespace BizHawk.Client.EmuHawk public bool LogUnderruns { get; set; } - private bool InitializeBufferWithSilence - { - get { return true; } - } - - private bool RecoverFromUnderrunsWithSilence - { - get { return true; } - } - - private int SilenceLeaveRoomForFrameCount - { - get { return Global.Config.SoundThrottle ? 1 : 2; } // Why 2? I don't know, but it seems to work well with the clock throttle's behavior. - } - internal void HandleInitializationOrUnderrun(bool isUnderrun, ref int samplesNeeded) { - if ((!isUnderrun && InitializeBufferWithSilence) || (isUnderrun && RecoverFromUnderrunsWithSilence)) + // Fill device buffer with silence but leave enough room for one frame + int samplesPerFrame = (int)Math.Round(SampleRate / Global.Emulator.CoreComm.VsyncRate); + int silenceSamples = Math.Max(samplesNeeded - samplesPerFrame, 0); + _outputDevice.WriteSamples(new short[silenceSamples * 2], silenceSamples); + samplesNeeded -= silenceSamples; + + if (isUnderrun) { - int samplesPerFrame = (int)Math.Round(SampleRate / Global.Emulator.CoreComm.VsyncRate); - int silenceSamples = Math.Max(samplesNeeded - (SilenceLeaveRoomForFrameCount * samplesPerFrame), 0); - _outputDevice.WriteSamples(new short[silenceSamples * 2], silenceSamples); - samplesNeeded -= silenceSamples; + if (LogUnderruns) Console.WriteLine("Sound underrun detected!"); + _outputProvider.OnVolatility(); } } - internal void OnUnderrun() - { - if (!IsStarted) return; - - if (LogUnderruns) Console.WriteLine("Sound underrun detected!"); - _outputProvider.OnVolatility(); - } - public void UpdateSound(float atten) { if (!Global.Config.SoundEnabled || !IsStarted || _bufferedProvider == null || _disposed)