Changeset f552e9e


Ignore:
Timestamp:
Mar 10, 2013, 5:13:51 PM (11 years ago)
Author:
Paul Brossier <piem@piem.org>
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:
9ead7a9
Parents:
33102ab
Message:

lib/aubio/midiconv.py: improve note2midi

Location:
python
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • python/lib/aubio/midiconv.py

    r33102ab rf552e9e  
    22
    33def note2midi(note):
    4     " convert a note name to a midi note value "
    5     # from C-2 to G8, though we do accept everything in the upper octave
     4    " convert note name to midi note number, e.g. [C-1, G9] -> [0, 127] "
    65    _valid_notenames = {'C': 0, 'D': 2, 'E': 4, 'F': 5, 'G': 7, 'A': 9, 'B': 11}
    76    _valid_modifiers = {None: 0, u'♮': 0, '#': +1, u'♯': +1, u'\udd2a': +2, 'b': -1, u'♭': -1, u'\ufffd': -2}
    87    _valid_octaves = range(-1, 10)
     8    if type(note) not in (str, unicode):
     9        raise TypeError, "a string is required, got %s" % note
    910    if not (1 < len(note) < 5):
    1011        raise ValueError, "string of 2 to 4 characters expected, got %d (%s)" % (len(note), note)
  • python/tests/test_note2midi.py

    r33102ab rf552e9e  
    1313        ( 'A4', 69 ),
    1414        ( 'A#4', 70 ),
     15        ( 'Bb4', 70 ),
     16        ( u'B♭4', 70 ),
    1517        ( 'G8', 115 ),
    1618        ( u'G♯8', 116 ),
    17         ( u'G♭9', 126 ),
    1819        ( 'G9', 127 ),
    1920        ( u'G\udd2a2', 45 ),
     
    3637
    3738    def test_note2midi_wrong_modifier(self):
    38         " fails when passed an invalid note name"
     39        " fails when passed a note with an invalid modifier "
    3940        self.assertRaises(ValueError, note2midi, 'C.1')
    4041
    41     def test_note2midi_wronge_midifier_again(self):
    42         " fails when passed a wrong modifier"
     42    def test_note2midi_another_wrong_modifier_again(self):
     43        " fails when passed a note with a invalid note name "
    4344        self.assertRaises(ValueError, note2midi, 'CB-3')
    4445
    4546    def test_note2midi_wrong_octave(self):
    46         " fails when passed a wrong octave"
     47        " fails when passed a wrong octave number "
    4748        self.assertRaises(ValueError, note2midi, 'CBc')
    4849
    4950    def test_note2midi_out_of_range(self):
    50         " fails when passed a non-existing note"
     51        " fails when passed a out of range note"
    5152        self.assertRaises(ValueError, note2midi, 'A9')
     53
     54    def test_note2midi_wrong_data_type(self):
     55        " fails when passed a non-string value "
     56        self.assertRaises(TypeError, note2midi, 123)
    5257
    5358if __name__ == '__main__':
Note: See TracChangeset for help on using the changeset viewer.