Skip to content

Commit

Permalink
Include note name in estimated pitch label.
Browse files Browse the repository at this point in the history
e.g. it might show "440Hz (A4)"
This should fulfill tlecomte#234
  • Loading branch information
celeste-sinead committed Mar 14, 2024
1 parent a407c59 commit 891253d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
2 changes: 1 addition & 1 deletion friture/spectrum.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ def handle_new_data(self, floatdata):
# likely to correspond to a fundamental frequency.
harmonic_products = self.harmonic_product_spectrum(sp1)
pitch_idx = argmax(harmonic_products)
fpitch = self.freq[pitch_idx]
fpitch = max(self.freq[pitch_idx], 1e-20)

self.PlotZoneSpect.setdata(self.freq, dB_spectrogram, fmax, fpitch)

Expand Down
14 changes: 13 additions & 1 deletion friture/spectrumPlotWidget.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,18 @@ def set_baseline_displayUnits(self, baseline):
def set_baseline_dataUnits(self, baseline):
self._baseline = Baseline.DATA_ZERO


def freq_to_note(self, freq: float) -> str:
if freq == 0:
return ""
# number of semitones from C4
# A4 = 440Hz and is 9 semitones above C4
semitone = round(np.log2(freq/440) * 12) + 9
octave = int(np.floor(semitone / 12)) + 4
notes = ["C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"]
return f'{notes[semitone % 12]}{octave}'


def setdata(self, x, y, fmax, fpitch):
if not self.paused:
if fmax < 2e2:
Expand All @@ -145,7 +157,7 @@ def setdata(self, x, y, fmax, fpitch):
text = "%d Hz" % (np.rint(fmax))
self._spectrum_data.setFmax(text, self.normHorizontalScaleTransform.toScreen(fmax))
self._spectrum_data.setFpitch(
f'{int(fpitch)} Hz',
f'{int(fpitch)} Hz ({self.freq_to_note(fpitch)})',
self.normHorizontalScaleTransform.toScreen(fpitch)
)

Expand Down

0 comments on commit 891253d

Please sign in to comment.