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