source: python/tests/utils.py @ 20c8769

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5
Last change on this file since 20c8769 was 5d8cc71, checked in by Paul Brossier <piem@piem.org>, 8 years ago

python/tests/utils.py: by default, use 5 seconds brownoise

  • Property mode set to 100644
File size: 2.5 KB
Line 
1#! /usr/bin/env python
2
3import os
4import glob
5import numpy as np
6from tempfile import mkstemp
7
8DEFAULT_SOUND = '22050Hz_5s_brownnoise.wav'
9
10def array_from_text_file(filename, dtype = 'float'):
11    filename = os.path.join(os.path.dirname(__file__), filename)
12    with open(filename) as f:
13        lines = f.readlines()
14    return np.array([line.split() for line in lines],
15            dtype = dtype)
16
17def list_all_sounds(rel_dir):
18    datadir = os.path.join(os.path.dirname(__file__), rel_dir)
19    return glob.glob(os.path.join(datadir,'*.*'))
20
21def get_default_test_sound(TestCase, rel_dir = 'sounds'):
22    all_sounds = list_all_sounds(rel_dir)
23    if len(all_sounds) == 0:
24        TestCase.skipTest("please add some sounds in \'python/tests/sounds\'")
25    else:
26        default_sound = all_sounds[0]
27        if DEFAULT_SOUND in map(os.path.basename, all_sounds):
28            while os.path.basename(default_sound) != DEFAULT_SOUND:
29                default_sound = all_sounds.pop(0)
30        return default_sound
31
32def get_tmp_sink_path():
33    fd, path = mkstemp()
34    os.close(fd)
35    return path
36
37def del_tmp_sink_path(path):
38    try:
39        os.unlink(path)
40    except WindowsError as e:
41        print("deleting {:s} failed ({:s}), reopening".format(path, repr(e)))
42        with open(path, 'wb') as f:
43            f.close()
44        try:
45            os.unlink(path)
46        except WindowsError as f:
47            print("deleting {:s} failed ({:s}), aborting".format(path, repr(e)))
48
49def array_from_yaml_file(filename):
50    import yaml
51    f = open(filename)
52    yaml_data = yaml.safe_load(f)
53    f.close()
54    return yaml_data
55
56def count_samples_in_file(file_path):
57    from aubio import source
58    hopsize = 256
59    s = source(file_path, 0, hopsize)
60    total_frames = 0
61    while True:
62        _, read = s()
63        total_frames += read
64        if read < hopsize: break
65    return total_frames
66
67def count_samples_in_directory(samples_dir):
68    total_frames = 0
69    for f in os.walk(samples_dir):
70        if len(f[2]):
71            for each in f[2]:
72                file_path = os.path.join(f[0], each)
73                if file_path:
74                    total_frames += count_samples_in_file(file_path)
75    return total_frames
76
77def count_files_in_directory(samples_dir):
78    total_files = 0
79    for f in os.walk(samples_dir):
80        if len(f[2]):
81            for each in f[2]:
82                file_path = os.path.join(f[0], each)
83                if file_path:
84                    total_files += 1
85    return total_files
Note: See TracBrowser for help on using the repository browser.