Changeset f264b17 for python/lib/aubio/midiconv.py
- Timestamp:
- Jun 22, 2016, 1:00:10 PM (8 years ago)
- Branches:
- feature/autosink, feature/cnn, feature/cnn_org, feature/constantq, feature/crepe, feature/crepe_org, feature/pitchshift, feature/pydocstrings, feature/timestretch, fix/ffmpeg5, master, pitchshift, sampler, timestretch, yinfft+
- Children:
- 4b9443c4
- Parents:
- 60fc05b (diff), 6769586 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
python/lib/aubio/midiconv.py
r60fc05b rf264b17 1 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) 2 14 3 15 def note2midi(note): 4 16 " convert note name to midi note number, e.g. [C-1, G9] -> [0, 127] " 5 17 _valid_notenames = {'C': 0, 'D': 2, 'E': 4, 'F': 5, 'G': 7, 'A': 9, 'B': 11} 6 _valid_modifiers = {None: 0, u'♮': 0, '#': +1, u'♯': +1, u'\udd2a': +2, 'b': -1, u'♭': -1, u'\ufffd': -2} 18 _valid_modifiers = {None: 0, u'♮': 0, '#': +1, u'♯': +1, u'\udd2a': +2, 19 'b': -1, u'♭': -1, u'\ufffd': -2} 7 20 _valid_octaves = range(-1, 10) 8 if type(note) not in (str, unicode): 9 raise TypeError, "a string is required, got %s" % note 10 if not (1 < len(note) < 5): 11 raise ValueError, "string of 2 to 4 characters expected, got %d (%s)" % (len(note), note) 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)) 12 26 notename, modifier, octave = [None]*3 13 27 … … 27 41 28 42 if notename not in _valid_notenames: 29 raise ValueError , "%s is not a valid note name" % notename43 raise ValueError("%s is not a valid note name" % notename) 30 44 if modifier not in _valid_modifiers: 31 raise ValueError , "%s is not a valid modifier" % modifier45 raise ValueError("%s is not a valid modifier" % modifier) 32 46 if octave not in _valid_octaves: 33 raise ValueError , "%s is not a valid octave" % octave47 raise ValueError("%s is not a valid octave" % octave) 34 48 35 49 midi = 12 + octave * 12 + _valid_notenames[notename] + _valid_modifiers[modifier] 36 50 if midi > 127: 37 raise ValueError , "%s is outside of the range C-2 to G8" % note51 raise ValueError("%s is outside of the range C-2 to G8" % note) 38 52 return midi 39 53 40 54 def midi2note(midi): 41 55 " convert midi note number to note name, e.g. [0, 127] -> [C-1, G9] " 42 if type(midi) != int: 43 raise TypeError, "an integer is required, got %s" % midi 44 if not (-1 < midi < 128): 45 raise ValueError, "an integer between 0 and 127 is excepted, got %d" % midi 46 midi = int(midi) 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) 47 60 _valid_notenames = ['C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#', 'A', 'A#', 'B'] 48 return _valid_notenames[midi % 12] + str( midi / 12- 1)61 return _valid_notenames[midi % 12] + str(int(midi / 12) - 1) 49 62 50 63 def freq2note(freq): 64 " convert frequency in Hz to nearest note name, e.g. [0, 22050.] -> [C-1, G9] " 51 65 from aubio import freqtomidi 52 66 return midi2note(int(freqtomidi(freq)))
Note: See TracChangeset
for help on using the changeset viewer.