source: python/tests/test_source.py @ ad1df9b

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5pitchshiftsamplertimestretchyinfft+
Last change on this file since ad1df9b was fbd7c80, checked in by Paul Brossier <piem@piem.org>, 9 years ago

python/tests/test_source.py: simplify print

  • Property mode set to 100755
File size: 3.7 KB
RevLine 
[75e715f]1#! /usr/bin/env python
[6192ce7]2
3from numpy.testing import TestCase, assert_equal, assert_almost_equal
4from aubio import fvec, source
5from numpy import array
[e1bfde5]6from utils import list_all_sounds
[6192ce7]7
[e1bfde5]8list_of_sounds = list_all_sounds('sounds')
9path = None
[6192ce7]10
[31a5c405]11class 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]16class 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
33class 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
[fbd7c80]41        result_str = "read {:.2f}s ({:d} frames in {:d} blocks at {:d}Hz) from {:s}"
42        params = total_frames / float(f.samplerate), total_frames, int(total_frames/f.hop_size), f.samplerate, f.uri
43        print (result_str.format(*params))
[11b49d7]44        return total_frames
[6192ce7]45
[e1bfde5]46    def test_samplerate_hopsize(self):
47        for p in list_of_sounds:
48            for samplerate, hop_size in zip([0, 44100, 8000, 32000], [ 512, 512, 64, 256]):
49                f = source(p, samplerate, hop_size)
50                assert f.samplerate != 0
[11b49d7]51                self.read_from_source(f)
[e1bfde5]52
53    def test_samplerate_none(self):
54        for p in list_of_sounds:
55            f = source(p)
56            assert f.samplerate != 0
[11b49d7]57            self.read_from_source(f)
[e1bfde5]58
59    def test_samplerate_0(self):
60        for p in list_of_sounds:
61            f = source(p, 0)
62            assert f.samplerate != 0
[11b49d7]63            self.read_from_source(f)
[e1bfde5]64
65    def test_wrong_samplerate(self):
66        for p in list_of_sounds:
67            try:
68                f = source(p, -1)
[689106e]69            except ValueError as e:
[c76345e]70                pass
[e1bfde5]71            else:
[c76345e]72                self.fail('negative samplerate does not raise ValueError')
[6192ce7]73
[e1bfde5]74    def test_wrong_hop_size(self):
75        for p in list_of_sounds:
[01e7be0]76            try:
77                f = source(p, 0, -1)
[689106e]78            except ValueError as e:
[c76345e]79                pass
[01e7be0]80            else:
[c76345e]81                self.fail('negative hop_size does not raise ValueError')
[e1bfde5]82
83    def test_zero_hop_size(self):
84        for p in list_of_sounds:
85            f = source(p, 0, 0)
86            assert f.samplerate != 0
87            assert f.hop_size != 0
[11b49d7]88            self.read_from_source(f)
89
90    def test_seek_to_half(self):
91        from random import randint
92        for p in list_of_sounds:
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
[e1bfde5]101
[31a5c405]102class aubio_source_readmulti_test_case(aubio_source_read_test_case):
[4949182]103
[11b49d7]104    def read_from_source(self, f):
[31a5c405]105        total_frames = 0
106        while True:
107            vec, read = f.do_multi()
108            total_frames += read
109            if read < f.hop_size: break
[fbd7c80]110        result_str = "read {:.2f}s ({:d} frames in {:d} channels and {:d} blocks at {:d}Hz) from {:s}"
111        params = total_frames / float(f.samplerate), total_frames, f.channels, int(total_frames/f.hop_size), f.samplerate, f.uri
112        print (result_str.format(*params))
[11b49d7]113        return total_frames
[4949182]114
[e1bfde5]115if __name__ == '__main__':
116    from unittest import main
117    main()
Note: See TracBrowser for help on using the repository browser.