source: python/lib/aubio/midiconv.py @ d554321

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5sampleryinfft+
Last change on this file since d554321 was 473ab11, checked in by Paul Brossier <piem@piem.org>, 7 years ago

python/lib/aubio/midiconv.py: add unicode double sharp and double flat

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