# Copyright 2019 Mycroft AI Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """ Cache handler - reads all the .dialog files (The default mycroft responses) and does a tts inference. It then saves the .wav files to mark1 device * * * * D E P R E C A T E D * * * * THIS MODULE IS DEPRECATED IN FAVOR OF tts/cache.py. IT WILL BE REMOVED IN THE NEXT MAJOR RELEASE, 21.08 """ # TODO: remove in 21.08 import base64 import glob import os import re import shutil import hashlib import json import mycroft.util as util from urllib import parse from requests_futures.sessions import FuturesSession from mycroft.util.log import LOG REGEX_SPL_CHARS = re.compile(r'[@#$%^*()<>/\|}{~:]') MIMIC2_URL = 'https://mimic-api.mycroft.ai/synthesize?text=' # For now we only get the cache for mimic2-kusal TTS = 'Mimic2' # Check for more default dialogs res_path = os.path.abspath(os.path.join(os.path.abspath(__file__), '..', '..', 'res', 'text', 'en-us')) wifi_setup_path = '/usr/local/mycroft/mycroft-wifi-setup/dialog/en-us' cache_dialog_path = [res_path, wifi_setup_path] def generate_cache_text(cache_audio_dir, cache_text_file): """ This prepares a text file with all the sentences from *.dialog files present in mycroft/res/text/en-us and mycroft-wifi setup skill Args: cache_audio_dir (path): DEPRECATED path to store .wav files cache_text_file (file): file containing the sentences """ # TODO: remove in 21.08 if cache_audio_dir is not None: LOG.warning( "the cache_audio_dir argument is deprecated. ensure the directory " "exists before executing this function. support for this argument " "will be removed in version 21.08" ) if not os.path.exists(cache_audio_dir): os.makedirs(cache_audio_dir) try: if not os.path.isfile(cache_text_file): text_file = open(cache_text_file, 'w') for each_path in cache_dialog_path: if os.path.exists(each_path): write_cache_text(each_path, text_file) text_file.close() LOG.info("Completed generating cache") else: LOG.info("Cache file 'cache_text.txt' already exists") except Exception: LOG.exception("Could not open text file to write cache") def write_cache_text(cache_path, f): # TODO: remove in 21.08 for file in glob.glob(cache_path + "/*.dialog"): try: with open(file, 'r') as fp: all_dialogs = fp.readlines() for each_dialog in all_dialogs: # split the sentences each_dialog = re.split( r'(?