source: python/tests/utils.py @ 3c4fb67

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

python/tests/utils.py: try reopening the file is deleting it fails on windows

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