Compare commits

...

3 Commits

Author SHA1 Message Date
tildearrow 2ca5856800 a fix 2023-09-04 04:25:21 -05:00
tildearrow 60df7e26f4 GUI: even more chan osc improvements 2023-09-04 04:14:47 -05:00
tildearrow ab7b26a2e7 GUI: improve chan osc wave centering 2023-09-04 01:18:48 -05:00
4 changed files with 158 additions and 27 deletions

20
src/engine/workPool.cpp Normal file
View File

@ -0,0 +1,20 @@
/**
* Furnace Tracker - multi-system chiptune tracker
* Copyright (C) 2021-2023 tildearrow and contributors
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "workPool.h"

66
src/engine/workPool.h Normal file
View File

@ -0,0 +1,66 @@
/**
* Furnace Tracker - multi-system chiptune tracker
* Copyright (C) 2021-2023 tildearrow and contributors
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifndef _WORKPOOL_H
#define _WORKPOOL_H
#include <thread>
#include <mutex>
#include <functional>
#include <condition_variable>
struct DivWorkThread {
std::mutex lock;
std::thread* thread;
std::condition_variable notify;
bool busy, terminate;
void run();
DivWorkThread():
busy(false) {}
};
/**
* this class provides an implementation of a "thread pool" for executing tasks in parallel.
* it is highly recommended to use `new` when allocating a DivWorkPool.
*/
class DivWorkPool {
DivWorkThread* workThreads;
public:
/**
* push a new job to this work pool.
* if all work threads are busy, this will block until one is free.
*/
bool push();
/**
* check whether this work pool is busy.
*/
bool busy();
/**
* wait for all work threads to finish.
*/
bool wait();
DivWorkPool(unsigned int threads=0);
~DivWorkPool();
};
#endif

View File

@ -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]<waveLenCandL) {
waveLenCandL=fft->corrBuf[j];
waveLenBottom=j;
@ -515,27 +521,28 @@ 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];
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;
for (int j=needlePos-1-(displaySize>>1)-(int)waveLen, k=0; k<waveLen; j++, k++) {
double one=((double)buf->data[j&0xffff]/32768.0);
double two=(double)k*(-2.0*M_PI)/waveLen;
dft[0]+=one*cos(two);
dft[1]+=one*sin(two);
}
@ -545,26 +552,66 @@ void FurnaceGUI::drawChanOsc() {
if (chanOscWaveCorr) {
needlePos-=phase*waveLen;
needlePos-=(2*waveLen-fmod(displaySize,waveLen*2))*0.5;
//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; 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");
}
}
needlePos-=displaySize;
for (unsigned short j=0; j<precision; j++) {
float y=(float)buf->data[(unsigned short)(needlePos+(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)(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; j<precision; j++) {
float y=(float)buf->data[(unsigned short)(needlePos+(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)(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();
}

View File

@ -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; i<e->song.systemLen; i++) {
DivSystem system=e->song.system[i];
if (e->getChannelCount(system)>0) {