7 from tempfile import mkstemp
9 DEFAULT_SOUND = '22050Hz_5s_brownnoise.wav'
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)
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,'*.*'))
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\'")
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)
30 def get_tmp_sink_path():
35 def del_tmp_sink_path(path):
38 except WindowsError as e:
39 # removing the temporary directory sometimes fails on windows
41 errmsg = "failed deleting temporary file {:s} ({:s})"
42 warnings.warn(UserWarning(errmsg.format(path, repr(e))))
44 def array_from_yaml_file(filename):
47 yaml_data = yaml.safe_load(f)
51 def count_samples_in_file(file_path):
52 from aubio import source
54 s = source(file_path, 0, hopsize)
59 if read < hopsize: break
62 def count_samples_in_directory(samples_dir):
64 for f in os.walk(samples_dir):
67 file_path = os.path.join(f[0], each)
69 total_frames += count_samples_in_file(file_path)
72 def count_files_in_directory(samples_dir):
74 for f in os.walk(samples_dir):
77 file_path = os.path.join(f[0], each)
82 def parse_file_samplerate(soundfile):
85 re_sr = re.compile(r'/([0-9]{4,})Hz_.*')
86 match_samplerate = re_sr.findall(soundfile)
88 samplerate = int(match_samplerate[0])
91 warnings.warn(UserWarning("could not parse samplerate for {:s}"