source: python/tests/utils.py @ 770f7b4

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5pitchshiftsamplertimestretchyinfft+
Last change on this file since 770f7b4 was 376d5e9, checked in by Paul Brossier <piem@piem.org>, 9 years ago

tests/: continue python3 preparation

  • Property mode set to 100644
File size: 2.0 KB
Line 
1#! /usr/bin/env python
2
3def 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    with open(filename) as f:
8        lines = f.readlines()
9    return array([line.split() for line in lines],
10            dtype = dtype)
11
12def list_all_sounds(rel_dir):
13    import os.path, glob
14    datadir = os.path.join(os.path.dirname(__file__), rel_dir)
15    return glob.glob(os.path.join(datadir,'*.*'))
16
17def get_default_test_sound(TestCase, rel_dir = 'sounds'):
18    all_sounds = list_all_sounds(rel_dir)
19    if len(all_sounds) == 0:
20        TestCase.skipTest("please add some sounds in \'python/tests/sounds\'")
21    else:
22        return all_sounds[0]
23
24def get_tmp_sink_path():
25    from tempfile import mkstemp
26    import os
27    fd, path = mkstemp()
28    os.close(fd)
29    return path
30
31def del_tmp_sink_path(path):
32    import os
33    os.unlink(path)
34
35def array_from_yaml_file(filename):
36    import yaml
37    f = open(filename)
38    yaml_data = yaml.safe_load(f)
39    f.close()
40    return yaml_data
41
42def count_samples_in_file(file_path):
43    from aubio import source
44    hopsize = 256
45    s = source(file_path, 0, hopsize)
46    total_frames = 0
47    while True:
48        samples, read = s()
49        total_frames += read
50        if read < hopsize: break
51    return total_frames
52
53def count_samples_in_directory(samples_dir):
54    import os
55    total_frames = 0
56    for f in os.walk(samples_dir):
57        if len(f[2]):
58            for each in f[2]:
59                file_path = os.path.join(f[0], each)
60                if file_path:
61                    total_frames += count_samples_in_file(file_path)
62    return total_frames
63
64def count_files_in_directory(samples_dir):
65    import os
66    total_files = 0
67    for f in os.walk(samples_dir):
68        if len(f[2]):
69            for each in f[2]:
70                file_path = os.path.join(f[0], each)
71                if file_path:
72                    total_files += 1
73    return total_files
Note: See TracBrowser for help on using the repository browser.