2 # -*- coding: utf-8 -*-
4 from __future__ import unicode_literals
6 from aubio import note2midi, freq2note, note2freq, float_type
7 from numpy.testing import TestCase
8 from _tools import parametrize, assert_raises, skipTest
10 list_of_known_notes = (
31 list_of_known_notes_with_unicode_issues = (
36 list_of_unknown_notes = (
47 class Test_note2midi_good_values(object):
49 @parametrize('note, midi', list_of_known_notes)
50 def test_note2midi_known_values(self, note, midi):
51 " known values are correctly converted "
52 assert note2midi(note) == midi
54 @parametrize('note, midi', list_of_known_notes_with_unicode_issues)
55 def test_note2midi_known_values_with_unicode_issues(self, note, midi):
56 " difficult values are correctly converted unless expected failure "
58 assert note2midi(note) == midi
59 except UnicodeEncodeError as e:
60 # platforms with decoding failures include:
61 # - osx: python <= 2.7.10
62 # - win: python <= 2.7.12
64 strmsg = "len(u'\\U0001D12A') != 1, expected decoding failure"
65 strmsg += " | upgrade to Python 3 to fix"
66 strmsg += " | {:s} | {:s} {:s}"
67 if len('\U0001D12A') != 1 and sys.version[0] == '2':
68 skipTest(strmsg.format(repr(e), sys.platform, sys.version))
72 class note2midi_wrong_values(TestCase):
74 def test_note2midi_missing_octave(self):
75 " fails when passed only one character"
76 self.assertRaises(ValueError, note2midi, 'C')
78 def test_note2midi_wrong_modifier(self):
79 " fails when passed a note with an invalid modifier "
80 self.assertRaises(ValueError, note2midi, 'C.1')
82 def test_note2midi_another_wrong_modifier_again(self):
83 " fails when passed a note with a invalid note name "
84 self.assertRaises(ValueError, note2midi, 'CB-3')
86 def test_note2midi_wrong_octave(self):
87 " fails when passed a wrong octave number "
88 self.assertRaises(ValueError, note2midi, 'CBc')
90 def test_note2midi_out_of_range(self):
91 " fails when passed a note out of range"
92 self.assertRaises(ValueError, note2midi, 'A9')
94 def test_note2midi_wrong_note_name(self):
95 " fails when passed a note with a wrong name"
96 self.assertRaises(ValueError, note2midi, 'W9')
98 def test_note2midi_low_octave(self):
99 " fails when passed a note with a too low octave"
100 self.assertRaises(ValueError, note2midi, 'C-9')
102 def test_note2midi_wrong_data_type(self):
103 " fails when passed a non-string value "
104 self.assertRaises(TypeError, note2midi, 123)
106 def test_note2midi_wrong_data_too_long(self):
107 " fails when passed a note with a note name longer than expected"
108 self.assertRaises(ValueError, note2midi, 'CB+-3')
110 class Test_note2midi_unknown_values(object):
112 @parametrize('note', list_of_unknown_notes)
113 def test_note2midi_unknown_values(self, note):
114 " unknown values throw out an error "
115 assert_raises(ValueError, note2midi, note)
117 class freq2note_simple_test(TestCase):
119 def test_freq2note_above(self):
120 " make sure freq2note(441) == A4 "
121 self.assertEqual("A4", freq2note(441))
123 def test_freq2note_under(self):
124 " make sure freq2note(439) == A4 "
125 self.assertEqual("A4", freq2note(439))
127 class note2freq_simple_test(TestCase):
129 def test_note2freq(self):
130 " make sure note2freq('A3') == 220"
131 self.assertEqual(220, note2freq("A3"))
133 def test_note2freq_under(self):
134 " make sure note2freq(A4) == 440"
135 if float_type == 'float32':
136 self.assertEqual(440, note2freq("A4"))
138 self.assertLess(abs(note2freq("A4")-440), 1.e-12)
140 if __name__ == '__main__':
141 from _tools import run_module_suite