[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): |
---|
[7165fc56] | 16 | """Convert note name to midi note number. |
---|
| 17 | |
---|
| 18 | Input string `note` should be composed of one note root |
---|
| 19 | and one octave, with optionally one modifier in between. |
---|
| 20 | |
---|
| 21 | List of valid components: |
---|
| 22 | |
---|
| 23 | - note roots: `C`, `D`, `E`, `F`, `G`, `A`, `B`, |
---|
| 24 | - modifiers: `b`, `#`, as well as unicode characters |
---|
| 25 | `𝄫`, `♭`, `♮`, `♯` and `𝄪`, |
---|
| 26 | - octave numbers: `-1` -> `11`. |
---|
| 27 | |
---|
| 28 | Parameters |
---|
| 29 | ---------- |
---|
| 30 | note : str |
---|
| 31 | note name |
---|
| 32 | |
---|
| 33 | Returns |
---|
| 34 | ------- |
---|
| 35 | int |
---|
| 36 | corresponding midi note number |
---|
| 37 | |
---|
| 38 | Examples |
---|
| 39 | -------- |
---|
| 40 | >>> aubio.note2midi('C#4') |
---|
| 41 | 61 |
---|
| 42 | >>> aubio.note2midi('B♭5') |
---|
| 43 | 82 |
---|
| 44 | |
---|
| 45 | Raises |
---|
| 46 | ------ |
---|
| 47 | TypeError |
---|
| 48 | If `note` was not a string. |
---|
| 49 | ValueError |
---|
| 50 | If an error was found while converting `note`. |
---|
| 51 | |
---|
| 52 | See Also |
---|
| 53 | -------- |
---|
| 54 | midi2note, freqtomidi, miditofreq |
---|
| 55 | """ |
---|
[33102ab] | 56 | _valid_notenames = {'C': 0, 'D': 2, 'E': 4, 'F': 5, 'G': 7, 'A': 9, 'B': 11} |
---|
[3aad0b1] | 57 | _valid_modifiers = { |
---|
| 58 | u'𝄫': -2, # double flat |
---|
| 59 | u'♭': -1, 'b': -1, '\u266d': -1, # simple flat |
---|
| 60 | u'♮': 0, '\u266e': 0, None: 0, # natural |
---|
| 61 | '#': +1, u'♯': +1, '\u266f': +1, # sharp |
---|
| 62 | u'𝄪': +2, # double sharp |
---|
| 63 | } |
---|
[7d89e61] | 64 | _valid_octaves = range(-1, 10) |
---|
[016813e] | 65 | if not isinstance(note, str_instances): |
---|
[1827c498] | 66 | raise TypeError("a string is required, got %s (%s)" % (note, str(type(note)))) |
---|
| 67 | if len(note) not in range(2, 5): |
---|
| 68 | raise ValueError("string of 2 to 4 characters expected, got %d (%s)" \ |
---|
| 69 | % (len(note), note)) |
---|
[33102ab] | 70 | notename, modifier, octave = [None]*3 |
---|
| 71 | |
---|
| 72 | if len(note) == 4: |
---|
| 73 | notename, modifier, octave_sign, octave = note |
---|
| 74 | octave = octave_sign + octave |
---|
| 75 | elif len(note) == 3: |
---|
| 76 | notename, modifier, octave = note |
---|
| 77 | if modifier == '-': |
---|
| 78 | octave = modifier + octave |
---|
| 79 | modifier = None |
---|
| 80 | else: |
---|
| 81 | notename, octave = note |
---|
| 82 | |
---|
| 83 | notename = notename.upper() |
---|
| 84 | octave = int(octave) |
---|
| 85 | |
---|
| 86 | if notename not in _valid_notenames: |
---|
[b8ed85e] | 87 | raise ValueError("%s is not a valid note name" % notename) |
---|
[33102ab] | 88 | if modifier not in _valid_modifiers: |
---|
[b8ed85e] | 89 | raise ValueError("%s is not a valid modifier" % modifier) |
---|
[33102ab] | 90 | if octave not in _valid_octaves: |
---|
[b8ed85e] | 91 | raise ValueError("%s is not a valid octave" % octave) |
---|
[33102ab] | 92 | |
---|
| 93 | midi = 12 + octave * 12 + _valid_notenames[notename] + _valid_modifiers[modifier] |
---|
| 94 | if midi > 127: |
---|
[b8ed85e] | 95 | raise ValueError("%s is outside of the range C-2 to G8" % note) |
---|
[33102ab] | 96 | return midi |
---|
[9ead7a9] | 97 | |
---|
| 98 | def midi2note(midi): |
---|
[7165fc56] | 99 | """Convert midi note number to note name. |
---|
| 100 | |
---|
| 101 | Parameters |
---|
| 102 | ---------- |
---|
| 103 | midi : int [0, 128] |
---|
| 104 | input midi note number |
---|
| 105 | |
---|
| 106 | Returns |
---|
| 107 | ------- |
---|
| 108 | str |
---|
| 109 | note name |
---|
| 110 | |
---|
| 111 | Examples |
---|
| 112 | -------- |
---|
| 113 | >>> aubio.midi2note(70) |
---|
| 114 | 'A#4' |
---|
| 115 | >>> aubio.midi2note(59) |
---|
| 116 | 'B3' |
---|
| 117 | |
---|
| 118 | Raises |
---|
| 119 | ------ |
---|
| 120 | TypeError |
---|
| 121 | If `midi` was not an integer. |
---|
| 122 | ValueError |
---|
| 123 | If `midi` is out of the range `[0, 128]`. |
---|
| 124 | |
---|
| 125 | See Also |
---|
| 126 | -------- |
---|
| 127 | note2midi, miditofreq, freqtomidi |
---|
| 128 | """ |
---|
[016813e] | 129 | if not isinstance(midi, int_instances): |
---|
[b8ed85e] | 130 | raise TypeError("an integer is required, got %s" % midi) |
---|
[1827c498] | 131 | if midi not in range(0, 128): |
---|
| 132 | raise ValueError("an integer between 0 and 127 is excepted, got %d" % midi) |
---|
[9ead7a9] | 133 | _valid_notenames = ['C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#', 'A', 'A#', 'B'] |
---|
[1827c498] | 134 | return _valid_notenames[midi % 12] + str(int(midi / 12) - 1) |
---|
[3cc1491] | 135 | |
---|
| 136 | def freq2note(freq): |
---|
[7165fc56] | 137 | """Convert frequency in Hz to nearest note name. |
---|
| 138 | |
---|
| 139 | Parameters |
---|
| 140 | ---------- |
---|
| 141 | freq : float [0, 23000[ |
---|
| 142 | input frequency, in Hz |
---|
| 143 | |
---|
| 144 | Returns |
---|
| 145 | ------- |
---|
| 146 | str |
---|
| 147 | name of the nearest note |
---|
| 148 | |
---|
| 149 | Example |
---|
| 150 | ------- |
---|
| 151 | >>> aubio.freq2note(440) |
---|
| 152 | 'A4' |
---|
| 153 | >>> aubio.freq2note(220.1) |
---|
| 154 | 'A3' |
---|
| 155 | """ |
---|
[3cc1491] | 156 | from aubio import freqtomidi |
---|
| 157 | return midi2note(int(freqtomidi(freq))) |
---|