[7d89e61] | 1 | # -*- coding: utf-8 -*- |
---|
[1827c498] | 2 | """ utilities to convert midi note number to and from note names """ |
---|
[33102ab] | 3 | |
---|
[744190f] | 4 | __all__ = ['note2midi', 'midi2note', 'freq2note'] |
---|
| 5 | |
---|
[e76842e] | 6 | import sys |
---|
[1827c498] | 7 | py3 = sys.version_info[0] == 3 |
---|
| 8 | if py3: |
---|
[016813e] | 9 | str_instances = str |
---|
| 10 | int_instances = int |
---|
[e76842e] | 11 | else: |
---|
[016813e] | 12 | str_instances = (str, unicode) |
---|
| 13 | int_instances = (int, long) |
---|
[e76842e] | 14 | |
---|
[33102ab] | 15 | def note2midi(note): |
---|
[f552e9e] | 16 | " convert note name to midi note number, e.g. [C-1, G9] -> [0, 127] " |
---|
[33102ab] | 17 | _valid_notenames = {'C': 0, 'D': 2, 'E': 4, 'F': 5, 'G': 7, 'A': 9, 'B': 11} |
---|
[3aad0b1] | 18 | _valid_modifiers = { |
---|
| 19 | u'𝄫': -2, # double flat |
---|
| 20 | u'♭': -1, 'b': -1, '\u266d': -1, # simple flat |
---|
| 21 | u'♮': 0, '\u266e': 0, None: 0, # natural |
---|
| 22 | '#': +1, u'♯': +1, '\u266f': +1, # sharp |
---|
| 23 | u'𝄪': +2, # double sharp |
---|
| 24 | } |
---|
[7d89e61] | 25 | _valid_octaves = range(-1, 10) |
---|
[016813e] | 26 | if not isinstance(note, str_instances): |
---|
[1827c498] | 27 | raise TypeError("a string is required, got %s (%s)" % (note, str(type(note)))) |
---|
| 28 | if len(note) not in range(2, 5): |
---|
| 29 | raise ValueError("string of 2 to 4 characters expected, got %d (%s)" \ |
---|
| 30 | % (len(note), note)) |
---|
[33102ab] | 31 | notename, modifier, octave = [None]*3 |
---|
| 32 | |
---|
| 33 | if len(note) == 4: |
---|
| 34 | notename, modifier, octave_sign, octave = note |
---|
| 35 | octave = octave_sign + octave |
---|
| 36 | elif len(note) == 3: |
---|
| 37 | notename, modifier, octave = note |
---|
| 38 | if modifier == '-': |
---|
| 39 | octave = modifier + octave |
---|
| 40 | modifier = None |
---|
| 41 | else: |
---|
| 42 | notename, octave = note |
---|
| 43 | |
---|
| 44 | notename = notename.upper() |
---|
| 45 | octave = int(octave) |
---|
| 46 | |
---|
| 47 | if notename not in _valid_notenames: |
---|
[b8ed85e] | 48 | raise ValueError("%s is not a valid note name" % notename) |
---|
[33102ab] | 49 | if modifier not in _valid_modifiers: |
---|
[b8ed85e] | 50 | raise ValueError("%s is not a valid modifier" % modifier) |
---|
[33102ab] | 51 | if octave not in _valid_octaves: |
---|
[b8ed85e] | 52 | raise ValueError("%s is not a valid octave" % octave) |
---|
[33102ab] | 53 | |
---|
| 54 | midi = 12 + octave * 12 + _valid_notenames[notename] + _valid_modifiers[modifier] |
---|
| 55 | if midi > 127: |
---|
[b8ed85e] | 56 | raise ValueError("%s is outside of the range C-2 to G8" % note) |
---|
[33102ab] | 57 | return midi |
---|
[9ead7a9] | 58 | |
---|
| 59 | def midi2note(midi): |
---|
| 60 | " convert midi note number to note name, e.g. [0, 127] -> [C-1, G9] " |
---|
[016813e] | 61 | if not isinstance(midi, int_instances): |
---|
[b8ed85e] | 62 | raise TypeError("an integer is required, got %s" % midi) |
---|
[1827c498] | 63 | if midi not in range(0, 128): |
---|
| 64 | raise ValueError("an integer between 0 and 127 is excepted, got %d" % midi) |
---|
[9ead7a9] | 65 | _valid_notenames = ['C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#', 'A', 'A#', 'B'] |
---|
[1827c498] | 66 | return _valid_notenames[midi % 12] + str(int(midi / 12) - 1) |
---|
[3cc1491] | 67 | |
---|
| 68 | def freq2note(freq): |
---|
[1827c498] | 69 | " convert frequency in Hz to nearest note name, e.g. [0, 22050.] -> [C-1, G9] " |
---|
[3cc1491] | 70 | from aubio import freqtomidi |
---|
| 71 | return midi2note(int(freqtomidi(freq))) |
---|