2025-08-13 03:41:35 -04:00
|
|
|
/**
|
|
|
|
|
* Furnace Tracker - multi-system chiptune tracker
|
|
|
|
|
* Copyright (C) 2021-2025 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.
|
|
|
|
|
*/
|
|
|
|
|
|
2025-08-19 01:47:51 -04:00
|
|
|
#define _USE_MATH_DEFINES
|
2025-08-13 03:41:35 -04:00
|
|
|
#include "gui.h"
|
2025-08-19 01:47:51 -04:00
|
|
|
#include "imgui.h"
|
|
|
|
|
#include "imgui_internal.h"
|
2025-08-13 03:41:35 -04:00
|
|
|
#include "misc/cpp/imgui_stdlib.h"
|
|
|
|
|
|
2025-08-21 22:30:26 +04:00
|
|
|
#define FURNACE_TUNER_FFT_SIZE 8192
|
2025-08-19 01:47:51 -04:00
|
|
|
|
|
|
|
|
void FurnaceGUI::drawTuner() {
|
|
|
|
|
if (nextWindow == GUI_WINDOW_TUNER) {
|
|
|
|
|
tunerOpen = true;
|
2025-08-13 03:41:35 -04:00
|
|
|
ImGui::SetNextWindowFocus();
|
2025-08-19 01:47:51 -04:00
|
|
|
nextWindow = GUI_WINDOW_NOTHING;
|
2025-08-13 03:41:35 -04:00
|
|
|
}
|
2025-08-19 01:47:51 -04:00
|
|
|
if (!tunerOpen) return;
|
|
|
|
|
if (ImGui::Begin("Tuner", &tunerOpen, globalWinFlags, _("Tuner"))) {
|
|
|
|
|
|
|
|
|
|
//fft buffer
|
2025-08-21 22:30:26 +04:00
|
|
|
if (!tunerFFTInBuf) tunerFFTInBuf=new double[FURNACE_TUNER_FFT_SIZE];
|
|
|
|
|
if (!tunerFFTOutBuf) tunerFFTOutBuf=(fftw_complex*)fftw_malloc(sizeof(fftw_complex)*FURNACE_TUNER_FFT_SIZE);
|
2025-08-19 01:47:51 -04:00
|
|
|
|
2025-08-21 22:30:26 +04:00
|
|
|
if (!tunerPlan) {
|
|
|
|
|
tunerPlan=fftw_plan_dft_r2c_1d(FURNACE_TUNER_FFT_SIZE, tunerFFTInBuf, tunerFFTOutBuf, FFTW_ESTIMATE);
|
2025-08-19 01:47:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int chans=e->getAudioDescGot().outChans;
|
2025-08-21 22:30:26 +04:00
|
|
|
int needle=e->oscReadPos;
|
2025-08-19 01:47:51 -04:00
|
|
|
|
2025-08-21 22:30:26 +04:00
|
|
|
for (int j=0; j<FURNACE_TUNER_FFT_SIZE; j++) {
|
|
|
|
|
int pos=(needle-FURNACE_TUNER_FFT_SIZE+j) & 0x7fff;
|
2025-08-19 01:47:51 -04:00
|
|
|
double sample=0.0;
|
|
|
|
|
for (int ch=0; ch<chans; ch++) {
|
|
|
|
|
sample+=e->oscBuf[ch][pos];
|
|
|
|
|
}
|
|
|
|
|
sample=sample/chans;
|
|
|
|
|
|
2025-08-21 22:30:26 +04:00
|
|
|
tunerFFTInBuf[j]=sample*(0.5*(1.0-cos(2.0*M_PI*j/(FURNACE_TUNER_FFT_SIZE-1))));
|
2025-08-19 01:47:51 -04:00
|
|
|
}
|
|
|
|
|
|
2025-08-21 22:30:26 +04:00
|
|
|
fftw_execute(tunerPlan);
|
2025-08-19 01:47:51 -04:00
|
|
|
|
2025-08-21 22:30:26 +04:00
|
|
|
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]);
|
2025-08-19 01:47:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//harmonic product spectrum
|
2025-08-21 22:30:26 +04:00
|
|
|
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];
|
2025-08-19 01:47:51 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//peak with interpolation
|
|
|
|
|
int peakIndex = std::distance(mag.begin(), std::max_element(mag.begin(), mag.end()));
|
|
|
|
|
double sampleRate = e->getAudioDescGot().rate;
|
|
|
|
|
|
|
|
|
|
double freq = 0.0;
|
|
|
|
|
if (peakIndex > 0 && peakIndex < (int)mag.size() - 1) {
|
|
|
|
|
double alpha = mag[peakIndex - 1];
|
|
|
|
|
double beta = mag[peakIndex];
|
|
|
|
|
double gamma = mag[peakIndex + 1];
|
|
|
|
|
double p = 0.5 * (alpha - gamma) / (alpha - 2.0 * beta + gamma);
|
2025-08-21 22:30:26 +04:00
|
|
|
freq = (peakIndex + p) * (sampleRate / (double)FURNACE_TUNER_FFT_SIZE);
|
2025-08-19 01:47:51 -04:00
|
|
|
}
|
|
|
|
|
else {
|
2025-08-21 22:30:26 +04:00
|
|
|
freq = (double)peakIndex * (sampleRate / (double)FURNACE_TUNER_FFT_SIZE);
|
2025-08-19 01:47:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//tuning formulas
|
2025-08-21 22:30:26 +04:00
|
|
|
double noteExact=0;
|
|
|
|
|
int noteRounded=0;
|
|
|
|
|
double cents=0.0f;
|
2025-08-19 01:47:51 -04:00
|
|
|
if (freq > 0 && freq < 5000.0) {
|
2025-08-21 22:30:26 +04:00
|
|
|
noteExact=CLAMP(log2(freq / e->song.tuning) * 12.0 + 117.0,0,180);
|
|
|
|
|
noteRounded=round(noteExact);
|
|
|
|
|
cents=noteExact-noteRounded;
|
2025-08-19 01:47:51 -04:00
|
|
|
}
|
2025-08-21 22:30:26 +04:00
|
|
|
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);
|
2025-08-19 01:47:51 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (ImGui::IsWindowFocused(ImGuiFocusedFlags_ChildWindows))
|
|
|
|
|
curWindow = GUI_WINDOW_TUNER;
|
|
|
|
|
|
|
|
|
|
ImGui::End();
|
2025-08-13 03:41:35 -04:00
|
|
|
}
|
2025-08-19 01:47:51 -04:00
|
|
|
|