python/tests/test_notes.py: use relative import
[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_MINIOI_MS = 30.
9
10 class aubio_notes_default(TestCase):
11
12     def test_members(self):
13         o = notes()
14         assert_equal ([o.buf_size, o.hop_size, o.method, o.samplerate],
15             [1024,512,'default',44100])
16
17
18 class aubio_notes_params(TestCase):
19
20     samplerate = 44100
21
22     def setUp(self):
23         self.o = notes(samplerate = self.samplerate)
24
25     def test_get_minioi_ms(self):
26         assert_equal (self.o.get_minioi_ms(), AUBIO_DEFAULT_NOTES_MINIOI_MS)
27
28     def test_set_minioi_ms(self):
29         val = 40.
30         self.o.set_minioi_ms(val)
31         assert_almost_equal (self.o.get_minioi_ms(), val)
32
33     def test_get_silence(self):
34         assert_equal (self.o.get_silence(), AUBIO_DEFAULT_NOTES_SILENCE)
35
36     def test_set_silence(self):
37         val = -50
38         self.o.set_silence(val)
39         assert_equal (self.o.get_silence(), val)
40
41 from .utils import list_all_sounds
42 list_of_sounds = list_all_sounds('sounds')
43
44 class aubio_notes_sinewave(TestCase):
45
46     def analyze_file(self, filepath, samplerate=0):
47         from aubio import source
48         import numpy as np
49         win_s = 512 # fft size
50         hop_s = 256 # hop size
51
52         s = source(filepath, samplerate, hop_s)
53         samplerate = s.samplerate
54
55         tolerance = 0.8
56
57         notes_o = notes("default", win_s, hop_s, samplerate)
58         total_frames = 0
59
60         results = []
61         while True:
62             samples, read = s()
63             new_note = notes_o(samples)
64             if (new_note[0] != 0):
65                 note_str = ' '.join(["%.2f" % i for i in new_note])
66                 results.append( [total_frames, np.copy(new_note)] )
67             total_frames += read
68             if read < hop_s: break
69         return results
70
71     def test_sinewave(self):
72         for filepath in list_of_sounds:
73             if '44100Hz_44100f_sine441.wav' in filepath:
74                 results = self.analyze_file(filepath)
75                 assert_equal (len(results), 1)
76                 assert_equal (len(results[0]), 2)
77                 assert_equal (results[0][0], 1280)
78                 assert_equal (results[0][1], [69, 123, -1])
79
80 if __name__ == '__main__':
81     main()