968c34a87c49d72a259f1439d228fd63004b8690
[aubio.git] / python / tests / test_note2midi.py
1 #! /usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 from __future__ import unicode_literals
5
6 from aubio import note2midi, freq2note
7 import unittest
8
9 list_of_known_notes = (
10         ( 'C-1', 0 ),
11         ( 'C#-1', 1 ),
12         ( 'd2', 38 ),
13         ( 'C3', 48 ),
14         ( 'B3', 59 ),
15         ( 'B#3', 60 ),
16         ( 'A4', 69 ),
17         ( 'A#4', 70 ),
18         ( 'Bb4', 70 ),
19         ( 'B♭4', 70 ),
20         ( 'G8', 115 ),
21         ( 'G♯8', 116 ),
22         ( 'G9', 127 ),
23         ( 'G\udd2a2', 45 ),
24         ( 'B\ufffd2', 45 ),
25         ( 'A♮2', 45 ),
26         )
27
28 class note2midi_good_values(unittest.TestCase):
29
30     def test_note2midi_known_values(self):
31         " known values are correctly converted "
32         for note, midi in list_of_known_notes:
33             self.assertEqual ( note2midi(note), midi )
34
35 class note2midi_wrong_values(unittest.TestCase):
36
37     def test_note2midi_missing_octave(self):
38         " fails when passed only one character"
39         self.assertRaises(ValueError, note2midi, 'C')
40
41     def test_note2midi_wrong_modifier(self):
42         " fails when passed a note with an invalid modifier "
43         self.assertRaises(ValueError, note2midi, 'C.1')
44
45     def test_note2midi_another_wrong_modifier_again(self):
46         " fails when passed a note with a invalid note name "
47         self.assertRaises(ValueError, note2midi, 'CB-3')
48
49     def test_note2midi_wrong_octave(self):
50         " fails when passed a wrong octave number "
51         self.assertRaises(ValueError, note2midi, 'CBc')
52
53     def test_note2midi_out_of_range(self):
54         " fails when passed a note out of range"
55         self.assertRaises(ValueError, note2midi, 'A9')
56
57     def test_note2midi_wrong_note_name(self):
58         " fails when passed a note with a wrong name"
59         self.assertRaises(ValueError, note2midi, 'W9')
60
61     def test_note2midi_low_octave(self):
62         " fails when passed a note with a too low octave"
63         self.assertRaises(ValueError, note2midi, 'C-9')
64
65     def test_note2midi_wrong_data_type(self):
66         " fails when passed a non-string value "
67         self.assertRaises(TypeError, note2midi, 123)
68
69
70 class freq2note_simple_test(unittest.TestCase):
71
72     def test_freq2note(self):
73         " make sure freq2note(441) == A4 "
74         self.assertEqual("A4", freq2note(441))
75
76 if __name__ == '__main__':
77     unittest.main()