History

lib/aubio/midiconv.py: improve note2midi

Paul Brossier committed 13 years ago

Commit f552e9e1 parent 33102ab2 files · +14 −8Browse files

Unified · Split

2 files
  1. python/lib/aubio/midiconv.py +3 −2
  2. python/tests/test_note2midi.py +11 −6
python/lib/aubio/midiconv.py+3−2
@@ -1,11 +1,12 @@
1# -*- encoding: utf8 -*-1# -*- encoding: utf8 -*-
2 2
3def note2midi(note):3def note2midi(note):
4 " convert a note name to a midi note value "4 " convert note name to midi note number, e.g. [C-1, G9] -> [0, 127] "
5 # from C-2 to G8, though we do accept everything in the upper octave
6 _valid_notenames = {'C': 0, 'D': 2, 'E': 4, 'F': 5, 'G': 7, 'A': 9, 'B': 11}5 _valid_notenames = {'C': 0, 'D': 2, 'E': 4, 'F': 5, 'G': 7, 'A': 9, 'B': 11}
7 _valid_modifiers = {None: 0, u'♮': 0, '#': +1, u'♯': +1, u'\udd2a': +2, 'b': -1, u'♭': -1, u'\ufffd': -2}6 _valid_modifiers = {None: 0, u'♮': 0, '#': +1, u'♯': +1, u'\udd2a': +2, 'b': -1, u'♭': -1, u'\ufffd': -2}
8 _valid_octaves = range(-1, 10) 7 _valid_octaves = range(-1, 10)
8 if type(note) not in (str, unicode):
9 raise TypeError, "a string is required, got %s" % note
9 if not (1 < len(note) < 5):10 if not (1 < len(note) < 5):
10 raise ValueError, "string of 2 to 4 characters expected, got %d (%s)" % (len(note), note)11 raise ValueError, "string of 2 to 4 characters expected, got %d (%s)" % (len(note), note)
11 notename, modifier, octave = [None]*312 notename, modifier, octave = [None]*3
python/tests/test_note2midi.py+11−6
@@ -12,9 +12,10 @@ list_of_known_notes = ( · 11 unmodified lines
12 ( 'B#3', 60 ),12 ( 'B#3', 60 ),
13 ( 'A4', 69 ),13 ( 'A4', 69 ),
14 ( 'A#4', 70 ),14 ( 'A#4', 70 ),
15 ( 'Bb4', 70 ),
16 ( u'B♭4', 70 ),
15 ( 'G8', 115 ),17 ( 'G8', 115 ),
16 ( u'G♯8', 116 ),18 ( u'G♯8', 116 ),
17 ( u'G♭9', 126 ),
18 ( 'G9', 127 ),19 ( 'G9', 127 ),
19 ( u'G\udd2a2', 45 ),20 ( u'G\udd2a2', 45 ),
20 ( u'B\ufffd2', 45 ),21 ( u'B\ufffd2', 45 ),
@@ -35,20 +36,24 @@ class TestNote2MidiWrongValues(unittest.TestCase): · 14 unmodified lines
35 self.assertRaises(ValueError, note2midi, 'C')36 self.assertRaises(ValueError, note2midi, 'C')
36 37
37 def test_note2midi_wrong_modifier(self):38 def test_note2midi_wrong_modifier(self):
38 " fails when passed an invalid note name"39 " fails when passed a note with an invalid modifier "
39 self.assertRaises(ValueError, note2midi, 'C.1')40 self.assertRaises(ValueError, note2midi, 'C.1')
40 41
41 def test_note2midi_wronge_midifier_again(self):42 def test_note2midi_another_wrong_modifier_again(self):
42 " fails when passed a wrong modifier"43 " fails when passed a note with a invalid note name "
43 self.assertRaises(ValueError, note2midi, 'CB-3')44 self.assertRaises(ValueError, note2midi, 'CB-3')
44 45
45 def test_note2midi_wrong_octave(self):46 def test_note2midi_wrong_octave(self):
46 " fails when passed a wrong octave"47 " fails when passed a wrong octave number "
47 self.assertRaises(ValueError, note2midi, 'CBc')48 self.assertRaises(ValueError, note2midi, 'CBc')
48 49
49 def test_note2midi_out_of_range(self):50 def test_note2midi_out_of_range(self):
50 " fails when passed a non-existing note"51 " fails when passed a out of range note"
51 self.assertRaises(ValueError, note2midi, 'A9')52 self.assertRaises(ValueError, note2midi, 'A9')
52 53
54 def test_note2midi_wrong_data_type(self):
55 " fails when passed a non-string value "
56 self.assertRaises(TypeError, note2midi, 123)
57
53if __name__ == '__main__':58if __name__ == '__main__':
54 unittest.main()59 unittest.main()

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t