source: python/tests/utils.py @ 417fee0

feature/crepe_org
Last change on this file since 417fee0 was cd46892, checked in by Paul Brossier <piem@piem.org>, 6 years ago

[tests] add parse_file_samplerate to fetch samplerate from path

  • Property mode set to 100644
File size: 2.7 KB
Line 
1#! /usr/bin/env python
2
3import os
4import re
5import glob
6import numpy as np
7from tempfile import mkstemp
8
9DEFAULT_SOUND = '22050Hz_5s_brownnoise.wav'
10
11def array_from_text_file(filename, dtype = 'float'):
12    realpathname = os.path.join(os.path.dirname(__file__), filename)
13    return np.loadtxt(realpathname, 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        default_sound = all_sounds[0]
25        if DEFAULT_SOUND in map(os.path.basename, all_sounds):
26            while os.path.basename(default_sound) != DEFAULT_SOUND:
27                default_sound = all_sounds.pop(0)
28        return default_sound
29
30def get_tmp_sink_path():
31    fd, path = mkstemp()
32    os.close(fd)
33    return path
34
35def del_tmp_sink_path(path):
36    try:
37        os.unlink(path)
38    except WindowsError as e:
39        # removing the temporary directory sometimes fails on windows
40        import warnings
41        errmsg = "failed deleting temporary file {:s} ({:s})"
42        warnings.warn(UserWarning(errmsg.format(path, repr(e))))
43
44def array_from_yaml_file(filename):
45    import yaml
46    f = open(filename)
47    yaml_data = yaml.safe_load(f)
48    f.close()
49    return yaml_data
50
51def count_samples_in_file(file_path):
52    from aubio import source
53    hopsize = 256
54    s = source(file_path, 0, hopsize)
55    total_frames = 0
56    while True:
57        _, read = s()
58        total_frames += read
59        if read < hopsize: break
60    return total_frames
61
62def count_samples_in_directory(samples_dir):
63    total_frames = 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_frames += count_samples_in_file(file_path)
70    return total_frames
71
72def count_files_in_directory(samples_dir):
73    total_files = 0
74    for f in os.walk(samples_dir):
75        if len(f[2]):
76            for each in f[2]:
77                file_path = os.path.join(f[0], each)
78                if file_path:
79                    total_files += 1
80    return total_files
81
82def parse_file_samplerate(soundfile):
83    samplerate = None
84    # parse samplerate
85    re_sr = re.compile(r'/([0-9]{4,})Hz_.*')
86    match_samplerate = re_sr.findall(soundfile)
87    if match_samplerate:
88        samplerate = int(match_samplerate[0])
89    else:
90        import warnings
91        warnings.warn(UserWarning("could not parse samplerate for {:s}"
92            .format(soundfile)))
93    return samplerate
Note: See TracBrowser for help on using the repository browser.