def test_padding_after_resize(audio): "Padding is added to the end but not the beginning" new_duration = (audio.duration + 1) * 1000 cropsig_pad_after = ResizeSignal(new_duration, pad_mode=AudioPadType.Zeros_After) # generate a random input signal that is 3s long inp, out = apply_transform(cropsig_pad_after, audio) # test end of signal is padded with zeros _test_eq(out[:, -10:], torch.zeros_like(out)[:, -10:]) # test front of signal is not padded with zeros _test_ne(out[:, 0:10], out[:, -10:])
def test_resize_signal_repeat(audio): """ Test pad_mode repeat by making sure that columns are equal at the appropriate offsets """ dur = audio.duration * 1000 repeat = 3 cropsig_repeat = ResizeSignal(dur * repeat, pad_mode=AudioPadType.Repeat) inp, out = apply_transform(cropsig_repeat, audio) for i in range(repeat): s = int(i * inp.nsamples) e = int(s + inp.nsamples) _test_eq(out[:, s:e], inp)
def test_crop_time_with_pipeline(ex_files): """ AudioToSpec->CropTime and ResizeSignal->AudioToSpec will result in same size images """ oa = OpenAudio(ex_files) crop_dur = random.randint(1000, 5000) DBMelSpec = SpectrogramTransformer(mel=True, to_db=True) pipe_cropsig = Pipeline( [oa, DBMelSpec(hop_length=128), CropTime(crop_dur)]) pipe_cropspec = Pipeline([ oa, ResizeSignal(crop_dur), DBMelSpec(hop_length=128), ]) for i in range(4): _test_eq(pipe_cropsig(i).width, pipe_cropspec(i).width)
def test_cropping(): "Can use the ResizeSignal Transform" audio = test_audio_tensor(seconds=10, sr=1000) inp, out1000 = apply_transform(ResizeSignal(1000), audio.clone()) inp, out2000 = apply_transform(ResizeSignal(2000), audio.clone()) inp, out5000 = apply_transform(ResizeSignal(5000), audio.clone()) _test_eq(out1000.duration, 1) _test_eq(out2000.duration, 2) _test_eq(out5000.duration, 5) _test_eq(out1000.nsamples, out1000.duration * inp.sr) _test_eq(out2000.nsamples, out2000.duration * inp.sr) _test_eq(out5000.nsamples, out5000.duration * inp.sr) # Multi Channel Cropping inp, mc1000 = apply_transform(ResizeSignal(1000), audio.clone()) inp, mc2000 = apply_transform(ResizeSignal(2000), audio.clone()) inp, mc5000 = apply_transform(ResizeSignal(5000), audio.clone()) _test_eq(mc1000.duration, 1) _test_eq(mc2000.duration, 2) _test_eq(mc5000.duration, 5)
def test_fail_invalid_pad_mode(): with pytest.raises(ValueError): ResizeSignal(12000, pad_mode="tenchify")
def test_resize_same_duration(audio): "Asking to resize to the duration should return the audio back" resize = ResizeSignal(audio.duration * 1000) inp, out = apply_transform(resize, audio) _test_eq(inp, out)
def test_padding_both_side_resize(audio): "Make sure they are padding on both sides" new_duration = (audio.duration + 1) * 1000 cropsig_pad_after = ResizeSignal(new_duration) inp, out = apply_transform(cropsig_pad_after, audio) _test_eq(out[:, 0:2], out[:, -2:])
def test_fail_invalid_pad_mode(): _test_fail(ResizeSignal(12000, pad_mode="tenchify"))