1 # -*- coding: utf-8 -*-
2 """ utilities to convert midi note number to and from note names """
5 from ._aubio import freqtomidi, miditofreq
7 __all__ = ['note2midi', 'midi2note', 'freq2note', 'note2freq']
9 py3 = sys.version_info[0] == 3
14 str_instances = (str, unicode)
15 int_instances = (int, long)
19 """Convert note name to midi note number.
21 Input string `note` should be composed of one note root
22 and one octave, with optionally one modifier in between.
24 List of valid components:
26 - note roots: `C`, `D`, `E`, `F`, `G`, `A`, `B`,
27 - modifiers: `b`, `#`, as well as unicode characters
28 `𝄫`, `♭`, `♮`, `♯` and `𝄪`,
29 - octave numbers: `-1` -> `11`.
39 corresponding midi note number
43 >>> aubio.note2midi('C#4')
45 >>> aubio.note2midi('B♭5')
51 If `note` was not a string.
53 If an error was found while converting `note`.
57 midi2note, freqtomidi, miditofreq
59 _valid_notenames = {'C': 0, 'D': 2, 'E': 4, 'F': 5, 'G': 7,
62 u'𝄫': -2, # double flat
63 u'♭': -1, 'b': -1, '\u266d': -1, # simple flat
64 u'♮': 0, '\u266e': 0, None: 0, # natural
65 '#': +1, u'♯': +1, '\u266f': +1, # sharp
66 u'𝄪': +2, # double sharp
68 _valid_octaves = range(-1, 10)
69 if not isinstance(note, str_instances):
70 msg = "a string is required, got {:s} ({:s})"
71 raise TypeError(msg.format(str(type(note)), repr(note)))
72 if len(note) not in range(2, 5):
73 msg = "string of 2 to 4 characters expected, got {:d} ({:s})"
74 raise ValueError(msg.format(len(note), note))
75 notename, modifier, octave = [None] * 3
78 notename, modifier, octave_sign, octave = note
79 octave = octave_sign + octave
81 notename, modifier, octave = note
83 octave = modifier + octave
86 notename, octave = note
88 notename = notename.upper()
91 if notename not in _valid_notenames:
92 raise ValueError("%s is not a valid note name" % notename)
93 if modifier not in _valid_modifiers:
94 raise ValueError("%s is not a valid modifier" % modifier)
95 if octave not in _valid_octaves:
96 raise ValueError("%s is not a valid octave" % octave)
98 midi = (octave + 1) * 12 + _valid_notenames[notename] \
99 + _valid_modifiers[modifier]
101 raise ValueError("%s is outside of the range C-2 to G8" % note)
106 """Convert midi note number to note name.
111 input midi note number
120 >>> aubio.midi2note(70)
122 >>> aubio.midi2note(59)
128 If `midi` was not an integer.
130 If `midi` is out of the range `[0, 128]`.
134 note2midi, miditofreq, freqtomidi
136 if not isinstance(midi, int_instances):
137 raise TypeError("an integer is required, got %s" % midi)
138 if midi not in range(0, 128):
139 msg = "an integer between 0 and 127 is excepted, got {:d}"
140 raise ValueError(msg.format(midi))
141 _valid_notenames = ['C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#',
143 return _valid_notenames[midi % 12] + str(int(midi / 12) - 1)
147 """Convert frequency in Hz to nearest note name.
151 freq : float [0, 23000[
152 input frequency, in Hz
157 name of the nearest note
161 >>> aubio.freq2note(440)
163 >>> aubio.freq2note(220.1)
166 nearest_note = int(freqtomidi(freq) + .5)
167 return midi2note(nearest_note)
171 """Convert note name to corresponding frequency, in Hz.
180 freq : float [0, 23000[
185 >>> aubio.note2freq('A4')
187 >>> aubio.note2freq('A3')
190 midi = note2midi(note)
191 return miditofreq(midi)