add comments to tuner

This commit is contained in:
David Westgate 2024-11-15 17:24:31 -08:00
parent baa909c79d
commit 427610578e

View File

@ -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