Skip to content

Commit 7d12b7f

Browse files
committed
added say_localized_text method
1 parent 6325b8b commit 7d12b7f

File tree

1 file changed

+56
-1
lines changed

1 file changed

+56
-1
lines changed

anki_vector/behavior.py

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,8 @@ async def roll_visible_cube(self) -> protocol.RollBlockResponse:
179179

180180
# TODO Make this cancellable with is_cancellable_behavior
181181
@connection.on_connection_thread()
182-
async def say_text(self, text: str, use_vector_voice: bool = True, duration_scalar: float = 1.0) -> protocol.SayTextResponse:
182+
async def say_text(self, text: str, use_vector_voice: bool = True,
183+
duration_scalar: float = 1.0) -> protocol.SayTextResponse:
183184
"""Make Vector speak text.
184185
185186
.. testcode::
@@ -201,6 +202,60 @@ async def say_text(self, text: str, use_vector_voice: bool = True, duration_scal
201202
duration_scalar=duration_scalar)
202203
return await self.conn.grpc_interface.SayText(say_text_request)
203204

205+
def say_localized_text(self, text: str, use_vector_voice: bool = True, duration_scalar: float = 1.0,
206+
language: str = 'en') -> protocol.SayTextResponse:
207+
"""Make Vector speak text with a different localized voice.
208+
209+
.. testcode::
210+
211+
import anki_vector
212+
with anki_vector.Robot() as robot:
213+
robot.behavior.say_localized_text("Hello World",language="de")
214+
215+
:param text: The words for Vector to say.
216+
:param use_vector_voice: Whether to use Vector's robot voice
217+
(otherwise, he uses a generic human male voice).
218+
:param duration_scalar: Adjust the relative duration of the
219+
generated text to speech audio.
220+
:param language: Adjust the language spoken for this text
221+
222+
possible values:
223+
de: German
224+
en: English
225+
ja or jp: Japanese
226+
fr: French
227+
228+
:return: object that provides the status and utterance state
229+
"""
230+
231+
if language == 'en':
232+
locale = 'en_US'
233+
if language == 'de':
234+
locale = 'de_DE'
235+
elif language == 'fr':
236+
locale = 'fr_FR'
237+
elif language == 'ja' or language == 'jp':
238+
locale = 'ja_JP'
239+
duration_scalar = duration_scalar / 3
240+
else:
241+
locale = language
242+
243+
if self.robot.force_async:
244+
# @TODO: make sure requests are sent in conjunction when say_future would be returned -
245+
# currently it's blocking async, to ensure the language is switched back after talking
246+
# because otherwise the persisted different locale would lead to failed cloud requests,
247+
# as on english seems to be supported right now and vector would show network-error on his screen
248+
self.change_locale(locale=locale).result()
249+
say_future = self.say_text(text, use_vector_voice, duration_scalar)
250+
res = say_future.result()
251+
self.change_locale(locale='en_US').result()
252+
return res
253+
else:
254+
self.change_locale(locale=locale)
255+
say_future = self.say_text(text, use_vector_voice, duration_scalar)
256+
self.change_locale(locale='en_US')
257+
return say_future
258+
204259
# TODO Make this cancellable with is_cancellable_behavior
205260
@connection.on_connection_thread()
206261
async def app_intent(self, intent: str, param: str = None) -> protocol.AppIntentResponse:

0 commit comments

Comments
 (0)