[py] add helper to check if we are on a 32bit system
[aubio.git] / python / tests / utils.py
1 #! /usr/bin/env python
2
3 import os
4 import re
5 import glob
6 import struct
7 import numpy as np
8 from tempfile import mkstemp
9
10 DEFAULT_SOUND = '22050Hz_5s_brownnoise.wav'
11
12 def is32bit():
13     return struct.calcsize("P") * 8 == 32
14
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)
18
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,'*.*'))
22
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:
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
33
34 def get_tmp_sink_path():
35     fd, path = mkstemp()
36     os.close(fd)
37     return path
38
39 def del_tmp_sink_path(path):
40     try:
41         os.unlink(path)
42     except WindowsError as e:
43         # removing the temporary directory sometimes fails on windows
44         import warnings
45         errmsg = "failed deleting temporary file {:s} ({:s})"
46         warnings.warn(UserWarning(errmsg.format(path, repr(e))))
47
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
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:
61         _, read = s()
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
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
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