Merge pull request #22 from recolorme/tuner

Merge Tuner
This commit is contained in:
Eknous 2025-08-21 20:10:43 +04:00 committed by GitHub
commit e9e94949ea
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 154 additions and 1 deletions

View file

@ -937,6 +937,7 @@ src/gui/sysEx.cpp
src/gui/sysManager.cpp
src/gui/sysMiscInfo.cpp
src/gui/sysPicker.cpp
src/gui/tuner.cpp
src/gui/tutorial.cpp
src/gui/userPresets.cpp
src/gui/util.cpp

Binary file not shown.

View file

@ -315,6 +315,9 @@ void FurnaceGUI::doAction(int what) {
case GUI_ACTION_WINDOW_NOTES:
nextWindow=GUI_WINDOW_NOTES;
break;
case GUI_ACTION_WINDOW_TUNER:
nextWindow = GUI_WINDOW_TUNER;
break;
case GUI_ACTION_WINDOW_CHANNELS:
nextWindow=GUI_WINDOW_CHANNELS;
break;
@ -423,6 +426,9 @@ void FurnaceGUI::doAction(int what) {
case GUI_WINDOW_NOTES:
notesOpen=false;
break;
case GUI_WINDOW_TUNER:
notesOpen = false;
break;
case GUI_WINDOW_CHANNELS:
channelsOpen=false;
break;

View file

@ -1004,6 +1004,11 @@ Pos=60,60\n\
Size=395,171\n\
Collapsed=0\n\
\n\
[Window][Tuner]\n\
Pos=60,60\n\
Size=395,171\n\
Collapsed=0\n\
\n\
[Window][Warning##Export AudioFileDialogOverWriteDialog]\n\
Pos=381,351\n\
Size=500,71\n\
@ -3748,6 +3753,7 @@ bool FurnaceGUI::loop() {
DECLARE_METRIC(compatFlags)
DECLARE_METRIC(piano)
DECLARE_METRIC(notes)
DECLARE_METRIC(tuner)
DECLARE_METRIC(channels)
DECLARE_METRIC(patManager)
DECLARE_METRIC(sysManager)
@ -4339,6 +4345,7 @@ bool FurnaceGUI::loop() {
IMPORT_CLOSE(compatFlagsOpen);
IMPORT_CLOSE(pianoOpen);
IMPORT_CLOSE(notesOpen);
IMPORT_CLOSE(tunerOpen);
IMPORT_CLOSE(channelsOpen);
IMPORT_CLOSE(regViewOpen);
IMPORT_CLOSE(logOpen);
@ -4704,6 +4711,7 @@ bool FurnaceGUI::loop() {
if (ImGui::MenuItem(_("oscilloscope (per-channel)"),BIND_FOR(GUI_ACTION_WINDOW_CHAN_OSC),chanOscOpen)) chanOscOpen=!chanOscOpen;
if (ImGui::MenuItem(_("oscilloscope (X-Y)"),BIND_FOR(GUI_ACTION_WINDOW_XY_OSC),xyOscOpen)) xyOscOpen=!xyOscOpen;
if (ImGui::MenuItem(_("volume meter"),BIND_FOR(GUI_ACTION_WINDOW_VOL_METER),volMeterOpen)) volMeterOpen=!volMeterOpen;
if (ImGui::MenuItem(_("tuner"), BIND_FOR(GUI_ACTION_WINDOW_TUNER), tunerOpen)) tunerOpen = !tunerOpen;
ImGui::EndMenu();
}
if (ImGui::BeginMenu(_("tempo"))) {
@ -4978,6 +4986,7 @@ bool FurnaceGUI::loop() {
MEASURE(compatFlags,drawCompatFlags());
MEASURE(piano,drawPiano());
MEASURE(notes,drawNotes());
MEASURE(tuner,drawTuner());
MEASURE(channels,drawChannels());
MEASURE(patManager,drawPatManager());
MEASURE(sysManager,drawSysManager());
@ -8066,6 +8075,7 @@ void FurnaceGUI::syncState() {
pianoOpen=e->getConfBool("pianoOpen",false);
#endif
notesOpen=e->getConfBool("notesOpen",false);
tunerOpen=e->getConfBool("tunerOpen",false);
channelsOpen=e->getConfBool("channelsOpen",false);
patManagerOpen=e->getConfBool("patManagerOpen",false);
sysManagerOpen=e->getConfBool("sysManagerOpen",false);
@ -8224,6 +8234,7 @@ void FurnaceGUI::commitState(DivConfig& conf) {
conf.set("compatFlagsOpen",compatFlagsOpen);
conf.set("pianoOpen",pianoOpen);
conf.set("notesOpen",notesOpen);
conf.set("tunerOpen",tunerOpen);
conf.set("channelsOpen",channelsOpen);
conf.set("patManagerOpen",patManagerOpen);
conf.set("sysManagerOpen",sysManagerOpen);
@ -8618,6 +8629,7 @@ FurnaceGUI::FurnaceGUI():
compatFlagsOpen(false),
pianoOpen(false),
notesOpen(false),
tunerOpen(false),
channelsOpen(false),
regViewOpen(false),
logOpen(false),

View file

@ -548,6 +548,7 @@ enum FurnaceGUIWindows {
GUI_WINDOW_COMPAT_FLAGS,
GUI_WINDOW_PIANO,
GUI_WINDOW_NOTES,
GUI_WINDOW_TUNER,
GUI_WINDOW_CHANNELS,
GUI_WINDOW_PAT_MANAGER,
GUI_WINDOW_SYS_MANAGER,
@ -752,6 +753,7 @@ enum FurnaceGUIActions {
GUI_ACTION_WINDOW_COMPAT_FLAGS,
GUI_ACTION_WINDOW_PIANO,
GUI_ACTION_WINDOW_NOTES,
GUI_ACTION_WINDOW_TUNER,
GUI_ACTION_WINDOW_CHANNELS,
GUI_ACTION_WINDOW_PAT_MANAGER,
GUI_ACTION_WINDOW_SYS_MANAGER,
@ -2366,7 +2368,7 @@ class FurnaceGUI {
bool editControlsOpen, ordersOpen, insListOpen, songInfoOpen, patternOpen, insEditOpen;
bool waveListOpen, waveEditOpen, sampleListOpen, sampleEditOpen, aboutOpen, settingsOpen;
bool mixerOpen, debugOpen, inspectorOpen, oscOpen, volMeterOpen, statsOpen, compatFlagsOpen;
bool pianoOpen, notesOpen, channelsOpen, regViewOpen, logOpen, effectListOpen, chanOscOpen;
bool pianoOpen, notesOpen, tunerOpen, channelsOpen, regViewOpen, logOpen, effectListOpen, chanOscOpen;
bool subSongsOpen, findOpen, spoilerOpen, patManagerOpen, sysManagerOpen, clockOpen, speedOpen;
bool groovesOpen, xyOscOpen, memoryOpen, csPlayerOpen, cvOpen, userPresetsOpen;
@ -2938,6 +2940,7 @@ class FurnaceGUI {
void drawCompatFlags();
void drawPiano();
void drawNotes(bool asChild=false);
void drawTuner();
void drawChannels();
void drawPatManager();
void drawSysManager();

View file

@ -640,6 +640,7 @@ const FurnaceGUIActionDef guiActions[GUI_ACTION_MAX]={
D("WINDOW_COMPAT_FLAGS", _N("Compatibility Flags"), 0),
D("WINDOW_PIANO", _N("Piano"), 0),
D("WINDOW_NOTES", _N("Song Comments"), 0),
D("WINDOW_TUNER", _N("Tuner"), 0),
D("WINDOW_CHANNELS", _N("Channels"), 0),
D("WINDOW_PAT_MANAGER", _N("Pattern Manager"), 0),
D("WINDOW_SYS_MANAGER", _N("Chip Manager"), 0),

130
src/gui/tuner.cpp Normal file
View file

@ -0,0 +1,130 @@
/**
* 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.
*/
#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
void FurnaceGUI::drawTuner() {
if (nextWindow == GUI_WINDOW_TUNER) {
tunerOpen = true;
ImGui::SetNextWindowFocus();
nextWindow = GUI_WINDOW_NOTHING;
}
if (!tunerOpen) return;
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 (!plan) {
plan=fftw_plan_dft_r2c_1d(FURNACE_FFT_SIZE, inBuf, outBuf, FFTW_ESTIMATE);
}
int chans=e->getAudioDescGot().outChans;
int needle=e->oscWritePos;
for (int j=0; j<FURNACE_FFT_SIZE; j++) {
int pos=(needle-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))));
}
fftw_execute(plan);
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]);
}
//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];
}
}
//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);
freq = (peakIndex + p) * (sampleRate / (double)FURNACE_FFT_SIZE);
}
else {
freq = (double)peakIndex * (sampleRate / (double)FURNACE_FFT_SIZE);
}
//tuning formulas
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);
}
else {
ImGui::Text("Note: -");
ImGui::Text("Freq: 0.00 Hz");
ImGui::Text("Cents offset: +0.0");
}
}
if (ImGui::IsWindowFocused(ImGuiFocusedFlags_ChildWindows))
curWindow = GUI_WINDOW_TUNER;
ImGui::End();
}