From e1bfde5b4ebe6465f95565b3f3ba978db22bbf30 Mon Sep 17 00:00:00 2001 From: Paul Brossier Date: Wed, 6 Mar 2013 15:25:23 -0500 Subject: [PATCH] tests/test_{source,sink}.py: add sink, improve source, use sounds in python/tests/sounds --- python/tests/test_sink.py | 37 +++++++++++++++++++ python/tests/test_source.py | 74 +++++++++++++++++++++++++++++-------- python/tests/utils.py | 14 ++++--- 3 files changed, 104 insertions(+), 21 deletions(-) create mode 100755 python/tests/test_sink.py diff --git a/python/tests/test_sink.py b/python/tests/test_sink.py new file mode 100755 index 00000000..21723ded --- /dev/null +++ b/python/tests/test_sink.py @@ -0,0 +1,37 @@ +#! /usr/bin/env python + +from numpy.testing import TestCase, assert_equal, assert_almost_equal +from aubio import fvec, source, sink +from numpy import array +from utils import list_all_sounds + +list_of_sounds = list_all_sounds('sounds') +path = None + +class aubio_sink_test_case(TestCase): + + def setUp(self): + if not len(list_of_sounds): self.skipTest('add some sound files in \'python/tests/sounds\'') + + def test_read(self): + for path in list_of_sounds: + for samplerate, hop_size in zip([0, 44100, 8000, 32000], [512, 1024, 64, 256]): + f = source(path, samplerate, hop_size) + if samplerate == 0: samplerate = f.samplerate + g = sink('/tmp/f.wav', samplerate) + total_frames = 0 + while True: + vec, read = f() + #print g(vec, read) + total_frames += read + if read < f.hop_size: break + print "read", "%.2fs" % (total_frames / float(f.samplerate) ), + print "(", total_frames, "frames", "in", + print total_frames / f.hop_size, "blocks", "at", "%dHz" % f.samplerate, ")", + print "from", f.uri, + print "to", g.uri + +if __name__ == '__main__': + from unittest import main + main() + diff --git a/python/tests/test_source.py b/python/tests/test_source.py index ae3b79bb..d26e4e78 100755 --- a/python/tests/test_source.py +++ b/python/tests/test_source.py @@ -3,25 +3,67 @@ from numpy.testing import TestCase, assert_equal, assert_almost_equal from aubio import fvec, source from numpy import array +from utils import list_all_sounds -path = "/Users/piem/archives/sounds/loops/drum_Chocolate_Milk_-_Ation_Speaks_Louder_Than_Words.wav" +list_of_sounds = list_all_sounds('sounds') +path = None -class aubio_filter_test_case(TestCase): +class aubio_source_test_case(TestCase): - def test_members(self): - f = source(path) - print dir(f) + def setUp(self): + if not len(list_of_sounds): self.skipTest('add some sound files in \'python/tests/sounds\'') - def test_read(self): - f = source(path) - total_frames = 0 - while True: - vec, read = f() - total_frames += read - if read < f.hop_size: break - print "read", total_frames / float(f.samplerate), " seconds from", path + def read_from_sink(self, f): + total_frames = 0 + while True: + vec, read = f() + total_frames += read + if read < f.hop_size: break + print "read", "%.2fs" % (total_frames / float(f.samplerate) ), + print "(", total_frames, "frames", "in", + print total_frames / f.hop_size, "blocks", "at", "%dHz" % f.samplerate, ")", + print "from", f.uri -if __name__ == '__main__': - from unittest import main - main() + def test_samplerate_hopsize(self): + for p in list_of_sounds: + for samplerate, hop_size in zip([0, 44100, 8000, 32000], [ 512, 512, 64, 256]): + f = source(p, samplerate, hop_size) + assert f.samplerate != 0 + self.read_from_sink(f) + + def test_samplerate_none(self): + for p in list_of_sounds: + f = source(p) + assert f.samplerate != 0 + self.read_from_sink(f) + + def test_samplerate_0(self): + for p in list_of_sounds: + f = source(p, 0) + assert f.samplerate != 0 + self.read_from_sink(f) + + def test_wrong_samplerate(self): + for p in list_of_sounds: + try: + f = source(p, -1) + except Exception, e: + print e + else: + self.fail('does not fail with wrong samplerate') + def test_wrong_hop_size(self): + for p in list_of_sounds: + f = source(p, 0, -1) + print f.hop_size + + def test_zero_hop_size(self): + for p in list_of_sounds: + f = source(p, 0, 0) + assert f.samplerate != 0 + assert f.hop_size != 0 + self.read_from_sink(f) + +if __name__ == '__main__': + from unittest import main + main() diff --git a/python/tests/utils.py b/python/tests/utils.py index 28c076e1..fa4dc44f 100644 --- a/python/tests/utils.py +++ b/python/tests/utils.py @@ -1,9 +1,13 @@ #! /usr/bin/env python def array_from_text_file(filename, dtype = 'float'): - import os.path - from numpy import array - filename = os.path.join(os.path.dirname(__file__), filename) - return array([line.split() for line in open(filename).readlines()], - dtype = dtype) + import os.path + from numpy import array + filename = os.path.join(os.path.dirname(__file__), filename) + return array([line.split() for line in open(filename).readlines()], + dtype = dtype) +def list_all_sounds(rel_dir): + import os.path, glob + datadir = os.path.join(os.path.dirname(__file__), rel_dir) + return glob.glob(os.path.join(datadir,'*.*')) -- 2.26.2