Changeset ad45776 for python/tests
- Timestamp:
- Nov 2, 2018, 12:10:29 AM (6 years ago)
- Branches:
- feature/autosink, feature/cnn, feature/cnn_org, feature/constantq, feature/crepe, feature/crepe_org, feature/pitchshift, feature/timestretch, fix/ffmpeg5, master
- Children:
- 235a891
- Parents:
- ecd370b
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
python/tests/test_source.py
recd370b rad45776 1 1 #! /usr/bin/env python 2 2 3 from nose2 import main 4 from nose2.tools import params 3 5 4 from numpy.testing import TestCase, assert_equal 6 5 from aubio import source 7 from .utils import list_all_sounds 8 9 import warnings 10 warnings.filterwarnings('ignore', category=UserWarning, append=True) 6 from utils import list_all_sounds 7 import unittest 8 from _tools import parametrize, assert_raises, assert_equal, skipTest 11 9 12 10 list_of_sounds = list_all_sounds('sounds') … … 14 12 hop_sizes = [512, 1024, 64] 15 13 16 path =None14 default_test_sound = len(list_of_sounds) and list_of_sounds[0] or None 17 15 18 16 all_params = [] … … 22 20 all_params.append((hop_size, samplerate, soundfile)) 23 21 22 no_sounds_msg = "no test sounds, add some in 'python/tests/sounds/'!" 24 23 25 class aubio_source_test_case_base(TestCase): 24 _debug = False 26 25 27 def setUp(self): 28 if not len(list_of_sounds): 29 self.skipTest('add some sound files in \'python/tests/sounds\'') 30 self.default_test_sound = list_of_sounds[0] 26 class Test_aubio_source_test_case: 31 27 32 class aubio_source_test_case(aubio_source_test_case_base): 33 34 @params(*list_of_sounds) 28 @parametrize('filename', list_of_sounds) 35 29 def test_close_file(self, filename): 36 30 samplerate = 0 # use native samplerate … … 39 33 f.close() 40 34 41 @param s(*list_of_sounds)35 @parametrize('filename', list_of_sounds) 42 36 def test_close_file_twice(self, filename): 43 37 samplerate = 0 # use native samplerate … … 47 41 f.close() 48 42 49 class aubio_source_read_test_case(aubio_source_test_case_base):43 class Test_aubio_source_read: 50 44 51 45 def read_from_source(self, f): … … 57 51 assert_equal(samples[read:], 0) 58 52 break 59 #result_str = "read {:.2f}s ({:d} frames in {:d} blocks at {:d}Hz) from {:s}" 60 #result_params = total_frames / float(f.samplerate), total_frames, total_frames//f.hop_size, f.samplerate, f.uri 61 #print (result_str.format(*result_params)) 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)) 62 59 return total_frames 63 60 64 @param s(*all_params)61 @parametrize('hop_size, samplerate, soundfile', all_params) 65 62 def test_samplerate_hopsize(self, hop_size, samplerate, soundfile): 66 63 try: 67 64 f = source(soundfile, samplerate, hop_size) 68 65 except RuntimeError as e: 69 self.skipTest('failed opening with hop_s = {:d}, samplerate = {:d} ({:s})'.format(hop_size, samplerate, str(e))) 66 err_msg = 'failed opening with hop_s={:d}, samplerate={:d} ({:s})' 67 skipTest(err_msg.format(hop_size, samplerate, str(e))) 70 68 assert f.samplerate != 0 71 69 read_frames = self.read_from_source(f) … … 76 74 if len(match_f) == 1: 77 75 expected_frames = int(match_f[0]) 78 self.assertEqual(expected_frames, read_frames)76 assert_equal(expected_frames, read_frames) 79 77 80 @param s(*list_of_sounds)78 @parametrize('p', list_of_sounds) 81 79 def test_samplerate_none(self, p): 82 80 f = source(p) … … 84 82 self.read_from_source(f) 85 83 86 @param s(*list_of_sounds)84 @parametrize('p', list_of_sounds) 87 85 def test_samplerate_0(self, p): 88 86 f = source(p, 0) … … 90 88 self.read_from_source(f) 91 89 92 @param s(*list_of_sounds)90 @parametrize('p', list_of_sounds) 93 91 def test_zero_hop_size(self, p): 94 92 f = source(p, 0, 0) … … 97 95 self.read_from_source(f) 98 96 99 @param s(*list_of_sounds)97 @parametrize('p', list_of_sounds) 100 98 def test_seek_to_half(self, p): 101 99 from random import randint … … 109 107 assert a == b + c 110 108 111 @param s(*list_of_sounds)109 @parametrize('p', list_of_sounds) 112 110 def test_duration(self, p): 113 111 total_frames = 0 … … 118 116 total_frames += read 119 117 if read < f.hop_size: break 120 self.assertEqual(duration, total_frames)118 assert_equal (duration, total_frames) 121 119 122 120 123 class aubio_source_test_wrong_params(TestCase):121 class Test_aubio_source_wrong_params: 124 122 125 123 def test_wrong_file(self): 126 with self.assertRaises(RuntimeError):124 with assert_raises(RuntimeError): 127 125 source('path_to/unexisting file.mp3') 128 126 129 class aubio_source_test_wrong_params_with_file(aubio_source_test_case_base): 127 @unittest.skipIf(default_test_sound is None, no_sounds_msg) 128 class Test_aubio_source_wrong_params_with_file(TestCase): 130 129 131 130 def test_wrong_samplerate(self): 132 with self.assertRaises(ValueError):133 source( self.default_test_sound, -1)131 with assert_raises(ValueError): 132 source(default_test_sound, -1) 134 133 135 134 def test_wrong_hop_size(self): 136 with self.assertRaises(ValueError):137 source( self.default_test_sound, 0, -1)135 with assert_raises(ValueError): 136 source(default_test_sound, 0, -1) 138 137 139 138 def test_wrong_channels(self): 140 with self.assertRaises(ValueError):141 source( self.default_test_sound, 0, 0, -1)139 with assert_raises(ValueError): 140 source(default_test_sound, 0, 0, -1) 142 141 143 142 def test_wrong_seek(self): 144 f = source( self.default_test_sound)145 with self.assertRaises(ValueError):143 f = source(default_test_sound) 144 with assert_raises(ValueError): 146 145 f.seek(-1) 147 146 148 147 def test_wrong_seek_too_large(self): 149 f = source( self.default_test_sound)148 f = source(default_test_sound) 150 149 try: 151 with self.assertRaises(ValueError):150 with assert_raises(ValueError): 152 151 f.seek(f.duration + f.samplerate * 10) 153 except AssertionError:154 s elf.skipTest('seeking after end of stream failed raising ValueError')152 except: 153 skipTest('seeking after end of stream failed raising ValueError') 155 154 156 class aubio_source_readmulti_test_case(aubio_source_read_test_case):155 class Test_aubio_source_readmulti(Test_aubio_source_read): 157 156 158 157 def read_from_source(self, f): … … 164 163 assert_equal(samples[:,read:], 0) 165 164 break 166 #result_str = "read {:.2f}s ({:d} frames in {:d} channels and {:d} blocks at {:d}Hz) from {:s}" 167 #result_params = total_frames / float(f.samplerate), total_frames, f.channels, int(total_frames/f.hop_size), f.samplerate, f.uri 168 #print (result_str.format(*result_params)) 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)) 169 172 return total_frames 170 173 171 class aubio_source_with(aubio_source_test_case_base):174 class Test_aubio_source_with: 172 175 173 #@params(*list_of_sounds) 174 @params(*list_of_sounds) 176 @parametrize('filename', list_of_sounds) 175 177 def test_read_from_mono(self, filename): 176 178 total_frames = 0 … … 186 188 187 189 if __name__ == '__main__': 188 main() 190 import sys, pytest 191 pytest.main(sys.argv)
Note: See TracChangeset
for help on using the changeset viewer.