source: python/tests/test_source.py @ 870ad70

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

python/tests/test_source.py: improve exception checks

  • Property mode set to 100755
File size: 2.6 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
[e1bfde5]11class aubio_source_test_case(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
[e1bfde5]16    def read_from_sink(self, f):
17        total_frames = 0
18        while True:
19            vec, read = f()
20            total_frames += read
21            if read < f.hop_size: break
22        print "read", "%.2fs" % (total_frames / float(f.samplerate) ),
23        print "(", total_frames, "frames", "in",
24        print total_frames / f.hop_size, "blocks", "at", "%dHz" % f.samplerate, ")",
25        print "from", f.uri
[6192ce7]26
[e1bfde5]27    def test_samplerate_hopsize(self):
28        for p in list_of_sounds:
29            for samplerate, hop_size in zip([0, 44100, 8000, 32000], [ 512, 512, 64, 256]):
30                f = source(p, samplerate, hop_size)
31                assert f.samplerate != 0
32                self.read_from_sink(f)
33
34    def test_samplerate_none(self):
35        for p in list_of_sounds:
36            f = source(p)
37            assert f.samplerate != 0
38            self.read_from_sink(f)
39
40    def test_samplerate_0(self):
41        for p in list_of_sounds:
42            f = source(p, 0)
43            assert f.samplerate != 0
44            self.read_from_sink(f)
45
46    def test_wrong_samplerate(self):
47        for p in list_of_sounds:
48            try:
49                f = source(p, -1)
[c76345e]50            except ValueError, e:
51                pass
[e1bfde5]52            else:
[c76345e]53                self.fail('negative samplerate does not raise ValueError')
[6192ce7]54
[e1bfde5]55    def test_wrong_hop_size(self):
56        for p in list_of_sounds:
[01e7be0]57            try:
58                f = source(p, 0, -1)
[c76345e]59            except ValueError, e:
60                pass
[01e7be0]61            else:
[c76345e]62                self.fail('negative hop_size does not raise ValueError')
[e1bfde5]63
64    def test_zero_hop_size(self):
65        for p in list_of_sounds:
66            f = source(p, 0, 0)
67            assert f.samplerate != 0
68            assert f.hop_size != 0
69            self.read_from_sink(f)
70
[4949182]71    def test_close_file(self):
72        samplerate = 0 # use native samplerate
73        hop_size = 256
74        for p in list_of_sounds:
75            f = source(p, samplerate, hop_size)
76            f.close()
77
78    def test_close_file_twice(self):
79        samplerate = 0 # use native samplerate
80        hop_size = 256
81        for p in list_of_sounds:
82            f = source(p, samplerate, hop_size)
83            f.close()
84            f.close()
85
[e1bfde5]86if __name__ == '__main__':
87    from unittest import main
88    main()
Note: See TracBrowser for help on using the repository browser.