def test_encode_large_ogg(self): sr = SR11025() synth = SilenceSynthesizer(sr) samples = synth.synthesize(Seconds(190)) raw = samples.encode(fmt='OGG', subtype='VORBIS') # prior to this test, the line above caused a segfault, so the assertion # below is fairly worthless, and mostly a formality self.assertIsNotNone(raw)
def test_can_take_fft_of_2d_stacked_signal(self): samples = SilenceSynthesizer(SR22050()).synthesize(Milliseconds(2500)) windowsize = TimeSlice(duration=Milliseconds(200)) stepsize = TimeSlice(duration=Milliseconds(100)) _, windowed = samples.sliding_window_with_leftovers( windowsize=windowsize, stepsize=stepsize, dopad=True) coeffs = fft(windowed) self.assertIsInstance(coeffs, ArrayWithUnits) self.assertEqual(2, len(coeffs.dimensions)) self.assertEqual(windowed.dimensions[0], coeffs.dimensions[0]) self.assertIsInstance(coeffs.dimensions[1], FrequencyDimension)
def test_can_do_multithreaded_resampling(self): synth = SilenceSynthesizer(SR44100()) audio = [synth.synthesize(Seconds(5)) for _ in xrange(10)] pool = ThreadPool(4) def x(samples): rs = Resample(int(SR44100()), int(SR11025())) return rs(samples, end_of_input=True) resampled = pool.map(x, audio) self.assertEqual(10, len(resampled))
def test_can_do_multithreaded_resampling(self): synth = SilenceSynthesizer(SR44100()) audio = [synth.synthesize(Seconds(5)) for _ in range(10)] pool = ThreadPool(4) def x(samples): rs = Resample(int(SR44100()), int(SR11025())) return rs(samples, end_of_input=True) resampled = pool.map(x, audio) self.assertEqual(10, len(resampled))
def test_can_pad_for_better_frequency_resolution(self): samples = SilenceSynthesizer(SR22050()).synthesize(Milliseconds(2500)) windowsize = TimeSlice(duration=Milliseconds(200)) stepsize = TimeSlice(duration=Milliseconds(100)) _, windowed = samples.sliding_window_with_leftovers( windowsize=windowsize, stepsize=stepsize, dopad=True) coeffs = fft(windowed, padding_samples=1024) self.assertIsInstance(coeffs, ArrayWithUnits) self.assertEqual(2, len(coeffs.dimensions)) self.assertEqual(windowed.dimensions[0], coeffs.dimensions[0]) self.assertIsInstance(coeffs.dimensions[1], FrequencyDimension) expected_size = ((windowed.shape[-1] + 1024) // 2) + 1 self.assertEqual(expected_size, coeffs.shape[-1])
def test_can_decompose(self): sr = SR22050() samples = SilenceSynthesizer(sr).synthesize(Milliseconds(9999)) wscheme = sr.windowing_scheme(8192, 4096) duration = TimeSlice(wscheme.duration) frequency = TimeSlice(wscheme.frequency) _, windowed = samples.sliding_window_with_leftovers(duration, frequency, dopad=True) fa = frequency_decomposition(windowed, [32, 64, 128, 256, 512, 1024, 2048, 4096]) self.assertEqual(windowed.dimensions[0], fa.dimensions[0]) self.assertIsInstance(fa.dimensions[1], ExplicitFrequencyDimension)
def test_can_decompose(self): sr = SR22050() samples = SilenceSynthesizer(sr).synthesize(Milliseconds(9999)) wscheme = sr.windowing_scheme(8192, 4096) duration = TimeSlice(wscheme.duration) frequency = TimeSlice(wscheme.frequency) _, windowed = samples.sliding_window_with_leftovers( duration, frequency, dopad=True) fa = frequency_decomposition( windowed, [32, 64, 128, 256, 512, 1024, 2048, 4096]) self.assertEqual(windowed.dimensions[0], fa.dimensions[0]) self.assertIsInstance( fa.dimensions[1], ExplicitFrequencyDimension)
def test_can_contract_audio_samples(self): sr = SR22050() samples = SilenceSynthesizer(sr).synthesize(Milliseconds(1000)) print('First', samples.shape, samples.dimensions) stretched = time_stretch(samples, 2.0).squeeze() print('Second', stretched.shape, stretched.dimensions) self.assertEqual(len(samples) // 2, len(stretched))
def test_stft_raises_for_invalid_dimensions(self): sr = SR22050() samples = SilenceSynthesizer(sr).synthesize(Milliseconds(6666)) wscheme = sr.windowing_scheme(512, 256) tf = stft(samples, wscheme, HanningWindowingFunc()) self.assertRaises(ValueError, lambda: stft(tf, wscheme, HanningWindowingFunc()))
def test_discrete_samples_multiple_dimensions(self): sr = SR22050() samples = SilenceSynthesizer(sr).synthesize(Milliseconds(6666)) stacked = ArrayWithUnits( np.zeros((10, ) + samples.shape, dtype=samples.dtype), (IdentityDimension(), ) + samples.dimensions) stacked[:] = samples self.assertEqual((512, 1024), HalfLapped().discrete_samples(stacked))
def test_has_correct_shape(self): sr = SR22050() samples = SilenceSynthesizer(sr).synthesize(Milliseconds(9999)) wscheme = sr.windowing_scheme(256, 128) scale = GeometricScale(50, sr.nyquist, 0.4, 32) scale.ensure_overlap_ratio() tf = stft(samples, wscheme, HanningWindowingFunc()) geom = apply_scale(tf, scale, window=HanningWindowingFunc()) self.assertEqual(tf.shape[:-1] + (len(scale), ), geom.shape)
def test_can_stretch_audio_batch(self): sr = SR22050() samples = SilenceSynthesizer(sr).synthesize(Milliseconds(6666)) stacked = ArrayWithUnits( np.zeros((10, ) + samples.shape, dtype=samples.dtype), (IdentityDimension(), ) + samples.dimensions) stacked[:] = samples stretched = time_stretch(stacked, 2.0) self.assertEqual(10, stretched.shape[0]) self.assertEqual(int(len(samples) // 2), stretched.shape[1])
def test_has_correct_dimensions(self): sr = SR22050() samples = SilenceSynthesizer(sr).synthesize(Milliseconds(6666)) wscheme = sr.windowing_scheme(512, 256) tf = stft(samples, wscheme, HanningWindowingFunc()) self.assertIsInstance(tf, ArrayWithUnits) self.assertEqual(2, len(tf.dimensions)) self.assertIsInstance(tf.dimensions[0], TimeDimension) self.assertEqual(tf.dimensions[0].samplerate, wscheme) self.assertIsInstance(tf.dimensions[1], FrequencyDimension) self.assertIsInstance(tf.dimensions[1].scale, LinearScale)
def test_has_correct_dimensions(self): sr = SR22050() samples = SilenceSynthesizer(sr).synthesize(Milliseconds(9999)) wscheme = sr.windowing_scheme(256, 128) scale = GeometricScale(50, sr.nyquist, 0.4, 32) scale.ensure_overlap_ratio() tf = stft(samples, wscheme, HanningWindowingFunc()) geom = apply_scale(tf, scale, window=HanningWindowingFunc()) self.assertIsInstance(geom, ArrayWithUnits) self.assertEqual(2, len(geom.dimensions)) self.assertIsInstance(geom.dimensions[0], TimeDimension) self.assertEqual(geom.dimensions[0].samplerate, wscheme) self.assertIsInstance(geom.dimensions[1], FrequencyDimension) self.assertEqual(scale, geom.dimensions[1].scale)
def test_can_take_stft_of_batch(self): sr = SR22050() samples = SilenceSynthesizer(sr).synthesize(Milliseconds(6666)) stacked = ArrayWithUnits( np.zeros((10, ) + samples.shape, dtype=samples.dtype), (IdentityDimension(), ) + samples.dimensions) stacked[:] = samples wscheme = sr.windowing_scheme(512, 256) tf = stft(stacked, wscheme, HanningWindowingFunc()) self.assertEqual(10, len(tf)) self.assertIsInstance(tf, ArrayWithUnits) self.assertEqual(3, len(tf.dimensions)) self.assertIsInstance(tf.dimensions[0], IdentityDimension) self.assertIsInstance(tf.dimensions[1], TimeDimension) self.assertEqual(tf.dimensions[1].samplerate, wscheme) self.assertIsInstance(tf.dimensions[2], FrequencyDimension) self.assertIsInstance(tf.dimensions[2].scale, LinearScale)
def test_can_contract_audio_samples(self): sr = SR22050() samples = SilenceSynthesizer(sr).synthesize(Milliseconds(1000)) stretched = time_stretch(samples, 2.0).squeeze() self.assertEqual(len(samples) // 2, len(stretched))
def test_correct_output_with_stereo(self): synth = SilenceSynthesizer(SR44100()) samples = synth.synthesize(Seconds(1)).stereo rs = Resample(int(samples.samplerate), int(SR11025()), nchannels=2) resampled = rs(samples, end_of_input=True) self.assertEqual((11025, 2), resampled.shape)
def test_can_take_fft_of_1d_signal(self): samples = SilenceSynthesizer(SR22050()).synthesize(Milliseconds(2500)) coeffs = fft(samples) self.assertIsInstance(coeffs, ArrayWithUnits) self.assertEqual(1, len(coeffs.dimensions)) self.assertIsInstance(coeffs.dimensions[0], FrequencyDimension)
def test_has_correct_number_of_bins(self): sr = SR22050() samples = SilenceSynthesizer(sr).synthesize(Milliseconds(6666)) wscheme = sr.windowing_scheme(512, 256) tf = stft(samples, wscheme, HanningWindowingFunc()) self.assertEqual(tf.shape[1], 257)
def test_can_stretch_audio_samples(self): sr = SR22050() samples = SilenceSynthesizer(sr).synthesize(Milliseconds(1000)) stretched = time_stretch(samples, 0.5).squeeze() self.assertEqual(int(2 * len(samples)), len(stretched))