Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • python/tests/test_source.py

    r11b49d7 r0b6d23d  
    11#! /usr/bin/env python
    22
    3 from numpy.testing import TestCase, assert_equal, assert_almost_equal
    4 from aubio import fvec, source
    5 from numpy import array
     3from nose2 import main
     4from nose2.tools import params
     5from numpy.testing import TestCase
     6from aubio import source
    67from utils import list_all_sounds
    78
    89list_of_sounds = list_all_sounds('sounds')
     10samplerates = [0, 44100, 8000, 32000]
     11hop_sizes = [512, 1024, 64]
     12
    913path = None
     14
     15all_params = []
     16for soundfile in list_of_sounds:
     17    for hop_size in hop_sizes:
     18        for samplerate in samplerates:
     19            all_params.append((hop_size, samplerate, soundfile))
     20
    1021
    1122class aubio_source_test_case_base(TestCase):
     
    1324    def setUp(self):
    1425        if not len(list_of_sounds): self.skipTest('add some sound files in \'python/tests/sounds\'')
     26        self.default_test_sound = list_of_sounds[0]
    1527
    1628class aubio_source_test_case(aubio_source_test_case_base):
     
    3648        total_frames = 0
    3749        while True:
    38             vec, read = f()
     50            _ , read = f()
    3951            total_frames += read
    4052            if read < f.hop_size: break
    41         print "read", "%.2fs" % (total_frames / float(f.samplerate) ),
    42         print "(", total_frames, "frames", "in",
    43         print total_frames / f.hop_size, "blocks", "at", "%dHz" % f.samplerate, ")",
    44         print "from", f.uri
     53        #result_str = "read {:.2f}s ({:d} frames in {:d} blocks at {:d}Hz) from {:s}"
     54        #result_params = total_frames / float(f.samplerate), total_frames, total_frames//f.hop_size, f.samplerate, f.uri
     55        #print (result_str.format(*result_params))
    4556        return total_frames
    4657
    47     def test_samplerate_hopsize(self):
    48         for p in list_of_sounds:
    49             for samplerate, hop_size in zip([0, 44100, 8000, 32000], [ 512, 512, 64, 256]):
    50                 f = source(p, samplerate, hop_size)
    51                 assert f.samplerate != 0
    52                 self.read_from_source(f)
     58    @params(*all_params)
     59    def test_samplerate_hopsize(self, hop_size, samplerate, soundfile):
     60        try:
     61            f = source(soundfile, samplerate, hop_size)
     62        except RuntimeError as e:
     63            self.skipTest('failed opening with hop_s = {:d}, samplerate = {:d} ({:s})'.format(hop_size, samplerate, str(e)))
     64        assert f.samplerate != 0
     65        self.read_from_source(f)
    5366
    54     def test_samplerate_none(self):
    55         for p in list_of_sounds:
    56             f = source(p)
    57             assert f.samplerate != 0
    58             self.read_from_source(f)
     67    @params(*list_of_sounds)
     68    def test_samplerate_none(self, p):
     69        f = source(p)
     70        assert f.samplerate != 0
     71        self.read_from_source(f)
    5972
    60     def test_samplerate_0(self):
    61         for p in list_of_sounds:
    62             f = source(p, 0)
    63             assert f.samplerate != 0
    64             self.read_from_source(f)
     73    @params(*list_of_sounds)
     74    def test_samplerate_0(self, p):
     75        f = source(p, 0)
     76        assert f.samplerate != 0
     77        self.read_from_source(f)
     78
     79    @params(*list_of_sounds)
     80    def test_zero_hop_size(self, p):
     81        f = source(p, 0, 0)
     82        assert f.samplerate != 0
     83        assert f.hop_size != 0
     84        self.read_from_source(f)
     85
     86    @params(*list_of_sounds)
     87    def test_seek_to_half(self, p):
     88        from random import randint
     89        f = source(p, 0, 0)
     90        assert f.samplerate != 0
     91        assert f.hop_size != 0
     92        a = self.read_from_source(f)
     93        c = randint(0, a)
     94        f.seek(c)
     95        b = self.read_from_source(f)
     96        assert a == b + c
     97
     98    @params(*list_of_sounds)
     99    def test_duration(self, p):
     100        total_frames = 0
     101        f = source(p)
     102        duration = f.duration
     103        while True:
     104            _, read = f()
     105            total_frames += read
     106            if read < f.hop_size: break
     107        self.assertEqual(duration, total_frames)
     108
     109
     110class aubio_source_test_wrong_params(TestCase):
     111
     112    def test_wrong_file(self):
     113        with self.assertRaises(RuntimeError):
     114            source('path_to/unexisting file.mp3')
     115
     116class aubio_source_test_wrong_params_with_file(aubio_source_test_case_base):
    65117
    66118    def test_wrong_samplerate(self):
    67         for p in list_of_sounds:
    68             try:
    69                 f = source(p, -1)
    70             except ValueError, e:
    71                 pass
    72             else:
    73                 self.fail('negative samplerate does not raise ValueError')
     119        with self.assertRaises(ValueError):
     120            source(self.default_test_sound, -1)
    74121
    75122    def test_wrong_hop_size(self):
    76         for p in list_of_sounds:
    77             try:
    78                 f = source(p, 0, -1)
    79             except ValueError, e:
    80                 pass
    81             else:
    82                 self.fail('negative hop_size does not raise ValueError')
     123        with self.assertRaises(ValueError):
     124            source(self.default_test_sound, 0, -1)
    83125
    84     def test_zero_hop_size(self):
    85         for p in list_of_sounds:
    86             f = source(p, 0, 0)
    87             assert f.samplerate != 0
    88             assert f.hop_size != 0
    89             self.read_from_source(f)
     126    def test_wrong_channels(self):
     127        with self.assertRaises(ValueError):
     128            source(self.default_test_sound, 0, 0, -1)
    90129
    91     def test_seek_to_half(self):
    92         from random import randint
    93         for p in list_of_sounds:
    94             f = source(p, 0, 0)
    95             assert f.samplerate != 0
    96             assert f.hop_size != 0
    97             a = self.read_from_source(f)
    98             c = randint(0, a)
    99             f.seek(c)
    100             b = self.read_from_source(f)
    101             assert a == b + c
     130    def test_wrong_seek(self):
     131        f = source(self.default_test_sound)
     132        with self.assertRaises(ValueError):
     133            f.seek(-1)
     134
     135    def test_wrong_seek_too_large(self):
     136        f = source(self.default_test_sound)
     137        try:
     138            with self.assertRaises(ValueError):
     139                f.seek(f.duration + f.samplerate * 10)
     140        except AssertionError:
     141            self.skipTest('seeking after end of stream failed raising ValueError')
    102142
    103143class aubio_source_readmulti_test_case(aubio_source_read_test_case):
     
    106146        total_frames = 0
    107147        while True:
    108             vec, read = f.do_multi()
     148            _, read = f.do_multi()
    109149            total_frames += read
    110150            if read < f.hop_size: break
    111         print "read", "%.2fs" % (total_frames / float(f.samplerate) ),
    112         print "(", total_frames, "frames", "in",
    113         print f.channels, "channels and",
    114         print total_frames / f.hop_size, "blocks", "at", "%dHz" % f.samplerate, ")",
    115         print "from", f.uri
     151        #result_str = "read {:.2f}s ({:d} frames in {:d} channels and {:d} blocks at {:d}Hz) from {:s}"
     152        #result_params = total_frames / float(f.samplerate), total_frames, f.channels, int(total_frames/f.hop_size), f.samplerate, f.uri
     153        #print (result_str.format(*result_params))
    116154        return total_frames
    117155
    118156if __name__ == '__main__':
    119     from unittest import main
    120157    main()
Note: See TracChangeset for help on using the changeset viewer.