ref: 7be77bb824974e41d749cb1c34eab75ed586730c
parent: 7af37df141c7342e302f89fdf0442a04f932f0b6
parent: 476cb419d37fa81bd100c9446f691ca274bf934f
author: Paul Brossier <piem@piem.org>
date: Mon Oct 29 21:04:07 EDT 2018
Merge branch 'feature/note2freq'
--- a/python/lib/aubio/midiconv.py
+++ b/python/lib/aubio/midiconv.py
@@ -1,10 +1,10 @@
# -*- coding: utf-8 -*-
""" utilities to convert midi note number to and from note names """
-__all__ = ['note2midi', 'midi2note', 'freq2note']
+__all__ = ['note2midi', 'midi2note', 'freq2note', 'note2freq']
import sys
-from ._aubio import freqtomidi
+from ._aubio import freqtomidi, miditofreq
py3 = sys.version_info[0] == 3
if py3:
@@ -75,3 +75,26 @@
" convert frequency in Hz to nearest note name, e.g. [0, 22050.] -> [C-1, G9] "
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)
--- a/python/tests/test_note2midi.py
+++ b/python/tests/test_note2midi.py
@@ -3,7 +3,7 @@
from __future__ import unicode_literals
-from aubio import note2midi, freq2note
+from aubio import note2midi, freq2note, note2freq
from nose2.tools import params
import unittest
@@ -118,6 +118,16 @@
def test_freq2note_under(self):
" make sure freq2note(439) == A4 "
self.assertEqual("A4", freq2note(439))
+
+class note2freq_simple_test(unittest.TestCase):
+
+ def test_note2freq(self):
+ " make sure note2freq('A3') == 220"
+ self.assertEqual(220, note2freq("A3"))
+
+ def test_note2freq_under(self):
+ " make sure note2freq(A4) == 440"
+ self.assertEqual(440, note2freq("A4"))
if __name__ == '__main__':
import nose2