rearrange variables, add needle ui, use builtin note arrays, code style
This commit is contained in:
parent
e9e94949ea
commit
c7c3637b1b
4 changed files with 147 additions and 84 deletions
|
|
@ -19,14 +19,11 @@
|
|||
|
||||
#define _USE_MATH_DEFINES
|
||||
#include "gui.h"
|
||||
#include "../ta-log.h"
|
||||
#include "imgui.h"
|
||||
#include "imgui_internal.h"
|
||||
#include "misc/cpp/imgui_stdlib.h"
|
||||
|
||||
#define FURNACE_FFT_SIZE 16384//4096
|
||||
#define FURNACE_FFT_RATE 80.0
|
||||
#define FURNACE_FFT_CUTOFF 0.1
|
||||
#define FURNACE_TUNER_FFT_SIZE 8192
|
||||
|
||||
void FurnaceGUI::drawTuner() {
|
||||
if (nextWindow == GUI_WINDOW_TUNER) {
|
||||
|
|
@ -38,40 +35,39 @@ void FurnaceGUI::drawTuner() {
|
|||
if (ImGui::Begin("Tuner", &tunerOpen, globalWinFlags, _("Tuner"))) {
|
||||
|
||||
//fft buffer
|
||||
static double inBuf[FURNACE_FFT_SIZE];
|
||||
static fftw_complex outBuf[FURNACE_FFT_SIZE];
|
||||
static fftw_plan plan=nullptr;
|
||||
if (!tunerFFTInBuf) tunerFFTInBuf=new double[FURNACE_TUNER_FFT_SIZE];
|
||||
if (!tunerFFTOutBuf) tunerFFTOutBuf=(fftw_complex*)fftw_malloc(sizeof(fftw_complex)*FURNACE_TUNER_FFT_SIZE);
|
||||
|
||||
if (!plan) {
|
||||
plan=fftw_plan_dft_r2c_1d(FURNACE_FFT_SIZE, inBuf, outBuf, FFTW_ESTIMATE);
|
||||
if (!tunerPlan) {
|
||||
tunerPlan=fftw_plan_dft_r2c_1d(FURNACE_TUNER_FFT_SIZE, tunerFFTInBuf, tunerFFTOutBuf, FFTW_ESTIMATE);
|
||||
}
|
||||
|
||||
int chans=e->getAudioDescGot().outChans;
|
||||
int needle=e->oscWritePos;
|
||||
int needle=e->oscReadPos;
|
||||
|
||||
for (int j=0; j<FURNACE_FFT_SIZE; j++) {
|
||||
int pos=(needle-j) & 0x7fff;
|
||||
for (int j=0; j<FURNACE_TUNER_FFT_SIZE; j++) {
|
||||
int pos=(needle-FURNACE_TUNER_FFT_SIZE+j) & 0x7fff;
|
||||
double sample=0.0;
|
||||
for (int ch=0; ch<chans; ch++) {
|
||||
sample+=e->oscBuf[ch][pos];
|
||||
}
|
||||
sample=sample/chans;
|
||||
|
||||
inBuf[j]=sample * (0.5 * (1.0 - cos(2.0 * M_PI * j / (FURNACE_FFT_SIZE - 1))));
|
||||
tunerFFTInBuf[j]=sample*(0.5*(1.0-cos(2.0*M_PI*j/(FURNACE_TUNER_FFT_SIZE-1))));
|
||||
}
|
||||
|
||||
fftw_execute(plan);
|
||||
fftw_execute(tunerPlan);
|
||||
|
||||
std::vector<double> mag(FURNACE_FFT_SIZE / 2);
|
||||
for (int k=0; k < FURNACE_FFT_SIZE / 2; k++) {
|
||||
mag[k]=sqrt(outBuf[k][0]*outBuf[k][0]+outBuf[k][1]*outBuf[k][1]);
|
||||
std::vector<double> mag(FURNACE_TUNER_FFT_SIZE/2);
|
||||
for (int k=0; k < FURNACE_TUNER_FFT_SIZE / 2; k++) {
|
||||
mag[k]=sqrt(tunerFFTOutBuf[k][0]*tunerFFTOutBuf[k][0]+tunerFFTOutBuf[k][1]*tunerFFTOutBuf[k][1]);
|
||||
}
|
||||
|
||||
//harmonic product spectrum
|
||||
int harmonics = 2;
|
||||
for (int h = 2; h <= harmonics; h++) {
|
||||
for (int k = 0; k < FURNACE_FFT_SIZE / (2 * h); k++) {
|
||||
mag[k] *= mag[k * h];
|
||||
int harmonics=2;
|
||||
for (int h=2; h<=harmonics; h++) {
|
||||
for (int k=0; k<FURNACE_TUNER_FFT_SIZE/(2*h); k++) {
|
||||
mag[k]*=mag[k*h];
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -85,40 +81,68 @@ void FurnaceGUI::drawTuner() {
|
|||
double beta = mag[peakIndex];
|
||||
double gamma = mag[peakIndex + 1];
|
||||
double p = 0.5 * (alpha - gamma) / (alpha - 2.0 * beta + gamma);
|
||||
freq = (peakIndex + p) * (sampleRate / (double)FURNACE_FFT_SIZE);
|
||||
freq = (peakIndex + p) * (sampleRate / (double)FURNACE_TUNER_FFT_SIZE);
|
||||
}
|
||||
else {
|
||||
freq = (double)peakIndex * (sampleRate / (double)FURNACE_FFT_SIZE);
|
||||
freq = (double)peakIndex * (sampleRate / (double)FURNACE_TUNER_FFT_SIZE);
|
||||
}
|
||||
|
||||
|
||||
//tuning formulas
|
||||
double noteExact=0;
|
||||
int noteRounded=0;
|
||||
double cents=0.0f;
|
||||
if (freq > 0 && freq < 5000.0) {
|
||||
double noteExact = log2(freq / 440.0) * 12.0 + 45.0;
|
||||
int noteRounded = (int)std::round(noteExact);
|
||||
|
||||
int noteInOct = noteRounded % 12;
|
||||
if (noteInOct < 0) noteInOct += 12;
|
||||
|
||||
int octave = noteRounded / 12;
|
||||
if (noteRounded < 0 && (noteRounded % 12)) --octave;
|
||||
|
||||
double cents = (noteExact - noteRounded) * 100.0;
|
||||
|
||||
static const char* names[12] = {
|
||||
"C","C#","D","D#","E","F","F#","G","G#","A","A#","B"
|
||||
};
|
||||
|
||||
|
||||
|
||||
ImGui::Text("Note: %s%d", names[noteInOct], octave);
|
||||
ImGui::Text("Freq: %.2f Hz", freq);
|
||||
ImGui::Text("Cents offset: %+0.1f", cents);
|
||||
noteExact=CLAMP(log2(freq / e->song.tuning) * 12.0 + 117.0,0,180);
|
||||
noteRounded=round(noteExact);
|
||||
cents=noteExact-noteRounded;
|
||||
}
|
||||
else {
|
||||
ImGui::Text("Note: -");
|
||||
ImGui::Text("Freq: 0.00 Hz");
|
||||
ImGui::Text("Cents offset: +0.0");
|
||||
String noteText=fmt::sprintf("%s",(freq > 0 && freq < 5000.0)?noteName(noteExact):"---");
|
||||
ImGui::Text("Note: %s", noteText.c_str());
|
||||
ImGui::Text("Freq: %f Hz", freq);
|
||||
ImGui::Text("Cents: %f ", cents*100.0f);
|
||||
|
||||
{
|
||||
ImDrawList* dl=ImGui::GetWindowDrawList();
|
||||
ImVec2 origin=ImGui::GetWindowPos();
|
||||
ImVec2 size=ImGui::GetWindowSize();
|
||||
float titleBar = ImGui::GetCurrentWindow()->TitleBarHeight;
|
||||
origin.y+=titleBar;
|
||||
size.y-=titleBar;
|
||||
// debug stuff
|
||||
ImVec2* plot=new ImVec2[FURNACE_TUNER_FFT_SIZE];
|
||||
for (size_t i=0; i<FURNACE_TUNER_FFT_SIZE; i++) {
|
||||
plot[i].x=origin.x+size.x*((float)i/FURNACE_TUNER_FFT_SIZE);
|
||||
plot[i].y=origin.y+size.y-size.y*(tunerFFTInBuf[i]+1.0f)/2.0f;
|
||||
}
|
||||
dl->AddPolyline(plot, FURNACE_TUNER_FFT_SIZE, 0xff00ff00, 0, 1.0f);
|
||||
for (size_t i=0; i<mag.size(); i++) {
|
||||
plot[i].x=origin.x+size.x*((float)i/mag.size());
|
||||
plot[i].y=origin.y+size.y-size.y*mag[i]/FURNACE_TUNER_FFT_SIZE;
|
||||
}
|
||||
dl->AddPolyline(plot, mag.size(), 0xffffffff, 0, 1.0f);
|
||||
delete[] plot;
|
||||
ImVec2 needleCenter=origin+ImVec2(size.x/2,size.y/4*3);
|
||||
float needleLength = (size.x/2)*0.9f;
|
||||
const float halfSpan=0.7; // radians
|
||||
const float trim=.2;
|
||||
float angle=cents*halfSpan;
|
||||
ImVec2 needleTip = needleCenter + ImVec2(
|
||||
needleLength*sin(angle),
|
||||
-needleLength*cos(angle)
|
||||
);
|
||||
// text
|
||||
ImGui::PushFont(bigFont);
|
||||
ImVec2 textSize=ImGui::CalcTextSize(noteText.c_str());
|
||||
dl->AddText(needleCenter-ImVec2(textSize.x/2.0f,textSize.y/2.0f),ImGui::ColorConvertFloat4ToU32(uiColors[GUI_COLOR_TEXT]),noteText.c_str());
|
||||
ImGui::PopFont();
|
||||
// needle
|
||||
needleCenter = needleCenter+ImVec2(
|
||||
needleLength*sin(angle)*trim,
|
||||
-needleLength*cos(angle)*trim
|
||||
);
|
||||
|
||||
dl->AddLine(needleCenter, needleTip, 0xff00ffff, 2.0f);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue