summaryrefslogtreecommitdiff
path: root/python/tests/utils.py
blob: 2e3c31e5e67bc5c96c73655bad87b3ecb777722a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#! /usr/bin/env python

import os
import glob
import numpy as np
from tempfile import mkstemp

def array_from_text_file(filename, dtype = 'float'):
    filename = os.path.join(os.path.dirname(__file__), filename)
    with open(filename) as f:
        lines = f.readlines()
    return np.array([line.split() for line in lines],
            dtype = dtype)

def list_all_sounds(rel_dir):
    datadir = os.path.join(os.path.dirname(__file__), rel_dir)
    return glob.glob(os.path.join(datadir,'*.*'))

def get_default_test_sound(TestCase, rel_dir = 'sounds'):
    all_sounds = list_all_sounds(rel_dir)
    if len(all_sounds) == 0:
        TestCase.skipTest("please add some sounds in \'python/tests/sounds\'")
    else:
        return all_sounds[0]

def get_tmp_sink_path():
    fd, path = mkstemp()
    os.close(fd)
    return path

def del_tmp_sink_path(path):
    try:
        os.unlink(path)
    except WindowsError as e:
        print("deleting {:s} failed ({:s}), reopening".format(path, repr(e)))
        with open(path, 'wb') as f:
            f.close()
        try:
            os.unlink(path)
        except WindowsError as f:
            print("deleting {:s} failed ({:s}), aborting".format(path, repr(e)))

def array_from_yaml_file(filename):
    import yaml
    f = open(filename)
    yaml_data = yaml.safe_load(f)
    f.close()
    return yaml_data

def count_samples_in_file(file_path):
    from aubio import source
    hopsize = 256
    s = source(file_path, 0, hopsize)
    total_frames = 0
    while True:
        _, read = s()
        total_frames += read
        if read < hopsize: break
    return total_frames

def count_samples_in_directory(samples_dir):
    total_frames = 0
    for f in os.walk(samples_dir):
        if len(f[2]):
            for each in f[2]:
                file_path = os.path.join(f[0], each)
                if file_path:
                    total_frames += count_samples_in_file(file_path)
    return total_frames

def count_files_in_directory(samples_dir):
    total_files = 0
    for f in os.walk(samples_dir):
        if len(f[2]):
            for each in f[2]:
                file_path = os.path.join(f[0], each)
                if file_path:
                    total_files += 1
    return total_files