source: python/tests/test_source.py @ 6e8f021

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5sampler
Last change on this file since 6e8f021 was 39be048, checked in by Paul Brossier <piem@piem.org>, 8 years ago

python/tests/test_source.py: test with interface (PEP 343)

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