diff --git a/speech_samplegen.py b/speech_samplegen.py index eefb63b..7fd931d 100644 --- a/speech_samplegen.py +++ b/speech_samplegen.py @@ -5,16 +5,16 @@ from Foundation import NSURL import json import csv import random +import string import os import re import subprocess import time -import progressbar +from tqdm import tqdm from generate_similar import similar_phoneme_phrase,similar_phrase -from speech_tools import format_filename -OUTPUT_NAME = 'story_words_test' +OUTPUT_NAME = 'story_phrases' dest_dir = os.path.abspath('.') + '/outputs/' + OUTPUT_NAME + '/' dest_file = './outputs/' + OUTPUT_NAME + '.csv' @@ -24,21 +24,25 @@ def hms_string(sec_elapsed): s = sec_elapsed % 60. return "{}:{:>02}:{:>05.2f}".format(h, m, s) -def prog_bar(title): - widgets = [title, progressbar.Counter(), 'th entry - ', progressbar.FormatLabel( - ''), ' [', progressbar.Bar(), '] - ', progressbar.ETA()] - prog = progressbar.ProgressBar(widgets=widgets) - - def update_prog(current): - widgets[3] = progressbar.FormatLabel(current) - prog.update() - return (update_prog, prog) - - def create_dir(direc): if not os.path.exists(direc): os.makedirs(direc) +def format_filename(s): + """ + Take a string and return a valid filename constructed from the string. + Uses a whitelist approach: any characters not present in valid_chars are + removed. Also spaces are replaced with underscores. + + Note: this method may produce invalid filenames such as ``, `.` or `..` + When I use this method I prepend a date string like '2009_01_15_19_46_32_' + and append a file extension like '.txt', so I avoid the potential of using + an invalid filename. + """ + valid_chars = "-_.() %s%s" % (string.ascii_letters, string.digits) + filename = ''.join(c for c in s if c in valid_chars) + filename = filename.replace(' ','_') # I don't like spaces in filenames. + return filename def dest_filename(w, v, r, t): rand_no = str(random.randint(0, 10000)) @@ -55,7 +59,7 @@ def dest_path(v, r, n): def cli_gen_audio(speech_cmd, rate, voice, out_path): subprocess.call( ['say', '-v', voice, '-r', - str(rate), '-o', out_path, speech_cmd]) + str(rate), '-o', out_path, "'"+speech_cmd+"'"]) class SynthFile(object): @@ -173,7 +177,7 @@ class SynthVariant(object): def synth_generator(): us_voices_ids = SynthVariant.voices_for_lang('en-US') - voice_rates = [150, 180, 210, 250] + voice_rates = [150, 180, 210]#, 250] voice_synths = [] create_dir(dest_dir) for vp in us_voices_ids: @@ -191,9 +195,10 @@ def synth_generator(): for s in voice_synths: s.create_synth_dirs() for v in ['low', 'medium', 'high']: - (update, prog) = prog_bar(prog_title) - for w in prog(words): - update('"{}" with {} variant ({})'.format(w, s, v)) + prog = tqdm(words) + prog.set_postfix(variant=v,voice=s.name,rate=s.rate) + for w in tqdm(words): + prog.set_postfix(word=w) synthed = s.generate_audio(w, v) writer(synthed) end_time = time.time() @@ -201,7 +206,7 @@ def synth_generator(): print("It took {} to synthsize all variants.".format(time_str)) return synth_for_words -def synth_logger(fname, csv=False): +def synth_logger(fname, csv_mode=False): f = open(fname, 'w') s_csv_w = csv.writer(f, quoting=csv.QUOTE_MINIMAL) def csv_writer(s): @@ -212,18 +217,18 @@ def synth_logger(fname, csv=False): synth_list.append(s) def close_file(): - if csv: + if csv_mode: f.close() else: json.dump([s.get_json() for s in synth_list], f) f.close() - if csv: + if csv_mode: return csv_writer, close_file else: return json_writer, close_file def generate_audio_for_text_list(text_list): - (writer, closer) = synth_logger(dest_file, csv=True) + (writer, closer) = synth_logger(dest_file, csv_mode=True) synth_for_texts = synth_generator() try: synth_for_texts(text_list, writer) @@ -239,7 +244,7 @@ def generate_audio_for_stories(): story_file = './inputs/all_stories.json' stories_data = json.load(open(story_file)) # text_list = [t[0] for i in stories_data.values() for t in i] - text_list = [i.replace('-','') for g in stories_data.values() for i in g] + text_list = [i for g in stories_data.values() for i in g] generate_audio_for_text_list(text_list) def generate_test_audio_for_stories(): @@ -257,6 +262,6 @@ def generate_test_audio_for_stories(): if __name__ == '__main__': - generate_test_audio_for_stories() + # generate_test_audio_for_stories() # generate_audio_for_text_list(['I want to go home','education']) - # generate_audio_for_stories() + generate_audio_for_stories() diff --git a/speech_tools.py b/speech_tools.py index c252ac5..15f42f0 100644 --- a/speech_tools.py +++ b/speech_tools.py @@ -132,21 +132,3 @@ def threadsafe_generator(f): def g(*a, **kw): return threadsafe_iter(f(*a, **kw)) return g - - - -def format_filename(s): - """ - Take a string and return a valid filename constructed from the string. - Uses a whitelist approach: any characters not present in valid_chars are - removed. Also spaces are replaced with underscores. - - Note: this method may produce invalid filenames such as ``, `.` or `..` - When I use this method I prepend a date string like '2009_01_15_19_46_32_' - and append a file extension like '.txt', so I avoid the potential of using - an invalid filename. - """ - valid_chars = "-_.() %s%s" % (string.ascii_letters, string.digits) - filename = ''.join(c for c in s if c in valid_chars) - filename = filename.replace(' ','_') # I don't like spaces in filenames. - return filename