Showing the changes to python (2 of 2 files). Show all
Unified · Split
| @@ -1,10 +1,10 @@ | |||
| 1 | # -*- coding: utf-8 -*- | 1 | # -*- coding: utf-8 -*- |
| 2 | """ utilities to convert midi note number to and from note names """ | 2 | """ utilities to convert midi note number to and from note names """ |
| 3 | 3 | ||
| 4 | __all__ = ['note2midi', 'midi2note', 'freq2note'] | 4 | __all__ = ['note2midi', 'midi2note', 'freq2note', 'note2freq'] |
| 5 | 5 | ||
| 6 | import sys | 6 | import sys |
| 7 | from ._aubio import freqtomidi | 7 | from ._aubio import freqtomidi, miditofreq |
| 8 | 8 | ||
| 9 | py3 = sys.version_info[0] == 3 | 9 | py3 = sys.version_info[0] == 3 |
| 10 | if py3: | 10 | if py3: |
| @@ -75,3 +75,26 @@ def freq2note(freq): · 64 unmodified lines | |||
| 75 | " convert frequency in Hz to nearest note name, e.g. [0, 22050.] -> [C-1, G9] " | 75 | " convert frequency in Hz to nearest note name, e.g. [0, 22050.] -> [C-1, G9] " |
| 76 | nearest_note = int(freqtomidi(freq) + .5) | 76 | nearest_note = int(freqtomidi(freq) + .5) |
| 77 | return midi2note(nearest_note) | 77 | return midi2note(nearest_note) |
| 78 | |||
| 79 | def note2freq(note): | ||
| 80 | """Convert note name to corresponding frequency, in Hz. | ||
| 81 | |||
| 82 | Parameters | ||
| 83 | ---------- | ||
| 84 | note : str | ||
| 85 | input note name | ||
| 86 | |||
| 87 | Returns | ||
| 88 | ------- | ||
| 89 | freq : float [0, 23000[ | ||
| 90 | frequency, in Hz | ||
| 91 | |||
| 92 | Example | ||
| 93 | ------- | ||
| 94 | >>> aubio.note2freq('A4') | ||
| 95 | 440 | ||
| 96 | >>> aubio.note2freq('A3') | ||
| 97 | 220.1 | ||
| 98 | """ | ||
| 99 | midi = note2midi(note) | ||
| 100 | return miditofreq(midi) | ||
| @@ -3,7 +3,7 @@ · 2 unmodified lines | |||
| 3 | 3 | ||
| 4 | from __future__ import unicode_literals | 4 | from __future__ import unicode_literals |
| 5 | 5 | ||
| 6 | from aubio import note2midi, freq2note | 6 | from aubio import note2midi, freq2note, note2freq |
| 7 | from nose2.tools import params | 7 | from nose2.tools import params |
| 8 | import unittest | 8 | import unittest |
| 9 | 9 | ||
| @@ -119,6 +119,16 @@ class freq2note_simple_test(unittest.TestCase): · 109 unmodified lines | |||
| 119 | " make sure freq2note(439) == A4 " | 119 | " make sure freq2note(439) == A4 " |
| 120 | self.assertEqual("A4", freq2note(439)) | 120 | self.assertEqual("A4", freq2note(439)) |
| 121 | 121 | ||
| 122 | class note2freq_simple_test(unittest.TestCase): | ||
| 123 | |||
| 124 | def test_note2freq(self): | ||
| 125 | " make sure note2freq('A3') == 220" | ||
| 126 | self.assertEqual(220, note2freq("A3")) | ||
| 127 | |||
| 128 | def test_note2freq_under(self): | ||
| 129 | " make sure note2freq(A4) == 440" | ||
| 130 | self.assertEqual(440, note2freq("A4")) | ||
| 131 | |||
| 122 | if __name__ == '__main__': | 132 | if __name__ == '__main__': |
| 123 | import nose2 | 133 | import nose2 |
| 124 | nose2.main() | 134 | nose2.main() |