Merge pull request #58668 from godotengine/revert-57985-pitchshift-tweaks

Revert "PitchShift effect quality and performance tweaks for different pitch scale values"
This commit is contained in:
Ellen Poe 2022-03-01 20:09:32 -08:00 committed by GitHub
commit 1c91d09797
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 55 additions and 80 deletions

View File

@ -74,7 +74,7 @@
* *
*****************************************************************************/ *****************************************************************************/
void SMBPitchShift::PitchShift(float pitchShift, int64_t numSampsToProcess, int64_t fftFrameSize, int64_t osamp, float sampleRate, float *indata, float *outdata,int stride) { void SMBPitchShift::PitchShift(float pitchShift, long numSampsToProcess, long fftFrameSize, long osamp, float sampleRate, float *indata, float *outdata,int stride) {
/* /*
@ -85,32 +85,19 @@ void SMBPitchShift::PitchShift(float pitchShift, int64_t numSampsToProcess, int6
*/ */
double magn, phase, tmp, window, real, imag; double magn, phase, tmp, window, real, imag;
double freqPerBin, expct, reciprocalFftFrameSize; double freqPerBin, expct;
int64_t i,k, qpd, index, inFifoLatency, stepSize, fftFrameSize2; long i,k, qpd, index, inFifoLatency, stepSize, fftFrameSize2;
/* set up some handy variables */ /* set up some handy variables */
fftFrameSize2 = fftFrameSize/2; fftFrameSize2 = fftFrameSize/2;
reciprocalFftFrameSize = 1./fftFrameSize;
stepSize = fftFrameSize/osamp; stepSize = fftFrameSize/osamp;
freqPerBin = reciprocalFftFrameSize * sampleRate; freqPerBin = sampleRate/(double)fftFrameSize;
expct = Math_TAU * reciprocalFftFrameSize * stepSize; expct = 2.*Math_PI*(double)stepSize/(double)fftFrameSize;
inFifoLatency = fftFrameSize-stepSize; inFifoLatency = fftFrameSize-stepSize;
if (gRover == 0) { if (gRover == 0) { gRover = inFifoLatency;
gRover = inFifoLatency; }
}
// If pitchShift changes clear arrays to prevent some artifacts and quality loss. /* initialize our static arrays */
if (lastPitchShift != pitchShift) {
lastPitchShift = pitchShift;
memset(gInFIFO, 0, MAX_FRAME_LENGTH * sizeof(float));
memset(gOutFIFO, 0, MAX_FRAME_LENGTH * sizeof(float));
memset(gFFTworksp, 0, 2 * MAX_FRAME_LENGTH * sizeof(double));
memset(gLastPhase, 0, (MAX_FRAME_LENGTH / 2 + 1) * sizeof(double));
memset(gSumPhase, 0, (MAX_FRAME_LENGTH / 2 + 1) * sizeof(double));
memset(gOutputAccum, 0, 2 * MAX_FRAME_LENGTH * sizeof(double));
memset(gAnaFreq, 0, MAX_FRAME_LENGTH * sizeof(double));
memset(gAnaMagn, 0, MAX_FRAME_LENGTH * sizeof(double));
}
/* main processing loop */ /* main processing loop */
for (i = 0; i < numSampsToProcess; i++){ for (i = 0; i < numSampsToProcess; i++){
@ -125,7 +112,7 @@ void SMBPitchShift::PitchShift(float pitchShift, int64_t numSampsToProcess, int6
/* do windowing and re,im interleave */ /* do windowing and re,im interleave */
for (k = 0; k < fftFrameSize;k++) { for (k = 0; k < fftFrameSize;k++) {
window = -.5*cos(Math_TAU * reciprocalFftFrameSize * k)+.5; window = -.5*cos(2.*Math_PI*(double)k/(double)fftFrameSize)+.5;
gFFTworksp[2*k] = gInFIFO[k] * window; gFFTworksp[2*k] = gInFIFO[k] * window;
gFFTworksp[2*k+1] = 0.; gFFTworksp[2*k+1] = 0.;
} }
@ -137,7 +124,6 @@ void SMBPitchShift::PitchShift(float pitchShift, int64_t numSampsToProcess, int6
/* this is the analysis step */ /* this is the analysis step */
for (k = 0; k <= fftFrameSize2; k++) { for (k = 0; k <= fftFrameSize2; k++) {
/* de-interlace FFT buffer */ /* de-interlace FFT buffer */
real = gFFTworksp[2*k]; real = gFFTworksp[2*k];
imag = gFFTworksp[2*k+1]; imag = gFFTworksp[2*k+1];
@ -155,15 +141,13 @@ void SMBPitchShift::PitchShift(float pitchShift, int64_t numSampsToProcess, int6
/* map delta phase into +/- Pi interval */ /* map delta phase into +/- Pi interval */
qpd = tmp/Math_PI; qpd = tmp/Math_PI;
if (qpd >= 0) { if (qpd >= 0) { qpd += qpd&1;
qpd += qpd&1; } else { qpd -= qpd&1;
} else { }
qpd -= qpd&1;
}
tmp -= Math_PI*(double)qpd; tmp -= Math_PI*(double)qpd;
/* get deviation from bin frequency from the +/- Pi interval */ /* get deviation from bin frequency from the +/- Pi interval */
tmp = osamp*tmp/Math_TAU; tmp = osamp*tmp/(2.*Math_PI);
/* compute the k-th partials' true frequency */ /* compute the k-th partials' true frequency */
tmp = (double)k*freqPerBin + tmp*freqPerBin; tmp = (double)k*freqPerBin + tmp*freqPerBin;
@ -176,8 +160,8 @@ void SMBPitchShift::PitchShift(float pitchShift, int64_t numSampsToProcess, int6
/* ***************** PROCESSING ******************* */ /* ***************** PROCESSING ******************* */
/* this does the actual pitch shifting */ /* this does the actual pitch shifting */
memset(gSynMagn, 0, fftFrameSize*sizeof(double)); memset(gSynMagn, 0, fftFrameSize*sizeof(float));
memset(gSynFreq, 0, fftFrameSize*sizeof(double)); memset(gSynFreq, 0, fftFrameSize*sizeof(float));
for (k = 0; k <= fftFrameSize2; k++) { for (k = 0; k <= fftFrameSize2; k++) {
index = k*pitchShift; index = k*pitchShift;
if (index <= fftFrameSize2) { if (index <= fftFrameSize2) {
@ -200,7 +184,7 @@ void SMBPitchShift::PitchShift(float pitchShift, int64_t numSampsToProcess, int6
tmp /= freqPerBin; tmp /= freqPerBin;
/* take osamp into account */ /* take osamp into account */
tmp = Math_TAU*tmp/osamp; tmp = 2.*Math_PI*tmp/osamp;
/* add the overlap phase advance back in */ /* add the overlap phase advance back in */
tmp += (double)k*expct; tmp += (double)k*expct;
@ -215,35 +199,33 @@ void SMBPitchShift::PitchShift(float pitchShift, int64_t numSampsToProcess, int6
} }
/* zero negative frequencies */ /* zero negative frequencies */
for (k = fftFrameSize+2; k < 2*MAX_FRAME_LENGTH; k++) { for (k = fftFrameSize+2; k < 2*fftFrameSize; k++) { gFFTworksp[k] = 0.;
gFFTworksp[k] = 0.; }
}
/* do inverse transform */ /* do inverse transform */
smbFft(gFFTworksp, fftFrameSize, 1); smbFft(gFFTworksp, fftFrameSize, 1);
/* do windowing and add to output accumulator */ /* do windowing and add to output accumulator */
for(k=0; k < fftFrameSize; k++) { for(k=0; k < fftFrameSize; k++) {
window = -.5*cos(Math_TAU * reciprocalFftFrameSize * k)+.5; window = -.5*cos(2.*Math_PI*(double)k/(double)fftFrameSize)+.5;
gOutputAccum[k] += 2.*window*gFFTworksp[2*k]/(fftFrameSize2*osamp); gOutputAccum[k] += 2.*window*gFFTworksp[2*k]/(fftFrameSize2*osamp);
} }
for (k = 0; k < stepSize; k++) { for (k = 0; k < stepSize; k++) { gOutFIFO[k] = gOutputAccum[k];
gOutFIFO[k] = gOutputAccum[k]; }
}
/* shift accumulator */ /* shift accumulator */
memmove(gOutputAccum, gOutputAccum+stepSize, fftFrameSize*sizeof(double)); memmove(gOutputAccum, gOutputAccum+stepSize, fftFrameSize*sizeof(float));
/* move input FIFO */ /* move input FIFO */
for (k = 0; k < inFifoLatency; k++) { for (k = 0; k < inFifoLatency; k++) { gInFIFO[k] = gInFIFO[k+stepSize];
gInFIFO[k] = gInFIFO[k+stepSize]; }
}
} }
} }
} }
void SMBPitchShift::smbFft(double *fftBuffer, int64_t fftFrameSize, int64_t sign)
void SMBPitchShift::smbFft(float *fftBuffer, long fftFrameSize, long sign)
/* /*
FFT routine, (C)1996 S.M.Bernsee. Sign = -1 is FFT, 1 is iFFT (inverse) FFT routine, (C)1996 S.M.Bernsee. Sign = -1 is FFT, 1 is iFFT (inverse)
Fills fftBuffer[0...2*fftFrameSize-1] with the Fourier transform of the Fills fftBuffer[0...2*fftFrameSize-1] with the Fourier transform of the
@ -256,16 +238,14 @@ void SMBPitchShift::smbFft(double *fftBuffer, int64_t fftFrameSize, int64_t sign
of the frequencies of interest is in fftBuffer[0...fftFrameSize]. of the frequencies of interest is in fftBuffer[0...fftFrameSize].
*/ */
{ {
double wr, wi, arg, *p1, *p2, temp; float wr, wi, arg, *p1, *p2, temp;
double tr, ti, ur, ui, *p1r, *p1i, *p2r, *p2i; float tr, ti, ur, ui, *p1r, *p1i, *p2r, *p2i;
int64_t i, bitm, j, le, le2, k, logN; long i, bitm, j, le, le2, k;
logN = (int64_t)(log(fftFrameSize) / log(2.) + .5);
for (i = 2; i < 2*fftFrameSize-2; i += 2) { for (i = 2; i < 2*fftFrameSize-2; i += 2) {
for (bitm = 2, j = 0; bitm < 2*fftFrameSize; bitm <<= 1) { for (bitm = 2, j = 0; bitm < 2*fftFrameSize; bitm <<= 1) {
if (i & bitm) { if (i & bitm) { j++;
j++; }
}
j <<= 1; j <<= 1;
} }
if (i < j) { if (i < j) {
@ -275,8 +255,7 @@ void SMBPitchShift::smbFft(double *fftBuffer, int64_t fftFrameSize, int64_t sign
*p1 = *p2; *p2 = temp; *p1 = *p2; *p2 = temp;
} }
} }
for (k = 0, le = 2; k < (long)(log((double)fftFrameSize)/log(2.)+.5); k++) {
for (k = 0, le = 2; k < logN; k++) {
le <<= 1; le <<= 1;
le2 = le>>1; le2 = le>>1;
ur = 1.0; ur = 1.0;
@ -309,14 +288,6 @@ void SMBPitchShift::smbFft(double *fftBuffer, int64_t fftFrameSize, int64_t sign
void AudioEffectPitchShiftInstance::process(const AudioFrame *p_src_frames, AudioFrame *p_dst_frames, int p_frame_count) { void AudioEffectPitchShiftInstance::process(const AudioFrame *p_src_frames, AudioFrame *p_dst_frames, int p_frame_count) {
float sample_rate = AudioServer::get_singleton()->get_mix_rate(); float sample_rate = AudioServer::get_singleton()->get_mix_rate();
// For pitch_scale 1.0 it's cheaper to just pass samples without processing them.
if (Math::is_equal_approx(base->pitch_scale, 1.0f)) {
for (int i = 0; i < p_frame_count; i++) {
p_dst_frames[i] = p_src_frames[i];
}
return;
}
float *in_l = (float *)p_src_frames; float *in_l = (float *)p_src_frames;
float *in_r = in_l + 1; float *in_r = in_l + 1;
@ -390,4 +361,7 @@ AudioEffectPitchShift::AudioEffectPitchShift() {
pitch_scale = 1.0; pitch_scale = 1.0;
oversampling = 4; oversampling = 4;
fft_size = FFT_SIZE_2048; fft_size = FFT_SIZE_2048;
wet = 0.0;
dry = 0.0;
filter = false;
} }

View File

@ -40,33 +40,31 @@ class SMBPitchShift {
float gInFIFO[MAX_FRAME_LENGTH]; float gInFIFO[MAX_FRAME_LENGTH];
float gOutFIFO[MAX_FRAME_LENGTH]; float gOutFIFO[MAX_FRAME_LENGTH];
double gFFTworksp[2 * MAX_FRAME_LENGTH]; float gFFTworksp[2 * MAX_FRAME_LENGTH];
double gLastPhase[MAX_FRAME_LENGTH / 2 + 1]; float gLastPhase[MAX_FRAME_LENGTH / 2 + 1];
double gSumPhase[MAX_FRAME_LENGTH / 2 + 1]; float gSumPhase[MAX_FRAME_LENGTH / 2 + 1];
double gOutputAccum[2 * MAX_FRAME_LENGTH]; float gOutputAccum[2 * MAX_FRAME_LENGTH];
double gAnaFreq[MAX_FRAME_LENGTH]; float gAnaFreq[MAX_FRAME_LENGTH];
double gAnaMagn[MAX_FRAME_LENGTH]; float gAnaMagn[MAX_FRAME_LENGTH];
double gSynFreq[MAX_FRAME_LENGTH]; float gSynFreq[MAX_FRAME_LENGTH];
double gSynMagn[MAX_FRAME_LENGTH]; float gSynMagn[MAX_FRAME_LENGTH];
int64_t gRover; long gRover;
float lastPitchShift;
void smbFft(double *fftBuffer, int64_t fftFrameSize, int64_t sign); void smbFft(float *fftBuffer, long fftFrameSize, long sign);
public: public:
void PitchShift(float pitchShift, int64_t numSampsToProcess, int64_t fftFrameSize, int64_t osamp, float sampleRate, float *indata, float *outdata, int stride); void PitchShift(float pitchShift, long numSampsToProcess, long fftFrameSize, long osamp, float sampleRate, float *indata, float *outdata, int stride);
SMBPitchShift() { SMBPitchShift() {
gRover = 0; gRover = 0;
memset(gInFIFO, 0, MAX_FRAME_LENGTH * sizeof(float)); memset(gInFIFO, 0, MAX_FRAME_LENGTH * sizeof(float));
memset(gOutFIFO, 0, MAX_FRAME_LENGTH * sizeof(float)); memset(gOutFIFO, 0, MAX_FRAME_LENGTH * sizeof(float));
memset(gFFTworksp, 0, 2 * MAX_FRAME_LENGTH * sizeof(double)); memset(gFFTworksp, 0, 2 * MAX_FRAME_LENGTH * sizeof(float));
memset(gLastPhase, 0, (MAX_FRAME_LENGTH / 2 + 1) * sizeof(double)); memset(gLastPhase, 0, (MAX_FRAME_LENGTH / 2 + 1) * sizeof(float));
memset(gSumPhase, 0, (MAX_FRAME_LENGTH / 2 + 1) * sizeof(double)); memset(gSumPhase, 0, (MAX_FRAME_LENGTH / 2 + 1) * sizeof(float));
memset(gOutputAccum, 0, 2 * MAX_FRAME_LENGTH * sizeof(double)); memset(gOutputAccum, 0, 2 * MAX_FRAME_LENGTH * sizeof(float));
memset(gAnaFreq, 0, MAX_FRAME_LENGTH * sizeof(double)); memset(gAnaFreq, 0, MAX_FRAME_LENGTH * sizeof(float));
memset(gAnaMagn, 0, MAX_FRAME_LENGTH * sizeof(double)); memset(gAnaMagn, 0, MAX_FRAME_LENGTH * sizeof(float));
lastPitchShift = 1.0;
} }
}; };
@ -103,6 +101,9 @@ public:
float pitch_scale; float pitch_scale;
int oversampling; int oversampling;
FFTSize fft_size; FFTSize fft_size;
float wet;
float dry;
bool filter;
protected: protected:
static void _bind_methods(); static void _bind_methods();