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)
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]') plt.show()