[a4cc8e5] | 1 | #! /usr/bin/env python |
---|
| 2 | |
---|
[0b6d23d] | 3 | import os |
---|
[cd46892] | 4 | import re |
---|
[0b6d23d] | 5 | import glob |
---|
[f912e90] | 6 | import struct |
---|
[0b6d23d] | 7 | import numpy as np |
---|
| 8 | from tempfile import mkstemp |
---|
| 9 | |
---|
[5d8cc71] | 10 | DEFAULT_SOUND = '22050Hz_5s_brownnoise.wav' |
---|
| 11 | |
---|
[f912e90] | 12 | def is32bit(): |
---|
| 13 | return struct.calcsize("P") * 8 == 32 |
---|
| 14 | |
---|
[a4cc8e5] | 15 | def 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] | 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,'*.*')) |
---|
[2693655] | 22 | |
---|
[aee840b] | 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\'") |
---|
| 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] | 34 | def get_tmp_sink_path(): |
---|
| 35 | fd, path = mkstemp() |
---|
[18d8bef] | 36 | os.close(fd) |
---|
[ab35262] | 37 | return path |
---|
| 38 | |
---|
| 39 | def 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] | 48 | def 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 | |
---|
| 55 | def 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 | |
---|
| 66 | def 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 | |
---|
| 76 | def 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 | |
---|
| 86 | def 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 |
---|