sorta works
not exactly proud of it bleh. dont think ill make a GUI so idk with future progress on this
This commit is contained in:
parent
dd8f7ed21e
commit
14bdd7878d
6 changed files with 116 additions and 12 deletions
|
|
@ -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; 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();
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue