writing to csv proactively
parent
05f36daf7e
commit
7a520b79f4
|
|
@ -35,7 +35,8 @@ def dest_filename(w, v, r, t):
|
||||||
|
|
||||||
|
|
||||||
def dest_path(v, r, n):
|
def dest_path(v, r, n):
|
||||||
return dest_dir + v + '/' + str(r) + '/' + n
|
rel = v + '/' + str(r) + '/' + n
|
||||||
|
return (dest_dir + rel), rels
|
||||||
|
|
||||||
|
|
||||||
def cli_gen_audio(speech_cmd, rate, voice, out_path):
|
def cli_gen_audio(speech_cmd, rate, voice, out_path):
|
||||||
|
|
@ -65,9 +66,9 @@ class SynthFile(object):
|
||||||
}
|
}
|
||||||
|
|
||||||
def get_csv(self):
|
def get_csv(self):
|
||||||
return '{},{},{},{},{}\n'.format(self.word, self.phoneme, self.voice,
|
return '{},{},{},{},{},{}\n'.format(self.word, self.phoneme, self.voice,
|
||||||
self.rate, self.variant,
|
self.rate, self.variant,
|
||||||
self.filename)
|
self.filename)
|
||||||
|
|
||||||
|
|
||||||
class SynthVariant(object):
|
class SynthVariant(object):
|
||||||
|
|
@ -111,10 +112,10 @@ class SynthVariant(object):
|
||||||
# else:
|
# else:
|
||||||
# self.synth.startSpeakingString_toURL_(word,d_url)
|
# self.synth.startSpeakingString_toURL_(word,d_url)
|
||||||
fname = dest_filename(word, self.name, self.rate, variant)
|
fname = dest_filename(word, self.name, self.rate, variant)
|
||||||
d_path = dest_path(self.name, self.rate, fname)
|
d_path, r_path = dest_path(self.name, self.rate, fname)
|
||||||
# d_url = NSURL.fileURLWithPath_(d_path)
|
# d_url = NSURL.fileURLWithPath_(d_path)
|
||||||
cli_gen_audio(phon_cmd, self.rate, self.name, d_path)
|
cli_gen_audio(phon_cmd, self.rate, self.name, d_path)
|
||||||
return SynthFile(word, phoneme, fname, self.name, self.rate, variant)
|
return SynthFile(word, phoneme, r_path, self.name, self.rate, variant)
|
||||||
|
|
||||||
|
|
||||||
def synth_generator():
|
def synth_generator():
|
||||||
|
|
@ -154,16 +155,18 @@ def synth_generator():
|
||||||
print('Created ', s)
|
print('Created ', s)
|
||||||
voice_synths.append(s)
|
voice_synths.append(s)
|
||||||
|
|
||||||
def synth_for_words(words):
|
def synth_for_words(words, writer):
|
||||||
all_synths = []
|
# all_synths = []
|
||||||
prog_title = "Synthesizing {} words, current word".format(len(words))
|
prog_title = "Synthesizing {} words, current word".format(len(words))
|
||||||
(update, prog) = prog_bar(prog_title)
|
(update, prog) = prog_bar(prog_title)
|
||||||
for w in prog(words):
|
for w in prog(words):
|
||||||
for s in voice_synths:
|
for s in voice_synths:
|
||||||
for v in ['low', 'medium', 'high']:
|
for v in ['low', 'medium', 'high']:
|
||||||
update('"{}" with {} variant ({})'.format(w, s, v))
|
update('"{}" with {} variant ({})'.format(w, s, v))
|
||||||
all_synths.append(s.generate_audio(w, v))
|
synthed = s.generate_audio(w, v)
|
||||||
return all_synths
|
writer(synthed)
|
||||||
|
# all_synths.append(synthed)
|
||||||
|
# return all_synths
|
||||||
|
|
||||||
return synth_for_words
|
return synth_for_words
|
||||||
|
|
||||||
|
|
@ -178,14 +181,44 @@ def write_synths(synth_list, fname, csv=False):
|
||||||
f.close()
|
f.close()
|
||||||
|
|
||||||
|
|
||||||
|
def synth_logger(fname, csv=False):
|
||||||
|
f = open(fname, 'w')
|
||||||
|
|
||||||
|
def csv_writer(s):
|
||||||
|
f.write(s.get_csv())
|
||||||
|
synth_list = []
|
||||||
|
|
||||||
|
def json_writer(s):
|
||||||
|
synth_list.append(s)
|
||||||
|
|
||||||
|
def close_file():
|
||||||
|
if csv:
|
||||||
|
f.close()
|
||||||
|
else:
|
||||||
|
json.dump([s.get_json() for s in synth_list], f)
|
||||||
|
f.close()
|
||||||
|
if csv:
|
||||||
|
return csv_writer, close_file
|
||||||
|
else:
|
||||||
|
return json_writer, close_file
|
||||||
|
|
||||||
|
|
||||||
def generate_audio_for_stories():
|
def generate_audio_for_stories():
|
||||||
# story_file = './inputs/all_stories_hs.json'
|
# story_file = './inputs/all_stories_hs.json'
|
||||||
story_file = './inputs/all_stories.json'
|
story_file = './inputs/all_stories.json'
|
||||||
stories_data = json.load(open(story_file))
|
stories_data = json.load(open(story_file))
|
||||||
# word_list = [t[0] for i in stories_data.values() for t in i]
|
# word_list = [t[0] for i in stories_data.values() for t in i]
|
||||||
word_list = [i for g in stories_data.values() for i in g]
|
word_list = [i for g in stories_data.values() for i in g]
|
||||||
words_audio_synth = synth_generator()
|
(writer, closer) = synth_logger(dest_file, csv=True)
|
||||||
return words_audio_synth(word_list)
|
synth_for_words = synth_generator()
|
||||||
|
try:
|
||||||
|
synth_for_words(word_list, writer)
|
||||||
|
except:
|
||||||
|
import traceback
|
||||||
|
import sys
|
||||||
|
traceback.print_exc(file=sys.stdout)
|
||||||
|
pass
|
||||||
|
closer()
|
||||||
|
|
||||||
# words_audio_synth = synth_generator()
|
# words_audio_synth = synth_generator()
|
||||||
# synth = NSSpeechSynthesizer.alloc().init()
|
# synth = NSSpeechSynthesizer.alloc().init()
|
||||||
|
|
@ -200,6 +233,6 @@ def generate_audio_for_stories():
|
||||||
|
|
||||||
|
|
||||||
# synths = synth_generator()([OUTPUT_NAME])
|
# synths = synth_generator()([OUTPUT_NAME])
|
||||||
synths = generate_audio_for_stories()
|
generate_audio_for_stories()
|
||||||
write_synths(synths, dest_file, True)
|
# write_synths(synths, dest_file, True)
|
||||||
# write_synths(synths,'./outputs/synths.json')
|
# write_synths(synths,'./outputs/synths.json')
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue