1 # -*- coding: utf-8 -*-
2 """ utilities to convert midi note number to and from note names """
4 __all__ = ['note2midi', 'midi2note', 'freq2note', 'note2freq']
7 from ._aubio import freqtomidi, miditofreq
9 py3 = sys.version_info[0] == 3
14 str_instances = (str, unicode)
15 int_instances = (int, long)
18 """Convert note name to midi note number.
20 Input string `note` should be composed of one note root
21 and one octave, with optionally one modifier in between.
23 List of valid components:
25 - note roots: `C`, `D`, `E`, `F`, `G`, `A`, `B`,
26 - modifiers: `b`, `#`, as well as unicode characters
27 `𝄫`, `♭`, `♮`, `♯` and `𝄪`,
28 - octave numbers: `-1` -> `11`.
38 corresponding midi note number
42 >>> aubio.note2midi('C#4')
44 >>> aubio.note2midi('B♭5')
50 If `note` was not a string.
52 If an error was found while converting `note`.
56 midi2note, freqtomidi, miditofreq
58 _valid_notenames = {'C': 0, 'D': 2, 'E': 4, 'F': 5, 'G': 7, 'A': 9, 'B': 11}
60 u'𝄫': -2, # double flat
61 u'♭': -1, 'b': -1, '\u266d': -1, # simple flat
62 u'♮': 0, '\u266e': 0, None: 0, # natural
63 '#': +1, u'♯': +1, '\u266f': +1, # sharp
64 u'𝄪': +2, # double sharp
66 _valid_octaves = range(-1, 10)
67 if not isinstance(note, str_instances):
68 msg = "a string is required, got {:s} ({:s})"
69 raise TypeError(msg.format(str(type(note)), repr(note)))
70 if len(note) not in range(2, 5):
71 msg = "string of 2 to 4 characters expected, got {:d} ({:s})"
72 raise ValueError(msg.format(len(note), note))
73 notename, modifier, octave = [None]*3
76 notename, modifier, octave_sign, octave = note
77 octave = octave_sign + octave
79 notename, modifier, octave = note
81 octave = modifier + octave
84 notename, octave = note
86 notename = notename.upper()
89 if notename not in _valid_notenames:
90 raise ValueError("%s is not a valid note name" % notename)
91 if modifier not in _valid_modifiers:
92 raise ValueError("%s is not a valid modifier" % modifier)
93 if octave not in _valid_octaves:
94 raise ValueError("%s is not a valid octave" % octave)
96 midi = 12 + octave * 12 + _valid_notenames[notename] \
97 + _valid_modifiers[modifier]
99 raise ValueError("%s is outside of the range C-2 to G8" % note)
103 """Convert midi note number to note name.
108 input midi note number
117 >>> aubio.midi2note(70)
119 >>> aubio.midi2note(59)
125 If `midi` was not an integer.
127 If `midi` is out of the range `[0, 128]`.
131 note2midi, miditofreq, freqtomidi
133 if not isinstance(midi, int_instances):
134 raise TypeError("an integer is required, got %s" % midi)
135 if midi not in range(0, 128):
136 msg = "an integer between 0 and 127 is excepted, got {:d}"
137 raise ValueError(msg.format(midi))
138 _valid_notenames = ['C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#',
140 return _valid_notenames[midi % 12] + str(int(midi / 12) - 1)
143 """Convert frequency in Hz to nearest note name.
147 freq : float [0, 23000[
148 input frequency, in Hz
153 name of the nearest note
157 >>> aubio.freq2note(440)
159 >>> aubio.freq2note(220.1)
162 nearest_note = int(freqtomidi(freq) + .5)
163 return midi2note(nearest_note)
166 """Convert note name to corresponding frequency, in Hz.
175 freq : float [0, 23000[
180 >>> aubio.note2freq('A4')
182 >>> aubio.note2freq('A3')
185 midi = note2midi(note)
186 return miditofreq(midi)