-
Notifications
You must be signed in to change notification settings - Fork 33
/
test_audio.py
523 lines (390 loc) · 19.7 KB
/
test_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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
import unittest
from os.path import join
import wave
from praatio import audio
from praatio.utilities import utils
from praatio.utilities import errors
from tests.praatio_test_case import PraatioTestCase
# Wrap superclasses in a dummy test to prevent them from running
# https://stackoverflow.com/a/25695512
class AudioWrapper:
class TestAudio(PraatioTestCase):
def test_get_audio_duration(self):
"""Tests that the two audio duration methods output the same value."""
durationA = utils.getWavDuration(self.bobWavFN)
durationB = audio.getDuration(self.bobWavFN)
self.assertEqual(1.194625, durationA)
self.assertEqual(durationA, durationB)
def test_extract_subwavs(self):
"""Tests that extractSubwavs will output files with the expected duration"""
outputWavFN = join(self.outputRoot, "bobby_word.wav")
audio.extractSubwav(self.bobWavFN, outputWavFN, 0.06, 0.40)
duration = utils.getWavDuration(outputWavFN)
self.assertEqual(0.34, duration)
def test_query_wav_duration(self):
wav = audio.QueryWav(self.bobWavFN)
self.assertEqual(1.194625, wav.duration)
def test_query_wav_get_frames(self):
wav = audio.QueryWav(self.bobWavFN)
wavObj = wave.open(self.bobWavFN, "r")
self.assertEqual(
audio.readFramesAtTime(wavObj, 0.5, 1.12), wav.getFrames(0.5, 1.12)
)
def test_query_wav_get_frames_defaults_to_all_frames(self):
wav = audio.QueryWav(self.bobWavFN)
wavObj = wave.open(self.bobWavFN, "r")
expectedFrames = audio.readFramesAtTime(wavObj, 0, wav.duration)
self.assertEqual(expectedFrames, wav.getFrames())
def test_query_wav_get_samples(self):
wav = audio.QueryWav(self.bobWavFN)
wavObj = wave.open(self.bobWavFN, "r")
self.assertEqual(
audio.convertFromBytes(
audio.readFramesAtTime(wavObj, 0.5, 1.12), wavObj.getsampwidth()
),
wav.getSamples(0.5, 1.12),
)
def test_wav_open(self):
# The open() method should have the same result as
# as manually creating a Wav
sut = audio.Wav.open(self.bobWavFN)
wav = wave.open(self.bobWavFN, "r")
frames = audio.readFramesAtTime(wav, 0, audio.getDuration(self.bobWavFN))
wavObj = audio.Wav(frames, wav.getparams())
self.assertEqual(wavObj, sut)
def test_wav_equal_to_non_wav_is_false(self):
sut = audio.Wav.open(self.bobWavFN)
wav = audio.QueryWav(self.bobWavFN)
self.assertNotEqual(5, sut)
self.assertNotEqual(wav, sut) # QueryWavs are not Wavs
def test_wav_new_creates_a_unique_copy(self):
wav = audio.Wav.open(self.bobWavFN)
sut = wav.new()
self.assertEqual(wav, sut)
sut.deleteSegment(0.5, 1.0)
self.assertNotEqual(wav, sut)
def test_wav_concatenate(self):
expectedWav = audio.Wav.open(self.bobWavFN)
wav = wave.open(self.bobWavFN, "r")
firstHalfFrames = audio.readFramesAtTime(wav, 0.0, 0.7)
lastHalfFrames = audio.readFramesAtTime(
wav, 0.7, audio.getDuration(self.bobWavFN)
)
sut = audio.Wav(firstHalfFrames, wav.getparams())
sut.concatenate(lastHalfFrames)
self.assertEqual(expectedWav, sut)
def test_wav_delete_segment(self):
sut = audio.Wav.open(self.bobWavFN)
wav = wave.open(self.bobWavFN, "r")
expectedFrames = audio.readFramesAtTime(wav, 0.25, 0.93)
sut.deleteSegment(0.93, sut.duration)
sut.deleteSegment(0, 0.25)
self.assertEqual(expectedFrames, sut.frames)
def test_wav_duration(self):
fullWavFile = audio.Wav.open(self.bobWavFN)
wav = wave.open(self.bobWavFN, "r")
subsegmentFrames = audio.readFramesAtTime(wav, 0.25, 0.93)
wavSubsegment = audio.Wav(subsegmentFrames, wav.getparams())
self.assertEqual(1.194625, fullWavFile.duration)
self.assertEqual(0.68, wavSubsegment.duration)
def test_wav_get_frames(self):
wavObj = audio.Wav.open(self.bobWavFN)
wav = wave.open(self.bobWavFN, "r")
expectedFrames = audio.readFramesAtTime(wav, 0.25, 0.93)
sut = wavObj.getFrames(0.25, 0.93)
self.assertEqual(expectedFrames, sut)
def test_read_frames_at_time_throws_exception_if_keep_and_delete_specified(
self,
):
wav = wave.open(self.bobWavFN, "r")
with self.assertRaises(errors.ArgumentError) as _:
audio.readFramesAtTimes(
wav, keepIntervals=[(1, 1.3)], deleteIntervals=[(0, 1)]
)
def test_read_frames_at_time_throws_exception_if_timestamps_exceed_audio_duration(
self,
):
wav = wave.open(self.bobWavFN, "r")
with self.assertRaises(errors.ArgumentError) as _:
audio.readFramesAtTimes(wav, keepIntervals=[(100.5, 101)])
def test_read_frames_at_time_will_read_the_whole_file_by_default(self):
wav = wave.open(self.bobWavFN, "r")
sut = audio.readFramesAtTimes(wav)
wavObj = audio.Wav.open(self.bobWavFN)
expectedFrames = wavObj.frames
self.assertEqual(expectedFrames, sut)
def test_wav_get_samples(self):
# This test isn't really testing anything since there is
# only a single way the methods convert from bytes
wavObj = audio.Wav.open(self.bobWavFN)
wav = wave.open(self.bobWavFN, "r")
expectedFrames = audio.readFramesAtTime(wav, 0.25, 0.93)
expectedSamples = audio.convertFromBytes(expectedFrames, wav.getsampwidth())
sut = wavObj.getSamples(0.25, 0.93)
self.assertEqual(expectedSamples, sut)
def test_wav_get_subwav(self):
wavObj = audio.Wav.open(self.bobWavFN)
sut = wavObj.getSubwav(0.25, 0.93)
wav = wave.open(self.bobWavFN, "r")
expectedFrames = audio.readFramesAtTime(wav, 0.25, 0.93)
expectedSubwav = audio.Wav(expectedFrames, wav.getparams())
self.assertEqual(expectedSubwav, sut)
def test_wav_insert(self):
expectedWav = audio.Wav.open(self.bobWavFN)
wav = wave.open(self.bobWavFN, "r")
firstThirdFrames = audio.readFramesAtTime(wav, 0.0, 0.25)
secondThirdFrames = audio.readFramesAtTime(wav, 0.25, 0.93)
finalThirdFrames = audio.readFramesAtTime(wav, 0.93, expectedWav.duration)
sut = audio.Wav(finalThirdFrames, expectedWav.params)
sut.insert(0, firstThirdFrames)
sut.insert(0.25, secondThirdFrames)
self.assertEqual(expectedWav, sut)
def test_wav_replace_segment_with_segment_of_equal_length(self):
sut = audio.Wav.open(self.bobWavFN)
sut.replaceSegment(0.5, 1.0, sut.getFrames(0.0, 0.5))
self.assertEqual(1.194625, sut.duration)
def test_wav_replace_segment_with_segment_of_non_equal_length(self):
expectedDuration = audio.getDuration(self.bobWavFN) + 0.5
sut = audio.Wav.open(self.bobWavFN)
sut.replaceSegment(0.5, 1.0, sut.getFrames(0.0, 1.0))
self.assertEqual(expectedDuration, sut.duration)
def test_open_audio_with_keep_list(self):
start1, end1 = (0.06, 0.40)
start2, end2 = (0.75, 1.12)
expectedDuration = (end1 - start1) + (end2 - start2)
wav = wave.open(self.bobWavFN, "r")
readFrames = audio.readFramesAtTimes(
wav, keepIntervals=[(start1, end1, "Bobby"), (start2, end2, "Ledger")]
)
sut = audio.Wav(readFrames, wav.getparams())
sut.save(join(self.outputRoot, "bobby_word_tmp.wav"))
self.assertAlmostEqual(expectedDuration, sut.duration)
def test_open_audio_with_keep_intervals_and_deleted_segments_are_replaced_with_silence(
self,
):
expectedDuration = audio.getDuration(self.bobWavFN)
wavObj = wave.open(self.bobWavFN, "r")
wav = audio.Wav.open(self.bobWavFN)
generator = audio.AudioGenerator.fromWav(wav)
readFrames = audio.readFramesAtTimes(
wavObj,
keepIntervals=[(0.06, 0.40, "Bobby"), (0.75, 1.12, "Ledger")],
replaceFunc=generator.generateSilence,
)
sut = audio.Wav(readFrames, wav.params)
sut.save(join(self.outputRoot, "bobby_word_keep_intervals_and_silence.wav"))
self.assertAlmostEqual(expectedDuration, sut.duration)
def test_read_frames_at_times_with_delete_intervals(self):
start1, end1 = (0.06, 0.40)
start2, end2 = (0.75, 1.12)
expectedDuration = (
audio.getDuration(self.bobWavFN) - (end1 - start1) - (end2 - start2)
)
wav = wave.open(self.bobWavFN, "r")
readFrames = audio.readFramesAtTimes(
wav, deleteIntervals=[(start1, end1, "Bobby"), (start2, end2, "Ledger")]
)
sut = audio.Wav(readFrames, wav.getparams())
self.assertAlmostEqual(expectedDuration, sut.duration, 4)
def test_frames_at_times_with_deleted_segments_replaced_with_silence(self):
start1, end1 = (0.06, 0.40)
start2, end2 = (0.75, 1.12)
expectedDuration = audio.getDuration(self.bobWavFN)
wavObj = wave.open(self.bobWavFN, "r")
wav = audio.Wav.open(self.bobWavFN)
generator = audio.AudioGenerator.fromWav(wav)
readFrames = audio.readFramesAtTimes(
wavObj,
deleteIntervals=[(start1, end1, "Bobby"), (start2, end2, "Ledger")],
replaceFunc=generator.generateSilence,
)
sut = audio.Wav(readFrames, wavObj.getparams())
sut.save(join(self.outputRoot, "bobby_word_delete_list_and_silence.wav"))
self.assertAlmostEqual(expectedDuration, sut.duration)
# Silent regions should be silent, non-silent regions should be not silent
self.assertEqual(
generator.generateSilence(end1 - start1), sut.getFrames(start1, end1)
)
self.assertNotEqual(
generator.generateSilence(start2 - end1), sut.getFrames(end1, start2)
)
self.assertEqual(
generator.generateSilence(end2 - start2), sut.getFrames(start2, end2)
)
class TestAudioWith16Bits48000Hz(AudioWrapper.TestAudio):
def __init__(self, *args, **kargs):
super(AudioWrapper.TestAudio, self).__init__(*args, **kargs)
self.bobWavFN = join(self.dataRoot, "bobby_16bit_48khz.wav")
wav = wave.open(self.bobWavFN, "r")
assert wav.getsampwidth() == 2
assert wav.getframerate() == 48_000
class TestAudioWith32Bits48000Hz(AudioWrapper.TestAudio):
def __init__(self, *args, **kargs):
super(AudioWrapper.TestAudio, self).__init__(*args, **kargs)
self.bobWavFN = join(self.dataRoot, "bobby_32bit_48khz.wav")
wav = wave.open(self.bobWavFN, "r")
assert wav.getsampwidth() == 4
assert wav.getframerate() == 48_000
class TestAudioWith16Bits16000Hz(AudioWrapper.TestAudio):
def __init__(self, *args, **kargs):
super(AudioWrapper.TestAudio, self).__init__(*args, **kargs)
self.bobWavFN = join(self.dataRoot, "bobby_16bit_16khz.wav")
wav = wave.open(self.bobWavFN, "r")
assert wav.getsampwidth() == 2
assert wav.getframerate() == 16_000
class TestAudioWith32Bits16000Hz(AudioWrapper.TestAudio):
def __init__(self, *args, **kargs):
super(AudioWrapper.TestAudio, self).__init__(*args, **kargs)
self.bobWavFN = join(self.dataRoot, "bobby_32bit_16khz.wav")
wav = wave.open(self.bobWavFN, "r")
assert wav.getsampwidth() == 4
assert wav.getframerate() == 16_000
class TestAudio(PraatioTestCase):
def test_wav_open_fails_when_there_are_more_than_2_channels(self):
self.bobWavFN = join(self.dataRoot, "bobby_16bit_16khz_2ch.wav")
wav = wave.open(self.bobWavFN, "r")
assert wav.getsampwidth() == 2
assert wav.getframerate() == 16_000
assert wav.getnchannels() == 2
with self.assertRaises(errors.ArgumentError) as _:
audio.Wav.open(self.bobWavFN)
def test_calculate_max_amplitude(self):
self.assertEqual(127, audio.calculateMaxAmplitude(1))
self.assertEqual(32_767, audio.calculateMaxAmplitude(2))
self.assertEqual(8_388_607, audio.calculateMaxAmplitude(3))
self.assertEqual(2_147_483_647, audio.calculateMaxAmplitude(4))
def test_convert_to_and_from_bytes_when_sample_width_is_one(self):
values = (0, 10, 90, 127, 0, -10, -90, -127)
valuesAsBytes = audio.convertToBytes(values, 1)
sut = audio.convertFromBytes(valuesAsBytes, 1)
self.assertEqual(values, sut)
def test_convert_to_and_from_bytes_when_sample_width_is_four(self):
values = (
0,
483_647,
147_483_647,
2_147_483_647,
0,
-483_647,
-147_483_647,
-2_147_483_647,
)
valuesAsBytes = audio.convertToBytes(values, 4)
sut = audio.convertFromBytes(valuesAsBytes, 4)
self.assertEqual(values, sut)
def test_audio_generator_build_sine_wave_generator(self):
generator = audio.AudioGenerator(2, 16_000)
sineGenerator = generator.buildSineWaveGenerator(100, 10_000)
sineWav = sineGenerator(1.0)
sut = audio.convertFromBytes(sineWav, 2)
maxValue = 10_000 # Set by the method call
self.assertEqual(16_000, len(sut))
self.assertEqual(maxValue * -1, min(sut))
self.assertEqual(maxValue, max(sut))
self.assertEqual(100, sut.count(maxValue * -1))
self.assertEqual(100, sut.count(maxValue))
self.assertEqual(200, sut.count(0))
def test_audio_generator_generate_sine_wave_of_100hz(self):
generator = audio.AudioGenerator(2, 16_000)
sineWav = generator.generateSineWave(1.0, 100)
sut = audio.convertFromBytes(sineWav, 2)
maxValue = 32767 # From the sample width
self.assertEqual(16_000, len(sut))
self.assertEqual(maxValue * -1, min(sut))
self.assertEqual(maxValue, max(sut))
self.assertEqual(100, sut.count(maxValue * -1))
self.assertEqual(100, sut.count(maxValue))
self.assertEqual(200, sut.count(0))
def test_audio_generator_generate_sine_wave_with_set_amplitude(self):
generator = audio.AudioGenerator(2, 16_000)
sineWav = generator.generateSineWave(1.0, 100, 10_000)
sut = audio.convertFromBytes(sineWav, 2)
maxValue = 10_000 # Set by the method call
self.assertEqual(16_000, len(sut))
self.assertEqual(maxValue * -1, min(sut))
self.assertEqual(maxValue, max(sut))
self.assertEqual(100, sut.count(maxValue * -1))
self.assertEqual(100, sut.count(maxValue))
self.assertEqual(200, sut.count(0))
def test_audio_generator_generate_sine_wave_of_400hz(self):
generator = audio.AudioGenerator(2, 16_000)
sineWav = generator.generateSineWave(1.0, 400)
sut = audio.convertFromBytes(sineWav, 2)
maxValue = 32767 # From the sample width
self.assertEqual(16_000, len(sut))
self.assertEqual(maxValue * -1, min(sut))
self.assertEqual(maxValue, max(sut))
self.assertEqual(400, sut.count(maxValue * -1))
self.assertEqual(400, sut.count(maxValue))
self.assertEqual(800, sut.count(0))
def test_audio_generator_generate_silence(self):
generator = audio.AudioGenerator(2, 16_000)
silence = generator.generateSilence(1.0)
sut = audio.convertFromBytes(silence, 2)
self.assertEqual(16_000, len(sut))
self.assertEqual(0, min(sut))
self.assertEqual(0, max(sut))
def test_wav_find_nearest_zero_crossing(self):
numChannels = 1
sampleWidth = 2
frameRate = 16_000
generator = audio.AudioGenerator(sampleWidth, frameRate)
# zero crossings occur at 0 and 0.5 (and almost at 1.0 -- off by one sample)
sineWavFrames = generator.generateSineWave(1.0, 1)
sineWav = audio.Wav(
sineWavFrames,
[numChannels, sampleWidth, frameRate, 16_000, "unused", "unused"],
)
# Should return the specified time if its a zero crossing
self.assertAlmostEqual(0, sineWav.findNearestZeroCrossing(0))
self.assertAlmostEqual(0.5, sineWav.findNearestZeroCrossing(0.5))
# If equidistant, it should choose the one on the left
self.assertAlmostEqual(0.0, sineWav.findNearestZeroCrossing(0.24))
self.assertAlmostEqual(0.0, sineWav.findNearestZeroCrossing(0.25))
self.assertAlmostEqual(0.5, sineWav.findNearestZeroCrossing(0.26))
self.assertAlmostEqual(0.5, sineWav.findNearestZeroCrossing(0.4))
self.assertAlmostEqual(0.5, sineWav.findNearestZeroCrossing(0.6))
self.assertAlmostEqual(0.5, sineWav.findNearestZeroCrossing(1.0))
def test_wav_find_nearest_zero_crossing_throws_argument_error(self):
# With a sampling rate of 1 frame per second and a step size of
# 1 second, findNearestZeroCrossing won't have a large enough
# sample size to work with and will throw an exception
sampleWidth = 2
samples = [5, 10, 20, 15, 0, -1, -7, -3, 0, 10]
frames = audio.convertToBytes(samples, sampleWidth)
params = [1, sampleWidth, 1, len(samples), "", ""] # 1 frame per second
sut = audio.Wav(frames, params)
with self.assertRaises(errors.ArgumentError) as _:
sut.findNearestZeroCrossing(0, 1)
def test_wav_find_nearest_zero_crossing_with_simple_cases(self):
sampleWidth = 2
samples = [5, 10, 20, 15, 0, -1, -7, -3, 0, 10]
frames = audio.convertToBytes(samples, sampleWidth)
params = [1, sampleWidth, 1, len(samples), "", ""] # 1 frame per second
sut = audio.Wav(frames, params)
self.assertEqual(4, sut.findNearestZeroCrossing(0, 2))
self.assertEqual(8, sut.findNearestZeroCrossing(9, 2))
def test_wav_find_nearest_zero_crossing_when_there_is_no_zero_value(self):
sampleWidth = 2
samples = [10, 20, 1, -5, -7, -3, -1, 3, 10, 4]
frames = audio.convertToBytes(samples, sampleWidth)
params = [1, sampleWidth, 1, len(samples), "", ""] # 1 frame per second
sut = audio.Wav(frames, params)
self.assertEqual(2, sut.findNearestZeroCrossing(0, 2))
self.assertEqual(6, sut.findNearestZeroCrossing(9, 2))
def test_wav_find_nearest_zero_crossing_chooses_the_smaller_value_at_zero_crossing(
self,
):
sampleWidth = 2
samples = [10, 20, 15, -1, -7, -3, -2, 1, 10, 4]
frames = audio.convertToBytes(samples, sampleWidth)
params = [1, sampleWidth, 1, len(samples), "", ""] # 1 frame per second
sut = audio.Wav(frames, params)
# abs(15) is larger than abs(-1), so although the zero crossing
# is at 15, we choose -1, since its closer to zero
self.assertEqual(3, sut.findNearestZeroCrossing(0, 2))
# abs(-2) is larger than abs(1), so although the zero crossing
# is at -2, we choose -1 since its closer to zero
self.assertEqual(7, sut.findNearestZeroCrossing(9, 2))
if __name__ == "__main__":
unittest.main()