source: python/tests/utils.py @ 0bae15a

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

[tests] use np.loadtxt in utils

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