Changes in python/lib/aubio/midiconv.py [7165fc56:476cb41]
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
python/lib/aubio/midiconv.py
r7165fc56 r476cb41 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: … … 14 16 15 17 def note2midi(note): 16 """Convert note name to midi note number. 17 18 Input string `note` should be composed of one note root 19 and one octave, with optionally one modifier in between. 20 21 List of valid components: 22 23 - note roots: `C`, `D`, `E`, `F`, `G`, `A`, `B`, 24 - modifiers: `b`, `#`, as well as unicode characters 25 `𝄫`, `♭`, `♮`, `♯` and `𝄪`, 26 - octave numbers: `-1` -> `11`. 27 28 Parameters 29 ---------- 30 note : str 31 note name 32 33 Returns 34 ------- 35 int 36 corresponding midi note number 37 38 Examples 39 -------- 40 >>> aubio.note2midi('C#4') 41 61 42 >>> aubio.note2midi('B♭5') 43 82 44 45 Raises 46 ------ 47 TypeError 48 If `note` was not a string. 49 ValueError 50 If an error was found while converting `note`. 51 52 See Also 53 -------- 54 midi2note, freqtomidi, miditofreq 55 """ 18 " convert note name to midi note number, e.g. [C-1, G9] -> [0, 127] " 56 19 _valid_notenames = {'C': 0, 'D': 2, 'E': 4, 'F': 5, 'G': 7, 'A': 9, 'B': 11} 57 20 _valid_modifiers = { … … 64 27 _valid_octaves = range(-1, 10) 65 28 if not isinstance(note, str_instances): 66 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))) 67 31 if len(note) not in range(2, 5): 68 raise ValueError("string of 2 to 4 characters expected, got %d (%s)" \69 %(len(note), note))32 msg = "string of 2 to 4 characters expected, got {:d} ({:s})" 33 raise ValueError(msg.format(len(note), note)) 70 34 notename, modifier, octave = [None]*3 71 35 … … 91 55 raise ValueError("%s is not a valid octave" % octave) 92 56 93 midi = 12 + octave * 12 + _valid_notenames[notename] + _valid_modifiers[modifier] 57 midi = 12 + octave * 12 + _valid_notenames[notename] \ 58 + _valid_modifiers[modifier] 94 59 if midi > 127: 95 60 raise ValueError("%s is outside of the range C-2 to G8" % note) … … 97 62 98 63 def midi2note(midi): 99 """Convert midi note number to note name. 64 " convert midi note number to note name, e.g. [0, 127] -> [C-1, G9] " 65 if not isinstance(midi, int_instances): 66 raise TypeError("an integer is required, got %s" % midi) 67 if midi not in range(0, 128): 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'] 72 return _valid_notenames[midi % 12] + str(int(midi / 12) - 1) 73 74 def freq2note(freq): 75 " convert frequency in Hz to nearest note name, e.g. [0, 22050.] -> [C-1, G9] " 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. 100 81 101 82 Parameters 102 83 ---------- 103 midi : int [0, 128]104 input midi note number84 note : str 85 input note name 105 86 106 87 Returns 107 88 ------- 108 str109 note name110 111 Examples112 --------113 >>> aubio.midi2note(70)114 'A#4'115 >>> aubio.midi2note(59)116 'B3'117 118 Raises119 ------120 TypeError121 If `midi` was not an integer.122 ValueError123 If `midi` is out of the range `[0, 128]`.124 125 See Also126 --------127 note2midi, miditofreq, freqtomidi128 """129 if not isinstance(midi, int_instances):130 raise TypeError("an integer is required, got %s" % midi)131 if midi not in range(0, 128):132 raise ValueError("an integer between 0 and 127 is excepted, got %d" % midi)133 _valid_notenames = ['C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#', 'A', 'A#', 'B']134 return _valid_notenames[midi % 12] + str(int(midi / 12) - 1)135 136 def freq2note(freq):137 """Convert frequency in Hz to nearest note name.138 139 Parameters140 ----------141 89 freq : float [0, 23000[ 142 input frequency, in Hz 143 144 Returns 145 ------- 146 str 147 name of the nearest note 90 frequency, in Hz 148 91 149 92 Example 150 93 ------- 151 >>> aubio. freq2note(440)152 'A4'153 >>> aubio. freq2note(220.1)154 'A3'94 >>> aubio.note2freq('A4') 95 440 96 >>> aubio.note2freq('A3') 97 220.1 155 98 """ 156 from aubio import freqtomidi157 return midi 2note(int(freqtomidi(freq)))99 midi = note2midi(note) 100 return miditofreq(midi)
Note: See TracChangeset
for help on using the changeset viewer.