python/lib/aubio/slicing.py: rewrite slicing loop from aubiocut, add some tests
[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 array_from_yaml_file(filename):
16     import yaml
17     f = open(filename)
18     yaml_data = yaml.safe_load(f)
19     f.close()
20     return yaml_data
21
22 def count_samples_in_file(file_path):
23     from aubio import source
24     hopsize = 256
25     s = source(file_path, 0, hopsize)
26     total_frames = 0
27     while True:
28         samples, read = s()
29         total_frames += read
30         if read < hopsize: break
31     return total_frames
32
33 def count_samples_in_directory(samples_dir):
34     import os
35     total_frames = 0
36     for f in os.walk(samples_dir):
37         if len(f[2]):
38             for each in f[2]:
39                 file_path = os.path.join(f[0], each)
40                 if file_path:
41                     total_frames += count_samples_in_file(file_path)
42     return total_frames