[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 |
---|
[6192ce7] | 7 | |
---|
[e1bfde5] | 8 | list_of_sounds = list_all_sounds('sounds') |
---|
| 9 | path = None |
---|
[6192ce7] | 10 | |
---|
[31a5c405] | 11 | class aubio_source_test_case_base(TestCase): |
---|
[6192ce7] | 12 | |
---|
[e1bfde5] | 13 | def setUp(self): |
---|
| 14 | if not len(list_of_sounds): self.skipTest('add some sound files in \'python/tests/sounds\'') |
---|
[6192ce7] | 15 | |
---|
[31a5c405] | 16 | class aubio_source_test_case(aubio_source_test_case_base): |
---|
| 17 | |
---|
| 18 | def test_close_file(self): |
---|
| 19 | samplerate = 0 # use native samplerate |
---|
| 20 | hop_size = 256 |
---|
| 21 | for p in list_of_sounds: |
---|
| 22 | f = source(p, samplerate, hop_size) |
---|
| 23 | f.close() |
---|
| 24 | |
---|
| 25 | def test_close_file_twice(self): |
---|
| 26 | samplerate = 0 # use native samplerate |
---|
| 27 | hop_size = 256 |
---|
| 28 | for p in list_of_sounds: |
---|
| 29 | f = source(p, samplerate, hop_size) |
---|
| 30 | f.close() |
---|
| 31 | f.close() |
---|
| 32 | |
---|
| 33 | class aubio_source_read_test_case(aubio_source_test_case_base): |
---|
| 34 | |
---|
[11b49d7] | 35 | def read_from_source(self, f): |
---|
[e1bfde5] | 36 | total_frames = 0 |
---|
| 37 | while True: |
---|
| 38 | vec, read = f() |
---|
| 39 | total_frames += read |
---|
| 40 | if read < f.hop_size: break |
---|
| 41 | print "read", "%.2fs" % (total_frames / float(f.samplerate) ), |
---|
| 42 | print "(", total_frames, "frames", "in", |
---|
| 43 | print total_frames / f.hop_size, "blocks", "at", "%dHz" % f.samplerate, ")", |
---|
| 44 | print "from", f.uri |
---|
[11b49d7] | 45 | return total_frames |
---|
[6192ce7] | 46 | |
---|
[e1bfde5] | 47 | def test_samplerate_hopsize(self): |
---|
| 48 | for p in list_of_sounds: |
---|
| 49 | for samplerate, hop_size in zip([0, 44100, 8000, 32000], [ 512, 512, 64, 256]): |
---|
| 50 | f = source(p, samplerate, hop_size) |
---|
| 51 | assert f.samplerate != 0 |
---|
[11b49d7] | 52 | self.read_from_source(f) |
---|
[e1bfde5] | 53 | |
---|
| 54 | def test_samplerate_none(self): |
---|
| 55 | for p in list_of_sounds: |
---|
| 56 | f = source(p) |
---|
| 57 | assert f.samplerate != 0 |
---|
[11b49d7] | 58 | self.read_from_source(f) |
---|
[e1bfde5] | 59 | |
---|
| 60 | def test_samplerate_0(self): |
---|
| 61 | for p in list_of_sounds: |
---|
| 62 | f = source(p, 0) |
---|
| 63 | assert f.samplerate != 0 |
---|
[11b49d7] | 64 | self.read_from_source(f) |
---|
[e1bfde5] | 65 | |
---|
| 66 | def test_wrong_samplerate(self): |
---|
| 67 | for p in list_of_sounds: |
---|
| 68 | try: |
---|
| 69 | f = source(p, -1) |
---|
[c76345e] | 70 | except ValueError, e: |
---|
| 71 | pass |
---|
[e1bfde5] | 72 | else: |
---|
[c76345e] | 73 | self.fail('negative samplerate does not raise ValueError') |
---|
[6192ce7] | 74 | |
---|
[e1bfde5] | 75 | def test_wrong_hop_size(self): |
---|
| 76 | for p in list_of_sounds: |
---|
[01e7be0] | 77 | try: |
---|
| 78 | f = source(p, 0, -1) |
---|
[c76345e] | 79 | except ValueError, e: |
---|
| 80 | pass |
---|
[01e7be0] | 81 | else: |
---|
[c76345e] | 82 | self.fail('negative hop_size does not raise ValueError') |
---|
[e1bfde5] | 83 | |
---|
| 84 | def test_zero_hop_size(self): |
---|
| 85 | for p in list_of_sounds: |
---|
| 86 | f = source(p, 0, 0) |
---|
| 87 | assert f.samplerate != 0 |
---|
| 88 | assert f.hop_size != 0 |
---|
[11b49d7] | 89 | self.read_from_source(f) |
---|
| 90 | |
---|
| 91 | def test_seek_to_half(self): |
---|
| 92 | from random import randint |
---|
| 93 | for p in list_of_sounds: |
---|
| 94 | f = source(p, 0, 0) |
---|
| 95 | assert f.samplerate != 0 |
---|
| 96 | assert f.hop_size != 0 |
---|
| 97 | a = self.read_from_source(f) |
---|
| 98 | c = randint(0, a) |
---|
| 99 | f.seek(c) |
---|
| 100 | b = self.read_from_source(f) |
---|
| 101 | assert a == b + c |
---|
[e1bfde5] | 102 | |
---|
[31a5c405] | 103 | class aubio_source_readmulti_test_case(aubio_source_read_test_case): |
---|
[4949182] | 104 | |
---|
[11b49d7] | 105 | def read_from_source(self, f): |
---|
[31a5c405] | 106 | total_frames = 0 |
---|
| 107 | while True: |
---|
| 108 | vec, read = f.do_multi() |
---|
| 109 | total_frames += read |
---|
| 110 | if read < f.hop_size: break |
---|
| 111 | print "read", "%.2fs" % (total_frames / float(f.samplerate) ), |
---|
| 112 | print "(", total_frames, "frames", "in", |
---|
| 113 | print f.channels, "channels and", |
---|
| 114 | print total_frames / f.hop_size, "blocks", "at", "%dHz" % f.samplerate, ")", |
---|
| 115 | print "from", f.uri |
---|
[11b49d7] | 116 | return total_frames |
---|
[4949182] | 117 | |
---|
[e1bfde5] | 118 | if __name__ == '__main__': |
---|
| 119 | from unittest import main |
---|
| 120 | main() |
---|