import scipy.io as spio import os import soundfile as sf import numpy as np from pyworld import pyworld as pw from expe_options import expe_options from expe_build_conditions import expe_build_conditions from tqdm import tqdm global f0_arr f0_arr = np.array([]) def pyworld_process(syll, lang): ''' Generates the individual stimuli files ''' global f0_arr fs = 0 wavIn = os.path.join( './stimuli/'+lang.lower(), str(syll) + '.wav') mat = os.path.join("./stimuli_store/"+lang.lower(), syll + '.pyworld.mat') # Intialise varibales so they are accessible in the correct scope if not os.path.exists(mat): x, fs = sf.read(wavIn) # --- Using PyWORLD script _f0, t = pw.dio(x, fs) f0 = pw.stonemask(x, _f0, t, fs) sp = pw.cheaptrick(x, f0, t, fs) ap = pw.d4c(x, f0, t, fs) freq = np.arange(0, np.size(sp, axis=1), dtype='float64') / (np.size(sp)) * fs/2 x_rms = np.sqrt(np.mean(x**2)) duration = len(x/fs) spio.savemat(mat, mdict={'fs': fs, 'f0': f0, 'sp': sp, 'ap': ap, 'x_rms': x_rms, 'frequencies': freq, 'temporal_positions': t, 'duration': duration }) f0_arr = np.append(f0_arr, f0) if __name__ == "__main__": import argparse parser = argparse.ArgumentParser() parser.add_argument("language", help="Language of files to test") args = parser.parse_args() lang = args.language options = expe_options() expe_build_conditions(options, lang) new_files = os.path.join( './stimuli/'+lang.lower(), '*') for f in tqdm(os.listdir('./stimuli/'+lang.lower())): syll = os.path.splitext(f)[0] pyworld_process(syll, lang) print "Avg f0 for all files: " + str(np.mean(f0_arr))