Showing the changes to python (2 of 2 files). Show all
Unified · Split
| @@ -3,54 +3,94 @@ · 2 unmodified lines | |||
| 3 | from numpy.testing import TestCase, assert_equal, assert_almost_equal | 3 | from numpy.testing import TestCase, assert_equal, assert_almost_equal |
| 4 | from aubio import fvec, source, sink | 4 | from aubio import fvec, source, sink |
| 5 | from numpy import array | 5 | from numpy import array |
| 6 | from utils import list_all_sounds | 6 | from utils import list_all_sounds, get_tmp_sink_path, del_tmp_sink_path |
| 7 | 7 | ||
| 8 | list_of_sounds = list_all_sounds('sounds') | 8 | list_of_sounds = list_all_sounds('sounds') |
| 9 | path = None | 9 | path = None |
| 10 | 10 | ||
| 11 | many_files = 300 # 256 opened files is too much | ||
| 12 | |||
| 11 | class aubio_sink_test_case(TestCase): | 13 | class aubio_sink_test_case(TestCase): |
| 12 | 14 | ||
| 13 | def setUp(self): | 15 | def setUp(self): |
| 14 | if not len(list_of_sounds): self.skipTest('add some sound files in \'python/tests/sounds\'') | 16 | if not len(list_of_sounds): |
| 17 | self.skipTest('add some sound files in \'python/tests/sounds\'') | ||
| 15 | 18 | ||
| 16 | def test_many_sinks(self): | 19 | def test_many_sinks(self): |
| 17 | for i in range(100): | 20 | from tempfile import mkdtemp |
| 18 | g = sink('/tmp/f.wav', 0) | 21 | import os.path |
| 19 | write = 256 | 22 | import shutil |
| 23 | tmpdir = mkdtemp() | ||
| 24 | sink_list = [] | ||
| 25 | for i in range(many_files): | ||
| 26 | path = os.path.join(tmpdir, 'f-' + str(i) + '.wav') | ||
| 27 | g = sink(path, 0) | ||
| 28 | sink_list.append(g) | ||
| 29 | write = 32 | ||
| 20 | for n in range(200): | 30 | for n in range(200): |
| 21 | vec = fvec(write) | 31 | vec = fvec(write) |
| 22 | g(vec, write) | 32 | g(vec, write) |
| 23 | del g | 33 | g.close() |
| 34 | shutil.rmtree(tmpdir) | ||
| 35 | |||
| 36 | def test_many_sinks_not_closed(self): | ||
| 37 | from tempfile import mkdtemp | ||
| 38 | import os.path | ||
| 39 | import shutil | ||
| 40 | tmpdir = mkdtemp() | ||
| 41 | sink_list = [] | ||
| 42 | try: | ||
| 43 | for i in range(many_files): | ||
| 44 | path = os.path.join(tmpdir, 'f-' + str(i) + '.wav') | ||
| 45 | g = sink(path, 0) | ||
| 46 | sink_list.append(g) | ||
| 47 | write = 256 | ||
| 48 | for n in range(200): | ||
| 49 | vec = fvec(write) | ||
| 50 | g(vec, write) | ||
| 51 | except StandardError: | ||
| 52 | pass | ||
| 53 | else: | ||
| 54 | self.fail("does not fail on too many files open") | ||
| 55 | for g in sink_list: | ||
| 56 | g.close() | ||
| 57 | shutil.rmtree(tmpdir) | ||
| 24 | 58 | ||
| 25 | def test_read(self): | 59 | def test_read(self): |
| 26 | for path in list_of_sounds: | 60 | for path in list_of_sounds: |
| 27 | for samplerate, hop_size in zip([0, 44100, 8000, 32000], [512, 1024, 64, 256]): | 61 | for samplerate, hop_size in zip([0, 44100, 8000, 32000], [512, 1024, 64, 256]): |
| 28 | f = source(path, samplerate, hop_size) | 62 | f = source(path, samplerate, hop_size) |
| 29 | if samplerate == 0: samplerate = f.samplerate | 63 | if samplerate == 0: samplerate = f.samplerate |
| 30 | g = sink('/tmp/f.wav', samplerate) | 64 | sink_path = get_tmp_sink_path() |
| 65 | g = sink(sink_path, samplerate) | ||
| 31 | total_frames = 0 | 66 | total_frames = 0 |
| 32 | while True: | 67 | while True: |
| 33 | vec, read = f() | 68 | vec, read = f() |
| 34 | g(vec, read) | 69 | g(vec, read) |
| 35 | total_frames += read | 70 | total_frames += read |
| 36 | if read < f.hop_size: break | 71 | if read < f.hop_size: break |
| 37 | print "read", "%.2fs" % (total_frames / float(f.samplerate) ), | 72 | if 0: |
| 38 | print "(", total_frames, "frames", "in", | 73 | print "read", "%.2fs" % (total_frames / float(f.samplerate) ), |
| 39 | print total_frames / f.hop_size, "blocks", "at", "%dHz" % f.samplerate, ")", | 74 | print "(", total_frames, "frames", "in", |
| 40 | print "from", f.uri, | 75 | print total_frames / f.hop_size, "blocks", "at", "%dHz" % f.samplerate, ")", |
| 41 | print "to", g.uri | 76 | print "from", f.uri, |
| 42 | #del f, g | 77 | print "to", g.uri |
| 78 | del_tmp_sink_path(sink_path) | ||
| 43 | 79 | ||
| 44 | def test_close_file(self): | 80 | def test_close_file(self): |
| 45 | samplerate = 44100 | 81 | samplerate = 44100 |
| 46 | g = sink('/tmp/f.wav', samplerate) | 82 | sink_path = get_tmp_sink_path() |
| 83 | g = sink(sink_path, samplerate) | ||
| 47 | g.close() | 84 | g.close() |
| 85 | del_tmp_sink_path(sink_path) | ||
| 48 | 86 | ||
| 49 | def test_close_file_twice(self): | 87 | def test_close_file_twice(self): |
| 50 | samplerate = 44100 | 88 | samplerate = 44100 |
| 51 | g = sink('/tmp/f.wav', samplerate) | 89 | sink_path = get_tmp_sink_path() |
| 90 | g = sink(sink_path, samplerate) | ||
| 52 | g.close() | 91 | g.close() |
| 53 | g.close() | 92 | g.close() |
| 93 | del_tmp_sink_path(sink_path) | ||
| 54 | 94 | ||
| 55 | if __name__ == '__main__': | 95 | if __name__ == '__main__': |
| 56 | from unittest import main | 96 | from unittest import main |
| @@ -19,12 +19,16 @@ def get_default_test_sound(TestCase, rel_dir = 'sounds'): · 18 unmodified lines | |||
| 19 | else: | 19 | else: |
| 20 | return all_sounds[0] | 20 | return all_sounds[0] |
| 21 | 21 | ||
| 22 | def get_tmp_sink_name(): | 22 | def get_tmp_sink_path(): |
| 23 | from tempfile import mkstemp | 23 | from tempfile import mkstemp |
| 24 | import os | 24 | import os |
| 25 | fd, name = mkstemp() | 25 | fd, path = mkstemp() |
| 26 | os.close(fd) | 26 | os.close(fd) |
| 27 | return name | 27 | return path |
| 28 | |||
| 29 | def del_tmp_sink_path(path): | ||
| 30 | import os | ||
| 31 | os.unlink(path) | ||
| 28 | 32 | ||
| 29 | def array_from_yaml_file(filename): | 33 | def array_from_yaml_file(filename): |
| 30 | import yaml | 34 | import yaml |