source: python/tests/utils.py @ 18d8bef

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

python/tests/utils.py: add get_tmp_sink_name

  • Property mode set to 100644
File size: 1.9 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    return array([line.split() for line in open(filename).readlines()],
8        dtype = dtype)
9
10def 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
15def 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
22def get_tmp_sink_name():
23    from tempfile import mkstemp
24    import os
25    fd, name = mkstemp()
26    os.close(fd)
27    return name
28
29def array_from_yaml_file(filename):
30    import yaml
31    f = open(filename)
32    yaml_data = yaml.safe_load(f)
33    f.close()
34    return yaml_data
35
36def count_samples_in_file(file_path):
37    from aubio import source
38    hopsize = 256
39    s = source(file_path, 0, hopsize)
40    total_frames = 0
41    while True:
42        samples, read = s()
43        total_frames += read
44        if read < hopsize: break
45    return total_frames
46
47def count_samples_in_directory(samples_dir):
48    import os
49    total_frames = 0
50    for f in os.walk(samples_dir):
51        if len(f[2]):
52            for each in f[2]:
53                file_path = os.path.join(f[0], each)
54                if file_path:
55                    total_frames += count_samples_in_file(file_path)
56    return total_frames
57
58def count_files_in_directory(samples_dir):
59    import os
60    total_files = 0
61    for f in os.walk(samples_dir):
62        if len(f[2]):
63            for each in f[2]:
64                file_path = os.path.join(f[0], each)
65                if file_path:
66                    total_files += 1
67    return total_files
Note: See TracBrowser for help on using the repository browser.