Skip to content

Commit 0babc87

Browse files
committed
Switch to on the fly generation for test_sox_compatibility
1 parent 6795f01 commit 0babc87

File tree

1 file changed

+57
-74
lines changed

1 file changed

+57
-74
lines changed

test/test_sox_compatibility.py

Lines changed: 57 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,17 @@
99

1010

1111
@common_utils.skipIfNoSoxBackend
12-
class TestFunctionalFiltering(common_utils.TorchaudioTestCase):
12+
class TestFunctionalFiltering(common_utils.TorchaudioTestCase, common_utils.TempDirMixin):
1313
backend = 'sox'
1414

15+
def setUp(self):
16+
super().setUp()
17+
18+
# write out 5s, 44.1kHz whitenoise file
19+
noise_waveform = common_utils.get_whitenoise(sample_rate=44100, duration=5)
20+
self.noise_filepath = self.get_temp_path('whitenoise.wav')
21+
common_utils.save_wav(self.noise_filepath, noise_waveform)
22+
1523
def test_gain(self):
1624
test_filepath = common_utils.get_asset_path('steam-train-whistle-daniel_simon.wav')
1725
waveform, _ = torchaudio.load(test_filepath)
@@ -74,16 +82,12 @@ def test_lowpass(self):
7482

7583
cutoff_freq = 3000
7684

77-
noise_waveform = common_utils.get_whitenoise(sample_rate=44100, duration=5)
78-
noise_filepath = self.get_temp_path('whitenoise.wav')
79-
common_utils.save_wav(noise_filepath, noise_waveform)
80-
8185
E = torchaudio.sox_effects.SoxEffectsChain()
82-
E.set_input_file(noise_filepath)
86+
E.set_input_file(self.noise_filepath)
8387
E.append_effect_to_chain("lowpass", [cutoff_freq])
8488
sox_output_waveform, sr = E.sox_build_flow_effects()
8589

86-
waveform, sample_rate = torchaudio.load(noise_filepath, normalization=True)
90+
waveform, sample_rate = torchaudio.load(self.noise_filepath, normalization=True)
8791
output_waveform = F.lowpass_biquad(waveform, sample_rate, cutoff_freq)
8892

8993
self.assertEqual(output_waveform, sox_output_waveform, atol=1e-4, rtol=1e-5)
@@ -94,14 +98,13 @@ def test_highpass(self):
9498
"""
9599

96100
cutoff_freq = 2000
97-
98-
noise_filepath = common_utils.get_asset_path('whitenoise.wav')
101+
99102
E = torchaudio.sox_effects.SoxEffectsChain()
100-
E.set_input_file(noise_filepath)
103+
E.set_input_file(self.noise_filepath)
101104
E.append_effect_to_chain("highpass", [cutoff_freq])
102105
sox_output_waveform, sr = E.sox_build_flow_effects()
103106

104-
waveform, sample_rate = torchaudio.load(noise_filepath, normalization=True)
107+
waveform, sample_rate = torchaudio.load(self.noise_filepath, normalization=True)
105108
output_waveform = F.highpass_biquad(waveform, sample_rate, cutoff_freq)
106109

107110
# TBD - this fails at the 1e-4 level, debug why
@@ -115,13 +118,12 @@ def test_allpass(self):
115118
central_freq = 1000
116119
q = 0.707
117120

118-
noise_filepath = common_utils.get_asset_path('whitenoise.wav')
119121
E = torchaudio.sox_effects.SoxEffectsChain()
120-
E.set_input_file(noise_filepath)
122+
E.set_input_file(self.noise_filepath)
121123
E.append_effect_to_chain("allpass", [central_freq, str(q) + 'q'])
122124
sox_output_waveform, sr = E.sox_build_flow_effects()
123125

124-
waveform, sample_rate = torchaudio.load(noise_filepath, normalization=True)
126+
waveform, sample_rate = torchaudio.load(self.noise_filepath, normalization=True)
125127
output_waveform = F.allpass_biquad(waveform, sample_rate, central_freq, q)
126128

127129
self.assertEqual(output_waveform, sox_output_waveform, atol=1e-4, rtol=1e-5)
@@ -135,13 +137,12 @@ def test_bandpass_with_csg(self):
135137
q = 0.707
136138
const_skirt_gain = True
137139

138-
noise_filepath = common_utils.get_asset_path('whitenoise.wav')
139140
E = torchaudio.sox_effects.SoxEffectsChain()
140-
E.set_input_file(noise_filepath)
141+
E.set_input_file(self.noise_filepath)
141142
E.append_effect_to_chain("bandpass", ["-c", central_freq, str(q) + 'q'])
142143
sox_output_waveform, sr = E.sox_build_flow_effects()
143144

144-
waveform, sample_rate = torchaudio.load(noise_filepath, normalization=True)
145+
waveform, sample_rate = torchaudio.load(self.noise_filepath, normalization=True)
145146
output_waveform = F.bandpass_biquad(waveform, sample_rate, central_freq, q, const_skirt_gain)
146147

147148
self.assertEqual(output_waveform, sox_output_waveform, atol=1e-4, rtol=1e-5)
@@ -155,13 +156,12 @@ def test_bandpass_without_csg(self):
155156
q = 0.707
156157
const_skirt_gain = False
157158

158-
noise_filepath = common_utils.get_asset_path('whitenoise.wav')
159159
E = torchaudio.sox_effects.SoxEffectsChain()
160-
E.set_input_file(noise_filepath)
160+
E.set_input_file(self.noise_filepath)
161161
E.append_effect_to_chain("bandpass", [central_freq, str(q) + 'q'])
162162
sox_output_waveform, sr = E.sox_build_flow_effects()
163163

164-
waveform, sample_rate = torchaudio.load(noise_filepath, normalization=True)
164+
waveform, sample_rate = torchaudio.load(self.noise_filepath, normalization=True)
165165
output_waveform = F.bandpass_biquad(waveform, sample_rate, central_freq, q, const_skirt_gain)
166166

167167
self.assertEqual(output_waveform, sox_output_waveform, atol=1e-4, rtol=1e-5)
@@ -174,13 +174,12 @@ def test_bandreject(self):
174174
central_freq = 1000
175175
q = 0.707
176176

177-
noise_filepath = common_utils.get_asset_path('whitenoise.wav')
178177
E = torchaudio.sox_effects.SoxEffectsChain()
179-
E.set_input_file(noise_filepath)
178+
E.set_input_file(self.noise_filepath)
180179
E.append_effect_to_chain("bandreject", [central_freq, str(q) + 'q'])
181180
sox_output_waveform, sr = E.sox_build_flow_effects()
182181

183-
waveform, sample_rate = torchaudio.load(noise_filepath, normalization=True)
182+
waveform, sample_rate = torchaudio.load(self.noise_filepath, normalization=True)
184183
output_waveform = F.bandreject_biquad(waveform, sample_rate, central_freq, q)
185184

186185
self.assertEqual(output_waveform, sox_output_waveform, atol=1e-4, rtol=1e-5)
@@ -194,13 +193,12 @@ def test_band_with_noise(self):
194193
q = 0.707
195194
noise = True
196195

197-
noise_filepath = common_utils.get_asset_path('whitenoise.wav')
198196
E = torchaudio.sox_effects.SoxEffectsChain()
199-
E.set_input_file(noise_filepath)
197+
E.set_input_file(self.noise_filepath)
200198
E.append_effect_to_chain("band", ["-n", central_freq, str(q) + 'q'])
201199
sox_output_waveform, sr = E.sox_build_flow_effects()
202200

203-
waveform, sample_rate = torchaudio.load(noise_filepath, normalization=True)
201+
waveform, sample_rate = torchaudio.load(self.noise_filepath, normalization=True)
204202
output_waveform = F.band_biquad(waveform, sample_rate, central_freq, q, noise)
205203

206204
self.assertEqual(output_waveform, sox_output_waveform, atol=1e-4, rtol=1e-5)
@@ -214,13 +212,12 @@ def test_band_without_noise(self):
214212
q = 0.707
215213
noise = False
216214

217-
noise_filepath = common_utils.get_asset_path('whitenoise.wav')
218215
E = torchaudio.sox_effects.SoxEffectsChain()
219-
E.set_input_file(noise_filepath)
216+
E.set_input_file(self.noise_filepath)
220217
E.append_effect_to_chain("band", [central_freq, str(q) + 'q'])
221218
sox_output_waveform, sr = E.sox_build_flow_effects()
222219

223-
waveform, sample_rate = torchaudio.load(noise_filepath, normalization=True)
220+
waveform, sample_rate = torchaudio.load(self.noise_filepath, normalization=True)
224221
output_waveform = F.band_biquad(waveform, sample_rate, central_freq, q, noise)
225222

226223
self.assertEqual(output_waveform, sox_output_waveform, atol=1e-4, rtol=1e-5)
@@ -234,13 +231,12 @@ def test_treble(self):
234231
q = 0.707
235232
gain = 40
236233

237-
noise_filepath = common_utils.get_asset_path('whitenoise.wav')
238234
E = torchaudio.sox_effects.SoxEffectsChain()
239-
E.set_input_file(noise_filepath)
235+
E.set_input_file(self.noise_filepath)
240236
E.append_effect_to_chain("treble", [gain, central_freq, str(q) + 'q'])
241237
sox_output_waveform, sr = E.sox_build_flow_effects()
242238

243-
waveform, sample_rate = torchaudio.load(noise_filepath, normalization=True)
239+
waveform, sample_rate = torchaudio.load(self.noise_filepath, normalization=True)
244240
output_waveform = F.treble_biquad(waveform, sample_rate, gain, central_freq, q)
245241

246242
self.assertEqual(output_waveform, sox_output_waveform, atol=1e-4, rtol=1e-5)
@@ -254,13 +250,12 @@ def test_bass(self):
254250
q = 0.707
255251
gain = 40
256252

257-
noise_filepath = common_utils.get_asset_path('whitenoise.wav')
258253
E = torchaudio.sox_effects.SoxEffectsChain()
259-
E.set_input_file(noise_filepath)
254+
E.set_input_file(self.noise_filepath)
260255
E.append_effect_to_chain("bass", [gain, central_freq, str(q) + 'q'])
261256
sox_output_waveform, sr = E.sox_build_flow_effects()
262257

263-
waveform, sample_rate = torchaudio.load(noise_filepath, normalization=True)
258+
waveform, sample_rate = torchaudio.load(self.noise_filepath, normalization=True)
264259
output_waveform = F.bass_biquad(waveform, sample_rate, gain, central_freq, q)
265260

266261
self.assertEqual(output_waveform, sox_output_waveform, atol=1.5e-4, rtol=1e-5)
@@ -270,13 +265,12 @@ def test_deemph(self):
270265
Test biquad deemph filter, compare to SoX implementation
271266
"""
272267

273-
noise_filepath = common_utils.get_asset_path('whitenoise.wav')
274268
E = torchaudio.sox_effects.SoxEffectsChain()
275-
E.set_input_file(noise_filepath)
269+
E.set_input_file(self.noise_filepath)
276270
E.append_effect_to_chain("deemph")
277271
sox_output_waveform, sr = E.sox_build_flow_effects()
278272

279-
waveform, sample_rate = torchaudio.load(noise_filepath, normalization=True)
273+
waveform, sample_rate = torchaudio.load(self.noise_filepath, normalization=True)
280274
output_waveform = F.deemph_biquad(waveform, sample_rate)
281275

282276
self.assertEqual(output_waveform, sox_output_waveform, atol=1e-4, rtol=1e-5)
@@ -286,13 +280,12 @@ def test_riaa(self):
286280
Test biquad riaa filter, compare to SoX implementation
287281
"""
288282

289-
noise_filepath = common_utils.get_asset_path('whitenoise.wav')
290283
E = torchaudio.sox_effects.SoxEffectsChain()
291-
E.set_input_file(noise_filepath)
284+
E.set_input_file(self.noise_filepath)
292285
E.append_effect_to_chain("riaa")
293286
sox_output_waveform, sr = E.sox_build_flow_effects()
294287

295-
waveform, sample_rate = torchaudio.load(noise_filepath, normalization=True)
288+
waveform, sample_rate = torchaudio.load(self.noise_filepath, normalization=True)
296289
output_waveform = F.riaa_biquad(waveform, sample_rate)
297290

298291
self.assertEqual(output_waveform, sox_output_waveform, atol=1e-4, rtol=1e-5)
@@ -302,13 +295,12 @@ def test_contrast(self):
302295
Test contrast effect, compare to SoX implementation
303296
"""
304297
enhancement_amount = 80.
305-
noise_filepath = common_utils.get_asset_path('whitenoise.wav')
306298
E = torchaudio.sox_effects.SoxEffectsChain()
307-
E.set_input_file(noise_filepath)
299+
E.set_input_file(self.noise_filepath)
308300
E.append_effect_to_chain("contrast", [enhancement_amount])
309301
sox_output_waveform, sr = E.sox_build_flow_effects()
310302

311-
waveform, sample_rate = torchaudio.load(noise_filepath, normalization=True)
303+
waveform, sample_rate = torchaudio.load(self.noise_filepath, normalization=True)
312304
output_waveform = F.contrast(waveform, enhancement_amount)
313305

314306
self.assertEqual(output_waveform, sox_output_waveform, atol=1e-4, rtol=1e-5)
@@ -319,13 +311,12 @@ def test_dcshift_with_limiter(self):
319311
"""
320312
shift = 0.5
321313
limiter_gain = 0.05
322-
noise_filepath = common_utils.get_asset_path('whitenoise.wav')
323314
E = torchaudio.sox_effects.SoxEffectsChain()
324-
E.set_input_file(noise_filepath)
315+
E.set_input_file(self.noise_filepath)
325316
E.append_effect_to_chain("dcshift", [shift, limiter_gain])
326317
sox_output_waveform, sr = E.sox_build_flow_effects()
327318

328-
waveform, _ = torchaudio.load(noise_filepath, normalization=True)
319+
waveform, _ = torchaudio.load(self.noise_filepath, normalization=True)
329320
output_waveform = F.dcshift(waveform, shift, limiter_gain)
330321

331322
self.assertEqual(output_waveform, sox_output_waveform, atol=1e-4, rtol=1e-5)
@@ -335,13 +326,12 @@ def test_dcshift_without_limiter(self):
335326
Test dcshift effect, compare to SoX implementation
336327
"""
337328
shift = 0.6
338-
noise_filepath = common_utils.get_asset_path('whitenoise.wav')
339329
E = torchaudio.sox_effects.SoxEffectsChain()
340-
E.set_input_file(noise_filepath)
330+
E.set_input_file(self.noise_filepath)
341331
E.append_effect_to_chain("dcshift", [shift])
342332
sox_output_waveform, sr = E.sox_build_flow_effects()
343333

344-
waveform, _ = torchaudio.load(noise_filepath, normalization=True)
334+
waveform, _ = torchaudio.load(self.noise_filepath, normalization=True)
345335
output_waveform = F.dcshift(waveform, shift)
346336

347337
self.assertEqual(output_waveform, sox_output_waveform, atol=1e-4, rtol=1e-5)
@@ -352,13 +342,12 @@ def test_overdrive(self):
352342
"""
353343
gain = 30
354344
colour = 40
355-
noise_filepath = common_utils.get_asset_path('whitenoise.wav')
356345
E = torchaudio.sox_effects.SoxEffectsChain()
357-
E.set_input_file(noise_filepath)
346+
E.set_input_file(self.noise_filepath)
358347
E.append_effect_to_chain("overdrive", [gain, colour])
359348
sox_output_waveform, sr = E.sox_build_flow_effects()
360349

361-
waveform, _ = torchaudio.load(noise_filepath, normalization=True)
350+
waveform, _ = torchaudio.load(self.noise_filepath, normalization=True)
362351
output_waveform = F.overdrive(waveform, gain, colour)
363352

364353
self.assertEqual(output_waveform, sox_output_waveform, atol=1e-4, rtol=1e-5)
@@ -372,13 +361,12 @@ def test_phaser_sine(self):
372361
delay_ms = 2.0
373362
decay = 0.4
374363
speed = 0.5
375-
noise_filepath = common_utils.get_asset_path('whitenoise.wav')
376364
E = torchaudio.sox_effects.SoxEffectsChain()
377-
E.set_input_file(noise_filepath)
365+
E.set_input_file(self.noise_filepath)
378366
E.append_effect_to_chain("phaser", [gain_in, gain_out, delay_ms, decay, speed, "-s"])
379367
sox_output_waveform, sr = E.sox_build_flow_effects()
380368

381-
waveform, sample_rate = torchaudio.load(noise_filepath, normalization=True)
369+
waveform, sample_rate = torchaudio.load(self.noise_filepath, normalization=True)
382370
output_waveform = F.phaser(waveform, sample_rate, gain_in, gain_out, delay_ms, decay, speed, sinusoidal=True)
383371

384372
self.assertEqual(output_waveform, sox_output_waveform, atol=1e-4, rtol=1e-5)
@@ -392,13 +380,12 @@ def test_phaser_triangle(self):
392380
delay_ms = 2.0
393381
decay = 0.4
394382
speed = 0.5
395-
noise_filepath = common_utils.get_asset_path('whitenoise.wav')
396383
E = torchaudio.sox_effects.SoxEffectsChain()
397-
E.set_input_file(noise_filepath)
384+
E.set_input_file(self.noise_filepath)
398385
E.append_effect_to_chain("phaser", [gain_in, gain_out, delay_ms, decay, speed, "-t"])
399386
sox_output_waveform, sr = E.sox_build_flow_effects()
400387

401-
waveform, sample_rate = torchaudio.load(noise_filepath, normalization=True)
388+
waveform, sample_rate = torchaudio.load(self.noise_filepath, normalization=True)
402389
output_waveform = F.phaser(waveform, sample_rate, gain_in, gain_out, delay_ms, decay, speed, sinusoidal=False)
403390

404391
self.assertEqual(output_waveform, sox_output_waveform, atol=1e-4, rtol=1e-5)
@@ -413,13 +400,12 @@ def test_flanger_triangle_linear(self):
413400
width = 0.9
414401
speed = 0.5
415402
phase = 30
416-
noise_filepath = common_utils.get_asset_path('whitenoise.wav')
417403
E = torchaudio.sox_effects.SoxEffectsChain()
418-
E.set_input_file(noise_filepath)
404+
E.set_input_file(self.noise_filepath)
419405
E.append_effect_to_chain("flanger", [delay, depth, regen, width, speed, "triangle", phase, "linear"])
420406
sox_output_waveform, sr = E.sox_build_flow_effects()
421407

422-
waveform, sample_rate = torchaudio.load(noise_filepath, normalization=True)
408+
waveform, sample_rate = torchaudio.load(self.noise_filepath, normalization=True)
423409
output_waveform = F.flanger(waveform, sample_rate, delay, depth, regen, width, speed, phase,
424410
modulation='triangular', interpolation='linear')
425411

@@ -435,13 +421,12 @@ def test_flanger_triangle_quad(self):
435421
width = 0.4
436422
speed = 0.5
437423
phase = 40
438-
noise_filepath = common_utils.get_asset_path('whitenoise.wav')
439424
E = torchaudio.sox_effects.SoxEffectsChain()
440-
E.set_input_file(noise_filepath)
425+
E.set_input_file(self.noise_filepath)
441426
E.append_effect_to_chain("flanger", [delay, depth, regen, width, speed, "triangle", phase, "quadratic"])
442427
sox_output_waveform, sr = E.sox_build_flow_effects()
443428

444-
waveform, sample_rate = torchaudio.load(noise_filepath, normalization=True)
429+
waveform, sample_rate = torchaudio.load(self.noise_filepath, normalization=True)
445430
output_waveform = F.flanger(waveform, sample_rate, delay, depth, regen, width, speed, phase,
446431
modulation='triangular', interpolation='quadratic')
447432

@@ -457,13 +442,12 @@ def test_flanger_sine_linear(self):
457442
width = 0.23
458443
speed = 1.3
459444
phase = 60
460-
noise_filepath = common_utils.get_asset_path('whitenoise.wav')
461445
E = torchaudio.sox_effects.SoxEffectsChain()
462-
E.set_input_file(noise_filepath)
446+
E.set_input_file(self.noise_filepath)
463447
E.append_effect_to_chain("flanger", [delay, depth, regen, width, speed, "sine", phase, "linear"])
464448
sox_output_waveform, sr = E.sox_build_flow_effects()
465449

466-
waveform, sample_rate = torchaudio.load(noise_filepath, normalization=True)
450+
waveform, sample_rate = torchaudio.load(self.noise_filepath, normalization=True)
467451
output_waveform = F.flanger(waveform, sample_rate, delay, depth, regen, width, speed, phase,
468452
modulation='sinusoidal', interpolation='linear')
469453

@@ -479,13 +463,12 @@ def test_flanger_sine_quad(self):
479463
width = 0.23
480464
speed = 1.3
481465
phase = 25
482-
noise_filepath = common_utils.get_asset_path('whitenoise.wav')
483466
E = torchaudio.sox_effects.SoxEffectsChain()
484-
E.set_input_file(noise_filepath)
467+
E.set_input_file(self.noise_filepath)
485468
E.append_effect_to_chain("flanger", [delay, depth, regen, width, speed, "sine", phase, "quadratic"])
486469
sox_output_waveform, sr = E.sox_build_flow_effects()
487470

488-
waveform, sample_rate = torchaudio.load(noise_filepath, normalization=True)
471+
waveform, sample_rate = torchaudio.load(self.noise_filepath, normalization=True)
489472
output_waveform = F.flanger(waveform, sample_rate, delay, depth, regen, width, speed, phase,
490473
modulation='sinusoidal', interpolation='quadratic')
491474

@@ -500,19 +483,19 @@ def test_equalizer(self):
500483
q = 0.707
501484
gain = 1
502485

503-
noise_filepath = common_utils.get_asset_path('whitenoise.wav')
504486
E = torchaudio.sox_effects.SoxEffectsChain()
505-
E.set_input_file(noise_filepath)
487+
E.set_input_file(self.noise_filepath)
506488
E.append_effect_to_chain("equalizer", [center_freq, q, gain])
507489
sox_output_waveform, sr = E.sox_build_flow_effects()
508490

509-
waveform, sample_rate = torchaudio.load(noise_filepath, normalization=True)
491+
waveform, sample_rate = torchaudio.load(self.noise_filepath, normalization=True)
510492
output_waveform = F.equalizer_biquad(waveform, sample_rate, center_freq, gain, q)
511493

512494
self.assertEqual(output_waveform, sox_output_waveform, atol=1e-4, rtol=1e-5)
513495

514496
def test_perf_biquad_filtering(self):
515497

498+
# FIXME - misnamed, fn_sine does not match asset path
516499
fn_sine = common_utils.get_asset_path('whitenoise.wav')
517500

518501
b0 = 0.4

0 commit comments

Comments
 (0)