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

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

lib/aubio/midiconv.py: prepare for python3

  • Property mode set to 100644
File size: 2.2 KB
RevLine 
[7d89e61]1# -*- coding: utf-8 -*-
[33102ab]2
[e76842e]3import sys
4PY3 = sys.version_info[0] == 3
5if PY3:
6    string_types = [str]
7else:
8    string_types = [str, unicode]
9
[33102ab]10def note2midi(note):
[f552e9e]11    " convert note name to midi note number, e.g. [C-1, G9] -> [0, 127] "
[33102ab]12    _valid_notenames = {'C': 0, 'D': 2, 'E': 4, 'F': 5, 'G': 7, 'A': 9, 'B': 11}
13    _valid_modifiers = {None: 0, u'♮': 0, '#': +1, u'♯': +1, u'\udd2a': +2, 'b': -1, u'♭': -1, u'\ufffd': -2}
[7d89e61]14    _valid_octaves = range(-1, 10)
[e76842e]15    if type(note) not in string_types:
16        raise TypeError("a string is required, got %s (%s)" % (note, str(type(note)) ))
[33102ab]17    if not (1 < len(note) < 5):
[b8ed85e]18        raise ValueError(
19                "string of 2 to 4 characters expected, got %d (%s)" %
20                (len(note), note))
[33102ab]21    notename, modifier, octave = [None]*3
22
23    if len(note) == 4:
24        notename, modifier, octave_sign, octave = note
25        octave = octave_sign + octave
26    elif len(note) == 3:
27        notename, modifier, octave = note
28        if modifier == '-':
29            octave = modifier + octave
30            modifier = None
31    else:
32        notename, octave = note
33
34    notename = notename.upper()
35    octave = int(octave)
36
37    if notename not in _valid_notenames:
[b8ed85e]38        raise ValueError("%s is not a valid note name" % notename)
[33102ab]39    if modifier not in _valid_modifiers:
[b8ed85e]40        raise ValueError("%s is not a valid modifier" % modifier)
[33102ab]41    if octave not in _valid_octaves:
[b8ed85e]42        raise ValueError("%s is not a valid octave" % octave)
[33102ab]43
44    midi = 12 + octave * 12 + _valid_notenames[notename] + _valid_modifiers[modifier]
45    if midi > 127:
[b8ed85e]46        raise ValueError("%s is outside of the range C-2 to G8" % note)
[33102ab]47    return midi
[9ead7a9]48
49def midi2note(midi):
50    " convert midi note number to note name, e.g. [0, 127] -> [C-1, G9] "
51    if type(midi) != int:
[b8ed85e]52        raise TypeError("an integer is required, got %s" % midi)
[9ead7a9]53    if not (-1 < midi < 128):
[b8ed85e]54        raise ValueError(
55                "an integer between 0 and 127 is excepted, got %d" % midi)
[9ead7a9]56    midi = int(midi)
57    _valid_notenames = ['C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#', 'A', 'A#', 'B']
[7ffb554]58    return _valid_notenames[midi % 12] + str( int(midi / 12) - 1 )
[3cc1491]59
60def freq2note(freq):
61    from aubio import freqtomidi
62    return midi2note(int(freqtomidi(freq)))
Note: See TracBrowser for help on using the repository browser.