16 Cache handler - reads all the .dialog files (The default 17 mycroft responses) and does a tts inference. 18 It then saves the .wav files to mark1 device 30 from urllib
import parse
31 from requests_futures.sessions
import FuturesSession
35 REGEX_SPL_CHARS = re.compile(
r'[@#$%^*()<>/\|}{~:]')
36 MIMIC2_URL =
'https://mimic-api.mycroft.ai/synthesize?text=' 42 res_path = os.path.abspath(os.path.join(os.path.abspath(__file__),
'..',
43 '..',
'res',
'text',
'en-us'))
44 wifi_setup_path =
'/usr/local/mycroft/mycroft-wifi-setup/dialog/en-us' 45 cache_dialog_path = [res_path, wifi_setup_path]
50 This prepares a text file with all the sentences 51 from *.dialog files present in 52 mycroft/res/text/en-us and mycroft-wifi setup skill 54 cache_audio_dir (path): path to store .wav files 55 cache_text_file (file): file containing the sentences 58 if not os.path.isfile(cache_text_file):
59 os.makedirs(cache_audio_dir)
60 f = open(cache_text_file,
'w')
61 for each_path
in cache_dialog_path:
62 if os.path.exists(each_path):
65 LOG.debug(
"Completed generating cache")
67 LOG.debug(
"Cache file 'cache_text.txt' already exists")
69 LOG.error(
"Could not open text file to write cache")
73 for file
in glob.glob(cache_path +
"/*.dialog"):
75 with open(file,
'r') as fp: 76 all_dialogs = fp.readlines() 77 for each_dialog
in all_dialogs:
79 each_dialog = re.split(
80 r'(?<!\w\.\w.)(?<![A-Z][a-z]\.)(?<=\.|\;|\?)\s',
82 for each
in each_dialog:
83 if (REGEX_SPL_CHARS.search(each)
is None):
88 f.write(each.strip() +
'\n')
96 This method takes the sentences from the text file generated 97 using generate_cache_text() and performs TTS inference on 98 mimic2-api. The wav files and phonemes are stored in 101 cache_audio_dir (path): path to store .wav files 102 cache_text_file (file): file containing the sentences 104 if os.path.isfile(cache_text_file)
and \
105 os.path.exists(cache_audio_dir):
106 if not os.listdir(cache_audio_dir):
107 session = FuturesSession()
108 with open(cache_text_file,
'r') as fp: 109 all_dialogs = fp.readlines() 110 for each_dialog
in all_dialogs:
111 each_dialog = each_dialog.strip()
112 key = str(hashlib.md5(
113 each_dialog.encode(
'utf-8',
'ignore')).hexdigest())
114 wav_file = os.path.join(cache_audio_dir, key +
'.wav')
115 each_dialog = parse.quote(each_dialog)
117 mimic2_url = MIMIC2_URL + each_dialog +
'&visimes=True' 119 req = session.get(mimic2_url)
120 results = req.result().json()
121 audio = base64.b64decode(results[
'audio_base64'])
122 vis = results[
'visimes']
124 with open(wav_file,
'wb')
as audiofile:
125 audiofile.write(audio)
127 pho_file = os.path.join(cache_audio_dir,
129 with open(pho_file,
"w")
as cachefile:
130 cachefile.write(json.dumps(vis))
132 except Exception
as e:
134 LOG.error(
"Unable to get pre-loaded cache " 135 "due to ({})".format(repr(e)))
137 LOG.debug(
"Completed getting cache for {}".format(TTS))
140 LOG.debug(
"Pre-loaded cache for {} already exists".
143 missing_path = cache_text_file
if not \
144 os.path.isfile(cache_text_file)\
146 LOG.error(
"Path ({}) does not exist for getting the cache" 147 .format(missing_path))
152 This method copies the cache from 'cache_audio_dir' 153 to TTS specific cache directory given by 154 get_cache_directory() 156 cache_audio_dir (path): path containing .wav files 158 if os.path.exists(cache_audio_dir):
160 dest = util.get_cache_directory(
'tts/' +
'Mimic2')
161 files = os.listdir(cache_audio_dir)
163 shutil.copy2(os.path.join(cache_audio_dir, f), dest)
164 LOG.debug(
"Copied all pre-loaded cache for {} to {}" 167 LOG.debug(
"No Source directory for {} pre-loaded cache" 175 cache_text_file = os.path.join(cache_audio_dir,
176 '..',
'cache_text.txt')
def copy_cache(cache_audio_dir)
def write_cache_text(cache_path, f)
def main(cache_audio_dir)
def generate_cache_text(cache_audio_dir, cache_text_file)
def download_audio(cache_audio_dir, cache_text_file)