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