source: python/tests/test_source.py @ ad45776

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/timestretchfix/ffmpeg5
Last change on this file since ad45776 was ad45776, checked in by Paul Brossier <piem@piem.org>, 5 years ago

[tests] update source tests

  • Property mode set to 100755
File size: 6.1 KB
RevLine 
[75e715f]1#! /usr/bin/env python
[6192ce7]2
[ad45776]3
[f91737d]4from numpy.testing import TestCase, assert_equal
[0b6d23d]5from aubio import source
[ad45776]6from utils import list_all_sounds
7import unittest
8from _tools import parametrize, assert_raises, assert_equal, skipTest
[d45f527]9
[e1bfde5]10list_of_sounds = list_all_sounds('sounds')
[5f5f843]11samplerates = [0, 44100, 8000, 32000]
12hop_sizes = [512, 1024, 64]
13
[ad45776]14default_test_sound = len(list_of_sounds) and list_of_sounds[0] or None
[6192ce7]15
[5f5f843]16all_params = []
17for soundfile in list_of_sounds:
18    for hop_size in hop_sizes:
19        for samplerate in samplerates:
20            all_params.append((hop_size, samplerate, soundfile))
21
[ad45776]22no_sounds_msg = "no test sounds, add some in 'python/tests/sounds/'!"
[5f5f843]23
[ad45776]24_debug = False
[6192ce7]25
[ad45776]26class Test_aubio_source_test_case:
[31a5c405]27
[ad45776]28    @parametrize('filename', list_of_sounds)
[980a4f4]29    def test_close_file(self, filename):
[31a5c405]30        samplerate = 0 # use native samplerate
31        hop_size = 256
[980a4f4]32        f = source(filename, samplerate, hop_size)
33        f.close()
[31a5c405]34
[ad45776]35    @parametrize('filename', list_of_sounds)
[980a4f4]36    def test_close_file_twice(self, filename):
[31a5c405]37        samplerate = 0 # use native samplerate
38        hop_size = 256
[980a4f4]39        f = source(filename, samplerate, hop_size)
40        f.close()
41        f.close()
[31a5c405]42
[ad45776]43class Test_aubio_source_read:
[31a5c405]44
[11b49d7]45    def read_from_source(self, f):
[e1bfde5]46        total_frames = 0
47        while True:
[f91737d]48            samples , read = f()
[e1bfde5]49            total_frames += read
[f91737d]50            if read < f.hop_size:
51                assert_equal(samples[read:], 0)
52                break
[ad45776]53        if _debug:
54            result_str = "read {:.2f}s ({:d} frames"
55            result_str += " in {:d} blocks at {:d}Hz) from {:s}"
56            result_params = total_frames / float(f.samplerate), total_frames, \
57                    total_frames//f.hop_size, f.samplerate, f.uri
58            print (result_str.format(*result_params))
[11b49d7]59        return total_frames
[6192ce7]60
[ad45776]61    @parametrize('hop_size, samplerate, soundfile', all_params)
[5f5f843]62    def test_samplerate_hopsize(self, hop_size, samplerate, soundfile):
63        try:
64            f = source(soundfile, samplerate, hop_size)
65        except RuntimeError as e:
[ad45776]66            err_msg = 'failed opening with hop_s={:d}, samplerate={:d} ({:s})'
67            skipTest(err_msg.format(hop_size, samplerate, str(e)))
[5f5f843]68        assert f.samplerate != 0
[8a49bb9]69        read_frames = self.read_from_source(f)
70        if 'f_' in soundfile and samplerate == 0:
71            import re
72            f = re.compile('.*_\([0:9]*f\)_.*')
73            match_f = re.findall('([0-9]*)f_', soundfile)
74            if len(match_f) == 1:
75                expected_frames = int(match_f[0])
[ad45776]76                assert_equal(expected_frames, read_frames)
[5f5f843]77
[ad45776]78    @parametrize('p', list_of_sounds)
[5f5f843]79    def test_samplerate_none(self, p):
80        f = source(p)
81        assert f.samplerate != 0
82        self.read_from_source(f)
83
[ad45776]84    @parametrize('p', list_of_sounds)
[5f5f843]85    def test_samplerate_0(self, p):
86        f = source(p, 0)
87        assert f.samplerate != 0
88        self.read_from_source(f)
89
[ad45776]90    @parametrize('p', list_of_sounds)
[5f5f843]91    def test_zero_hop_size(self, p):
92        f = source(p, 0, 0)
93        assert f.samplerate != 0
94        assert f.hop_size != 0
95        self.read_from_source(f)
96
[ad45776]97    @parametrize('p', list_of_sounds)
[5f5f843]98    def test_seek_to_half(self, p):
[11b49d7]99        from random import randint
[5f5f843]100        f = source(p, 0, 0)
101        assert f.samplerate != 0
102        assert f.hop_size != 0
103        a = self.read_from_source(f)
104        c = randint(0, a)
105        f.seek(c)
106        b = self.read_from_source(f)
107        assert a == b + c
108
[ad45776]109    @parametrize('p', list_of_sounds)
[5f5f843]110    def test_duration(self, p):
111        total_frames = 0
112        f = source(p)
113        duration = f.duration
114        while True:
[0b6d23d]115            _, read = f()
[5f5f843]116            total_frames += read
117            if read < f.hop_size: break
[ad45776]118        assert_equal (duration, total_frames)
[cfa46b9]119
[36e8956]120
[ad45776]121class Test_aubio_source_wrong_params:
[36e8956]122
123    def test_wrong_file(self):
[ad45776]124        with assert_raises(RuntimeError):
[0b6d23d]125            source('path_to/unexisting file.mp3')
[36e8956]126
[ad45776]127@unittest.skipIf(default_test_sound is None, no_sounds_msg)
128class Test_aubio_source_wrong_params_with_file(TestCase):
[15a005d]129
[36e8956]130    def test_wrong_samplerate(self):
[ad45776]131        with assert_raises(ValueError):
132            source(default_test_sound, -1)
[36e8956]133
134    def test_wrong_hop_size(self):
[ad45776]135        with assert_raises(ValueError):
136            source(default_test_sound, 0, -1)
[36e8956]137
138    def test_wrong_channels(self):
[ad45776]139        with assert_raises(ValueError):
140            source(default_test_sound, 0, 0, -1)
[36e8956]141
142    def test_wrong_seek(self):
[ad45776]143        f = source(default_test_sound)
144        with assert_raises(ValueError):
[36e8956]145            f.seek(-1)
146
147    def test_wrong_seek_too_large(self):
[ad45776]148        f = source(default_test_sound)
[36e8956]149        try:
[ad45776]150            with assert_raises(ValueError):
[36e8956]151                f.seek(f.duration + f.samplerate * 10)
[ad45776]152        except:
153            skipTest('seeking after end of stream failed raising ValueError')
[36e8956]154
[ad45776]155class Test_aubio_source_readmulti(Test_aubio_source_read):
[4949182]156
[11b49d7]157    def read_from_source(self, f):
[31a5c405]158        total_frames = 0
159        while True:
[f91737d]160            samples, read = f.do_multi()
[31a5c405]161            total_frames += read
[f91737d]162            if read < f.hop_size:
163                assert_equal(samples[:,read:], 0)
164                break
[ad45776]165        if _debug:
166            result_str = "read {:.2f}s ({:d} frames in {:d} channels"
167            result_str += " and {:d} blocks at {:d}Hz) from {:s}"
168            result_params = total_frames / float(f.samplerate), total_frames, \
169                    f.channels, int(total_frames/f.hop_size), \
170                    f.samplerate, f.uri
171            print (result_str.format(*result_params))
[11b49d7]172        return total_frames
[4949182]173
[ad45776]174class Test_aubio_source_with:
[39be048]175
[ad45776]176    @parametrize('filename', list_of_sounds)
[39be048]177    def test_read_from_mono(self, filename):
178        total_frames = 0
179        hop_size = 2048
180        with source(filename, 0, hop_size) as input_source:
181            assert_equal(input_source.hop_size, hop_size)
182            #assert_equal(input_source.samplerate, samplerate)
183            total_frames = 0
184            for frames in input_source:
185                total_frames += frames.shape[-1]
186            # check we read as many samples as we expected
187            assert_equal(total_frames, input_source.duration)
188
[e1bfde5]189if __name__ == '__main__':
[ad45776]190    import sys, pytest
191    pytest.main(sys.argv)
Note: See TracBrowser for help on using the repository browser.