Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_biquad_delay(self):
si, _ = torchaudio.info(self.test_filepath)
E = torchaudio.sox_effects.SoxEffectsChain()
E.set_input_file(self.test_filepath)
E.append_effect_to_chain("biquad", ["0.25136437", "0.50272873", "0.25136437",
"1.0", "-0.17123075", "0.17668821"])
E.append_effect_to_chain("delay", ["15000s"])
x, _ = E.sox_build_flow_effects()
# check if effect worked
self.assertTrue(x.size(1) == (si.length / si.channels) + 15000)
def test_invalid_effect_name(self):
E = torchaudio.sox_effects.SoxEffectsChain()
E.set_input_file(self.test_filepath)
# there is no effect named "special"
with self.assertRaises(LookupError):
E.append_effect_to_chain("special", [""])
def test_gain(self):
E = torchaudio.sox_effects.SoxEffectsChain()
E.set_input_file(self.test_filepath)
E.append_effect_to_chain("gain", ["5"])
x, sr = E.sox_build_flow_effects()
E.clear_chain()
self.assertTrue(x.abs().max().item(), 1.)
E.set_input_file(self.test_filepath)
E.append_effect_to_chain("gain", ["-e", "-5"])
x, sr = E.sox_build_flow_effects()
E.clear_chain()
self.assertLess(x.abs().max().item(), 1.)
E.set_input_file(self.test_filepath)
E.append_effect_to_chain("gain", ["-b", "8"])
x, sr = E.sox_build_flow_effects()
E.clear_chain()
self.assertTrue(x.abs().max().item(), 1.)
E.set_input_file(self.test_filepath)
def test_band_chorus(self):
si_in, ei_in = torchaudio.info(self.test_filepath)
ei_in.encoding = torchaudio.get_sox_encoding_t(1)
E = torchaudio.sox_effects.SoxEffectsChain(out_encinfo=ei_in, out_siginfo=si_in)
E.set_input_file(self.test_filepath)
E.append_effect_to_chain("band", ["-n", "10k", "3.5k"])
E.append_effect_to_chain("chorus", [.5, .7, 55, 0.4, .25, 2, '-s'])
E.append_effect_to_chain("rate", [si_in.rate])
E.append_effect_to_chain("channels", [si_in.channels])
x, sr = E.sox_build_flow_effects()
# The chorus effect will make the output file longer than the input
self.assertEqual(x.size(0), si_in.channels)
self.assertGreaterEqual(x.size(1) * x.size(0), si_in.length)
def test_rate_channels(self):
target_rate = 16000
target_channels = 1
E = torchaudio.sox_effects.SoxEffectsChain()
E.set_input_file(self.test_filepath)
E.append_effect_to_chain("rate", [target_rate])
E.append_effect_to_chain("channels", [target_channels])
x, sr = E.sox_build_flow_effects()
# check if effects worked
self.assertEqual(sr, target_rate)
self.assertEqual(x.size(0), target_channels)
def test_unimplemented_effect(self):
E = torchaudio.sox_effects.SoxEffectsChain()
E.set_input_file(self.test_filepath)
# the sox spectrogram function is not implemented in torchaudio
with self.assertRaises(NotImplementedError):
E.append_effect_to_chain("spectrogram", [""])
def __init__(self):
self.asset_dirpath = os.path.join(self.test_dirpath, "assets")
sound_files = ["sinewave.wav", "steam-train-whistle-daniel_simon.mp3"]
self.data = [os.path.join(self.asset_dirpath, fn) for fn in sound_files]
self.si, self.ei = torchaudio.info(os.path.join(self.asset_dirpath, "sinewave.wav"))
self.si.precision = 16
self.E = torchaudio.sox_effects.SoxEffectsChain()
self.E.append_effect_to_chain("rate", [self.si.rate]) # resample to 16000hz
self.E.append_effect_to_chain("channels", [self.si.channels]) # mono signal
self.E.append_effect_to_chain("trim", [0, "16000s"]) # first 16000 samples of audio
def test_lowpass_speed(self):
speed = .8
si, _ = torchaudio.info(self.test_filepath)
E = torchaudio.sox_effects.SoxEffectsChain()
E.set_input_file(self.test_filepath)
E.append_effect_to_chain("lowpass", 100)
E.append_effect_to_chain("speed", speed)
E.append_effect_to_chain("rate", si.rate)
x, sr = E.sox_build_flow_effects()
# check if effects worked
self.assertEqual(x.size(1), int((si.length / si.channels) / speed))
def read_audio(fp, trim_silence=False):
if trim_silence:
E = torchaudio.sox_effects.SoxEffectsChain()
E.set_input_file(fp)
E.append_effect_to_chain("silence", [1, 100, 1])
E.append_effect_to_chain("reverse")
E.append_effect_to_chain("silence", [1, 100, 1])
E.append_effect_to_chain("reverse")
sig, sample_rate = E.sox_build_flow_effects()
else:
sig, sample_rate = torchaudio.load(fp)
sig = sig.contiguous()
return sig, sample_rate
def read_audio(fp, downsample=True, trim_silence=True):
if downsample or trim_silence:
E = torchaudio.sox_effects.SoxEffectsChain()
E.set_input_file(fp)
if trim_silence:
E.append_effect_to_chain("silence", [1, 100, 1])
E.append_effect_to_chain("reverse")
E.append_effect_to_chain("silence", [1, 100, 1])
E.append_effect_to_chain("reverse")
if downsample:
E.append_effect_to_chain("gain", ["-h"])
E.append_effect_to_chain("channels", [1])
E.append_effect_to_chain("rate", [16000])
E.append_effect_to_chain("gain", ["-rh"])
E.append_effect_to_chain("dither", ["-s"])
sig, sr = E.sox_build_flow_effects()