Changeset f264b17 for python/tests/test_source.py
- Timestamp:
- Jun 22, 2016, 1:00:10 PM (9 years ago)
- Branches:
- feature/autosink, feature/cnn, feature/cnn_org, feature/constantq, feature/crepe, feature/crepe_org, feature/pitchshift, feature/pydocstrings, feature/timestretch, fix/ffmpeg5, master, pitchshift, sampler, timestretch, yinfft+
- Children:
- 4b9443c4
- Parents:
- 60fc05b (diff), 6769586 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
python/tests/test_source.py
r60fc05b rf264b17 1 1 #! /usr/bin/env python 2 2 3 from numpy.testing import TestCase, assert_equal, assert_almost_equal 4 from aubio import fvec, source 5 from numpy import array 3 from nose2 import main 4 from nose2.tools import params 5 from numpy.testing import TestCase 6 from aubio import source 6 7 from utils import list_all_sounds 7 8 8 9 list_of_sounds = list_all_sounds('sounds') 10 samplerates = [0, 44100, 8000, 32000] 11 hop_sizes = [512, 1024, 64] 12 9 13 path = None 14 15 all_params = [] 16 for 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 10 21 11 22 class aubio_source_test_case_base(TestCase): … … 13 24 def setUp(self): 14 25 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] 15 27 16 28 class aubio_source_test_case(aubio_source_test_case_base): … … 36 48 total_frames = 0 37 49 while True: 38 vec, read = f()50 _ , read = f() 39 51 total_frames += read 40 52 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)) 45 56 return total_frames 46 57 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) 53 66 54 def test_samplerate_none(self):55 for p in list_of_sounds:56 57 58 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) 59 72 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 110 class 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 116 class aubio_source_test_wrong_params_with_file(aubio_source_test_case_base): 65 117 66 118 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) 74 121 75 122 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) 83 125 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) 90 129 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') 102 142 103 143 class aubio_source_readmulti_test_case(aubio_source_read_test_case): … … 106 146 total_frames = 0 107 147 while True: 108 vec, read = f.do_multi()148 _, read = f.do_multi() 109 149 total_frames += read 110 150 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)) 116 154 return total_frames 117 155 118 156 if __name__ == '__main__': 119 from unittest import main120 157 main()
Note: See TracChangeset
for help on using the changeset viewer.