remote_tts.py
Go to the documentation of this file.
1 # Copyright 2017 Mycroft AI Inc.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 #
15 import abc
16 import re
17 from requests_futures.sessions import FuturesSession
18 
19 from mycroft.tts import TTS
20 from mycroft.util import remove_last_slash, play_wav
21 from mycroft.util.log import LOG
22 
23 
24 class RemoteTTSTimeoutException(Exception):
25  pass
26 
27 
28 class RemoteTTS(TTS):
29  """
30  Abstract class for a Remote TTS engine implementation.
31 
32  It provides a common logic to perform multiple requests by splitting the
33  whole sentence into small ones.
34  """
35 
36  def __init__(self, lang, config, url, api_path, validator):
37  super(RemoteTTS, self).__init__(lang, config, validator)
38  self.api_path = api_path
39  self.auth = None
40  self.url = remove_last_slash(url)
41  self.session = FuturesSession()
42 
43  def execute(self, sentence, ident=None):
44  phrases = self.__get_phrases(sentence)
45 
46  if len(phrases) > 0:
47  for req in self.__requests(phrases):
48  try:
49  self.begin_audio()
50  self.__play(req)
51  except Exception as e:
52  LOG.error(e.message)
53  finally:
54  self.end_audio()
55 
56  @staticmethod
57  def __get_phrases(sentence):
58  phrases = re.split(r'\.+[\s+|\n]', sentence)
59  phrases = [p.replace('\n', '').strip() for p in phrases]
60  phrases = [p for p in phrases if len(p) > 0]
61  return phrases
62 
63  def __requests(self, phrases):
64  reqs = []
65  for p in phrases:
66  reqs.append(self.__request(p))
67  return reqs
68 
69  def __request(self, p):
70  return self.session.get(
71  self.url + self.api_path, params=self.build_request_params(p),
72  timeout=10, verify=False, auth=self.auth)
73 
74  @abc.abstractmethod
75  def build_request_params(self, sentence):
76  pass
77 
78  def __play(self, req):
79  resp = req.result()
80  if resp.status_code == 200:
81  self.__save(resp.content)
82  play_wav(self.filename).communicate()
83  else:
84  LOG.error(
85  '%s Http Error: %s for url: %s' %
86  (resp.status_code, resp.reason, resp.url))
87 
88  def __save(self, data):
89  with open(self.filename, 'wb') as f:
90  f.write(data)
def build_request_params(self, sentence)
Definition: remote_tts.py:75
def __init__(self, lang, config, url, api_path, validator)
Definition: remote_tts.py:36
def __requests(self, phrases)
Definition: remote_tts.py:63
def execute(self, sentence, ident=None)
Definition: remote_tts.py:43


mycroft_ros
Author(s):
autogenerated on Mon Apr 26 2021 02:35:40