[75e715f] | 1 | #! /usr/bin/env python |
---|
[6192ce7] | 2 | |
---|
| 3 | from numpy.testing import TestCase, assert_equal, assert_almost_equal |
---|
| 4 | from aubio import fvec, source |
---|
| 5 | from numpy import array |
---|
[e1bfde5] | 6 | from utils import list_all_sounds |
---|
[5f5f843] | 7 | from nose2.tools import params |
---|
[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\'') |
---|
[6192ce7] | 26 | |
---|
[31a5c405] | 27 | class aubio_source_test_case(aubio_source_test_case_base): |
---|
| 28 | |
---|
| 29 | def test_close_file(self): |
---|
| 30 | samplerate = 0 # use native samplerate |
---|
| 31 | hop_size = 256 |
---|
| 32 | for p in list_of_sounds: |
---|
| 33 | f = source(p, samplerate, hop_size) |
---|
| 34 | f.close() |
---|
| 35 | |
---|
| 36 | def test_close_file_twice(self): |
---|
| 37 | samplerate = 0 # use native samplerate |
---|
| 38 | hop_size = 256 |
---|
| 39 | for p in list_of_sounds: |
---|
| 40 | f = source(p, samplerate, hop_size) |
---|
| 41 | f.close() |
---|
| 42 | f.close() |
---|
| 43 | |
---|
| 44 | class aubio_source_read_test_case(aubio_source_test_case_base): |
---|
| 45 | |
---|
[11b49d7] | 46 | def read_from_source(self, f): |
---|
[e1bfde5] | 47 | total_frames = 0 |
---|
| 48 | while True: |
---|
| 49 | vec, read = f() |
---|
| 50 | total_frames += read |
---|
| 51 | if read < f.hop_size: break |
---|
[fbd7c80] | 52 | result_str = "read {:.2f}s ({:d} frames in {:d} blocks at {:d}Hz) from {:s}" |
---|
[5f5f843] | 53 | result_params = total_frames / float(f.samplerate), total_frames, int(total_frames/f.hop_size), f.samplerate, f.uri |
---|
| 54 | print (result_str.format(*result_params)) |
---|
[11b49d7] | 55 | return total_frames |
---|
[6192ce7] | 56 | |
---|
[5f5f843] | 57 | @params(*all_params) |
---|
| 58 | def test_samplerate_hopsize(self, hop_size, samplerate, soundfile): |
---|
| 59 | try: |
---|
| 60 | f = source(soundfile, samplerate, hop_size) |
---|
| 61 | except RuntimeError as e: |
---|
[355c761] | 62 | self.skipTest('failed opening with hop_s = {:d}, samplerate = {:d} ({:s})'.format(hop_size, samplerate, str(e))) |
---|
[5f5f843] | 63 | assert f.samplerate != 0 |
---|
| 64 | self.read_from_source(f) |
---|
| 65 | |
---|
| 66 | @params(*list_of_sounds) |
---|
| 67 | def test_samplerate_none(self, p): |
---|
| 68 | f = source(p) |
---|
| 69 | assert f.samplerate != 0 |
---|
| 70 | self.read_from_source(f) |
---|
| 71 | |
---|
| 72 | @params(*list_of_sounds) |
---|
| 73 | def test_samplerate_0(self, p): |
---|
| 74 | f = source(p, 0) |
---|
| 75 | assert f.samplerate != 0 |
---|
| 76 | self.read_from_source(f) |
---|
| 77 | |
---|
| 78 | @params(*list_of_sounds) |
---|
| 79 | def test_wrong_samplerate(self, p): |
---|
| 80 | try: |
---|
| 81 | f = source(p, -1) |
---|
| 82 | except ValueError as e: |
---|
| 83 | pass |
---|
| 84 | else: |
---|
| 85 | self.fail('negative samplerate does not raise ValueError') |
---|
| 86 | |
---|
| 87 | @params(*list_of_sounds) |
---|
| 88 | def test_wrong_hop_size(self, p): |
---|
| 89 | try: |
---|
| 90 | f = source(p, 0, -1) |
---|
| 91 | except ValueError as e: |
---|
| 92 | pass |
---|
| 93 | else: |
---|
| 94 | self.fail('negative hop_size does not raise ValueError') |
---|
| 95 | |
---|
| 96 | @params(*list_of_sounds) |
---|
| 97 | def test_zero_hop_size(self, p): |
---|
| 98 | f = source(p, 0, 0) |
---|
| 99 | assert f.samplerate != 0 |
---|
| 100 | assert f.hop_size != 0 |
---|
| 101 | self.read_from_source(f) |
---|
| 102 | |
---|
| 103 | @params(*list_of_sounds) |
---|
| 104 | def test_seek_to_half(self, p): |
---|
[11b49d7] | 105 | from random import randint |
---|
[5f5f843] | 106 | f = source(p, 0, 0) |
---|
| 107 | assert f.samplerate != 0 |
---|
| 108 | assert f.hop_size != 0 |
---|
| 109 | a = self.read_from_source(f) |
---|
| 110 | c = randint(0, a) |
---|
| 111 | f.seek(c) |
---|
| 112 | b = self.read_from_source(f) |
---|
| 113 | assert a == b + c |
---|
| 114 | |
---|
| 115 | @params(*list_of_sounds) |
---|
| 116 | def test_duration(self, p): |
---|
| 117 | total_frames = 0 |
---|
| 118 | f = source(p) |
---|
| 119 | duration = f.duration |
---|
| 120 | while True: |
---|
| 121 | vec, read = f() |
---|
| 122 | total_frames += read |
---|
| 123 | if read < f.hop_size: break |
---|
| 124 | self.assertEqual(duration, total_frames) |
---|
[cfa46b9] | 125 | |
---|
[31a5c405] | 126 | class aubio_source_readmulti_test_case(aubio_source_read_test_case): |
---|
[4949182] | 127 | |
---|
[11b49d7] | 128 | def read_from_source(self, f): |
---|
[31a5c405] | 129 | total_frames = 0 |
---|
| 130 | while True: |
---|
| 131 | vec, read = f.do_multi() |
---|
| 132 | total_frames += read |
---|
| 133 | if read < f.hop_size: break |
---|
[fbd7c80] | 134 | result_str = "read {:.2f}s ({:d} frames in {:d} channels and {:d} blocks at {:d}Hz) from {:s}" |
---|
[5f5f843] | 135 | result_params = total_frames / float(f.samplerate), total_frames, f.channels, int(total_frames/f.hop_size), f.samplerate, f.uri |
---|
| 136 | print (result_str.format(*result_params)) |
---|
[11b49d7] | 137 | return total_frames |
---|
[4949182] | 138 | |
---|
[e1bfde5] | 139 | if __name__ == '__main__': |
---|
[5f5f843] | 140 | from nose2 import main |
---|
[e1bfde5] | 141 | main() |
---|