From: Paul Brossier Date: Tue, 30 Oct 2018 12:22:44 +0000 (+0100) Subject: Merge branch 'master' into feature/docstrings X-Git-Tag: 0.4.8~90^2~16 X-Git-Url: https://git.aubio.org/?a=commitdiff_plain;h=ed596f7df20df54e45e0a0bd76da6d9cb98ad67f;p=aubio.git Merge branch 'master' into feature/docstrings --- ed596f7df20df54e45e0a0bd76da6d9cb98ad67f diff --cc python/lib/aubio/midiconv.py index 3d2aa637,7faa1be4..dcafda1c --- a/python/lib/aubio/midiconv.py +++ b/python/lib/aubio/midiconv.py @@@ -134,24 -72,29 +140,47 @@@ def midi2note(midi) return _valid_notenames[midi % 12] + str(int(midi / 12) - 1) def freq2note(freq): - " convert frequency in Hz to nearest note name, e.g. [0, 22050.] -> [C-1, G9] " + """Convert frequency in Hz to nearest note name. + + Parameters + ---------- + freq : float [0, 23000[ + input frequency, in Hz + + Returns + ------- + str + name of the nearest note + + Example + ------- + >>> aubio.freq2note(440) + 'A4' + >>> aubio.freq2note(220.1) + 'A3' + """ - from aubio import freqtomidi - return midi2note(int(freqtomidi(freq))) + nearest_note = int(freqtomidi(freq) + .5) + return midi2note(nearest_note) + + def note2freq(note): + """Convert note name to corresponding frequency, in Hz. + + Parameters + ---------- + note : str + input note name + + Returns + ------- + freq : float [0, 23000[ + frequency, in Hz + + Example + ------- + >>> aubio.note2freq('A4') + 440 + >>> aubio.note2freq('A3') + 220.1 + """ + midi = note2midi(note) + return miditofreq(midi)