Changeset 81abf91 for python/lib/aubio/midiconv.py
- Timestamp:
- Oct 30, 2018, 1:22:06 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:
- 8986239
- Parents:
- 92948ab (diff), 4bc10e2 (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
r92948ab r81abf91 2 2 """ utilities to convert midi note number to and from note names """ 3 3 4 __all__ = ['note2midi', 'midi2note', 'freq2note' ]4 __all__ = ['note2midi', 'midi2note', 'freq2note', 'note2freq'] 5 5 6 6 import sys 7 from ._aubio import freqtomidi, miditofreq 8 7 9 py3 = sys.version_info[0] == 3 8 10 if py3: … … 25 27 _valid_octaves = range(-1, 10) 26 28 if not isinstance(note, str_instances): 27 raise TypeError("a string is required, got %s (%s)" % (note, str(type(note)))) 29 msg = "a string is required, got {:s} ({:s})" 30 raise TypeError(msg.format(str(type(note)), repr(note))) 28 31 if len(note) not in range(2, 5): 29 raise ValueError("string of 2 to 4 characters expected, got %d (%s)" \30 %(len(note), note))32 msg = "string of 2 to 4 characters expected, got {:d} ({:s})" 33 raise ValueError(msg.format(len(note), note)) 31 34 notename, modifier, octave = [None]*3 32 35 … … 52 55 raise ValueError("%s is not a valid octave" % octave) 53 56 54 midi = 12 + octave * 12 + _valid_notenames[notename] + _valid_modifiers[modifier] 57 midi = 12 + octave * 12 + _valid_notenames[notename] \ 58 + _valid_modifiers[modifier] 55 59 if midi > 127: 56 60 raise ValueError("%s is outside of the range C-2 to G8" % note) … … 62 66 raise TypeError("an integer is required, got %s" % midi) 63 67 if midi not in range(0, 128): 64 raise ValueError("an integer between 0 and 127 is excepted, got %d" % midi) 65 _valid_notenames = ['C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#', 'A', 'A#', 'B'] 68 msg = "an integer between 0 and 127 is excepted, got {:d}" 69 raise ValueError(msg.format(midi)) 70 _valid_notenames = ['C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#', 71 'A', 'A#', 'B'] 66 72 return _valid_notenames[midi % 12] + str(int(midi / 12) - 1) 67 73 68 74 def freq2note(freq): 69 75 " convert frequency in Hz to nearest note name, e.g. [0, 22050.] -> [C-1, G9] " 70 from aubio import freqtomidi 71 return midi2note(int(freqtomidi(freq))) 76 nearest_note = int(freqtomidi(freq) + .5) 77 return midi2note(nearest_note) 78 79 def note2freq(note): 80 """Convert note name to corresponding frequency, in Hz. 81 82 Parameters 83 ---------- 84 note : str 85 input note name 86 87 Returns 88 ------- 89 freq : float [0, 23000[ 90 frequency, in Hz 91 92 Example 93 ------- 94 >>> aubio.note2freq('A4') 95 440 96 >>> aubio.note2freq('A3') 97 220.1 98 """ 99 midi = note2midi(note) 100 return miditofreq(midi)
Note: See TracChangeset
for help on using the changeset viewer.