python/lib/aubio/slicing.py: use start and end stamps, make sure read > 0, improve...
[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 array_from_yaml_file(filename):
23     import yaml
24     f = open(filename)
25     yaml_data = yaml.safe_load(f)
26     f.close()
27     return yaml_data
28
29 def count_samples_in_file(file_path):
30     from aubio import source
31     hopsize = 256
32     s = source(file_path, 0, hopsize)
33     total_frames = 0
34     while True:
35         samples, read = s()
36         total_frames += read
37         if read < hopsize: break
38     return total_frames
39
40 def count_samples_in_directory(samples_dir):
41     import os
42     total_frames = 0
43     for f in os.walk(samples_dir):
44         if len(f[2]):
45             for each in f[2]:
46                 file_path = os.path.join(f[0], each)
47                 if file_path:
48                     total_frames += count_samples_in_file(file_path)
49     return total_frames
50
51 def count_files_in_directory(samples_dir):
52     import os
53     total_files = 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_files += 1
60     return total_files