source: python/tests/utils.py @ 2e5c52e

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5
Last change on this file since 2e5c52e 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
RevLine 
[a4cc8e5]1#! /usr/bin/env python
2
[0b6d23d]3import os
4import glob
5import numpy as np
6from tempfile import mkstemp
7
[5d8cc71]8DEFAULT_SOUND = '22050Hz_5s_brownnoise.wav'
9
[a4cc8e5]10def array_from_text_file(filename, dtype = 'float'):
[e1bfde5]11    filename = os.path.join(os.path.dirname(__file__), filename)
[376d5e9]12    with open(filename) as f:
13        lines = f.readlines()
[0b6d23d]14    return np.array([line.split() for line in lines],
[376d5e9]15            dtype = dtype)
[a4cc8e5]16
[e1bfde5]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,'*.*'))
[2693655]20
[aee840b]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:
[5d8cc71]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
[aee840b]31
[ab35262]32def get_tmp_sink_path():
33    fd, path = mkstemp()
[18d8bef]34    os.close(fd)
[ab35262]35    return path
36
37def del_tmp_sink_path(path):
[3c4fb67]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)))
[18d8bef]48
[2693655]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
[88432a9]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:
[0b6d23d]62        _, read = s()
[88432a9]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
[f36ecea]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.