forked from CPJKU/madmom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_io_audio.py
353 lines (304 loc) · 15 KB
/
test_io_audio.py
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
# encoding: utf-8
# pylint: skip-file
"""
This file contains tests for the madmom.audio.signal module.
"""
from __future__ import absolute_import, division, print_function
import io
import unittest
from os.path import join as pj
from madmom.io.audio import *
from . import AUDIO_PATH, DATA_PATH
sample_file = pj(AUDIO_PATH, 'sample.wav')
sample_file_22k = pj(AUDIO_PATH, 'sample_22050.wav')
stereo_sample_file = pj(AUDIO_PATH, 'stereo_sample.wav')
flac_file = pj(AUDIO_PATH, 'stereo_sample.flac')
m4a_file = pj(AUDIO_PATH, 'stereo_sample.m4a')
rg_flac_file = pj(AUDIO_PATH, 'stereo_sample_rg.flac')
loud_rg_flac_file = pj(AUDIO_PATH, 'stereo_chirp_rg.flac')
tmp_file = tempfile.NamedTemporaryFile(delete=False).name
class TestLoadWaveFileFunction(unittest.TestCase):
def test_types(self):
signal, sample_rate = load_wave_file(sample_file)
self.assertIsInstance(signal, np.ndarray)
self.assertTrue(signal.dtype == np.int16)
self.assertTrue(type(sample_rate) == int)
def test_file_handle(self):
# open file handle
with open(sample_file, 'rb') as file_handle:
signal, sample_rate = load_wave_file(file_handle)
self.assertIsInstance(signal, np.ndarray)
self.assertTrue(signal.dtype == np.int16)
self.assertTrue(type(sample_rate) == int)
def test_values(self):
# test wave loader
signal, sample_rate = load_wave_file(sample_file)
self.assertTrue(np.allclose(signal[:5],
[-2494, -2510, -2484, -2678, -2833]))
self.assertTrue(len(signal) == 123481)
self.assertTrue(sample_rate == 44100)
self.assertTrue(signal.shape == (123481,))
# stereo
signal, sample_rate = load_wave_file(stereo_sample_file)
self.assertTrue(np.allclose(signal[:4],
[[33, 38], [35, 36], [29, 34], [36, 31]]))
self.assertTrue(len(signal) == 182919)
self.assertTrue(sample_rate == 44100)
self.assertTrue(signal.shape == (182919, 2))
def test_start_stop(self):
# test wave loader
signal, sample_rate = load_wave_file(sample_file, start=1. / 44100,
stop=5. / 44100)
self.assertTrue(np.allclose(signal, [-2510, -2484, -2678, -2833]))
self.assertTrue(len(signal) == 4)
self.assertTrue(sample_rate == 44100)
def test_downmix(self):
# test wave loader
signal, sample_rate = load_wave_file(stereo_sample_file,
num_channels=1)
self.assertTrue(np.allclose(signal[:5], [35, 35, 31, 33, 33]))
self.assertTrue(len(signal) == 182919)
self.assertTrue(sample_rate == 44100)
self.assertTrue(signal.shape == (182919,))
def test_channel_choice(self):
signal, sample_rate = load_wave_file(stereo_sample_file, channel=0)
self.assertTrue(signal.shape == (182919,))
self.assertTrue(np.allclose(signal[:4], [33, 35, 29, 36]))
signal, sample_rate = load_wave_file(stereo_sample_file, channel=1)
self.assertTrue(np.allclose(signal[:4], [38, 36, 34, 31]))
def test_upmix(self):
signal, sample_rate = load_wave_file(sample_file, num_channels=2)
self.assertTrue(np.allclose(signal[:5],
[[-2494, -2494], [-2510, -2510],
[-2484, -2484], [-2678, -2678],
[-2833, -2833]]))
self.assertTrue(len(signal) == 123481)
self.assertTrue(sample_rate == 44100)
self.assertTrue(signal.shape == (123481, 2))
def test_errors(self):
# resampling of wav not supported
with self.assertRaises(ValueError):
load_wave_file(sample_file, sample_rate=22050)
# resampling of wav not supported
with self.assertRaises(ValueError):
load_wave_file(sample_file, dtype=float)
# file not found
with self.assertRaises(IOError):
load_wave_file(pj(AUDIO_PATH, 'foo_bar.wav'))
# not an audio file
with self.assertRaises(ValueError):
load_wave_file(pj(DATA_PATH, 'README'))
# closed file handle
with self.assertRaises(ValueError):
file_handle = open(sample_file, 'rb')
file_handle.close()
load_wave_file(file_handle)
class TestWriteWaveFileFunction(unittest.TestCase):
def setUp(self):
self.signal = Signal(sample_file)
write_wave_file(self.signal, tmp_file)
self.result = Signal(sample_file)
def test_types(self):
self.assertIsInstance(self.result, Signal)
self.assertIsInstance(self.result, np.ndarray)
self.assertTrue(self.result.dtype == np.int16)
self.assertTrue(type(self.result.sample_rate) == int)
def test_values(self):
# test wave loader
self.assertTrue(np.allclose(self.signal, self.result))
self.assertTrue(self.result.sample_rate == 44100)
self.assertTrue(self.result.shape == (123481,))
class TestLoadAudioFileFunction(unittest.TestCase):
# this tests both madmom.io.audio.load_wave_file() and
# madmom.io.audio.load_ffmpeg_file() functions via the universal
# load_audio_file() function
def test_types(self):
# test wave loader
signal, sample_rate = load_audio_file(sample_file)
self.assertIsInstance(signal, np.ndarray)
self.assertTrue(signal.dtype == np.int16)
self.assertTrue(type(sample_rate) == int)
# test ffmpeg loader
signal, sample_rate = load_audio_file(stereo_sample_file)
self.assertIsInstance(signal, np.ndarray)
self.assertTrue(signal.dtype == np.int16)
self.assertTrue(type(sample_rate) == int)
if sys.version_info[0] == 2:
# test unicode string type (Python 2 only)
signal, sample_rate = load_audio_file(unicode(sample_file))
def test_file_handle(self):
# test wave loader
with open(sample_file, 'rb') as file_handle:
signal, sample_rate = load_audio_file(file_handle)
self.assertIsInstance(signal, np.ndarray)
self.assertTrue(signal.dtype == np.int16)
self.assertTrue(type(sample_rate) == int)
# test ffmpeg loader
with open(sample_file, 'rb') as file_handle:
signal, sample_rate = load_audio_file(file_handle)
self.assertIsInstance(signal, np.ndarray)
self.assertTrue(signal.dtype == np.int16)
self.assertTrue(type(sample_rate) == int)
def test_values(self):
# test wave loader
signal, sample_rate = load_audio_file(sample_file)
self.assertTrue(np.allclose(signal[:5],
[-2494, -2510, -2484, -2678, -2833]))
self.assertTrue(len(signal) == 123481)
self.assertTrue(sample_rate == 44100)
self.assertTrue(signal.shape == (123481,))
# stereo
signal, sample_rate = load_audio_file(stereo_sample_file)
self.assertTrue(np.allclose(signal[:4],
[[33, 38], [35, 36], [29, 34], [36, 31]]))
self.assertTrue(len(signal) == 182919)
self.assertTrue(sample_rate == 44100)
self.assertTrue(signal.shape == (182919, 2))
# test ffmpeg loader
signal, sample_rate = load_audio_file(stereo_sample_file)
self.assertTrue(np.allclose(signal[:4],
[[33, 38], [35, 36], [29, 34], [36, 31]]))
self.assertTrue(len(signal) == 182919)
self.assertTrue(sample_rate == 44100)
self.assertTrue(signal.shape == (182919, 2))
def test_wave_channel_selection(self):
signal, sample_rate = load_audio_file(stereo_sample_file, channel=1)
self.assertTrue(signal.shape == (182919,))
self.assertTrue(np.allclose(signal[:4], [38, 36, 34, 31]))
def test_start_stop(self):
# test wave loader
signal, sample_rate = load_audio_file(sample_file, start=1. / 44100,
stop=5. / 44100)
self.assertTrue(np.allclose(signal, [-2510, -2484, -2678, -2833]))
self.assertTrue(len(signal) == 4)
self.assertTrue(sample_rate == 44100)
# test ffmpeg loader
signal, sample_rate = load_audio_file(stereo_sample_file,
start=1. / 44100,
stop=4. / 44100)
self.assertTrue(np.allclose(signal, [[35, 36], [29, 34], [36, 31]]))
self.assertTrue(len(signal) == 3)
self.assertTrue(sample_rate == 44100)
def test_downmix(self):
# test wave loader
signal, sample_rate = load_audio_file(stereo_sample_file,
num_channels=1)
self.assertTrue(np.allclose(signal[:5],
[35, 35, 31, 33, 33]))
self.assertTrue(len(signal) == 182919)
self.assertTrue(sample_rate == 44100)
self.assertTrue(signal.shape == (182919,))
# test ffmpeg loader
signal, sample_rate = load_audio_file(stereo_sample_file,
num_channels=1)
# results are rounded differently, thus allow atol=1
self.assertTrue(np.allclose(signal[:5], [35, 35, 31, 33, 33], atol=1))
# avconv results in a different length of 182909 samples
self.assertTrue(np.allclose(len(signal), 182919, atol=10))
self.assertTrue(sample_rate == 44100)
# test clipping
f = pj(AUDIO_PATH, 'stereo_chirp.wav')
signal, _ = load_audio_file(f, num_channels=1)
signal_stereo, _ = load_audio_file(f)
# sanity checks
self.assertTrue(np.allclose(signal_stereo[:, 0], signal_stereo[:, 1]))
self.assertTrue(np.allclose(signal_stereo.mean(axis=1),
signal_stereo[:, 0]))
# check clipping
self.assertTrue(np.allclose(signal, signal_stereo[:, 1]))
def test_upmix(self):
signal, sample_rate = load_audio_file(sample_file, num_channels=2)
self.assertTrue(np.allclose(signal[:5],
[[-2494, -2494], [-2510, -2510],
[-2484, -2484], [-2678, -2678],
[-2833, -2833]]))
self.assertTrue(len(signal) == 123481)
self.assertTrue(sample_rate == 44100)
self.assertTrue(signal.shape == (123481, 2))
def test_resample(self):
# method must chose ffmpeg loader
signal, sample_rate = load_audio_file(stereo_sample_file,
sample_rate=22050)
self.assertTrue(sample_rate == 22050)
# avconv does round differently, thus allow atol=1
# result: [[33, 38], [33, 33], [36, 31], [35, 35], [32, 35]]
self.assertTrue(np.allclose(signal[:5], [[34, 38], [32, 33], [37, 31],
[35, 35], [32, 34]], atol=1))
# also downmix
signal, sample_rate = load_audio_file(stereo_sample_file,
sample_rate=22050,
num_channels=1)
self.assertTrue(np.allclose(signal[:5], [36, 33, 34, 35, 33], atol=1))
# avconv results in a different length of 91450 samples
self.assertTrue(np.allclose(len(signal), 91460, atol=10))
def test_choose_channel(self):
signal, sample_rate = load_audio_file(flac_file,
sample_rate=22050,
num_channels=1, channel=0)
# avconv results in a different length of 91450 samples
self.assertTrue(np.allclose(len(signal), 91460, atol=10))
self.assertTrue(np.allclose(signal[:5], [34, 32, 37, 35, 32], atol=1))
def test_replaygain_disabled(self):
original = load_signal(flac_file)
signal = load_signal(rg_flac_file, replaygain_mode=None)
self.assertEqual(signal.spl(), original.spl())
def test_replaygain_track(self):
original = load_signal(flac_file)
data, sample_rate = load_audio_file(rg_flac_file,
replaygain_mode='track')
signal = Signal(data)
self.assertEqual(sample_rate, 44100)
# The FLAC file has an RG track gain of +8.39
self.assertAlmostEqual(signal.spl() - original.spl(), 8.39, places=3)
def test_replaygain_album(self):
original = load_signal(flac_file)
signal = load_signal(rg_flac_file, replaygain_mode='album')
# The FLAC file has an RG album gain of -8.12 (the other track is loud)
self.assertAlmostEqual(signal.spl() - original.spl(), -8.12, places=3)
def test_replaygain_preamp(self):
signal = load_signal(loud_rg_flac_file)
base_spl = signal.spl()
rg_sig = load_signal(loud_rg_flac_file, replaygain_mode='track')
# Remember, ffmpeg will limit gain to avoid clipping,
# so the SPL difference is non-obvious here.
self.assertTrue(base_spl > rg_sig.spl())
# Preamp on a file with track RG at -12.44 should happily go to -6.44dB
pre_rg_sig = load_signal(loud_rg_flac_file,
replaygain_mode='track',
replaygain_preamp=+6.0)
self.assertAlmostEqual(rg_sig.spl() + 6.0, pre_rg_sig.spl(), places=3)
def test_errors(self):
# file not found
with self.assertRaises(IOError):
load_audio_file(pj(AUDIO_PATH, 'sample.flac'))
# not an audio file
with self.assertRaises(LoadAudioFileError):
load_audio_file(pj(DATA_PATH, 'README'))
def load_signal(fn, *args, **kwargs):
return Signal(load_audio_file(fn, *args, **kwargs)[0])
class TestDecodeToDisk(unittest.TestCase):
def test_write_to_temp(self):
outfile = decode_to_disk(stereo_sample_file, tmp_suffix='.raw')
self.assertTrue(os.path.exists(outfile))
self.assertGreater(os.stat(outfile).st_size, 100 * 1024)
def test_write_with_replaygain(self):
outfile = decode_to_disk(loud_rg_flac_file, tmp_suffix='.raw')
outfile_rg = decode_to_disk(loud_rg_flac_file, tmp_suffix='.raw',
replaygain_mode='track')
with open(outfile, 'rb') as f:
data = np.frombuffer(f.read(), dtype=np.int16).reshape((-1, 2))
orig_spl = Signal(data).spl()
with open(outfile_rg, 'rb') as f:
data = np.frombuffer(f.read(), dtype=np.int16).reshape((-1, 2))
adjusted_spl = Signal(data).spl()
self.assertGreater(orig_spl, adjusted_spl)
class TestLoadAudioFromFileObject(unittest.TestCase):
def test_file_object(self):
for file_path in [sample_file, flac_file, m4a_file]:
disk_signal = Signal(file_path)
with open(file_path, 'rb') as file_handle:
memory_signal = Signal(io.BytesIO(file_handle.read()))
self.assertTrue((disk_signal == memory_signal).all)
# clean up
def teardown_module():
os.unlink(tmp_file)