Changeset 9374e57 for python/lib
- Timestamp:
- Nov 4, 2018, 8:01:44 PM (6 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
- Children:
- 8ebcd3d
- Parents:
- 2c8ada6
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
python/lib/aubio/midiconv.py
r2c8ada6 r9374e57 2 2 """ utilities to convert midi note number to and from note names """ 3 3 4 __all__ = ['note2midi', 'midi2note', 'freq2note', 'note2freq']5 6 4 import sys 7 5 from ._aubio import freqtomidi, miditofreq 6 7 __all__ = ['note2midi', 'midi2note', 'freq2note', 'note2freq'] 8 8 9 9 py3 = sys.version_info[0] == 3 … … 14 14 str_instances = (str, unicode) 15 15 int_instances = (int, long) 16 16 17 17 18 def note2midi(note): … … 56 57 midi2note, freqtomidi, miditofreq 57 58 """ 58 _valid_notenames = {'C': 0, 'D': 2, 'E': 4, 'F': 5, 'G': 7, 'A': 9, 'B': 11} 59 _valid_notenames = {'C': 0, 'D': 2, 'E': 4, 'F': 5, 'G': 7, 60 'A': 9, 'B': 11} 59 61 _valid_modifiers = { 60 u'𝄫': -2, # double flat61 u'♭': -1, 'b': -1, '\u266d': -1, # simple flat62 u'♮': 0, '\u266e': 0, None: 0, # natural63 '#': +1, u'♯': +1, '\u266f': +1, # sharp64 u'𝄪': +2, # double sharp62 u'𝄫': -2, # double flat 63 u'♭': -1, 'b': -1, '\u266d': -1, # simple flat 64 u'♮': 0, '\u266e': 0, None: 0, # natural 65 '#': +1, u'♯': +1, '\u266f': +1, # sharp 66 u'𝄪': +2, # double sharp 65 67 } 66 68 _valid_octaves = range(-1, 10) … … 94 96 raise ValueError("%s is not a valid octave" % octave) 95 97 96 midi = 12 + octave* 12 + _valid_notenames[notename] \97 + _valid_modifiers[modifier]98 midi = (octave + 1) * 12 + _valid_notenames[notename] \ 99 + _valid_modifiers[modifier] 98 100 if midi > 127: 99 101 raise ValueError("%s is outside of the range C-2 to G8" % note) 100 102 return midi 103 101 104 102 105 def midi2note(midi): … … 137 140 raise ValueError(msg.format(midi)) 138 141 _valid_notenames = ['C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#', 139 'A', 'A#', 'B']142 'A', 'A#', 'B'] 140 143 return _valid_notenames[midi % 12] + str(int(midi / 12) - 1) 144 141 145 142 146 def freq2note(freq): … … 163 167 return midi2note(nearest_note) 164 168 169 165 170 def note2freq(note): 166 171 """Convert note name to corresponding frequency, in Hz.
Note: See TracChangeset
for help on using the changeset viewer.