From c038ed0ed779ff8534555f27ad6e0235a29a9d71 Mon Sep 17 00:00:00 2001 From: jdpurcell Date: Mon, 26 Jan 2015 15:00:11 +0000 Subject: [PATCH] Sound/SoundOutputProvider - avoid buffer reallocation. --- BizHawk.Client.EmuHawk/Sound.cs | 4 +--- BizHawk.Client.EmuHawk/SoundOutputProvider.cs | 20 +++++++++++++++---- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/BizHawk.Client.EmuHawk/Sound.cs b/BizHawk.Client.EmuHawk/Sound.cs index 753043a6ee..f7c2e00d3a 100644 --- a/BizHawk.Client.EmuHawk/Sound.cs +++ b/BizHawk.Client.EmuHawk/Sound.cs @@ -307,9 +307,7 @@ namespace BizHawk.Client.EmuHawk } else { - samples = new short[samplesNeeded * ChannelCount]; - - samplesProvided = _outputProvider.GetSamples(samples, samplesNeeded); + _outputProvider.GetSamples(samplesNeeded, out samples, out samplesProvided); } } else if (_asyncSoundProvider != null) diff --git a/BizHawk.Client.EmuHawk/SoundOutputProvider.cs b/BizHawk.Client.EmuHawk/SoundOutputProvider.cs index 911779882e..fbda4e391d 100644 --- a/BizHawk.Client.EmuHawk/SoundOutputProvider.cs +++ b/BizHawk.Client.EmuHawk/SoundOutputProvider.cs @@ -43,6 +43,8 @@ namespace BizHawk.Client.EmuHawk private double _lastAdvertisedSamplesPerFrame; private Queue _baseSamplesPerFrame = new Queue(); + private short[] _outputBuffer = new short[0]; + private short[] _resampleBuffer = new short[0]; private double _resampleLengthRoundingError; @@ -62,6 +64,7 @@ namespace BizHawk.Client.EmuHawk _hardCorrectionHistory.Clear(); _lastAdvertisedSamplesPerFrame = 0.0; _baseSamplesPerFrame.Clear(); + _outputBuffer = new short[0]; _resampleBuffer = new short[0]; _resampleLengthRoundingError = 0.0; @@ -78,7 +81,7 @@ namespace BizHawk.Client.EmuHawk get { return SampleRate / Global.Emulator.CoreComm.VsyncRate; } } - public int GetSamples(short[] samples, int idealSampleCount) + public void GetSamples(int idealSampleCount, out short[] samples, out int sampleCount) { double scaleFactor = 1.0; @@ -131,8 +134,6 @@ namespace BizHawk.Client.EmuHawk UpdateHistory(_outputCountHistory, outputSampleCount, MaxHistoryLength); UpdateHistory(_hardCorrectionHistory, hardCorrected, MaxHistoryLength); - GetSamplesFromBuffer(samples, outputSampleCount); - if (LogDebug) { Console.WriteLine("Avg: {0:0.0} ms, Min: {1:0.0} ms, Max: {2:0.0} ms, Scale: {3:0.0000}", @@ -142,7 +143,9 @@ namespace BizHawk.Client.EmuHawk scaleFactor); } - return outputSampleCount; + sampleCount = outputSampleCount; + samples = GetOutputBuffer(sampleCount); + GetSamplesFromBuffer(samples, sampleCount); } private void GetSamplesFromBase(ref double scaleFactor) @@ -214,6 +217,15 @@ namespace BizHawk.Client.EmuHawk } } + private short[] GetOutputBuffer(int count) + { + if (_outputBuffer.Length < count * ChannelCount) + { + _outputBuffer = new short[count * ChannelCount]; + } + return _outputBuffer; + } + private short[] GetResampleBuffer(int count) { if (_resampleBuffer.Length < count * ChannelCount)