source: python/tests/utils.py @ a3f2695

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

[tests] [win] add missing import warnings

  • Property mode set to 100644
File size: 2.3 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'):
[0bae15a]11    realpathname = os.path.join(os.path.dirname(__file__), filename)
12    return np.loadtxt(realpathname, dtype = dtype)
[a4cc8e5]13
[e1bfde5]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,'*.*'))
[2693655]17
[aee840b]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:
[5d8cc71]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
[aee840b]28
[ab35262]29def get_tmp_sink_path():
30    fd, path = mkstemp()
[18d8bef]31    os.close(fd)
[ab35262]32    return path
33
34def del_tmp_sink_path(path):
[3c4fb67]35    try:
36        os.unlink(path)
37    except WindowsError as e:
[0ed0405]38        # removing the temporary directory sometimes fails on windows
[a3f2695]39        import warnings
[0ed0405]40        errmsg = "failed deleting temporary file {:s} ({:s})"
41        warnings.warn(UserWarning(errmsg.format(path, repr(e))))
[18d8bef]42
[2693655]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
[88432a9]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:
[0b6d23d]56        _, read = s()
[88432a9]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
[f36ecea]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.