import argparse
from sic_framework import utils
from sic_framework.core.actuator_python2 import SICActuator
from sic_framework.core.component_manager_python2 import SICComponentManager
from sic_framework.core.connector import SICConnector
from sic_framework.core.message_python2 import SICConfMessage, SICMessage, SICRequest
if utils.PYTHON_VERSION_IS_2:
import qi
# @dataclass
[docs]
class NaoqiTextToSpeechRequest(SICRequest):
"""
Request the robot to say a given text using text-to-speech.
"""
[docs]
def __init__(
self,
text,
language=None,
animated=False,
pitch=None,
speed=None,
pitch_shift=None,
volume=None,
):
"""
Initialize a text-to-speech request.
:param str text: Text to be spoken.
:param str language: "English" or "Dutch" or see http://doc.aldebaran.com/2-8/family/nao_technical/languages_naov6.html#language-codes-naov6
:param animated: Use animated text to speech, e.g. perform some gestures while talking
"""
super(NaoqiTextToSpeechRequest, self).__init__()
self.text = text
self.animated = animated
# TTS params
self.language = language
self.pitch = pitch
self.speed = speed
self.pitch_shift = pitch_shift
self.volume = volume
# @dataclass
[docs]
class NaoqiTextToSpeechConf(SICConfMessage):
"""
Configuration for the NAOqi text-to-speech engine.
Defines default voice parameters such as language, volume, pitch, and speed.
"""
[docs]
def __init__(
self,
language="English",
volume=None,
speed=None,
pitch=None,
pitch_shift=None,
):
"""
Initialize default parameters for the text-to-speech configuration.
If None, the default NAOqi values are used.
See also:
http://doc.aldebaran.com/2-4/naoqi/audio/altexttospeech-api.html#ALTextToSpeechProxy::setParameter__ssCR.floatCR
:param str language: Language to use "English" or "Dutch" or see http://doc.aldebaran.com/2-8/family/nao_technical/languages_naov6.html#language-codes-naov6
:param float volume: Sets the current gain applied to the signal synthesized by the text to speech engine if not None.
:param int pitch: Applies a pitch shift to the voice. Range is [50 - 100].
:param float pitch_shift: Applies a pitch shift to the voice. Range is [1.0 - 4.0]. 0 disables the effect.
:param int speed: sets the current voice speed. The default value is 100. Range is [50 - 400]
"""
super(SICConfMessage, self).__init__()
self.pitch = pitch
self.speed = speed
self.pitch_shift = pitch_shift
self.volume = volume
self.language = language
[docs]
class NaoqiTextToSpeechActuator(SICActuator):
[docs]
def __init__(self, *args, **kwargs):
super(NaoqiTextToSpeechActuator, self).__init__(*args, **kwargs)
self.session = qi.Session()
self.session.connect("tcp://127.0.0.1:9559")
self.tts = self.session.service("ALTextToSpeech")
self.atts = self.session.service("ALAnimatedSpeech")
self.set_params(self.params)
[docs]
def set_params(self, params):
# Check for all parameters if they are available and set
if hasattr(params, "language") and params.language is not None:
self.tts.setLanguage(params.language)
if hasattr(params, "volume") and params.volume is not None:
self.tts.setVolume(params.volume)
if hasattr(params, "speed") and params.speed is not None:
self.tts.setParameter("speed", params.speed)
if hasattr(params, "pitch") and params.pitch is not None:
self.tts.setParameter("pitch", params.pitch)
if hasattr(params, "pitch_shift") and params.pitch_shift is not None:
self.tts.setParameter("pitchShift", params.pitch_shift)
[docs]
@staticmethod
def get_conf():
"""
Return the default configuration for this actuator.
:returns: Default text-to-speech configuration.
:rtype: NaoqiTextToSpeechConf
"""
return NaoqiTextToSpeechConf()
[docs]
@staticmethod
def get_inputs():
return [NaoqiTextToSpeechRequest]
[docs]
@staticmethod
def get_output():
return SICMessage
[docs]
def execute(self, message):
"""
Execute the text-to-speech request.
Applies message-specific parameters, then speaks using ALTextToSpeech or ALAnimatedSpeech.
:param NaoqiTextToSpeechRequest message: Request containing speech parameters and text.
:returns: Acknowledgement message after speaking.
:rtype: SICMessage
"""
# Set tts parameters for current request
self.set_params(message)
if message.animated:
self.atts.say(message.text, message.language)
else:
self.tts.say(message.text)
return SICMessage()
[docs]
def stop(self, *args):
"""
Stop the text-to-speech actuator.
"""
self.session.close()
self._stopped.set()
super(NaoqiTextToSpeechActuator, self).stop()
[docs]
class NaoqiTextToSpeech(SICConnector):
component_class = NaoqiTextToSpeechActuator
if __name__ == "__main__":
SICComponentManager([NaoqiTextToSpeechActuator])