source: python/tests/utils.py @ a5986ff

feature/cnnfeature/crepe
Last change on this file since a5986ff was f912e90, checked in by Paul Brossier <piem@piem.org>, 5 years ago

[py] add helper to check if we are on a 32bit system

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