source: python/tests/utils.py @ 42dbcf4

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