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