From 60df7e26f44ddf28cebdfbd6472ac04c1295c55d Mon Sep 17 00:00:00 2001 From: tildearrow Date: Mon, 4 Sep 2023 04:14:47 -0500 Subject: [PATCH] GUI: even more chan osc improvements --- src/gui/chanOsc.cpp | 90 ++++++++++++++++++++++++++++++----------- src/gui/debugWindow.cpp | 1 + 2 files changed, 68 insertions(+), 23 deletions(-) diff --git a/src/gui/chanOsc.cpp b/src/gui/chanOsc.cpp index 4f62c0d1..6d59879b 100644 --- a/src/gui/chanOsc.cpp +++ b/src/gui/chanOsc.cpp @@ -500,14 +500,20 @@ void FurnaceGUI::drawChanOsc() { fft->outBuf[1][1]=0; fftw_execute(fft->planI); + // window + for (int j=0; j<(FURNACE_FFT_SIZE>>1); j++) { + fft->corrBuf[j]*=1.0-((double)j/(double)(FURNACE_FFT_SIZE<<1)); + } + // find size of period double waveLen=FURNACE_FFT_SIZE-1; double waveLenCandL=DBL_MAX; double waveLenCandH=DBL_MIN; int waveLenBottom=0; + int waveLenTop=0; // find lowest point - for (int j=(FURNACE_FFT_SIZE>>1); j>2; j--) { + for (int j=(FURNACE_FFT_SIZE>>2); j>2; j--) { if (fft->corrBuf[j]corrBuf[j]; waveLenBottom=j; @@ -515,19 +521,20 @@ void FurnaceGUI::drawChanOsc() { } // find highest point - for (int j=(FURNACE_FFT_SIZE>>1); j>waveLenBottom; j--) { + for (int j=(FURNACE_FFT_SIZE>>1)-1; j>waveLenBottom; j--) { if (fft->corrBuf[j]>waveLenCandH) { waveLenCandH=fft->corrBuf[j]; waveLen=j; } } + waveLenTop=waveLen; // 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); + chanOscPitch[ch]=pow(1.0-(waveLen/(double)(FURNACE_FFT_SIZE>>1)),4.0); + + waveLen*=(double)displaySize*2.0/(double)FURNACE_FFT_SIZE; // DFT of one period (x_1) double dft[2]; @@ -548,23 +555,63 @@ void FurnaceGUI::drawChanOsc() { //needlePos-=(2*waveLen-fmod(displaySize,waveLen*2))*0.5; } } + + // 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; jcorrBuf[(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"); + } } - needlePos-=displaySize; - for (unsigned short j=0; jdata[(unsigned short)(needlePos+(j*displaySize/precision))]/32768.0f; - if (minLevel>y) minLevel=y; - if (maxLeveldata[(unsigned short)(needlePos+(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)); + if (!debugFFT || !loudEnough) { + needlePos-=displaySize; + for (unsigned short j=0; jdata[(unsigned short)(needlePos+(j*displaySize/precision))]/32768.0f; + if (minLevel>y) minLevel=y; + if (maxLeveldata[(unsigned short)(needlePos+(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); @@ -693,9 +740,6 @@ void FurnaceGUI::drawChanOsc() { if (ImGui::IsItemClicked(ImGuiMouseButton_Right)) { chanOscOptions=!chanOscOptions; } - if (ImGui::IsItemClicked(ImGuiMouseButton_Left)) { - debugFFT=!debugFFT; - } } ImGui::PopStyleVar(); } diff --git a/src/gui/debugWindow.cpp b/src/gui/debugWindow.cpp index b3c07b07..91620403 100644 --- a/src/gui/debugWindow.cpp +++ b/src/gui/debugWindow.cpp @@ -212,6 +212,7 @@ void FurnaceGUI::drawDebug() { } if (ImGui::TreeNode("Oscilloscope Debug")) { int c=0; + ImGui::Checkbox("FFT debug view",&debugFFT); for (int i=0; isong.systemLen; i++) { DivSystem system=e->song.system[i]; if (e->getChannelCount(system)>0) {