GUI: re-organize chan osc code
prepare for possible multi-threading
This commit is contained in:
parent
55eeb241cf
commit
c99899a002
|
@ -384,6 +384,13 @@ void FurnaceGUI::drawChanOsc() {
|
||||||
int ch=oscChans[i];
|
int ch=oscChans[i];
|
||||||
|
|
||||||
if (buf!=NULL) {
|
if (buf!=NULL) {
|
||||||
|
// prepare
|
||||||
|
if (centerSettingReset) {
|
||||||
|
buf->readNeedle=buf->needle;
|
||||||
|
}
|
||||||
|
|
||||||
|
int displaySize=(float)(buf->rate)*(chanOscWindowSize/1000.0f);
|
||||||
|
|
||||||
// check FFT status existence
|
// check FFT status existence
|
||||||
if (!fft->ready) {
|
if (!fft->ready) {
|
||||||
logD("creating FFT plan for channel %d",ch);
|
logD("creating FFT plan for channel %d",ch);
|
||||||
|
@ -416,22 +423,18 @@ void FurnaceGUI::drawChanOsc() {
|
||||||
|
|
||||||
// initialization
|
// initialization
|
||||||
double phase=0.0;
|
double phase=0.0;
|
||||||
float minLevel=1.0f;
|
fft->loudEnough=false;
|
||||||
float maxLevel=-1.0f;
|
|
||||||
float dcOff=0.0f;
|
|
||||||
bool loudEnough=false;
|
|
||||||
|
|
||||||
fft->needle=buf->needle;
|
fft->needle=buf->needle;
|
||||||
|
|
||||||
// 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)(fft->needle-displaySize*2+((j*displaySize*2)/(FURNACE_FFT_SIZE)))]/32768.0;
|
fft->inBuf[j]=(double)buf->data[(unsigned short)(fft->needle-displaySize*2+((j*displaySize*2)/(FURNACE_FFT_SIZE)))]/32768.0;
|
||||||
if (fft->inBuf[j]>0.001 || fft->inBuf[j]<-0.001) loudEnough=true;
|
if (fft->inBuf[j]>0.001 || fft->inBuf[j]<-0.001) fft->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
|
// only proceed if not quiet
|
||||||
if (loudEnough) {
|
if (fft->loudEnough) {
|
||||||
fftw_execute(fft->plan);
|
fftw_execute(fft->plan);
|
||||||
|
|
||||||
// auto-correlation and second FFT
|
// auto-correlation and second FFT
|
||||||
|
@ -453,43 +456,43 @@ void FurnaceGUI::drawChanOsc() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// find size of period
|
// find size of period
|
||||||
double waveLen=FURNACE_FFT_SIZE-1;
|
|
||||||
double waveLenCandL=DBL_MAX;
|
double waveLenCandL=DBL_MAX;
|
||||||
double waveLenCandH=DBL_MIN;
|
double waveLenCandH=DBL_MIN;
|
||||||
int waveLenBottom=0;
|
fft->waveLen=FURNACE_FFT_SIZE-1;
|
||||||
int waveLenTop=0;
|
fft->waveLenBottom=0;
|
||||||
|
fft->waveLenTop=0;
|
||||||
|
|
||||||
// find lowest point
|
// find lowest point
|
||||||
for (int j=(FURNACE_FFT_SIZE>>2); j>2; j--) {
|
for (int j=(FURNACE_FFT_SIZE>>2); j>2; j--) {
|
||||||
if (fft->corrBuf[j]<waveLenCandL) {
|
if (fft->corrBuf[j]<waveLenCandL) {
|
||||||
waveLenCandL=fft->corrBuf[j];
|
waveLenCandL=fft->corrBuf[j];
|
||||||
waveLenBottom=j;
|
fft->waveLenBottom=j;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// find highest point
|
// find highest point
|
||||||
for (int j=(FURNACE_FFT_SIZE>>1)-1; j>waveLenBottom; j--) {
|
for (int j=(FURNACE_FFT_SIZE>>1)-1; j>fft->waveLenBottom; j--) {
|
||||||
if (fft->corrBuf[j]>waveLenCandH) {
|
if (fft->corrBuf[j]>waveLenCandH) {
|
||||||
waveLenCandH=fft->corrBuf[j];
|
waveLenCandH=fft->corrBuf[j];
|
||||||
waveLen=j;
|
fft->waveLen=j;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
waveLenTop=waveLen;
|
fft->waveLenTop=fft->waveLen;
|
||||||
|
|
||||||
// did we find the period size?
|
// did we find the period size?
|
||||||
if (waveLen<(FURNACE_FFT_SIZE-32)) {
|
if (fft->waveLen<(FURNACE_FFT_SIZE-32)) {
|
||||||
// we got pitch
|
// we got pitch
|
||||||
chanOscPitch[ch]=pow(1.0-(waveLen/(double)(FURNACE_FFT_SIZE>>1)),4.0);
|
chanOscPitch[ch]=pow(1.0-(fft->waveLen/(double)(FURNACE_FFT_SIZE>>1)),4.0);
|
||||||
|
|
||||||
waveLen*=(double)displaySize*2.0/(double)FURNACE_FFT_SIZE;
|
fft->waveLen*=(double)displaySize*2.0/(double)FURNACE_FFT_SIZE;
|
||||||
|
|
||||||
// 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;
|
||||||
dft[1]=0.0;
|
dft[1]=0.0;
|
||||||
for (int j=fft->needle-1-(displaySize>>1)-(int)waveLen, k=0; k<waveLen; j++, k++) {
|
for (int j=fft->needle-1-(displaySize>>1)-(int)fft->waveLen, k=0; k<fft->waveLen; j++, k++) {
|
||||||
double one=((double)buf->data[j&0xffff]/32768.0);
|
double one=((double)buf->data[j&0xffff]/32768.0);
|
||||||
double two=(double)k*(-2.0*M_PI)/waveLen;
|
double two=(double)k*(-2.0*M_PI)/fft->waveLen;
|
||||||
dft[0]+=one*cos(two);
|
dft[0]+=one*cos(two);
|
||||||
dft[1]+=one*sin(two);
|
dft[1]+=one*sin(two);
|
||||||
}
|
}
|
||||||
|
@ -498,67 +501,12 @@ void FurnaceGUI::drawChanOsc() {
|
||||||
phase=(0.5+(atan2(dft[1],dft[0])/(2.0*M_PI)));
|
phase=(0.5+(atan2(dft[1],dft[0])/(2.0*M_PI)));
|
||||||
|
|
||||||
if (chanOscWaveCorr) {
|
if (chanOscWaveCorr) {
|
||||||
fft->needle-=phase*waveLen;
|
fft->needle-=phase*fft->waveLen;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// FFT debug code!
|
|
||||||
if (debugFFT) {
|
|
||||||
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;
|
|
||||||
|
|
||||||
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 (b: %d t: %d)",waveLen,waveLenBottom,waveLenTop);
|
|
||||||
dl->AddText(inRect.Min,0xffffffff,cPhase.c_str());
|
|
||||||
|
|
||||||
dl->AddLine(
|
|
||||||
ImLerp(inRect.Min,inRect.Max,ImVec2((double)waveLenBottom/(double)FURNACE_FFT_SIZE,0.0)),
|
|
||||||
ImLerp(inRect.Min,inRect.Max,ImVec2((double)waveLenBottom/(double)FURNACE_FFT_SIZE,1.0)),
|
|
||||||
0xffffff00
|
|
||||||
);
|
|
||||||
dl->AddLine(
|
|
||||||
ImLerp(inRect.Min,inRect.Max,ImVec2((double)waveLenTop/(double)FURNACE_FFT_SIZE,0.0)),
|
|
||||||
ImLerp(inRect.Min,inRect.Max,ImVec2((double)waveLenTop/(double)FURNACE_FFT_SIZE,1.0)),
|
|
||||||
0xff00ff00
|
|
||||||
);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (debugFFT) {
|
|
||||||
dl->AddText(inRect.Min,0xffffffff,"\nquiet");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!debugFFT || !loudEnough) {
|
fft->needle-=displaySize;
|
||||||
fft->needle-=displaySize;
|
|
||||||
for (unsigned short j=0; j<precision; j++) {
|
|
||||||
float y=(float)buf->data[(unsigned short)(fft->needle+(j*displaySize/precision))]/32768.0f;
|
|
||||||
if (minLevel>y) minLevel=y;
|
|
||||||
if (maxLevel<y) maxLevel=y;
|
|
||||||
}
|
|
||||||
dcOff=(minLevel+maxLevel)*0.5f;
|
|
||||||
for (unsigned short j=0; j<precision; j++) {
|
|
||||||
float x=(float)j/(float)precision;
|
|
||||||
float y=(float)buf->data[(unsigned short)(fft->needle+(j*displaySize/precision))]/32768.0f;
|
|
||||||
y-=dcOff;
|
|
||||||
if (y<-0.5f) y=-0.5f;
|
|
||||||
if (y>0.5f) y=0.5f;
|
|
||||||
y*=chanOscAmplify;
|
|
||||||
waveform[j]=ImLerp(inRect.Min,inRect.Max,ImVec2(x,0.5f-y));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -613,12 +561,6 @@ void FurnaceGUI::drawChanOsc() {
|
||||||
if (precision<1) precision=1;
|
if (precision<1) precision=1;
|
||||||
if (precision>1024) precision=1024;
|
if (precision>1024) precision=1024;
|
||||||
|
|
||||||
if (centerSettingReset) {
|
|
||||||
buf->readNeedle=buf->needle;
|
|
||||||
}
|
|
||||||
|
|
||||||
int displaySize=(float)(buf->rate)*(chanOscWindowSize/1000.0f);
|
|
||||||
|
|
||||||
ImGui::ItemSize(size,style.FramePadding.y);
|
ImGui::ItemSize(size,style.FramePadding.y);
|
||||||
if (ImGui::ItemAdd(rect,ImGui::GetID("chOscDisplay"))) {
|
if (ImGui::ItemAdd(rect,ImGui::GetID("chOscDisplay"))) {
|
||||||
if (!e->isRunning()) {
|
if (!e->isRunning()) {
|
||||||
|
@ -627,6 +569,68 @@ void FurnaceGUI::drawChanOsc() {
|
||||||
waveform[j]=ImLerp(inRect.Min,inRect.Max,ImVec2(x,0.5f));
|
waveform[j]=ImLerp(inRect.Min,inRect.Max,ImVec2(x,0.5f));
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
int displaySize=(float)(buf->rate)*(chanOscWindowSize/1000.0f);
|
||||||
|
|
||||||
|
float minLevel=1.0f;
|
||||||
|
float maxLevel=-1.0f;
|
||||||
|
float dcOff=0.0f;
|
||||||
|
|
||||||
|
if (debugFFT) {
|
||||||
|
// FFT debug code!
|
||||||
|
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;
|
||||||
|
|
||||||
|
for (unsigned short j=0; j<precision; j++) {
|
||||||
|
float x=(float)j/(float)precision;
|
||||||
|
float y;
|
||||||
|
if (j>=precision/2) {
|
||||||
|
y=fft->inBuf[((j-(precision/2))*FURNACE_FFT_SIZE*2)/(precision)];
|
||||||
|
} else {
|
||||||
|
y=fft->corrBuf[(j*FURNACE_FFT_SIZE)/precision]*maxavg;
|
||||||
|
}
|
||||||
|
waveform[j]=ImLerp(inRect.Min,inRect.Max,ImVec2(x,0.5f-y));
|
||||||
|
}
|
||||||
|
if (fft->loudEnough) {
|
||||||
|
String cPhase=fmt::sprintf("\n%.1f (b: %d t: %d)",fft->waveLen,fft->waveLenBottom,fft->waveLenTop);
|
||||||
|
dl->AddText(inRect.Min,0xffffffff,cPhase.c_str());
|
||||||
|
|
||||||
|
dl->AddLine(
|
||||||
|
ImLerp(inRect.Min,inRect.Max,ImVec2((double)fft->waveLenBottom/(double)FURNACE_FFT_SIZE,0.0)),
|
||||||
|
ImLerp(inRect.Min,inRect.Max,ImVec2((double)fft->waveLenBottom/(double)FURNACE_FFT_SIZE,1.0)),
|
||||||
|
0xffffff00
|
||||||
|
);
|
||||||
|
dl->AddLine(
|
||||||
|
ImLerp(inRect.Min,inRect.Max,ImVec2((double)fft->waveLenTop/(double)FURNACE_FFT_SIZE,0.0)),
|
||||||
|
ImLerp(inRect.Min,inRect.Max,ImVec2((double)fft->waveLenTop/(double)FURNACE_FFT_SIZE,1.0)),
|
||||||
|
0xff00ff00
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
if (debugFFT) {
|
||||||
|
dl->AddText(inRect.Min,0xffffffff,"\nquiet");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
for (unsigned short j=0; j<precision; j++) {
|
||||||
|
float y=(float)buf->data[(unsigned short)(fft->needle+(j*displaySize/precision))]/32768.0f;
|
||||||
|
if (minLevel>y) minLevel=y;
|
||||||
|
if (maxLevel<y) maxLevel=y;
|
||||||
|
}
|
||||||
|
dcOff=(minLevel+maxLevel)*0.5f;
|
||||||
|
for (unsigned short j=0; j<precision; j++) {
|
||||||
|
float x=(float)j/(float)precision;
|
||||||
|
float y=(float)buf->data[(unsigned short)(fft->needle+(j*displaySize/precision))]/32768.0f;
|
||||||
|
y-=dcOff;
|
||||||
|
if (y<-0.5f) y=-0.5f;
|
||||||
|
if (y>0.5f) y=0.5f;
|
||||||
|
y*=chanOscAmplify;
|
||||||
|
waveform[j]=ImLerp(inRect.Min,inRect.Max,ImVec2(x,0.5f-y));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
ImU32 color=ImGui::GetColorU32(chanOscColor);
|
ImU32 color=ImGui::GetColorU32(chanOscColor);
|
||||||
if (chanOscUseGrad) {
|
if (chanOscUseGrad) {
|
||||||
|
|
|
@ -2060,8 +2060,10 @@ class FurnaceGUI {
|
||||||
double* corrBuf;
|
double* corrBuf;
|
||||||
size_t inBufPos;
|
size_t inBufPos;
|
||||||
double inBufPosFrac;
|
double inBufPosFrac;
|
||||||
|
double waveLen;
|
||||||
|
int waveLenBottom, waveLenTop;
|
||||||
unsigned short needle;
|
unsigned short needle;
|
||||||
bool ready;
|
bool ready, loudEnough;
|
||||||
fftw_plan plan;
|
fftw_plan plan;
|
||||||
fftw_plan planI;
|
fftw_plan planI;
|
||||||
ChanOscStatus():
|
ChanOscStatus():
|
||||||
|
@ -2070,8 +2072,12 @@ class FurnaceGUI {
|
||||||
corrBuf(NULL),
|
corrBuf(NULL),
|
||||||
inBufPos(0),
|
inBufPos(0),
|
||||||
inBufPosFrac(0.0f),
|
inBufPosFrac(0.0f),
|
||||||
|
waveLen(0.0),
|
||||||
|
waveLenBottom(0),
|
||||||
|
waveLenTop(0),
|
||||||
needle(0),
|
needle(0),
|
||||||
ready(false),
|
ready(false),
|
||||||
|
loudEnough(false),
|
||||||
plan(NULL),
|
plan(NULL),
|
||||||
planI(NULL) {}
|
planI(NULL) {}
|
||||||
} chanOscChan[DIV_MAX_CHANS];
|
} chanOscChan[DIV_MAX_CHANS];
|
||||||
|
|
Loading…
Reference in New Issue