python/tests/test_sink.py: add more tests, clean up created files
[aubio.git] / python / tests / utils.py
1 #! /usr/bin/env python
2
3 def array_from_text_file(filename, dtype = 'float'):
4     import os.path
5     from numpy import array
6     filename = os.path.join(os.path.dirname(__file__), filename)
7     return array([line.split() for line in open(filename).readlines()],
8         dtype = dtype)
9
10 def 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,'*.*'))
14
15 def get_default_test_sound(TestCase, rel_dir = 'sounds'):
16     all_sounds = list_all_sounds(rel_dir)
17     if len(all_sounds) == 0:
18         TestCase.skipTest("please add some sounds in \'python/tests/sounds\'")
19     else:
20         return all_sounds[0]
21
22 def get_tmp_sink_path():
23     from tempfile import mkstemp
24     import os
25     fd, path = mkstemp()
26     os.close(fd)
27     return path
28
29 def del_tmp_sink_path(path):
30     import os
31     os.unlink(path)
32
33 def array_from_yaml_file(filename):
34     import yaml
35     f = open(filename)
36     yaml_data = yaml.safe_load(f)
37     f.close()
38     return yaml_data
39
40 def count_samples_in_file(file_path):
41     from aubio import source
42     hopsize = 256
43     s = source(file_path, 0, hopsize)
44     total_frames = 0
45     while True:
46         samples, read = s()
47         total_frames += read
48         if read < hopsize: break
49     return total_frames
50
51 def count_samples_in_directory(samples_dir):
52     import os
53     total_frames = 0
54     for f in os.walk(samples_dir):
55         if len(f[2]):
56             for each in f[2]:
57                 file_path = os.path.join(f[0], each)
58                 if file_path:
59                     total_frames += count_samples_in_file(file_path)
60     return total_frames
61
62 def count_files_in_directory(samples_dir):
63     import os
64     total_files = 0
65     for f in os.walk(samples_dir):
66         if len(f[2]):
67             for each in f[2]:
68                 file_path = os.path.join(f[0], each)
69                 if file_path:
70                     total_files += 1
71     return total_files