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