source: python/tests/utils.py @ 71655fee

feature/cnn_orgfeature/crepe_org
Last change on this file since 71655fee 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
RevLine 
[a4cc8e5]1#! /usr/bin/env python
2
[0b6d23d]3import os
[cd46892]4import re
[0b6d23d]5import glob
6import numpy as np
7from tempfile import mkstemp
8
[5d8cc71]9DEFAULT_SOUND = '22050Hz_5s_brownnoise.wav'
10
[a4cc8e5]11def array_from_text_file(filename, dtype = 'float'):
[0bae15a]12    realpathname = os.path.join(os.path.dirname(__file__), filename)
13    return np.loadtxt(realpathname, 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:
[5d8cc71]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
[aee840b]29
[ab35262]30def get_tmp_sink_path():
31    fd, path = mkstemp()
[18d8bef]32    os.close(fd)
[ab35262]33    return path
34
35def del_tmp_sink_path(path):
[3c4fb67]36    try:
37        os.unlink(path)
38    except WindowsError as e:
[0ed0405]39        # removing the temporary directory sometimes fails on windows
[a3f2695]40        import warnings
[0ed0405]41        errmsg = "failed deleting temporary file {:s} ({:s})"
42        warnings.warn(UserWarning(errmsg.format(path, repr(e))))
[18d8bef]43
[2693655]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
[88432a9]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:
[0b6d23d]57        _, read = s()
[88432a9]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
[f36ecea]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
[cd46892]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.