def test_audio_mixed_with_snr(self): mixer = AudioMixer(base_audio=self.audio1, sampling_rate=8000) mixer.add_to_mix(self.audio2, snr=10, offset=0) unmixed = mixer.unmixed_audio assert unmixed.shape == (2, 8000) assert (unmixed[0, :] == 1).all() np.testing.assert_almost_equal(unmixed[1, :], 0.31622776) assert unmixed.dtype == np.float32 mixed = mixer.mixed_audio assert mixed.shape == (1, 8000) np.testing.assert_almost_equal(mixed[0, :], 1.31622776) assert mixed.dtype == np.float32
def test_simple_mix(self): mixer = AudioMixer(base_audio=self.audio1, sampling_rate=8000) mixer.add_to_mix(self.audio2, snr=None, offset=0) unmixed = mixer.unmixed_audio assert unmixed.shape == (2, 8000) assert (unmixed[0, :] == 1).all() assert (unmixed[1, :] == 2).all() assert unmixed.dtype == np.float32 mixed = mixer.mixed_audio assert mixed.shape == (1, 8000) assert (mixed == 3).all() assert mixed.dtype == np.float32
def test_audio_mixer_handles_empty_array(self): # Treat it more like a test of "it runs" rather than "it works" sr = 16000 t = np.linspace(0, 1, sr, dtype=np.float32) x1 = np.sin(440.0 * t).reshape(1, -1) mixer = AudioMixer( base_audio=x1, sampling_rate=sr, ) mixer.add_to_mix(np.array([])) xmix = mixer.mixed_audio np.testing.assert_equal(xmix, x1)
def test_simple_mix(self): mixer = AudioMixer(base_audio=self.audio1, sampling_rate=8000) mixer.add_to_mix(self.audio2, snr=None, offset=0) unmixed = mixer.unmixed_audio assert len(unmixed) == 2 assert all(u.shape == (1, 8000) for u in unmixed) assert (unmixed[0] == 1).all() assert (unmixed[1] == 2).all() assert all(u.dtype == np.float32 for u in unmixed) mixed = mixer.mixed_audio assert mixed.shape == (1, 8000) assert (mixed == 3).all() assert mixed.dtype == np.float32
def test_audio_mixer_handles_empty_array_with_offset(self): # Treat it more like a test of "it runs" rather than "it works" sr = 16000 t = np.linspace(0, 1, sr, dtype=np.float32) x1 = np.sin(440.0 * t).reshape(1, -1) mixer = AudioMixer( base_audio=x1, sampling_rate=sr, ) mixer.add_to_mix(np.array([]), offset=0.5) xmix = mixer.mixed_audio # 0s - 1s: identical np.testing.assert_equal(xmix[:sr], x1) # 1s - 1.5s: padding np.testing.assert_equal(xmix[sr:], 0)
def test_audio_mixed_with_offset_and_snr(self): mixer = AudioMixer(base_audio=self.audio1, sampling_rate=8000) mixer.add_to_mix(self.audio2, snr=10, offset=0.5) unmixed = mixer.unmixed_audio assert unmixed.shape == (2, 12000) # offset 0.5s == 4000 samples assert (unmixed[0, :8000] == 1).all() assert (unmixed[0, 8000:] == 0).all() assert (unmixed[1, :4000] == 0).all() np.testing.assert_almost_equal(unmixed[1, 4000:], 0.31622776) assert unmixed.dtype == np.float32 mixed = mixer.mixed_audio assert mixed.shape == (1, 12000) assert (mixed[0, :4000] == 1).all() np.testing.assert_almost_equal(mixed[0, 4000:8000], 1.31622776) np.testing.assert_almost_equal(mixed[0, 8000:], 0.31622776) assert mixed.dtype == np.float32
def test_audio_mixed_with_offset(self): mixer = AudioMixer(base_audio=self.audio1, sampling_rate=8000) mixer.add_to_mix(self.audio2, snr=None, offset=0.5) unmixed = mixer.unmixed_audio assert unmixed.shape == (2, 12000) # offset 0.5s == 4000 samples assert (unmixed[0, :8000] == 1).all() assert (unmixed[0, 8000:] == 0).all() assert (unmixed[1, :4000] == 0).all() assert (unmixed[1, 4000:] == 2).all() assert unmixed.dtype == np.float32 mixed = mixer.mixed_audio assert mixed.shape == (1, 12000) assert (mixed[0, :4000] == 1).all() assert (mixed[0, 4000:8000] == 3).all() assert (mixed[0, 8000:] == 2).all() assert mixed.dtype == np.float32
def test_audio_mixed_with_offset_and_snr(self): mixer = AudioMixer(base_audio=self.audio1, sampling_rate=8000) mixer.add_to_mix(self.audio2, snr=10, offset=0.5) unmixed = mixer.unmixed_audio assert len(unmixed) == 2 assert all(u.shape == (1, 12000) for u in unmixed) assert (unmixed[0][:, :8000] == 1).all() assert (unmixed[0][:, 8000:] == 0).all() assert (unmixed[1][:, :4000] == 0).all() np.testing.assert_almost_equal(unmixed[1][:, 4000:], 0.31622776) assert all(u.dtype == np.float32 for u in unmixed) mixed = mixer.mixed_audio assert mixed.shape == (1, 12000) assert (mixed[0, :4000] == 1).all() np.testing.assert_almost_equal(mixed[0, 4000:8000], 1.31622776) np.testing.assert_almost_equal(mixed[0, 8000:], 0.31622776) assert mixed.dtype == np.float32
def test_audio_mixed_with_offset(self): mixer = AudioMixer(base_audio=self.audio1, sampling_rate=8000) mixer.add_to_mix(self.audio2, snr=None, offset=0.5) unmixed = mixer.unmixed_audio assert len(unmixed) == 2 assert all(u.shape == (1, 12000) for u in unmixed) assert (unmixed[0][:, :8000] == 1).all() assert (unmixed[0][:, 8000:] == 0).all() assert (unmixed[1][:, :4000] == 0).all() assert (unmixed[1][:, 4000:] == 2).all() assert all(u.dtype == np.float32 for u in unmixed) mixed = mixer.mixed_audio assert mixed.shape == (1, 12000) assert (mixed[0, :4000] == 1).all() assert (mixed[0, 4000:8000] == 3).all() assert (mixed[0, 8000:] == 2).all() assert mixed.dtype == np.float32