From 5afcb1b168e001c7f2ea942421fa2e338b1a795f Mon Sep 17 00:00:00 2001 From: goyuken Date: Mon, 21 Oct 2013 17:19:37 +0000 Subject: [PATCH] gameboy - use blip_buf for audio resampling, like NES. this is also something that dual GB does, but i never got around to porting it back to single GB before. the speexresampler is very good for some things, like snes, but isn't appropriate for multi-megahertz audio downsampling. big speedup. --- .../Consoles/Nintendo/Gameboy/Gambatte.cs | 60 ++++++++++++++++--- 1 file changed, 51 insertions(+), 9 deletions(-) diff --git a/BizHawk.Emulation/Consoles/Nintendo/Gameboy/Gambatte.cs b/BizHawk.Emulation/Consoles/Nintendo/Gameboy/Gambatte.cs index 30ea61310b..f5fc2c0380 100644 --- a/BizHawk.Emulation/Consoles/Nintendo/Gameboy/Gambatte.cs +++ b/BizHawk.Emulation/Consoles/Nintendo/Gameboy/Gambatte.cs @@ -194,9 +194,14 @@ namespace BizHawk.Emulation.Consoles.GB LibGambatte.gambatte_runfor(GambatteState, VideoBuffer, 160, soundbuff, ref nsamp); if (rendersound) + { soundbuffcontains = (int)nsamp; + ProcessSound(); + } else + { soundbuffcontains = 0; + } FrameAdvancePost(); } @@ -777,7 +782,7 @@ namespace BizHawk.Emulation.Consoles.GB #region ISoundProvider public ISoundProvider SoundProvider { get { return null; } } - public ISyncSoundProvider SyncSoundProvider { get { return dcfilter; } } + public ISyncSoundProvider SyncSoundProvider { get { return this; } } public bool StartAsyncSound() { return false; } public void EndAsyncSound() { } @@ -790,21 +795,58 @@ namespace BizHawk.Emulation.Consoles.GB /// int soundbuffcontains = 0; - Sound.Utilities.SpeexResampler resampler; - Sound.Utilities.DCFilter dcfilter; + int soundoutbuffcontains = 0; + + short[] soundoutbuff = new short[2048]; + + int latchaudio = 0; + + //Sound.Utilities.SpeexResampler resampler; + //Sound.Utilities.DCFilter dcfilter; + + Sound.Utilities.BlipBuffer blip; + + void ProcessSound() + { + for (uint i = 0; i < soundbuffcontains; i++) + { + int curr = soundbuff[i * 2]; + + if (curr != latchaudio) + { + int diff = latchaudio - curr; + latchaudio = curr; + blip.AddDelta(i, diff); + } + } + blip.EndFrame((uint)soundbuffcontains); + + soundoutbuffcontains = blip.SamplesAvailable(); + + blip.ReadSamples(soundoutbuff, soundoutbuffcontains, true); + for (int i = 0; i < soundoutbuffcontains * 2; i += 2) + soundoutbuff[i + 1] = soundoutbuff[i]; + + soundbuffcontains = 0; + + } void InitSound() { - resampler = new Sound.Utilities.SpeexResampler(2, 2097152, 44100, 2097152, 44100, null, this); + //resampler = new Sound.Utilities.SpeexResampler(2, 2097152, 44100, 2097152, 44100, null, this); //dcfilter = Sound.Utilities.DCFilter.AsISyncSoundProvider(resampler, 65536); // lowpass filtering on an actual GB was probably pretty aggressive? - dcfilter = Sound.Utilities.DCFilter.AsISyncSoundProvider(resampler, 2048); + //dcfilter = Sound.Utilities.DCFilter.AsISyncSoundProvider(resampler, 2048); + blip = new Sound.Utilities.BlipBuffer(1024); + blip.SetRates(2097152, 44100); } void DisposeSound() { - resampler.Dispose(); - resampler = null; + blip.Dispose(); + blip = null; + //resampler.Dispose(); + //resampler = null; } public void DiscardSamples() @@ -814,8 +856,8 @@ namespace BizHawk.Emulation.Consoles.GB public void GetSamples(out short[] samples, out int nsamp) { - samples = soundbuff; - nsamp = soundbuffcontains; + samples = soundoutbuff; + nsamp = soundoutbuffcontains; } #endregion