def pyts_mod(sound, octaves, x_length): s_ap = np.array([[0, x_length / 2, x_length], [0, x_length, x_length * 1.5]]) x_s_fixed = tsm.wsola(sound, octaves) x_s_ap = tsm.wsola(sound, s_ap) x_s_ap = x_s_ap.T x_s_fixed =x_s_fixed.T return x_s_fixed, x_s_aps
def test_wsola_multichannel(n_chan, transpose): x, _ = sf.read('tests/data/dogbeetle.wav') x_wsola = tsm.wsola(x, 1.3) x_multi = np.tile(x, (n_chan, 1)) if transpose: x_multi = x_multi.T x_multi_wsola = tsm.wsola(x_multi, 1.3) for i in range(x_multi_wsola.shape[0]): assert np.allclose(x_wsola, x_multi_wsola[i, :])
def test_console_wsola(alpha, win_type, win_size, syn_hop_size, tolerance): test_file = 'tests/data/castanetsviolin.wav' x, sr = sf.read(test_file) y = wsola(x, alpha, win_type=win_type, win_size=win_size, syn_hop_size=syn_hop_size, tolerance=tolerance) cmd = [ 'python', 'pytsmod/console/console.py', 'wsola', test_file, 'temp_cli.wav', str(alpha), '-wt', win_type, '-ws', str(win_size), '-sh', str(syn_hop_size), '-t', str(tolerance) ] call(cmd) sf.write('temp.wav', y, sr) y_, _ = sf.read('temp.wav') y_cli, _ = sf.read('temp_cli.wav') os.remove('temp.wav') os.remove('temp_cli.wav') assert np.allclose(y_, y_cli)
def __getitem__(self, key): key, pitch_aug_factor, time_aug_factor = key wav = self.data[key] if self.normalize: # soundfile.read normalizes data to [-1,1] if dtype is not given array, rate = soundfile.read(wav, always_2d=self.always_2d) else: array, rate = soundfile.read(wav, dtype=self.dtype, always_2d=self.always_2d) if pitch_aug_factor != 0: # Pitch augmentation ratio = pow(2, 1 / 12) import pyworld as pw f0_pw, sp, ap = pw.wav2world(array, rate) # use default options array = pw.synthesize( f0_pw * (ratio**pitch_aug_factor), sp, ap, rate, pw.default_frame_period, ) if time_aug_factor != 1: # Time augmentation array = tsm.wsola(array, time_aug_factor) return rate, array
def time_stretching(wav_file, wav_file_new, target_duration, method=2): data, sample_rate = sf.read(wav_file) if target_duration > 0: if ~np.isnan(sample_rate): data = data.T current_duration = np.size(data, axis=0) / sample_rate N = target_duration / current_duration if method == 1: data_new = tsm.ola(data, N) wavfile.write(wav_file_new, sample_rate, data_new) return 1 elif method == 2: data_new = tsm.wsola(data, N) wavfile.write(wav_file_new, sample_rate, data_new) return 1 elif method == 3: data_new = tsm.phase_vocoder(data, N) wavfile.write(wav_file_new, sample_rate, data_new) return 1 else: return -2 else: return 0 else: return -1
def test_wsola_nonlinear(win_type, win_size, syn_hop_size, tolerance): x, _ = sf.read('tests/data/beethovenorchestra.wav') ap = loadmat('tests/data/anchorpoints.mat')['anchorpoints'].T - 1 wsola_nonlinear = loadmat('tests/data/wsola_nonlinear.mat') y = tsm.wsola(x, ap, win_type, win_size, syn_hop_size, tolerance) sample_name = f'orc_{win_type}_{win_size}_{syn_hop_size}_{tolerance}' y_matlab = wsola_nonlinear[sample_name].squeeze() assert np.allclose(y, y_matlab)
def augment(self, audio, fs, verbose=False): """Augments audio by adding random noise, shift and stretch ratio. """ random_noise_var_coeff = np.random.uniform(self.augmentation['noise_var']['min'], self.augmentation['noise_var']['max']) random_shift_time = np.random.uniform(self.augmentation['shift']['min'], self.augmentation['shift']['max']) random_strech_coeff = np.random.uniform(self.augmentation['strech']['min'], self.augmentation['strech']['max']) aug_audio = tsm.wsola(audio, random_strech_coeff) aug_audio = self.shift(aug_audio, random_shift_time, fs) aug_audio = self.add_white_noise(aug_audio, random_noise_var_coeff) if verbose: print(f'random_noise_var_coeff: {random_noise_var_coeff:.2f}\nrandom_shift_time: \ {random_shift_time:.2f}\nrandom_strech_coeff: {random_strech_coeff:.2f}') return aug_audio
def test_wsola_fixed(alpha, win_type, win_size, syn_hop_size, tolerance): x, _ = sf.read('tests/data/castanetsviolin.wav') matlab_results = loadmat('tests/data/wsola_fixed_rate.mat')['result'] y = tsm.wsola(x, alpha, win_type, win_size, syn_hop_size, tolerance) _, w = np.where(matlab_results[0, :] == np.array([[alpha]])) matlab_results = matlab_results[:, w] _, w = np.where( matlab_results[1, :] == np.array([[2 if win_type == 'hann' else 1]])) matlab_results = matlab_results[:, w] _, w = np.where(matlab_results[2, :] == np.array([[win_size]])) matlab_results = matlab_results[:, w] _, w = np.where(matlab_results[3, :] == np.array([[syn_hop_size]])) matlab_results = matlab_results[:, w] _, w = np.where(matlab_results[4, :] == np.array([[tolerance]])) matlab_results = matlab_results[:, w] y_matlab = matlab_results[5, :][0].squeeze() assert np.allclose(y, y_matlab)
def run(): c = configparser.ConfigParser() c.read(path[0] + '/console/descs.conf') c = c['DEFAULT'] a_parser = argparse.ArgumentParser(description=c['TSMOD_DESC']) subparsers = a_parser.add_subparsers(help=c['SUBPARSER_HELP'], dest='subparser_name') # create parser for OLA. parser_ola = subparsers.add_parser('ola', help=c['OLA_HELP'], description=c['OLA_DESC']) parser_ola.add_argument('input_file', type=str, help=c['INPUT_HELP']) parser_ola.add_argument('output_file', type=str, help=c['OUTPUT_HELP']) parser_ola.add_argument('alpha', type=float, help=c['A_HELP']) parser_ola.add_argument('--win_type', '-wt', default='hann', type=str, help=c['WT_HELP']) parser_ola.add_argument('--win_size', '-ws', default=1024, type=int, help=c['WS_HELP']) parser_ola.add_argument('--syn_hop_size', '-sh', default=512, type=int, help=c['SH_HELP']) # create parser for WSOLA. parser_wsola = subparsers.add_parser('wsola', help=c['WSOLA_HELP'], description=c['WSOLA_DESC']) parser_wsola.add_argument('input_file', type=str, help=c['INPUT_HELP']) parser_wsola.add_argument('output_file', type=str, help=c['OUTPUT_HELP']) parser_wsola.add_argument('alpha', type=float, help=c['A_HELP']) parser_wsola.add_argument('--win_type', '-wt', default='hann', type=str, help=c['WT_HELP']) parser_wsola.add_argument('--win_size', '-ws', default=1024, type=int, help=c['WS_HELP']) parser_wsola.add_argument('--syn_hop_size', '-sh', default=512, type=int, help=c['SH_HELP']) parser_wsola.add_argument('--tolerance', '-t', default=512, type=int, help=c['TOL_HELP']) # create parser for phase-vocoder. parser_pv = subparsers.add_parser('pv', help=c['PV_HELP'], description=c['PV_DESC']) parser_pv.add_argument('input_file', type=str, help=c['INPUT_HELP']) parser_pv.add_argument('output_file', type=str, help=c['OUTPUT_HELP']) parser_pv.add_argument('alpha', type=float, help=c['A_HELP']) parser_pv.add_argument('--win_type', '-wt', default='sin', type=str, help=c['WT_HELP']) parser_pv.add_argument('--win_size', '-ws', default=2048, type=int, help=c['WS_HELP']) parser_pv.add_argument('--syn_hop_size', '-sh', default=512, type=int, help=c['SH_HELP']) parser_pv.add_argument('--zero_pad', '-z', default=0, type=int, help=c['ZP_HELP']) parser_pv.add_argument('--restore_energy', '-e', action='store_true', help=c['RE_HELP']) parser_pv.add_argument('--fft_shift', '-fs', action='store_true', help=c['FS_HELP']) parser_pv.add_argument('--phase_lock', '-pl', action='store_true', help=c['PL_HELP']) # create parser for phase-vocoder int. parser_pvi = subparsers.add_parser('pv_int', help=c['PVI_HELP'], description=c['PVI_DESC']) parser_pvi.add_argument('input_file', type=str, help=c['INPUT_HELP']) parser_pvi.add_argument('output_file', type=str, help=c['OUTPUT_HELP']) parser_pvi.add_argument('alpha', type=int, help=c['A_PVI_HELP']) parser_pvi.add_argument('--win_type', '-wt', default='hann', type=str, help=c['WT_HELP']) parser_pvi.add_argument('--win_size', '-ws', default=2048, type=int, help=c['WS_HELP']) parser_pvi.add_argument('--syn_hop_size', '-sh', default=512, type=int, help=c['SH_HELP']) parser_pvi.add_argument('--zero_pad', '-z', default=None, type=int, help=c['ZP_HELP']) parser_pvi.add_argument('--restore_energy', '-e', action='store_true', help=c['RE_HELP']) parser_pvi.add_argument('--fft_shift', '-fs', action='store_true', help=c['FS_HELP']) args = a_parser.parse_args() x, sr = sf.read(args.input_file) if args.subparser_name == 'ola': y = ola(x, args.alpha, win_type=args.win_type, win_size=args.win_size, syn_hop_size=args.syn_hop_size) elif args.subparser_name == 'wsola': y = wsola(x, args.alpha, win_type=args.win_type, win_size=args.win_size, syn_hop_size=args.syn_hop_size, tolerance=args.tolerance) elif args.subparser_name == 'pv': y = pv(x, args.alpha, win_type=args.win_type, win_size=args.win_size, syn_hop_size=args.syn_hop_size, zero_pad=args.zero_pad, restore_energy=args.restore_energy, fft_shift=args.fft_shift, phase_lock=args.phase_lock) elif args.subparser_name == 'pv_int': y = pv_int(x, args.alpha, win_type=args.win_type, win_size=args.win_size, syn_hop_size=args.syn_hop_size, zero_pad=args.zero_pad, restore_energy=args.restore_energy, fft_shift=args.fft_shift) # elif args.subparser_name == 'hp': # pass sf.write(args.output_file, y.T, sr)