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_color_noise(self):
colors = naa.NoiseAug().model.COLOR_NOISES
for color in colors:
aug = naa.NoiseAug(color=color)
augmented_audio = aug.augment(self.audio)
self.assertFalse(np.array_equal(self.audio, augmented_audio))
self.assertTrue(len(self.audio), len(augmented_audio))
self.assertTrue(self.sampling_rate > 0)
def test_audio(self):
# https://freewavesamples.com/yamaha-v50-rock-beat-120-bpm
sample_wav_file = os.path.join(
os.environ.get("TEST_DIR"), 'res', 'audio', 'Yamaha-V50-Rock-Beat-120bpm.wav'
)
audio, sampling_rate = AudioLoader.load_audio(sample_wav_file)
flow = naf.Sequential([
naa.NoiseAug(),
naa.PitchAug(sampling_rate=sampling_rate, factor=(0.2, 1.5)),
naa.ShiftAug(sampling_rate=sampling_rate, duration=2),
naa.SpeedAug(factor=(1.5, 3))
])
augmented_audio = flow.augment(audio)
self.assertFalse(np.array_equal(audio, augmented_audio))
self.assertTrue(len(audio), len(augmented_audio))
def test_zone(self):
zone = (0, 1)
coverage = 1.
expected_aug_data_size = int(len(self.audio) * (zone[1] - zone[0]) * coverage)
# background noise
aug = naa.NoiseAug(zone=zone, noises=[self.noise], coverage=coverage)
aug.model.stateless = False
aug.augment(self.audio)
self.assertTrue(-1 <= len(aug.model.aug_data) - expected_aug_data_size <= 1)
# colored noise
aug = naa.NoiseAug(zone=zone, color='pink', coverage=coverage)
aug.model.stateless = False
aug.augment(self.audio)
self.assertTrue(-1 <= len(aug.model.aug_data) - expected_aug_data_size <= 1)
def test_coverage(self):
zone = (0.3, 0.7)
coverage = 0.1
expected_aug_data_size = int(len(self.audio) * (zone[1] - zone[0]) * coverage)
# background noise
aug = naa.NoiseAug(zone=zone, noises=[self.noise], coverage=coverage)
aug.model.stateless = False
aug.augment(self.audio)
self.assertTrue(-1 <= len(aug.model.aug_data) - expected_aug_data_size <= 1)
# colored noise
aug = naa.NoiseAug(zone=zone, color='pink', coverage=coverage)
aug.model.stateless = False
aug.augment(self.audio)
self.assertTrue(-1 <= len(aug.model.aug_data) - expected_aug_data_size <= 1)
def test_coverage(self):
zone = (0.3, 0.7)
coverage = 0.1
expected_aug_data_size = int(len(self.audio) * (zone[1] - zone[0]) * coverage)
# background noise
aug = naa.NoiseAug(zone=zone, noises=[self.noise], coverage=coverage)
aug.model.stateless = False
aug.augment(self.audio)
self.assertTrue(-1 <= len(aug.model.aug_data) - expected_aug_data_size <= 1)
# colored noise
aug = naa.NoiseAug(zone=zone, color='pink', coverage=coverage)
aug.model.stateless = False
aug.augment(self.audio)
self.assertTrue(-1 <= len(aug.model.aug_data) - expected_aug_data_size <= 1)
def test_background_noise(self):
# noise > audio
aug = naa.NoiseAug(noises=[self.noise])
augmented_audio = aug.augment(self.audio)
self.assertTrue(augmented_audio is not None)
# audio > noise
aug = naa.NoiseAug(noises=[self.audio])
augmented_audio = aug.augment(self.noise)
self.assertTrue(augmented_audio is not None)
def test_zone(self):
zone = (0, 1)
coverage = 1.
expected_aug_data_size = int(len(self.audio) * (zone[1] - zone[0]) * coverage)
# background noise
aug = naa.NoiseAug(zone=zone, noises=[self.noise], coverage=coverage)
aug.model.stateless = False
aug.augment(self.audio)
self.assertTrue(-1 <= len(aug.model.aug_data) - expected_aug_data_size <= 1)
# colored noise
aug = naa.NoiseAug(zone=zone, color='pink', coverage=coverage)
aug.model.stateless = False
aug.augment(self.audio)
self.assertTrue(-1 <= len(aug.model.aug_data) - expected_aug_data_size <= 1)
def process_audio_files_with_aug(filename, dirpath):
sr = 16000
audio_array, sample_rate = librosa.load(os.path.join(dirpath, 'flac', filename), sr=sr)
aug_crop = naa.CropAug(sampling_rate=sr)
audio_array_crop = aug_crop.augment(audio_array)
aug_loud = naa.LoudnessAug(loudness_factor=(2, 5))
audio_array_loud = aug_loud.augment(audio_array)
aug_noise = naa.NoiseAug(noise_factor=0.03)
audio_array_noise = aug_noise.augment(audio_array)
audio_array_list= [audio_array,audio_array_crop,audio_array_loud,
audio_array_noise ]
out_list = convert_audio_to_processed_list(audio_array_list, filename, dirpath)
return out_list