GUI: optimize chan osc

don't process FFT if not loud enough
don't process DFT if we couldn't determine wave length
This commit is contained in:
tildearrow 2023-09-03 20:09:03 -05:00
parent 90980a3062
commit 7a78ec1b60

View file

@ -469,16 +469,22 @@ void FurnaceGUI::drawChanOsc() {
// if you know how, please tell me // if you know how, please tell me
// initialization // initialization
double phase=0.0;
float minLevel=1.0f; float minLevel=1.0f;
float maxLevel=-1.0f; float maxLevel=-1.0f;
float dcOff=0.0f; float dcOff=0.0f;
unsigned short needlePos=buf->needle; unsigned short needlePos=buf->needle;
bool loudEnough=false;
// first FFT // first FFT
for (int j=0; j<FURNACE_FFT_SIZE; j++) { for (int j=0; j<FURNACE_FFT_SIZE; j++) {
fft->inBuf[j]=(double)buf->data[(unsigned short)(needlePos-displaySize*2+((j*displaySize*2)/(FURNACE_FFT_SIZE)))]/32768.0; fft->inBuf[j]=(double)buf->data[(unsigned short)(needlePos-displaySize*2+((j*displaySize*2)/(FURNACE_FFT_SIZE)))]/32768.0;
if (fft->inBuf[j]>0.001 || fft->inBuf[j]<-0.001) loudEnough=true;
fft->inBuf[j]*=0.55-0.45*cos(M_PI*(double)j/(double)(FURNACE_FFT_SIZE>>1)); fft->inBuf[j]*=0.55-0.45*cos(M_PI*(double)j/(double)(FURNACE_FFT_SIZE>>1));
} }
// only proceed if not quiet
if (loudEnough) {
fftw_execute(fft->plan); fftw_execute(fft->plan);
// auto-correlation and second FFT // auto-correlation and second FFT
@ -515,8 +521,14 @@ void FurnaceGUI::drawChanOsc() {
waveLen=j; waveLen=j;
} }
} }
// did we find the period size?
if (waveLen<(FURNACE_FFT_SIZE-32)) {
waveLen*=(double)displaySize*2.0/(double)FURNACE_FFT_SIZE; waveLen*=(double)displaySize*2.0/(double)FURNACE_FFT_SIZE;
// we got pitch
chanOscPitch[ch]=1.0-pow(waveLen/(double)(FURNACE_FFT_SIZE>>1),2.0);
// DFT of one period (x_1) // DFT of one period (x_1)
double dft[2]; double dft[2];
dft[0]=0.0; dft[0]=0.0;
@ -529,21 +541,14 @@ void FurnaceGUI::drawChanOsc() {
} }
// calculate and lock into phase // calculate and lock into phase
double phase=(0.5+(atan2(dft[1],dft[0])/(2.0*M_PI))); phase=(0.5+(atan2(dft[1],dft[0])/(2.0*M_PI)));
double maxavg=0.0;
for (unsigned short j=0; j<(FURNACE_FFT_SIZE>>1); j++) {
if (fabs(fft->corrBuf[j]>maxavg)) {
maxavg=fabs(fft->corrBuf[j]);
}
}
if (maxavg>0.0000001) maxavg=0.5/maxavg;
if (chanOscWaveCorr) { if (chanOscWaveCorr) {
needlePos-=phase*waveLen; needlePos-=phase*waveLen;
needlePos-=(2*waveLen-fmod(displaySize,waveLen*2))*0.5; needlePos-=(2*waveLen-fmod(displaySize,waveLen*2))*0.5;
} }
//chanOscPitch[ch]=(float)point/32.0f; }
}
needlePos-=displaySize; needlePos-=displaySize;
for (unsigned short j=0; j<precision; j++) { for (unsigned short j=0; j<precision; j++) {
@ -561,22 +566,6 @@ void FurnaceGUI::drawChanOsc() {
y*=chanOscAmplify; y*=chanOscAmplify;
waveform[j]=ImLerp(inRect.Min,inRect.Max,ImVec2(x,0.5f-y)); waveform[j]=ImLerp(inRect.Min,inRect.Max,ImVec2(x,0.5f-y));
} }
// FFT debug code!
/*if (debugFFT) {
for (unsigned short j=0; j<precision; j++) {
float x=(float)j/(float)precision;
float y=fft->corrBuf[(j*FURNACE_FFT_SIZE)/precision]*maxavg;
if (j>=precision/2) {
y=fft->inBuf[((j-(precision/2))*FURNACE_FFT_SIZE*2)/(precision)];
}
waveform[j]=ImLerp(inRect.Min,inRect.Max,ImVec2(x,0.5f-y));
}
}*/
/*String cPhase=fmt::sprintf("\n%.1f - %.2f (%.2f %.2f)",waveLen,phase,dft.imag(),dft.real());
dl->AddText(inRect.Min,0xffffffff,cPhase.c_str());*/
} }
ImU32 color=ImGui::GetColorU32(chanOscColor); ImU32 color=ImGui::GetColorU32(chanOscColor);
if (chanOscUseGrad) { if (chanOscUseGrad) {