History

tests/test_{source,sink}.py: add sink, improve source, use sounds in python/tests/sounds

Paul Brossier committed 13 years ago

Commit e1bfde51 parent 143cc433 files · +104 −21Browse files

Showing the changes to python/tests (3 of 3 files). Show all

Unified · Split

3 files
  1. python/tests/test_sink.py +37 −0
  2. python/tests/test_source.py +58 −16
  3. python/tests/utils.py +9 −5
python/tests/test_sink.py+37−0
@@ -0,0 +1,37 @@
1#! /usr/bin/env python
2
3from numpy.testing import TestCase, assert_equal, assert_almost_equal
4from aubio import fvec, source, sink
5from numpy import array
6from utils import list_all_sounds
7
8list_of_sounds = list_all_sounds('sounds')
9path = None
10
11class aubio_sink_test_case(TestCase):
12
13 def setUp(self):
14 if not len(list_of_sounds): self.skipTest('add some sound files in \'python/tests/sounds\'')
15
16 def test_read(self):
17 for path in list_of_sounds:
18 for samplerate, hop_size in zip([0, 44100, 8000, 32000], [512, 1024, 64, 256]):
19 f = source(path, samplerate, hop_size)
20 if samplerate == 0: samplerate = f.samplerate
21 g = sink('/tmp/f.wav', samplerate)
22 total_frames = 0
23 while True:
24 vec, read = f()
25 #print g(vec, read)
26 total_frames += read
27 if read < f.hop_size: break
28 print "read", "%.2fs" % (total_frames / float(f.samplerate) ),
29 print "(", total_frames, "frames", "in",
30 print total_frames / f.hop_size, "blocks", "at", "%dHz" % f.samplerate, ")",
31 print "from", f.uri,
32 print "to", g.uri
33
34if __name__ == '__main__':
35 from unittest import main
36 main()
37
python/tests/test_source.py+58−16
@@ -3,25 +3,67 @@ · 2 unmodified lines
3from numpy.testing import TestCase, assert_equal, assert_almost_equal3from numpy.testing import TestCase, assert_equal, assert_almost_equal
4from aubio import fvec, source4from aubio import fvec, source
5from numpy import array5from numpy import array
6from utils import list_all_sounds
6 7
7path = "/Users/piem/archives/sounds/loops/drum_Chocolate_Milk_-_Ation_Speaks_Louder_Than_Words.wav"8list_of_sounds = list_all_sounds('sounds')
9path = None
8 10
9class aubio_filter_test_case(TestCase):11class aubio_source_test_case(TestCase):
10 12
11 def test_members(self):13 def setUp(self):
12 f = source(path)14 if not len(list_of_sounds): self.skipTest('add some sound files in \'python/tests/sounds\'')
13 print dir(f)
14 15
15 def test_read(self):16 def read_from_sink(self, f):
16 f = source(path)17 total_frames = 0
17 total_frames = 018 while True:
18 while True:19 vec, read = f()
19 vec, read = f()20 total_frames += read
20 total_frames += read21 if read < f.hop_size: break
21 if read < f.hop_size: break22 print "read", "%.2fs" % (total_frames / float(f.samplerate) ),
22 print "read", total_frames / float(f.samplerate), " seconds from", path23 print "(", total_frames, "frames", "in",
24 print total_frames / f.hop_size, "blocks", "at", "%dHz" % f.samplerate, ")",
25 print "from", f.uri
23 26
24if __name__ == '__main__':27 def test_samplerate_hopsize(self):
25 from unittest import main28 for p in list_of_sounds:
26 main()29 for samplerate, hop_size in zip([0, 44100, 8000, 32000], [ 512, 512, 64, 256]):
30 f = source(p, samplerate, hop_size)
31 assert f.samplerate != 0
32 self.read_from_sink(f)
33
34 def test_samplerate_none(self):
35 for p in list_of_sounds:
36 f = source(p)
37 assert f.samplerate != 0
38 self.read_from_sink(f)
39
40 def test_samplerate_0(self):
41 for p in list_of_sounds:
42 f = source(p, 0)
43 assert f.samplerate != 0
44 self.read_from_sink(f)
45
46 def test_wrong_samplerate(self):
47 for p in list_of_sounds:
48 try:
49 f = source(p, -1)
50 except Exception, e:
51 print e
52 else:
53 self.fail('does not fail with wrong samplerate')
27 54
55 def test_wrong_hop_size(self):
56 for p in list_of_sounds:
57 f = source(p, 0, -1)
58 print f.hop_size
59
60 def test_zero_hop_size(self):
61 for p in list_of_sounds:
62 f = source(p, 0, 0)
63 assert f.samplerate != 0
64 assert f.hop_size != 0
65 self.read_from_sink(f)
66
67if __name__ == '__main__':
68 from unittest import main
69 main()
python/tests/utils.py+9−5
@@ -1,9 +1,13 @@
1#! /usr/bin/env python1#! /usr/bin/env python
2 2
3def array_from_text_file(filename, dtype = 'float'):3def array_from_text_file(filename, dtype = 'float'):
4 import os.path4 import os.path
5 from numpy import array5 from numpy import array
6 filename = os.path.join(os.path.dirname(__file__), filename)6 filename = os.path.join(os.path.dirname(__file__), filename)
7 return array([line.split() for line in open(filename).readlines()], 7 return array([line.split() for line in open(filename).readlines()],
8 dtype = dtype)8 dtype = dtype)
9 9
10def list_all_sounds(rel_dir):
11 import os.path, glob
12 datadir = os.path.join(os.path.dirname(__file__), rel_dir)
13 return glob.glob(os.path.join(datadir,'*.*'))

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t