[75e715f] | 1 | #! /usr/bin/env python |
---|
[6192ce7] | 2 | |
---|
[0b6d23d] | 3 | from nose2 import main |
---|
[5f5f843] | 4 | from nose2.tools import params |
---|
[0b6d23d] | 5 | from numpy.testing import TestCase |
---|
| 6 | from aubio import source |
---|
| 7 | from utils import list_all_sounds |
---|
[6192ce7] | 8 | |
---|
[d45f527] | 9 | import warnings |
---|
| 10 | warnings.filterwarnings('ignore', category=UserWarning, append=True) |
---|
| 11 | |
---|
[e1bfde5] | 12 | list_of_sounds = list_all_sounds('sounds') |
---|
[5f5f843] | 13 | samplerates = [0, 44100, 8000, 32000] |
---|
| 14 | hop_sizes = [512, 1024, 64] |
---|
| 15 | |
---|
[e1bfde5] | 16 | path = None |
---|
[6192ce7] | 17 | |
---|
[5f5f843] | 18 | all_params = [] |
---|
| 19 | for soundfile in list_of_sounds: |
---|
| 20 | for hop_size in hop_sizes: |
---|
| 21 | for samplerate in samplerates: |
---|
| 22 | all_params.append((hop_size, samplerate, soundfile)) |
---|
| 23 | |
---|
| 24 | |
---|
[31a5c405] | 25 | class aubio_source_test_case_base(TestCase): |
---|
[6192ce7] | 26 | |
---|
[e1bfde5] | 27 | def setUp(self): |
---|
[50a8260] | 28 | if not len(list_of_sounds): |
---|
| 29 | self.skipTest('add some sound files in \'python/tests/sounds\'') |
---|
[ca89d9f] | 30 | self.default_test_sound = list_of_sounds[0] |
---|
[6192ce7] | 31 | |
---|
[31a5c405] | 32 | class aubio_source_test_case(aubio_source_test_case_base): |
---|
| 33 | |
---|
[980a4f4] | 34 | @params(*list_of_sounds) |
---|
| 35 | def test_close_file(self, filename): |
---|
[31a5c405] | 36 | samplerate = 0 # use native samplerate |
---|
| 37 | hop_size = 256 |
---|
[980a4f4] | 38 | f = source(filename, samplerate, hop_size) |
---|
| 39 | f.close() |
---|
[31a5c405] | 40 | |
---|
[980a4f4] | 41 | @params(*list_of_sounds) |
---|
| 42 | def test_close_file_twice(self, filename): |
---|
[31a5c405] | 43 | samplerate = 0 # use native samplerate |
---|
| 44 | hop_size = 256 |
---|
[980a4f4] | 45 | f = source(filename, samplerate, hop_size) |
---|
| 46 | f.close() |
---|
| 47 | f.close() |
---|
[31a5c405] | 48 | |
---|
| 49 | class aubio_source_read_test_case(aubio_source_test_case_base): |
---|
| 50 | |
---|
[11b49d7] | 51 | def read_from_source(self, f): |
---|
[e1bfde5] | 52 | total_frames = 0 |
---|
| 53 | while True: |
---|
[0b6d23d] | 54 | _ , read = f() |
---|
[e1bfde5] | 55 | total_frames += read |
---|
| 56 | if read < f.hop_size: break |
---|
[0b6d23d] | 57 | #result_str = "read {:.2f}s ({:d} frames in {:d} blocks at {:d}Hz) from {:s}" |
---|
| 58 | #result_params = total_frames / float(f.samplerate), total_frames, total_frames//f.hop_size, f.samplerate, f.uri |
---|
[36e8956] | 59 | #print (result_str.format(*result_params)) |
---|
[11b49d7] | 60 | return total_frames |
---|
[6192ce7] | 61 | |
---|
[5f5f843] | 62 | @params(*all_params) |
---|
| 63 | def test_samplerate_hopsize(self, hop_size, samplerate, soundfile): |
---|
| 64 | try: |
---|
| 65 | f = source(soundfile, samplerate, hop_size) |
---|
| 66 | except RuntimeError as e: |
---|
[355c761] | 67 | self.skipTest('failed opening with hop_s = {:d}, samplerate = {:d} ({:s})'.format(hop_size, samplerate, str(e))) |
---|
[5f5f843] | 68 | assert f.samplerate != 0 |
---|
| 69 | self.read_from_source(f) |
---|
| 70 | |
---|
| 71 | @params(*list_of_sounds) |
---|
| 72 | def test_samplerate_none(self, p): |
---|
| 73 | f = source(p) |
---|
| 74 | assert f.samplerate != 0 |
---|
| 75 | self.read_from_source(f) |
---|
| 76 | |
---|
| 77 | @params(*list_of_sounds) |
---|
| 78 | def test_samplerate_0(self, p): |
---|
| 79 | f = source(p, 0) |
---|
| 80 | assert f.samplerate != 0 |
---|
| 81 | self.read_from_source(f) |
---|
| 82 | |
---|
| 83 | @params(*list_of_sounds) |
---|
| 84 | def test_zero_hop_size(self, p): |
---|
| 85 | f = source(p, 0, 0) |
---|
| 86 | assert f.samplerate != 0 |
---|
| 87 | assert f.hop_size != 0 |
---|
| 88 | self.read_from_source(f) |
---|
| 89 | |
---|
| 90 | @params(*list_of_sounds) |
---|
| 91 | def test_seek_to_half(self, p): |
---|
[11b49d7] | 92 | from random import randint |
---|
[5f5f843] | 93 | f = source(p, 0, 0) |
---|
| 94 | assert f.samplerate != 0 |
---|
| 95 | assert f.hop_size != 0 |
---|
| 96 | a = self.read_from_source(f) |
---|
| 97 | c = randint(0, a) |
---|
| 98 | f.seek(c) |
---|
| 99 | b = self.read_from_source(f) |
---|
| 100 | assert a == b + c |
---|
| 101 | |
---|
| 102 | @params(*list_of_sounds) |
---|
| 103 | def test_duration(self, p): |
---|
| 104 | total_frames = 0 |
---|
| 105 | f = source(p) |
---|
| 106 | duration = f.duration |
---|
| 107 | while True: |
---|
[0b6d23d] | 108 | _, read = f() |
---|
[5f5f843] | 109 | total_frames += read |
---|
| 110 | if read < f.hop_size: break |
---|
| 111 | self.assertEqual(duration, total_frames) |
---|
[cfa46b9] | 112 | |
---|
[36e8956] | 113 | |
---|
| 114 | class aubio_source_test_wrong_params(TestCase): |
---|
| 115 | |
---|
| 116 | def test_wrong_file(self): |
---|
| 117 | with self.assertRaises(RuntimeError): |
---|
[0b6d23d] | 118 | source('path_to/unexisting file.mp3') |
---|
[36e8956] | 119 | |
---|
[ca89d9f] | 120 | class aubio_source_test_wrong_params_with_file(aubio_source_test_case_base): |
---|
[15a005d] | 121 | |
---|
[36e8956] | 122 | def test_wrong_samplerate(self): |
---|
| 123 | with self.assertRaises(ValueError): |
---|
[0b6d23d] | 124 | source(self.default_test_sound, -1) |
---|
[36e8956] | 125 | |
---|
| 126 | def test_wrong_hop_size(self): |
---|
| 127 | with self.assertRaises(ValueError): |
---|
[0b6d23d] | 128 | source(self.default_test_sound, 0, -1) |
---|
[36e8956] | 129 | |
---|
| 130 | def test_wrong_channels(self): |
---|
| 131 | with self.assertRaises(ValueError): |
---|
[0b6d23d] | 132 | source(self.default_test_sound, 0, 0, -1) |
---|
[36e8956] | 133 | |
---|
| 134 | def test_wrong_seek(self): |
---|
[ca89d9f] | 135 | f = source(self.default_test_sound) |
---|
[36e8956] | 136 | with self.assertRaises(ValueError): |
---|
| 137 | f.seek(-1) |
---|
| 138 | |
---|
| 139 | def test_wrong_seek_too_large(self): |
---|
[ca89d9f] | 140 | f = source(self.default_test_sound) |
---|
[36e8956] | 141 | try: |
---|
| 142 | with self.assertRaises(ValueError): |
---|
| 143 | f.seek(f.duration + f.samplerate * 10) |
---|
[0b6d23d] | 144 | except AssertionError: |
---|
[36e8956] | 145 | self.skipTest('seeking after end of stream failed raising ValueError') |
---|
| 146 | |
---|
[31a5c405] | 147 | class aubio_source_readmulti_test_case(aubio_source_read_test_case): |
---|
[4949182] | 148 | |
---|
[11b49d7] | 149 | def read_from_source(self, f): |
---|
[31a5c405] | 150 | total_frames = 0 |
---|
| 151 | while True: |
---|
[0b6d23d] | 152 | _, read = f.do_multi() |
---|
[31a5c405] | 153 | total_frames += read |
---|
| 154 | if read < f.hop_size: break |
---|
[0b6d23d] | 155 | #result_str = "read {:.2f}s ({:d} frames in {:d} channels and {:d} blocks at {:d}Hz) from {:s}" |
---|
| 156 | #result_params = total_frames / float(f.samplerate), total_frames, f.channels, int(total_frames/f.hop_size), f.samplerate, f.uri |
---|
[36e8956] | 157 | #print (result_str.format(*result_params)) |
---|
[11b49d7] | 158 | return total_frames |
---|
[4949182] | 159 | |
---|
[e1bfde5] | 160 | if __name__ == '__main__': |
---|
| 161 | main() |
---|