source: python/tests/test_source.py @ d9a5466

feature/crepe_org
Last change on this file since d9a5466 was 51b5f9c, checked in by Paul Brossier <piem@piem.org>, 6 years ago

[tests] check resampling a source raises a warning when expected

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