source: python/tests/utils.py @ a6f9ebf

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

python/tests: fix most prospect warnings

  • Property mode set to 100644
File size: 1.9 KB
RevLine 
[a4cc8e5]1#! /usr/bin/env python
2
[0b6d23d]3import os
4import glob
5import numpy as np
6from tempfile import mkstemp
7
[a4cc8e5]8def array_from_text_file(filename, dtype = 'float'):
[e1bfde5]9    filename = os.path.join(os.path.dirname(__file__), filename)
[376d5e9]10    with open(filename) as f:
11        lines = f.readlines()
[0b6d23d]12    return np.array([line.split() for line in lines],
[376d5e9]13            dtype = dtype)
[a4cc8e5]14
[e1bfde5]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,'*.*'))
[2693655]18
[aee840b]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
[ab35262]26def get_tmp_sink_path():
27    fd, path = mkstemp()
[18d8bef]28    os.close(fd)
[ab35262]29    return path
30
31def del_tmp_sink_path(path):
32    os.unlink(path)
[18d8bef]33
[2693655]34def array_from_yaml_file(filename):
35    import yaml
36    f = open(filename)
37    yaml_data = yaml.safe_load(f)
38    f.close()
39    return yaml_data
[88432a9]40
41def count_samples_in_file(file_path):
42    from aubio import source
43    hopsize = 256
44    s = source(file_path, 0, hopsize)
45    total_frames = 0
46    while True:
[0b6d23d]47        _, read = s()
[88432a9]48        total_frames += read
49        if read < hopsize: break
50    return total_frames
51
52def count_samples_in_directory(samples_dir):
53    total_frames = 0
54    for f in os.walk(samples_dir):
55        if len(f[2]):
56            for each in f[2]:
57                file_path = os.path.join(f[0], each)
58                if file_path:
59                    total_frames += count_samples_in_file(file_path)
60    return total_frames
[f36ecea]61
62def count_files_in_directory(samples_dir):
63    total_files = 0
64    for f in os.walk(samples_dir):
65        if len(f[2]):
66            for each in f[2]:
67                file_path = os.path.join(f[0], each)
68                if file_path:
69                    total_files += 1
70    return total_files
Note: See TracBrowser for help on using the repository browser.