Skip to content

Commit

Permalink
Replace while loop with division (technically a right shift).
Browse files Browse the repository at this point in the history
  • Loading branch information
hrydgard committed Dec 20, 2016
1 parent ff4b99d commit 1e09595
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 13 deletions.
19 changes: 6 additions & 13 deletions Core/HW/SasAudio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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];

int samplesToRead = 0;
uint32_t t = sampleFrac + voice.pitch * (grainSize - delay);
while (t >= PSP_SAS_PITCH_BASE) {
samplesToRead++;
t -= PSP_SAS_PITCH_BASE;
}
temp[0] = voice.resampleHist[0];
temp[1] = voice.resampleHist[1];

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);
Expand Down
1 change: 1 addition & 0 deletions Core/HW/SasAudio.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 1e09595

Please sign in to comment.