diff --git a/CMakeLists.txt b/CMakeLists.txt index afc9689e2..ee89bb602 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 diff --git a/out/install/x64-Debug/bin/furnace.exe b/out/install/x64-Debug/bin/furnace.exe new file mode 100644 index 000000000..495c41790 Binary files /dev/null and b/out/install/x64-Debug/bin/furnace.exe differ diff --git a/src/gui/doAction.cpp b/src/gui/doAction.cpp index 4d8daed18..d2fdda595 100644 --- a/src/gui/doAction.cpp +++ b/src/gui/doAction.cpp @@ -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; diff --git a/src/gui/gui.cpp b/src/gui/gui.cpp index 52fca0687..48888a15a 100644 --- a/src/gui/gui.cpp +++ b/src/gui/gui.cpp @@ -4986,7 +4986,7 @@ bool FurnaceGUI::loop() { MEASURE(compatFlags,drawCompatFlags()); MEASURE(piano,drawPiano()); MEASURE(notes,drawNotes()); - MEASURE(tuner, drawTuner()); + MEASURE(tuner,drawTuner()); MEASURE(channels,drawChannels()); MEASURE(patManager,drawPatManager()); MEASURE(sysManager,drawSysManager()); @@ -8067,7 +8067,7 @@ void FurnaceGUI::syncState() { pianoOpen=e->getConfBool("pianoOpen",false); #endif notesOpen=e->getConfBool("notesOpen",false); - tunerOpen = e->getConfBool("tunerOpen", false); + tunerOpen=e->getConfBool("tunerOpen",false); channelsOpen=e->getConfBool("channelsOpen",false); patManagerOpen=e->getConfBool("patManagerOpen",false); sysManagerOpen=e->getConfBool("sysManagerOpen",false); @@ -8226,7 +8226,7 @@ void FurnaceGUI::commitState(DivConfig& conf) { conf.set("compatFlagsOpen",compatFlagsOpen); conf.set("pianoOpen",pianoOpen); conf.set("notesOpen",notesOpen); - conf.set("tunerOpen", tunerOpen); + conf.set("tunerOpen",tunerOpen); conf.set("channelsOpen",channelsOpen); conf.set("patManagerOpen",patManagerOpen); conf.set("sysManagerOpen",sysManagerOpen); diff --git a/src/gui/gui.h b/src/gui/gui.h index 5125fe3b6..1c904ad86 100644 --- a/src/gui/gui.h +++ b/src/gui/gui.h @@ -2934,7 +2934,7 @@ class FurnaceGUI { void drawCompatFlags(); void drawPiano(); void drawNotes(bool asChild=false); - void drawTuner(bool asChild=false); + void drawTuner(); void drawChannels(); void drawPatManager(); void drawSysManager(); diff --git a/src/gui/tuner.cpp b/src/gui/tuner.cpp index 57c532142..e090afc5b 100644 --- a/src/gui/tuner.cpp +++ b/src/gui/tuner.cpp @@ -17,17 +17,114 @@ * 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" -// NOTE: please don't ask me to enable text wrap. -// Dear ImGui doesn't have that feature. D: -void FurnaceGUI::drawTuner(bool asChild) { - if (nextWindow==GUI_WINDOW_TUNER) { - tunerOpen=true; +#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; + nextWindow = GUI_WINDOW_NOTHING; } - if (!tunerOpen&&!asChild) return; - bool began=asChild?ImGui::BeginChild("Song Info##Song Information"):ImGui::Begin("Song Comments",&tunerOpen,globalWinFlags,_("Tuner")); + 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; joscBuf[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 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(); } +