Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def setUpClass(cls):
env_config_path = os.path.abspath(os.path.join(
os.path.dirname(__file__), '..', '..', '..', '.env'))
load_dotenv(env_config_path)
# https://freewavesamples.com/yamaha-v50-rock-beat-120-bpm
cls.sample_wav_file = os.path.join(
os.environ.get("TEST_DIR"), 'res', 'audio', 'Yamaha-V50-Rock-Beat-120bpm.wav'
)
cls.audio, cls.sampling_rate = AudioLoader.load_audio(cls.sample_wav_file)
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 setUpClass(cls):
env_config_path = os.path.abspath(os.path.join(
os.path.dirname(__file__), '..', '..', '..', '.env'))
load_dotenv(env_config_path)
# https://freewavesamples.com/yamaha-v50-rock-beat-120-bpm
cls.sample_wav_file = os.path.join(
os.environ.get("TEST_DIR"), 'res', 'audio', 'Yamaha-V50-Rock-Beat-120bpm.wav'
)
# https://en.wikipedia.org/wiki/Colors_of_noise
cls.noise_wav_file = os.path.join(
os.environ.get("TEST_DIR"), 'res', 'audio', 'Pink_noise.ogg'
)
cls.audio, cls.sampling_rate = AudioLoader.load_audio(cls.sample_wav_file)
cls.noise, cls.noise_sampling_rate = AudioLoader.load_audio(cls.noise_wav_file)
def setUpClass(cls):
env_config_path = os.path.abspath(os.path.join(
os.path.dirname(__file__), '..', '..', '..', '.env'))
load_dotenv(env_config_path)
# https://freewavesamples.com/yamaha-v50-rock-beat-120-bpm
cls.sample_wav_file = os.path.join(
os.environ.get("TEST_DIR"), 'res', 'audio', 'Yamaha-V50-Rock-Beat-120bpm.wav'
)
cls.audio, cls.sampling_rate = AudioLoader.load_audio(cls.sample_wav_file)
def setUpClass(cls):
env_config_path = os.path.abspath(os.path.join(
os.path.dirname(__file__), '..', '..', '..', '.env'))
load_dotenv(env_config_path)
# https://freewavesamples.com/yamaha-v50-rock-beat-120-bpm
cls.sample_wav_file = os.path.join(
os.environ.get("TEST_DIR"), 'res', 'audio', 'Yamaha-V50-Rock-Beat-120bpm.wav'
)
cls.audio, cls.sampling_rate = AudioLoader.load_audio(cls.sample_wav_file)
def test_substitute(self):
audio, sampling_rate = AudioLoader.load_audio(self.sample_wav_file)
aug = naa.ShiftAug(sampling_rate, duration=0.5)
augmented_audio = aug.augment(audio)
self.assertFalse(np.array_equal(audio, augmented_audio))
self.assertTrue(len(audio), len(augmented_audio))
def setUpClass(cls):
env_config_path = os.path.abspath(os.path.join(
os.path.dirname(__file__), '..', '..', '..', '.env'))
load_dotenv(env_config_path)
# https://freewavesamples.com/yamaha-v50-rock-beat-120-bpm
cls.sample_wav_file = os.path.join(
os.environ.get("TEST_DIR"), 'res', 'audio', 'Yamaha-V50-Rock-Beat-120bpm.wav'
)
cls.audio, cls.sampling_rate = AudioLoader.load_audio(cls.sample_wav_file)
def test_substitute(self):
time_mask_para = 80
mel_spectrogram = AudioLoader.load_mel_spectrogram(self.sample_wav_file, n_mels=self.num_of_freq_channel)
aug = TimeMaskingAug(mask_factor=time_mask_para)
augmented_mel_spectrogram = aug.augment(mel_spectrogram)
self.assertEqual(len(mel_spectrogram[:, aug.model.t0]), np.count_nonzero(mel_spectrogram[:, aug.model.t0]))
self.assertEqual(0, np.count_nonzero(augmented_mel_spectrogram[:, aug.model.t0]))
def test_spectrogram(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'
)
mel_spectrogram = AudioLoader.load_mel_spectrogram(sample_wav_file, n_mels=128)
flow = naf.Sequential([
nas.FrequencyMaskingAug(mask_factor=50),
nas.TimeMaskingAug(mask_factor=20),
nas.TimeMaskingAug(mask_factor=30)])
augmented_mel_spectrogram = flow.augment(mel_spectrogram)
for aug in flow:
if aug.name == 'FrequencyMasking_Aug':
self.assertEqual(len(mel_spectrogram[aug.model.f0]), np.count_nonzero(mel_spectrogram[aug.model.f0]))
self.assertEqual(0, np.count_nonzero(augmented_mel_spectrogram[aug.model.f0]))
elif aug.name == 'TimeMasking_Aug':
self.assertEqual(len(mel_spectrogram[:, aug.model.t0]),
np.count_nonzero(mel_spectrogram[:, aug.model.t0]))
self.assertEqual(0, np.count_nonzero(augmented_mel_spectrogram[:, aug.model.t0]))
def test_multi_thread(self):
mel_spectrogram = AudioLoader.load_mel_spectrogram(self.sample_wav_file, n_mels=128)
n = 3
augs = [
nas.FrequencyMaskingAug(mask_factor=80),
nas.TimeMaskingAug(mask_factor=80)
]
for num_thread in [1, 3]:
for aug in augs:
augmented_data = aug.augment(mel_spectrogram, n=n, num_thread=num_thread)
self.assertEqual(len(augmented_data), n)