From 427610578e0dc94326ae3ad81659a7abd18c9b68 Mon Sep 17 00:00:00 2001 From: David Westgate Date: Fri, 15 Nov 2024 17:24:31 -0800 Subject: [PATCH] add comments to tuner --- code/tuner/main.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/code/tuner/main.py b/code/tuner/main.py index 567a72e..5e26195 100644 --- a/code/tuner/main.py +++ b/code/tuner/main.py @@ -15,6 +15,7 @@ DEBUG = True NOTES = "A B♭/A♯ B C D♭/C♯ D E♭/D♯ E F F♯/G♭ G A♭/G♯".split() +# Get the note and octave using known note forumla def calculate_note(frequency): if frequency > 0: note_num = round(math.log2(frequency / 440) * 12) @@ -25,6 +26,8 @@ def calculate_note(frequency): return 0 +# Attempt to get the dominant frequency by windowing our data sample, and taking an FFT +# In theory, the highest magnitude frequency of the FFT result should reflect what the human is trying to play in the signal def dominant_frequency(data, samplerate): windowed_data = data * np.hanning(len(data)) fft_result = np.fft.rfft(windowed_data) @@ -44,6 +47,7 @@ def audio_callback(indata: np.ndarray, frames: int, time: Any, status: Any): print(f"Status: {status}") rms = calculate_rms(indata) + # Ignore low noise to prevent spammy-ness and keep last audible result shown if rms > QUIET_THRESHOLD: mono_data = np.mean(indata, axis=1) frequency = dominant_frequency(mono_data, SAMPLE_RATE) @@ -81,5 +85,3 @@ except KeyboardInterrupt: finally: print("Stream closed.") -# https://python-sounddevice.readthedocs.io/en/0.5.1/examples.html#plot-microphone-signal-s-in-real-time -# https://github.com/mzucker/python-tuner/blob/master/tuner.py