예제 #1
0
    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)
예제 #2
0
            axes[1, n].pcolormesh(masks[n].detach().cpu().numpy().squeeze())
        for n, output in enumerate(Y_hat):
            axes[2, n].pcolormesh(output.squeeze())
#    elif type(model) == BaselineModel:
#        # all we can plot are intermediate outputs
    else:
        raise ValueError('Cannot plot for that type of model')

    for n, Yn in enumerate(Y_hat):
        if args.use_log:
            Yn = np.exp(Yn.squeeze())
        else:
            Yn = Yn.squeeze()
        y_hat = inv_spectrogram(Yn,
                                phase,
                                fft_length=int(window_size * 1e-3 * 16000),
                                sample_rate=16000,
                                hop_length=int(step_size * 1e-3 * 16000))

        y_hat = postprocess(y_hat, 16000, step_size)

        conddir = os.path.join(savedir, os.path.split(os.path.dirname(f))[-1])
        if not os.path.isdir(conddir):
            os.mkdir(conddir)
        wavwrite(
            os.path.join(conddir,
                         os.path.basename(f)[:-4] + '_{}.wav'.format(n)),
            y_hat, 16000)
        fig.savefig(os.path.join(conddir, os.path.basename(f)[:-3] + 'png'))
        plt.close()