source: python/tests/test_note2midi.py @ fc359e0

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5
Last change on this file since fc359e0 was 3aad0b1, checked in by Paul Brossier <piem@piem.org>, 8 years ago

python/lib/aubio/midiconv.py: improve unicode handling, skip UnicodeEncodeError? on python 2.x

  • Property mode set to 100755
File size: 3.8 KB
Line 
1#! /usr/bin/env python
2# -*- coding: utf-8 -*-
3
4from __future__ import unicode_literals
5
6from aubio import note2midi, freq2note
7from nose2.tools import params
8import unittest
9
10list_of_known_notes = (
11        ( 'C-1', 0 ),
12        ( 'C#-1', 1 ),
13        ( 'd2', 38 ),
14        ( 'C3', 48 ),
15        ( 'B3', 59 ),
16        ( 'B#3', 60 ),
17        ( 'C♯4', 61 ),
18        ( 'A4', 69 ),
19        ( 'A#4', 70 ),
20        ( 'A♯4', 70 ),
21        ( 'A\u266f4', 70 ),
22        ( 'Bb4', 70 ),
23        ( 'B♭4', 70 ),
24        ( 'B\u266d4', 70 ),
25        ( 'G8', 115 ),
26        ( 'G♯8', 116 ),
27        ( 'G9', 127 ),
28        ( 'A♮2', 45 ),
29        )
30
31list_of_known_notes_with_unicode_issues = (
32        ('C𝄪4', 62 ),
33        ('E𝄫4', 62 ),
34        )
35
36list_of_unknown_notes = (
37        ( 'G\udd2a2' ),
38        ( 'B\ufffd2' ),
39        ( 'B\u266e\u266e2' ),
40        ( 'B\u266f\u266d3' ),
41        ( 'B33' ),
42        ( 'C.3' ),
43        ( 'A' ),
44        ( '2' ),
45        )
46
47class note2midi_good_values(unittest.TestCase):
48
49    @params(*list_of_known_notes)
50    def test_note2midi_known_values(self, note, midi):
51        " known values are correctly converted "
52        self.assertEqual ( note2midi(note), midi )
53
54    @params(*list_of_known_notes_with_unicode_issues)
55    def test_note2midi_known_values_with_unicode_issues(self, note, midi):
56        " known values are correctly converted, unless decoding is expected to fail"
57        try:
58            self.assertEqual ( note2midi(note), midi )
59        except UnicodeEncodeError as e:
60            import sys
61            strfmt = "len(u'\\U0001D12A') != 1, excpected decoding failure | {:s} | {:s} {:s}"
62            strres = strfmt.format(e, sys.platform, sys.version)
63            # happens with: darwin 2.7.10, windows 2.7.12
64            if len('\U0001D12A') != 1 and sys.version[0] == '2':
65                self.skipTest(strres + " | upgrade to Python 3 to fix")
66            else:
67                raise
68
69class note2midi_wrong_values(unittest.TestCase):
70
71    def test_note2midi_missing_octave(self):
72        " fails when passed only one character"
73        self.assertRaises(ValueError, note2midi, 'C')
74
75    def test_note2midi_wrong_modifier(self):
76        " fails when passed a note with an invalid modifier "
77        self.assertRaises(ValueError, note2midi, 'C.1')
78
79    def test_note2midi_another_wrong_modifier_again(self):
80        " fails when passed a note with a invalid note name "
81        self.assertRaises(ValueError, note2midi, 'CB-3')
82
83    def test_note2midi_wrong_octave(self):
84        " fails when passed a wrong octave number "
85        self.assertRaises(ValueError, note2midi, 'CBc')
86
87    def test_note2midi_out_of_range(self):
88        " fails when passed a note out of range"
89        self.assertRaises(ValueError, note2midi, 'A9')
90
91    def test_note2midi_wrong_note_name(self):
92        " fails when passed a note with a wrong name"
93        self.assertRaises(ValueError, note2midi, 'W9')
94
95    def test_note2midi_low_octave(self):
96        " fails when passed a note with a too low octave"
97        self.assertRaises(ValueError, note2midi, 'C-9')
98
99    def test_note2midi_wrong_data_type(self):
100        " fails when passed a non-string value "
101        self.assertRaises(TypeError, note2midi, 123)
102
103    def test_note2midi_wrong_data_too_long(self):
104        " fails when passed a note with a note name longer than expected"
105        self.assertRaises(ValueError, note2midi, 'CB+-3')
106
107    @params(*list_of_unknown_notes)
108    def test_note2midi_unknown_values(self, note):
109        " unknown values throw out an error "
110        self.assertRaises(ValueError, note2midi, note)
111
112class freq2note_simple_test(unittest.TestCase):
113
114    def test_freq2note(self):
115        " make sure freq2note(441) == A4 "
116        self.assertEqual("A4", freq2note(441))
117
118if __name__ == '__main__':
119    import nose2
120    nose2.main()
Note: See TracBrowser for help on using the repository browser.