Merge branch 'feature/mfccparams'
[aubio.git] / python / tests / test_notes.py
1 #! /usr/bin/env python
2
3 from unittest import main
4 from numpy.testing import TestCase, assert_equal, assert_almost_equal
5 from aubio import notes
6
7 AUBIO_DEFAULT_NOTES_SILENCE = -70.
8 AUBIO_DEFAULT_NOTES_RELEASE_DROP = 10.
9 AUBIO_DEFAULT_NOTES_MINIOI_MS = 30.
10
11 class aubio_notes_default(TestCase):
12
13     def test_members(self):
14         o = notes()
15         assert_equal ([o.buf_size, o.hop_size, o.method, o.samplerate],
16             [1024,512,'default',44100])
17
18
19 class aubio_notes_params(TestCase):
20
21     samplerate = 44100
22
23     def setUp(self):
24         self.o = notes(samplerate = self.samplerate)
25
26     def test_get_minioi_ms(self):
27         assert_equal (self.o.get_minioi_ms(), AUBIO_DEFAULT_NOTES_MINIOI_MS)
28
29     def test_set_minioi_ms(self):
30         val = 40.
31         self.o.set_minioi_ms(val)
32         assert_almost_equal (self.o.get_minioi_ms(), val)
33
34     def test_get_silence(self):
35         assert_equal (self.o.get_silence(), AUBIO_DEFAULT_NOTES_SILENCE)
36
37     def test_set_silence(self):
38         val = -50
39         self.o.set_silence(val)
40         assert_equal (self.o.get_silence(), val)
41
42     def test_get_release_drop(self):
43         assert_equal (self.o.get_release_drop(), AUBIO_DEFAULT_NOTES_RELEASE_DROP)
44
45     def test_set_release_drop(self):
46         val = 50
47         self.o.set_release_drop(val)
48         assert_equal (self.o.get_release_drop(), val)
49
50     def test_set_release_drop_wrong(self):
51         val = -10
52         with self.assertRaises(ValueError):
53             self.o.set_release_drop(val)
54
55 from .utils import list_all_sounds
56 list_of_sounds = list_all_sounds('sounds')
57
58 class aubio_notes_sinewave(TestCase):
59
60     def analyze_file(self, filepath, samplerate=0):
61         from aubio import source
62         import numpy as np
63         win_s = 512 # fft size
64         hop_s = 256 # hop size
65
66         s = source(filepath, samplerate, hop_s)
67         samplerate = s.samplerate
68
69         tolerance = 0.8
70
71         notes_o = notes("default", win_s, hop_s, samplerate)
72         total_frames = 0
73
74         results = []
75         while True:
76             samples, read = s()
77             new_note = notes_o(samples)
78             if (new_note[0] != 0):
79                 note_str = ' '.join(["%.2f" % i for i in new_note])
80                 results.append( [total_frames, np.copy(new_note)] )
81             total_frames += read
82             if read < hop_s: break
83         return results
84
85     def test_sinewave(self):
86         for filepath in list_of_sounds:
87             if '44100Hz_44100f_sine441.wav' in filepath:
88                 results = self.analyze_file(filepath)
89                 assert_equal (len(results), 1)
90                 assert_equal (len(results[0]), 2)
91                 assert_equal (results[0][0], 1280)
92                 assert_equal (results[0][1], [69, 123, -1])
93
94 if __name__ == '__main__':
95     main()