1 | #! /usr/bin/env python |
---|
2 | |
---|
3 | import os |
---|
4 | import re |
---|
5 | import glob |
---|
6 | import numpy as np |
---|
7 | from tempfile import mkstemp |
---|
8 | |
---|
9 | DEFAULT_SOUND = '22050Hz_5s_brownnoise.wav' |
---|
10 | |
---|
11 | def 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 | |
---|
15 | def 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 | |
---|
19 | def 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 | |
---|
30 | def get_tmp_sink_path(): |
---|
31 | fd, path = mkstemp() |
---|
32 | os.close(fd) |
---|
33 | return path |
---|
34 | |
---|
35 | def 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 | |
---|
44 | def 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 | |
---|
51 | def 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 | |
---|
62 | def 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 | |
---|
72 | def 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 | |
---|
82 | def 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 |
---|