def test_add_noise_rms(): x, fs = wavread('tests/sp10.wav') n, _ = wavread('tests/ssn.wav') y, n_scaled = add_noise(x, n, fs, 5.0) snr = rms_energy(x) - rms_energy(n_scaled) assert np.isclose(snr, 5.0)
def test_add_noise_p56(): x, fs = wavread('tests/sp10.wav') n, _ = wavread('tests/ssn.wav') y, n_scaled = add_noise(x, n, fs, 5.0, speech_energy='P.56') snr = asl_meter(x, fs) - rms_energy(n_scaled) assert np.isclose(snr, 5.0)
def test_add_reverb(): x, fs = wavread('tests/sp10.wav') r, _ = wavread('tests/rir.wav') y = add_reverb(x, r, fs, speech_energy='P.56') y_ref, _ = wavread('tests/sp10_reverb_ref.wav') assert np.allclose(y, y_ref, atol=2e-3)
def generate_condition(self, snrs, noise, output_dir, reverb=None, files_per_condition=None): if noise not in self.noise.keys(): raise ValueError('noise not in dataset') if type(snrs) is not list: snrs = [snrs] n, nfs = wavread(self.noise[noise]) if reverb is not None: r, rfs = wavread(self.reverb[reverb]) condition_name = '{}_{}'.format(reverb, noise) else: condition_name = noise if not os.path.isdir(output_dir): os.mkdir(output_dir) # FIXME: avoid overwriting an existing folder? try: for snr in snrs: os.mkdir( os.path.join(output_dir, '{}_{}dB'.format(condition_name, snr))) except OSError: print('Condition folder already exists!') for snr in snrs: if files_per_condition is not None: speech_files = np.random.choice(self.speech, files_per_condition, replace=False).tolist() else: speech_files = self.speech for f in tqdm(speech_files, desc='{}dB'.format(snr)): x, fs = wavread(f) if fs != nfs: raise ValueError( 'Speech file and noise file have different fs!') if reverb is not None: if fs != rfs: raise ValueError( 'Speech file and reverb file have different fs!') x = add_reverb(x, r, fs, speech_energy=self.speech_energy) y = add_noise(x, n, fs, snr, speech_energy=self.speech_energy)[0] wavwrite( os.path.join(output_dir, '{}_{}dB'.format(condition_name, snr), os.path.basename(f)), y, fs)
def read_audio(filename, scipy_=True, normalization=True): if scipy_: audio, sample_rate = wavread(filename) audio = audio.astype('float32') if audio.ndim >= 2: audio = np.mean(audio, 1) else: with soundfile.SoundFile(filename) as sound_file: audio = sound_file.read(dtype='float32') sample_rate = sound_file.samplerate if audio.ndim >= 2: audio = np.mean(audio, 1) if normalization: audio = normalize(audio, sample_rate) return audio, sample_rate
from scipy.fftpack import fft, ifft from scipy import signal from maracas.utils import wavread from maracas import add_reverb import numpy as np import matplotlib.pyplot as plt x, fs = wavread('sp10.wav') assert np.allclose(fft(ifft(x)), x, atol=1e-12) f, t, Sxx = signal.spectrogram(x, fs) print('Shapes: {} and {}'.format(x.shape, (f.shape, t.shape, Sxx.shape))) plt.pcolormesh(t, f, Sxx) plt.ylabel('Frequency [Hz]') plt.xlabel('Time [sec]') plt.show() r, _ = wavread('rir.wav') n_x = add_reverb(x, r, fs, speech_energy='P.56') f, t, Sxx = signal.spectrogram(n_x, fs) print('Shapes: {} and {}'.format(x.shape, (f.shape, t.shape, Sxx.shape))) plt.pcolormesh(t, f, Sxx) plt.ylabel('Frequency [Hz]') plt.xlabel('Time [sec]')
def test_asl_meter(): x, fs = wavread('tests/sp10.wav') assert np.isclose(asl_meter(x, fs), -25.631381743520010)