8 from tempfile import mkstemp
10 DEFAULT_SOUND = '22050Hz_5s_brownnoise.wav'
13 return struct.calcsize("P") * 8 == 32
15 def array_from_text_file(filename, dtype = 'float'):
16 realpathname = os.path.join(os.path.dirname(__file__), filename)
17 return np.loadtxt(realpathname, dtype = dtype)
19 def list_all_sounds(rel_dir):
20 datadir = os.path.join(os.path.dirname(__file__), rel_dir)
21 return glob.glob(os.path.join(datadir,'*.*'))
23 def 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\'")
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)
34 def get_tmp_sink_path():
39 def del_tmp_sink_path(path):
42 except WindowsError as e:
43 # removing the temporary directory sometimes fails on windows
45 errmsg = "failed deleting temporary file {:s} ({:s})"
46 warnings.warn(UserWarning(errmsg.format(path, repr(e))))
48 def array_from_yaml_file(filename):
51 yaml_data = yaml.safe_load(f)
55 def count_samples_in_file(file_path):
56 from aubio import source
58 s = source(file_path, 0, hopsize)
63 if read < hopsize: break
66 def count_samples_in_directory(samples_dir):
68 for f in os.walk(samples_dir):
71 file_path = os.path.join(f[0], each)
73 total_frames += count_samples_in_file(file_path)
76 def count_files_in_directory(samples_dir):
78 for f in os.walk(samples_dir):
81 file_path = os.path.join(f[0], each)
86 def parse_file_samplerate(soundfile):
89 re_sr = re.compile(r'/([0-9]{4,})Hz_.*')
90 match_samplerate = re_sr.findall(soundfile)
92 samplerate = int(match_samplerate[0])
95 warnings.warn(UserWarning("could not parse samplerate for {:s}"