-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaudo_processing_test.py
More file actions
221 lines (175 loc) · 6.68 KB
/
Copy pathaudo_processing_test.py
File metadata and controls
221 lines (175 loc) · 6.68 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#!/usr/bin/env python3
import os
import copy
import pickle
import pytest
import lab
TEST_DIRECTORY = os.path.dirname(__file__)
def compare_sounds(result, expected, eps=1e-6):
# well formed?
assert isinstance(result["rate"], int), "Sampling rate should be an integer"
if "left" in result:
assert len(result["left"]) == len(
result["right"]
), "Left and Right channels do not have the same length"
# matches expected?
assert result["rate"] == expected["rate"], "Sampling rates do not match"
# compare (different for stereo vs mono)
if "left" in result:
assert "left" in expected, "Expected mono sound, got stereo"
assert len(result["left"]) == len(expected["left"]), "Lengths do not match"
for ix, ((res_l, res_r), (exp_l, exp_r)) in enumerate(
zip(
zip(result["left"], result["right"]),
zip(expected["left"], expected["right"]),
)
):
assert (
abs(res_l - exp_l) <= eps and abs(res_r - exp_r) < eps
), f"Values at index {ix} do not match."
else:
assert "samples" in expected, "Expected stereo sound, got mono"
assert len(result["samples"]) == len(
expected["samples"]
), "Lengths do not match"
for ix, (res, exp) in enumerate(zip(result["samples"], expected["samples"])):
assert abs(res - exp) <= eps, f"Values at index {ix} do not match."
def compare_against_file(x, fname, stereo=False):
compare_sounds(x, lab.load_wav(fname, stereo=stereo), eps=(2 / (2**15 - 1)))
def load_pickle_pair(name):
with open(os.path.join(TEST_DIRECTORY, "test_inputs", name), "rb") as f:
with open(os.path.join(TEST_DIRECTORY, "test_outputs", name), "rb") as f2:
return (pickle.load(f), pickle.load(f2))
def test_backwards_small():
inp = {
"rate": 20,
"samples": [1, 2, 3, 4, 5, 6],
}
inp2 = copy.deepcopy(inp)
out = {
"rate": 20,
"samples": [6, 5, 4, 3, 2, 1],
}
compare_sounds(lab.backwards(inp), out)
assert inp == inp2, "be careful not to modify the input!"
def test_backwards_real():
inp = lab.load_wav(os.path.join(TEST_DIRECTORY, "sounds", "chickadee.wav"))
inp2 = copy.deepcopy(inp)
outfile = os.path.join(TEST_DIRECTORY, "test_outputs", "chickadee_backwards.wav")
compare_against_file(lab.backwards(inp), outfile)
assert inp == inp2, "be careful not to modify the input!"
@pytest.mark.parametrize("test_number", [1, 2])
def test_backwards_random(test_number):
inps, exp = load_pickle_pair("backwards_%02d.pickle" % test_number)
inps2 = copy.deepcopy(inps)
compare_sounds(lab.backwards(*inps), exp)
assert inps == inps2, "be careful not to modify the input!"
def test_mix_small():
s1 = {
"rate": 30,
"samples": [1, 2, 3, 4, 5, 6],
}
s2 = {
"rate": 20,
"samples": [1, 2, 3, 4, 5, 6],
}
s3 = {
"rate": 30,
"samples": [7, 8, 9, 10],
}
s4 = {
"rate": 30,
"samples": [0.7 + 2.1, 1.4 + 2.4, 2.1 + 2.7, 2.8 + 3.0],
}
assert lab.mix(s1, s2, 0.5) is None
compare_sounds(lab.mix(s1, s3, 0.7), s4)
def test_mix_real():
inp1 = lab.load_wav(os.path.join(TEST_DIRECTORY, "sounds", "chord.wav"))
inp2 = lab.load_wav(os.path.join(TEST_DIRECTORY, "sounds", "crash.wav"))
inp3 = copy.deepcopy(inp1)
inp4 = copy.deepcopy(inp2)
res = lab.mix(inp1, inp2, 0.35)
outfile = os.path.join(TEST_DIRECTORY, "test_outputs", "chord_crash.wav")
compare_against_file(res, outfile)
assert inp1 == inp3, "be careful not to modify the input!"
assert inp2 == inp4, "be careful not to modify the input!"
@pytest.mark.parametrize("test_number", [1, 2])
def test_mix_random(test_number):
inps, exp = load_pickle_pair("mix_%02d.pickle" % test_number)
inps2 = copy.deepcopy(inps)
compare_sounds(lab.mix(*inps), exp)
assert inps == inps2, "be careful not to modify the inputs!"
def test_echo_small():
inp = {
"rate": 9,
"samples": [1, 2, 3],
}
inp2 = copy.deepcopy(inp)
exp = {
"rate": 9,
"samples": [1, 2, 3, 0, 0, 0.7, 1.4, 2.1, 0, 0, 0.49, 0.98, 1.47],
}
compare_sounds(lab.echo(inp, 2, 0.6, 0.7), exp)
assert inp == inp2, "be careful not to modify the inputs!"
def test_echo_real():
inp = lab.load_wav(os.path.join(TEST_DIRECTORY, "sounds", "synth.wav"))
inp2 = copy.deepcopy(inp)
outfile = os.path.join(TEST_DIRECTORY, "test_outputs", "synth_echo.wav")
compare_against_file(lab.echo(inp, 6, 0.5, 0.7), outfile)
assert inp == inp2, "be careful not to modify the input!"
@pytest.mark.parametrize("test_number", [1, 2])
def test_echo_random(test_number):
inps, exp = load_pickle_pair("echo_%02d.pickle" % test_number)
inps2 = copy.deepcopy(inps)
compare_sounds(lab.echo(*inps), exp)
assert inps == inps2, "be careful not to modify the inputs!"
def test_pan_small():
inp = {
"rate": 42,
"left": [4, 4, 4, 4, 4],
"right": [6, 6, 6, 6, 6],
}
inp2 = copy.deepcopy(inp)
exp = {
"rate": 42,
"left": [4, 3, 2, 1, 0],
"right": [0, 1.5, 3, 4.5, 6],
}
compare_sounds(lab.pan(inp), exp)
assert inp == inp2, "be careful not to modify the input!"
def test_pan_real():
inp = lab.load_wav(
os.path.join(TEST_DIRECTORY, "sounds", "mystery.wav"), stereo=True
)
inp2 = copy.deepcopy(inp)
outfile = os.path.join(TEST_DIRECTORY, "test_outputs", "mystery_pan.wav")
compare_against_file(lab.pan(inp), outfile, stereo=True)
assert inp == inp2, "be careful not to modify the input!"
@pytest.mark.parametrize("test_number", [1, 2])
def test_pan_random(test_number):
inps, exp = load_pickle_pair("pan_%02d.pickle" % test_number)
inps2 = copy.deepcopy(inps)
compare_sounds(lab.pan(*inps), exp)
assert inps == inps2, "be careful not to modify the input!"
def test_remove_vocals_small():
inp = {
"rate": 30,
"left": [7, 9, 3, 4],
"right": [12, 2, 9, 2],
}
inp2 = copy.deepcopy(inp)
exp = {
"rate": 30,
"samples": [-5, 7, -6, 2],
}
compare_sounds(lab.remove_vocals(inp), exp)
assert inp == inp2, "be careful not to modify the input!"
@pytest.mark.parametrize("test_number", [1, 2, 3])
def test_remove_vocals_random(test_number):
inps, exp = load_pickle_pair("remove_vocals_%02d.pickle" % test_number)
inps2 = copy.deepcopy(inps)
compare_sounds(lab.remove_vocals(*inps), exp)
assert inps == inps2, "be careful not to modify the input!"
if __name__ == "__main__":
import sys
res = pytest.main(["-k", " or ".join(sys.argv[1:]), "-v", __file__])