2 # -*- coding: utf-8 -*-
4 from __future__ import unicode_literals
6 from aubio import note2midi, freq2note, note2freq, float_type
7 from nose2.tools import params
10 list_of_known_notes = (
31 list_of_known_notes_with_unicode_issues = (
36 list_of_unknown_notes = (
47 class note2midi_good_values(unittest.TestCase):
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 )
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"
58 self.assertEqual ( note2midi(note), midi )
59 except UnicodeEncodeError as e:
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")
69 class note2midi_wrong_values(unittest.TestCase):
71 def test_note2midi_missing_octave(self):
72 " fails when passed only one character"
73 self.assertRaises(ValueError, note2midi, 'C')
75 def test_note2midi_wrong_modifier(self):
76 " fails when passed a note with an invalid modifier "
77 self.assertRaises(ValueError, note2midi, 'C.1')
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')
83 def test_note2midi_wrong_octave(self):
84 " fails when passed a wrong octave number "
85 self.assertRaises(ValueError, note2midi, 'CBc')
87 def test_note2midi_out_of_range(self):
88 " fails when passed a note out of range"
89 self.assertRaises(ValueError, note2midi, 'A9')
91 def test_note2midi_wrong_note_name(self):
92 " fails when passed a note with a wrong name"
93 self.assertRaises(ValueError, note2midi, 'W9')
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')
99 def test_note2midi_wrong_data_type(self):
100 " fails when passed a non-string value "
101 self.assertRaises(TypeError, note2midi, 123)
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')
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)
112 class freq2note_simple_test(unittest.TestCase):
114 def test_freq2note_above(self):
115 " make sure freq2note(441) == A4 "
116 self.assertEqual("A4", freq2note(441))
118 def test_freq2note_under(self):
119 " make sure freq2note(439) == A4 "
120 self.assertEqual("A4", freq2note(439))
122 class note2freq_simple_test(unittest.TestCase):
124 def test_note2freq(self):
125 " make sure note2freq('A3') == 220"
126 self.assertEqual(220, note2freq("A3"))
128 def test_note2freq_under(self):
129 " make sure note2freq(A4) == 440"
130 if float_type == 'float32':
131 self.assertEqual(440, note2freq("A4"))
133 self.assertLess(abs(note2freq("A4")-440), 1.e-12)
135 if __name__ == '__main__':