Home Assistant Unofficial Reference 2024.12.1
tts.py
Go to the documentation of this file.
1 """Support for IBM Watson TTS integration."""
2 
3 import logging
4 
5 from ibm_cloud_sdk_core.authenticators import IAMAuthenticator
6 from ibm_watson import TextToSpeechV1
7 import voluptuous as vol
8 
10  PLATFORM_SCHEMA as TTS_PLATFORM_SCHEMA,
11  Provider,
12 )
14 
15 _LOGGER = logging.getLogger(__name__)
16 
17 CONF_URL = "watson_url"
18 CONF_APIKEY = "watson_apikey"
19 
20 DEFAULT_URL = "https://api.us-south.text-to-speech.watson.cloud.ibm.com"
21 
22 CONF_VOICE = "voice"
23 CONF_OUTPUT_FORMAT = "output_format"
24 CONF_TEXT_TYPE = "text"
25 
26 # List from https://tinyurl.com/watson-tts-docs
27 SUPPORTED_VOICES = [
28  "de-DE_BirgitV2Voice",
29  "de-DE_BirgitV3Voice",
30  "de-DE_BirgitVoice",
31  "de-DE_DieterV2Voice",
32  "de-DE_DieterV3Voice",
33  "de-DE_DieterVoice",
34  "de-DE_ErikaV3Voice",
35  "en-AU_HeidiExpressive",
36  "en-AU_JackExpressive",
37  "en-GB_CharlotteV3Voice",
38  "en-GB_JamesV3Voice",
39  "en-GB_KateV3Voice",
40  "en-GB_KateVoice",
41  "en-US_AllisonExpressive",
42  "en-US_AllisonV2Voice",
43  "en-US_AllisonV3Voice",
44  "en-US_AllisonVoice",
45  "en-US_EmilyV3Voice",
46  "en-US_EmmaExpressive",
47  "en-US_HenryV3Voice",
48  "en-US_KevinV3Voice",
49  "en-US_LisaExpressive",
50  "en-US_LisaV2Voice",
51  "en-US_LisaV3Voice",
52  "en-US_LisaVoice",
53  "en-US_MichaelExpressive",
54  "en-US_MichaelV2Voice",
55  "en-US_MichaelV3Voice",
56  "en-US_MichaelVoice",
57  "en-US_OliviaV3Voice",
58  "es-ES_EnriqueV3Voice",
59  "es-ES_EnriqueVoice",
60  "es-ES_LauraV3Voice",
61  "es-ES_LauraVoice",
62  "es-LA_SofiaV3Voice",
63  "es-LA_SofiaVoice",
64  "es-US_SofiaV3Voice",
65  "es-US_SofiaVoice",
66  "fr-CA_LouiseV3Voice",
67  "fr-FR_NicolasV3Voice",
68  "fr-FR_ReneeV3Voice",
69  "fr-FR_ReneeVoice",
70  "it-IT_FrancescaV2Voice",
71  "it-IT_FrancescaV3Voice",
72  "it-IT_FrancescaVoice",
73  "ja-JP_EmiV3Voice",
74  "ja-JP_EmiVoice",
75  "ko-KR_JinV3Voice",
76  "nl-NL_MerelV3Voice",
77  "pt-BR_IsabelaV3Voice",
78  "pt-BR_IsabelaVoice",
79 ]
80 
81 DEPRECATED_VOICES = [
82  "de-DE_BirgitVoice",
83  "de-DE_DieterVoice",
84  "en-US_AllisonVoice",
85  "en-US_LisaVoice",
86  "en-US_MichaelVoice",
87  "es-ES_EnriqueVoice",
88  "es-ES_LauraVoice",
89  "es-LA_SofiaVoice",
90  "es-US_SofiaVoice",
91  "fr-FR_ReneeVoice",
92  "it-IT_FrancescaVoice",
93  "ja-JP_EmiVoice",
94  "pt-BR_IsabelaVoice",
95 ]
96 
97 SUPPORTED_OUTPUT_FORMATS = [
98  "audio/flac",
99  "audio/mp3",
100  "audio/mpeg",
101  "audio/ogg",
102  "audio/ogg;codecs=opus",
103  "audio/ogg;codecs=vorbis",
104  "audio/wav",
105 ]
106 
107 CONTENT_TYPE_EXTENSIONS = {
108  "audio/flac": "flac",
109  "audio/mp3": "mp3",
110  "audio/mpeg": "mp3",
111  "audio/ogg": "ogg",
112  "audio/ogg;codecs=opus": "ogg",
113  "audio/ogg;codecs=vorbis": "ogg",
114  "audio/wav": "wav",
115 }
116 
117 DEFAULT_VOICE = "en-US_AllisonV3Voice"
118 DEFAULT_OUTPUT_FORMAT = "audio/mp3"
119 
120 PLATFORM_SCHEMA = TTS_PLATFORM_SCHEMA.extend(
121  {
122  vol.Optional(CONF_URL, default=DEFAULT_URL): cv.string,
123  vol.Required(CONF_APIKEY): cv.string,
124  vol.Optional(CONF_VOICE, default=DEFAULT_VOICE): vol.In(SUPPORTED_VOICES),
125  vol.Optional(CONF_OUTPUT_FORMAT, default=DEFAULT_OUTPUT_FORMAT): vol.In(
126  SUPPORTED_OUTPUT_FORMATS
127  ),
128  }
129 )
130 
131 
132 def get_engine(hass, config, discovery_info=None):
133  """Set up IBM Watson TTS component."""
134 
135  authenticator = IAMAuthenticator(config[CONF_APIKEY])
136  service = TextToSpeechV1(authenticator)
137  service.set_service_url(config[CONF_URL])
138 
139  supported_languages = list({s[:5] for s in SUPPORTED_VOICES})
140  default_voice = config[CONF_VOICE]
141  output_format = config[CONF_OUTPUT_FORMAT]
142  service.set_default_headers({"x-watson-learning-opt-out": "true"})
143 
144  if default_voice in DEPRECATED_VOICES:
145  _LOGGER.warning(
146  "Watson TTS voice %s is deprecated, it may be removed in the future",
147  default_voice,
148  )
149 
150  return WatsonTTSProvider(service, supported_languages, default_voice, output_format)
151 
152 
154  """IBM Watson TTS api provider."""
155 
156  def __init__(self, service, supported_languages, default_voice, output_format):
157  """Initialize Watson TTS provider."""
158  self.serviceservice = service
159  self.supported_langssupported_langs = supported_languages
160  self.default_langdefault_lang = default_voice[:5]
161  self.default_voicedefault_voice = default_voice
162  self.output_formatoutput_format = output_format
163  self.namename = "Watson TTS"
164 
165  @property
167  """Return a list of supported languages."""
168  return self.supported_langssupported_langs
169 
170  @property
171  def default_language(self):
172  """Return the default language."""
173  return self.default_langdefault_lang
174 
175  @property
176  def default_options(self):
177  """Return dict include default options."""
178  return {CONF_VOICE: self.default_voicedefault_voice}
179 
180  @property
181  def supported_options(self):
182  """Return a list of supported options."""
183  return [CONF_VOICE]
184 
185  def get_tts_audio(self, message, language, options):
186  """Request TTS file from Watson TTS."""
187  response = self.serviceservice.synthesize(
188  text=message, accept=self.output_formatoutput_format, voice=options[CONF_VOICE]
189  ).get_result()
190 
191  return (CONTENT_TYPE_EXTENSIONS[self.output_formatoutput_format], response.content)
def __init__(self, service, supported_languages, default_voice, output_format)
Definition: tts.py:156
def get_tts_audio(self, message, language, options)
Definition: tts.py:185
def get_engine(hass, config, discovery_info=None)
Definition: tts.py:132