def interactive(params,comments,fname): sep = "\s+" ui = 5 # hard code 5ms frame size print(""" Klattsyn Interactive FILE <o> to open a .klp or .fb file <s> to save your results in .klp and .wav files. <p> to play the synthesized speech. <q> to quit without saving anything.\n EDIT <c> to input a constant parameter. <v> to input a varied parameter trajectory. <t> to show a table of parameters.""") if 'du' not in params.keys(): # set defaults if not read from file params['du']=500 params['ui']=ui else: ui = params['ui'] while True: prompt = "\nEnter 'o', 's', 'p', 'q', 'c', 'v', or 't': " try: cmd = raw_input(prompt).strip() except NameError: # python 3.x cmd = input(prompt).strip() if cmd == 'c': prompt = "\twhich parameter? " try: fld = raw_input(prompt).strip() except NameError: # python 3.x fld = input(prompt).strip() if fld not in kw.params_map.keys() or fld == 'ui': # ignore unknown parameter or UI continue prompt = "\twhat value should it be? " try: val = int(raw_input(prompt).strip()) except NameError: # python 3.x val = int(input(prompt).strip()) params[fld]=val if cmd == 's': prompt = '\tWhat should the first name of the .wav file be? ' try: temp = raw_input(prompt).strip() except NameError: # python 3.x temp = input(prompt).strip() try: fname, fext = os.path.splitext(temp) except ValueError: fname = temp synth = kw.synthesizer() synth.set_params(params) (d,rate) = synth.synthesize() scipy.io.wavfile.write(fname + '.wav', rate, d) klsyn.klpfile.write(fname + '.klp', synth=synth, comments=comments) print("\tFiles {} and {} were saved.".format(fname+'.wav',fname+'.klp')) if cmd == 'q': return 'finished' if cmd == 'p': synth = kw.synthesizer() synth.set_params(params) (d,rate) = synth.synthesize() scipy.io.wavfile.write('klattsyn_temp.wav', rate, d) playaudio('klattsyn_temp.wav') print("\n" ) if cmd == 'o': prompt = "\tenter the file name: " try: pfile = raw_input(prompt).strip() except NameError: # python 3.x pfile = input(prompt).strip() fname, fext = os.path.splitext(pfile) try: f = open(pfile, 'r') except: print("\tdidn't find file: {}".format(pfile)) continue d=f.read() f.close() if re.search('\s*_varied_params_\s*', d, re.MULTILINE) != None: (params, comments) = klsyn.klpfile.read(pfile) elif (re.match('^sec*.',d) != None): start = 0 end = -1 prompt1 = "\treading an ifcformants file - start time? " prompt2 = "\treading an ifcformants file - end time? " try: start = float(raw_input(prompt1).strip()) end = float(raw_input(prompt2).strip()) except NameError: # python 3.x start = float(input(prompt1).strip()) end = float(input(prompt2).strip()) (params,comments) = ifc2klp(pfile,start,end) fname = fname + "_klp" else: print("\tthe file format of {} was not recognized.".format(pfile)) continue print("\tfinished reading the file {}".format(pfile)) if cmd == 'v': prompt = "\tenter trajectory, which paramter? " try: fld = raw_input(prompt).strip() except NameError: # python 3.x fld = input(prompt).strip() if fld not in kw.params_map.keys(): continue if fld not in params.keys() or not isinstance(params[fld],list): params[fld] = [] print("\tenter time|value pairs like this \'0 60\'. Empty line terminates.") ot=ov=-1 while True: prompt = "\ttime|value pair: " try: time_val = raw_input(prompt).strip() except NameError: # python 3.x time_val = input(prompt).strip() if time_val == "": break try: (time,val) = re.split(sep,time_val) except ValueError: continue if time < 0: print("time can't be less than zero") continue if int(time) >= params.get('du'): maxtime = params.get('du')-ui print("\t{} is the current maximum time value.".format(maxtime)) continue if (ot == -1): # first time point ot = int(ui*round(float(time)/ui)) # force it to multiple of ui ov = float(val) continue t = int(ui*round(float(time)/ui)) # force it to multiple of ui v = float(val) if t <= ot: # see if we are going forwards or backwards print("enter sucessively larger time values.") continue dv = v-ov for t1 in range(ot,t+ui,ui): idx = int(t1/ui) newv = int(round(ov + ((dv*(t1-ot))/(t-ot)))) if (idx > len(params[fld])-1): while (idx > len(params[fld])): params[fld].append(0) params[fld].append(newv) else: oldval = params[fld].pop(idx) params[fld].insert(idx,newv) ov = v ot = t if cmd == 't': show_params(params)
if isinstance(v1, list): if isinstance(v2, list): val = v1[idx1] + s_prop * (v2[idx2] - v1[idx1]) params[fld1].append(int(round(val))) else: val = v1[idx1] + s_prop * (v2 - v1[idx1]) params[fld1].append(int(round(val))) else: val = v1 + s_prop * (v2[idx2] - v1) params[fld1].append(int(round(val))) i += 1 params['du'] = len(params[fld1]) * params1.get('ui') else: myv = v1 + s_prop * (v2 - v1) params[fld1] = int(round(myv)) text1 = '# produced by klp_continuum from ' + file1 + ' and ' + file2 + '\n' text2 = '# Step ' + str(s + 1) + ' of ' + str(nsteps) + '\n' comments1['header'] = text1 + text2 fname = "{}_{}_{}".format(fname1, fname2, s + 1) synth = kw.synthesizer() synth.set_params(params) (d, rate) = synth.synthesize() scipy.io.wavfile.write(fname + '.wav', rate, d) klsyn.klpfile.write(fname + '.klp', synth=synth, comments=comments1) playaudio(fname + '.wav') print "\n--Files {} and {} were saved.".format(fname + '.wav', fname + '.klp')
#!/usr/bin/env python # -*- coding: utf-8 -*- """ Created on Wed Nov 20 14:40:35 2013 @author: Ronald L. Sprouse ([email protected]) """ # Convert Klatt synthesizer .doc parameter files to .klp files. import os, sys import klsyn.klatt_wrap as klatt_wrap import klsyn.docfile import klsyn.klpfile Usage = 'doc2klp.py docfile ... [docfileN]' if __name__ == '__main__': ''' Convert Klatt synthesizer .doc parameter files to .klp files. ''' try: sys.argv[1] != None except: raise Exception('Usage: {:s}'.format(Usage)) for dfile in sys.argv[1:]: fname, fext = os.path.splitext(dfile) synth = klatt_wrap.synthesizer() params = klsyn.docfile.read(dfile) synth.set_params(params) klsyn.klpfile.write(fname + '.klp', synth=synth)
def interactive(params, comments, fname): sep = "\s+" ui = 5 # hard code 5ms frame size print(""" Klattsyn Interactive FILE <o> to open a .klp or .fb file <s> to save your results in .klp and .wav files. <p> to play the synthesized speech. <q> to quit without saving anything.\n EDIT <c> to input a constant parameter. <v> to input a varied parameter trajectory. <t> to show a table of parameters.""") if 'du' not in params.keys(): # set defaults if not read from file params['du'] = 500 params['ui'] = ui else: ui = params['ui'] while True: prompt = "\nEnter 'o', 's', 'p', 'q', 'c', 'v', or 't': " try: cmd = raw_input(prompt).strip() except NameError: # python 3.x cmd = input(prompt).strip() if cmd == 'c': prompt = "\twhich parameter? " try: fld = raw_input(prompt).strip() except NameError: # python 3.x fld = input(prompt).strip() if fld not in kw.params_map.keys( ) or fld == 'ui': # ignore unknown parameter or UI continue prompt = "\twhat value should it be? " try: val = int(raw_input(prompt).strip()) except NameError: # python 3.x val = int(input(prompt).strip()) params[fld] = val if cmd == 's': prompt = '\tWhat should the first name of the .wav file be? ' try: temp = raw_input(prompt).strip() except NameError: # python 3.x temp = input(prompt).strip() try: fname, fext = os.path.splitext(temp) except ValueError: fname = temp synth = kw.synthesizer() synth.set_params(params) (d, rate) = synth.synthesize() scipy.io.wavfile.write(fname + '.wav', rate, d) klsyn.klpfile.write(fname + '.klp', synth=synth, comments=comments) print("\tFiles {} and {} were saved.".format( fname + '.wav', fname + '.klp')) if cmd == 'q': return 'finished' if cmd == 'p': synth = kw.synthesizer() synth.set_params(params) (d, rate) = synth.synthesize() scipy.io.wavfile.write('klattsyn_temp.wav', rate, d) playaudio('klattsyn_temp.wav') print("\n") if cmd == 'o': prompt = "\tenter the file name: " try: pfile = raw_input(prompt).strip() except NameError: # python 3.x pfile = input(prompt).strip() fname, fext = os.path.splitext(pfile) try: f = open(pfile, 'r') except: print("\tdidn't find file: {}".format(pfile)) continue d = f.read() f.close() if re.search('\s*_varied_params_\s*', d, re.MULTILINE) != None: (params, comments) = klsyn.klpfile.read(pfile) elif (re.match('^sec*.', d) != None): start = 0 end = -1 prompt1 = "\treading an ifcformants file - start time? " prompt2 = "\treading an ifcformants file - end time? " try: start = float(raw_input(prompt1).strip()) end = float(raw_input(prompt2).strip()) except NameError: # python 3.x start = float(input(prompt1).strip()) end = float(input(prompt2).strip()) (params, comments) = ifc2klp(pfile, start, end) fname = fname + "_klp" else: print("\tthe file format of {} was not recognized.".format( pfile)) continue print("\tfinished reading the file {}".format(pfile)) if cmd == 'v': prompt = "\tenter trajectory, which paramter? " try: fld = raw_input(prompt).strip() except NameError: # python 3.x fld = input(prompt).strip() if fld not in kw.params_map.keys(): continue if fld not in params.keys() or not isinstance(params[fld], list): params[fld] = [] print( "\tenter time|value pairs like this \'0 60\'. Empty line terminates." ) ot = ov = -1 while True: prompt = "\ttime|value pair: " try: time_val = raw_input(prompt).strip() except NameError: # python 3.x time_val = input(prompt).strip() if time_val == "": break try: (time, val) = re.split(sep, time_val) except ValueError: continue if int(time) < 0: print("time can't be less than zero") continue if int(time) >= params.get('du'): maxtime = params.get('du') - ui print("\t{} is the current maximum time value.".format( maxtime)) continue if (ot == -1): # first time point ot = int( ui * round(float(time) / ui)) # force it to multiple of ui ov = float(val) continue t = int(ui * round(float(time) / ui)) # force it to multiple of ui v = float(val) if t <= ot: # see if we are going forwards or backwards print("enter sucessively larger time values.") continue dv = v - ov for t1 in range(ot, t + ui, ui): idx = int(t1 / ui) newv = int(round(ov + ((dv * (t1 - ot)) / (t - ot)))) if (idx > len(params[fld]) - 1): while (idx > len(params[fld])): params[fld].append(0) params[fld].append(newv) else: oldval = params[fld].pop(idx) params[fld].insert(idx, newv) ov = v ot = t if cmd == 't': show_params(params)
if isinstance(v1,list): if isinstance(v2,list): val = v1[idx1] + s_prop*(v2[idx2]-v1[idx1]) params[fld1].append(int(round(val))) else: val = v1[idx1] + s_prop*(v2-v1[idx1]) params[fld1].append(int(round(val))) else: val = v1 + s_prop*(v2[idx2]-v1) params[fld1].append(int(round(val))) i += 1 params['du'] = len(params[fld1])*params1.get('ui') else: myv = v1 + s_prop*(v2-v1) params[fld1] = int(round(myv)) text1 = '# produced by klp_continuum from ' + file1 + ' and '+ file2 + '\n' text2 = '# Step '+ str(s+1) + ' of '+str(nsteps)+'\n' comments1['header']= text1 + text2 fname = "{}_{}_{}".format(fname1,fname2,s+1) synth = kw.synthesizer() synth.set_params(params) (d,rate) = synth.synthesize() scipy.io.wavfile.write(fname + '.wav', rate, d) klsyn.klpfile.write(fname + '.klp', synth=synth, comments=comments1) playaudio(fname + '.wav') print( "\n--Files {} and {} were saved.".format(fname+'.wav',fname+'.klp'))