From 1e095951dd887a538a1c151df3fdec83c907c48f Mon Sep 17 00:00:00 2001 From: Henrik Rydgard Date: Tue, 20 Dec 2016 22:02:40 +0100 Subject: [PATCH] Replace while loop with division (technically a right shift). --- Core/HW/SasAudio.cpp | 19 ++++++------------- Core/HW/SasAudio.h | 1 + 2 files changed, 7 insertions(+), 13 deletions(-) diff --git a/Core/HW/SasAudio.cpp b/Core/HW/SasAudio.cpp index 6977b02c52..46a23a85c4 100644 --- a/Core/HW/SasAudio.cpp +++ b/Core/HW/SasAudio.cpp @@ -495,23 +495,16 @@ void SasInstance::MixVoice(SasVoice &voice) { // Resample to the correct pitch, writing exactly "grainSize" samples. // TODO: Special case no-resample case (and 2x and 0.5x) for speed, it's not uncommon - int16_t temp[256 * 4]; - int tempPos = 0; + int16_t temp[PSP_SAS_MAX_GRAIN + 2]; // Two passes: First read, then resample. u32 sampleFrac = voice.sampleFrac; - temp[tempPos++] = voice.resampleHist[0]; - temp[tempPos++] = voice.resampleHist[1]; + temp[0] = voice.resampleHist[0]; + temp[1] = voice.resampleHist[1]; - int samplesToRead = 0; - uint32_t t = sampleFrac + voice.pitch * (grainSize - delay); - while (t >= PSP_SAS_PITCH_BASE) { - samplesToRead++; - t -= PSP_SAS_PITCH_BASE; - } - - voice.ReadSamples(&temp[tempPos], samplesToRead); - tempPos += samplesToRead; + int samplesToRead = (sampleFrac + voice.pitch * (grainSize - delay)) >> PSP_SAS_PITCH_BASE_SHIFT; + voice.ReadSamples(&temp[2], samplesToRead); + int tempPos = 2 + samplesToRead; for (int i = delay; i < grainSize; i++) { const int16_t *s = temp + (sampleFrac >> PSP_SAS_PITCH_BASE_SHIFT); diff --git a/Core/HW/SasAudio.h b/Core/HW/SasAudio.h index d20bd82f99..195056d476 100644 --- a/Core/HW/SasAudio.h +++ b/Core/HW/SasAudio.h @@ -38,6 +38,7 @@ enum { PSP_SAS_PITCH_MAX = 0x4000, PSP_SAS_VOL_MAX = 0x1000, + PSP_SAS_MAX_GRAIN = 1024, // VERY conservative! 256 is quite common but don't think I've ever seen bigger. PSP_SAS_ADSR_CURVE_MODE_LINEAR_INCREASE = 0, PSP_SAS_ADSR_CURVE_MODE_LINEAR_DECREASE = 1,