Compare commits

...

3 Commits

Author SHA1 Message Date
tildearrow 7a78ec1b60 GUI: optimize chan osc
don't process FFT if not loud enough
don't process DFT if we couldn't determine wave length
2023-09-03 20:09:03 -05:00
tildearrow 90980a3062 GUI: center chan osc 2023-09-03 19:08:30 -05:00
tildearrow 83c64aa4b4 fix the crash
the hell? one double and suddenly it crashes on Android?
2023-09-03 17:18:31 -05:00
1 changed files with 76 additions and 95 deletions

View File

@ -23,7 +23,6 @@
#include "imgui.h"
#include "imgui_internal.h"
#include "misc/cpp/imgui_stdlib.h"
#include <complex>
#define FURNACE_FFT_SIZE 4096
#define FURNACE_FFT_RATE 80.0
@ -422,6 +421,15 @@ void FurnaceGUI::drawChanOsc() {
fft->corrBuf=(double*)fftw_malloc(FURNACE_FFT_SIZE*sizeof(double));
fft->plan=fftw_plan_dft_r2c_1d(FURNACE_FFT_SIZE,fft->inBuf,fft->outBuf,FFTW_ESTIMATE);
fft->planI=fftw_plan_dft_c2r_1d(FURNACE_FFT_SIZE,fft->outBuf,fft->corrBuf,FFTW_ESTIMATE);
if (fft->plan==NULL) {
logE("failed to create plan!");
}
if (fft->planI==NULL) {
logE("failed to create inverse plan!");
}
if (fft->inBuf==NULL || fft->outBuf==NULL || fft->corrBuf==NULL) {
logE("failed to create FFT buffers");
}
}
int displaySize=(float)(buf->rate)*(chanOscWindowSize/1000.0f);
@ -461,97 +469,86 @@ void FurnaceGUI::drawChanOsc() {
// if you know how, please tell me
// initialization
double phase=0.0;
float minLevel=1.0f;
float maxLevel=-1.0f;
float dcOff=0.0f;
unsigned short needlePos=buf->needle;
//unsigned short needlePosOrig=needlePos;
bool loudEnough=false;
// 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;
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));
}
//memset(&fft->inBuf[FURNACE_FFT_SIZE>>1],0,sizeof(double)*(FURNACE_FFT_SIZE>>1));
fftw_execute(fft->plan);
// auto-correlation and second FFT
for (int j=0; j<FURNACE_FFT_SIZE; j++) {
//fft->outBuf[j][0]-=fft->outBuf[0][0];
//fft->outBuf[j][1]-=fft->outBuf[0][1];
fft->outBuf[j][0]/=FURNACE_FFT_SIZE;
fft->outBuf[j][1]/=FURNACE_FFT_SIZE;
fft->outBuf[j][0]=fft->outBuf[j][0]*fft->outBuf[j][0]+fft->outBuf[j][1]*fft->outBuf[j][1];
fft->outBuf[j][1]=0;
}
fft->outBuf[0][0]=0;
fft->outBuf[0][1]=0;
fft->outBuf[1][0]=0;
fft->outBuf[1][1]=0;
//memset(&fft->outBuf[FURNACE_FFT_SIZE],0,sizeof(fftw_complex)*FURNACE_FFT_SIZE);
fftw_execute(fft->planI);
// high-pass
/*for (int j=1; j<(FURNACE_FFT_SIZE>>1); j++) {
fft->corrBuf[j]-=fft->corrBuf[j-1];
}*/
// only proceed if not quiet
if (loudEnough) {
fftw_execute(fft->plan);
// find size of period
double waveLen=FURNACE_FFT_SIZE;
double waveLenCandL=DBL_MAX;
double waveLenCandH=DBL_MIN;
int waveLenBottom=0;
/*
int waveLenTop=0;
double waveLenCandL2=DBL_MAX;*/
// find lowest point
for (int j=(FURNACE_FFT_SIZE>>1); j>2; j--) {
if (fft->corrBuf[j]<waveLenCandL) {
waveLenCandL=fft->corrBuf[j];
waveLenBottom=j;
// auto-correlation and second FFT
for (int j=0; j<FURNACE_FFT_SIZE; j++) {
fft->outBuf[j][0]/=FURNACE_FFT_SIZE;
fft->outBuf[j][1]/=FURNACE_FFT_SIZE;
fft->outBuf[j][0]=fft->outBuf[j][0]*fft->outBuf[j][0]+fft->outBuf[j][1]*fft->outBuf[j][1];
fft->outBuf[j][1]=0;
}
fft->outBuf[0][0]=0;
fft->outBuf[0][1]=0;
fft->outBuf[1][0]=0;
fft->outBuf[1][1]=0;
fftw_execute(fft->planI);
// find size of period
double waveLen=FURNACE_FFT_SIZE-1;
double waveLenCandL=DBL_MAX;
double waveLenCandH=DBL_MIN;
int waveLenBottom=0;
// find lowest point
for (int j=(FURNACE_FFT_SIZE>>1); j>2; j--) {
if (fft->corrBuf[j]<waveLenCandL) {
waveLenCandL=fft->corrBuf[j];
waveLenBottom=j;
}
}
// find highest point
for (int j=(FURNACE_FFT_SIZE>>1); j>waveLenBottom; j--) {
if (fft->corrBuf[j]>waveLenCandH) {
waveLenCandH=fft->corrBuf[j];
waveLen=j;
}
}
// did we find the period size?
if (waveLen<(FURNACE_FFT_SIZE-32)) {
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)
double dft[2];
dft[0]=0.0;
dft[1]=0.0;
for (int j=0; j<waveLen; j++) {
double one=((double)buf->data[(unsigned short)(needlePos-((int)waveLen)+j)&0xffff]/32768.0);
double two=(double)j*(-2.0*M_PI)/waveLen;
dft[0]+=one*cos(two);
dft[1]+=one*sin(two);
}
// calculate and lock into phase
phase=(0.5+(atan2(dft[1],dft[0])/(2.0*M_PI)));
if (chanOscWaveCorr) {
needlePos-=phase*waveLen;
needlePos-=(2*waveLen-fmod(displaySize,waveLen*2))*0.5;
}
}
}
// find highest point
for (int j=(FURNACE_FFT_SIZE>>1); j>waveLenBottom; j--) {
if (fft->corrBuf[j]>waveLenCandH) {
waveLenCandH=fft->corrBuf[j];
waveLen=j;
}
}
/*
// find next lowest point
for (int j=(FURNACE_FFT_SIZE>>1); j>waveLenTop; j--) {
if (fft->corrBuf[j]<waveLenCandL2) {
waveLenCandL2=fft->corrBuf[j];
waveLen=j-waveLenTop;
}
}*/
waveLen*=(double)displaySize*2.0/(double)FURNACE_FFT_SIZE;
// DFT of one period (x_1)
std::complex<double> dft(0,0);
for (int j=0; j<waveLen; j++) {
dft+=((double)buf->data[(unsigned short)(needlePos-waveLen+j)]/32768.0)*std::exp((-std::complex<double>(0.0,1.0)*2.0*M_PI/(double)waveLen)*(double)j);
}
// calculate and lock into phase
double phase=(0.5+(atan2(dft.imag(),dft.real())/(2.0*M_PI)));
//phase-=sin(4.0*phase*M_PI)/21.0;
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) {
needlePos-=phase*waveLen;
needlePos-=displaySize/waveLen;
}
//chanOscPitch[ch]=(float)point/32.0f;
needlePos-=displaySize;
for (unsigned short j=0; j<precision; j++) {
@ -569,22 +566,6 @@ void FurnaceGUI::drawChanOsc() {
y*=chanOscAmplify;
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);
if (chanOscUseGrad) {