TextToSpeechAgent¶
Overview¶
The TextToSpeechAgent
in the RAI framework is a modular agent responsible for converting incoming text into audio using a text-to-speech (TTS) model and playing it through a configured audio output device. It supports real-time playback control through ROS2 messages and handles asynchronous speech processing using threads and queues.
Class Definition¶
TextToSpeechAgent class definition
rai_s2s.tts.agents.TextToSpeechAgent
¶
Bases: BaseAgent
Agent responsible for converting text to speech and handling audio playback.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
speaker_config
|
SoundDeviceConfig
|
Configuration for the sound device used for playback. |
required |
ros2_name
|
str
|
Name of the ROS2 node. |
required |
tts
|
TTSModel
|
Text-to-speech model used for generating audio. |
required |
logger
|
Optional[Logger]
|
Logger instance for logging messages, by default None. |
None
|
max_speech_history
|
int
|
Maximum amount of speech ids to remember, by default 64 |
64
|
Source code in rai_s2s/tts/agents/tts_agent.py
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 |
|
run()
¶
Start the text-to-speech agent, initializing playback and launching the transcription thread.
Source code in rai_s2s/tts/agents/tts_agent.py
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
|
stop()
¶
Clean exit the text-to-speech agent, terminating playback and joining the transcription thread.
Source code in rai_s2s/tts/agents/tts_agent.py
202 203 204 205 206 207 208 209 |
|
Purpose¶
The TextToSpeechAgent
enables:
- Real-time conversion of text to speech
- Playback control (play/pause/stop) via ROS2 messages
- Dynamic loading of TTS models from configuration
- Robust audio handling using queues and event-driven logic
- Integration with human-robot interaction topics (HRI)
Initialization Parameters¶
Parameter | Type | Description |
---|---|---|
speaker_config |
SoundDeviceConfig |
Configuration for the audio output (speaker). |
ros2_name |
str |
Name of the ROS2 node. |
tts |
TTSModel |
Text-to-speech model instance. |
logger |
Optional[logging.Logger] |
Logger instance, or default logger if None . |
max_speech_history |
int |
Number of speech message IDs to remember (default: 64). |
Key Methods¶
from_config(cfg_path: Optional[str])
¶
Instantiates the agent from a configuration file, dynamically selecting the TTS model and setting up audio output.
run()
¶
Initializes the agent:
- Starts a thread to handle queued text-to-speech conversion
- Launches speaker playback via
SoundDeviceConnector
stop()
¶
Gracefully stops the agent by setting the termination flag and joining the transcription thread.
Communication¶
The Agent uses the ROS2HRIConnector
for connection through 2 ROS2 topics:
/to_human
: Incoming text messages to convert. Usesrai_interfaces/msg/HRIMessage
./voice_commands
: Playback control with ROS2std_msgs/msg/String
. Valid values:"play"
,"pause"
,"stop"
Best Practices¶
- Queue Management: Properly track transcription IDs to avoid queue collisions or memory leaks.
- Playback Sync: Ensure audio queues are flushed on
stop
to avoid replaying outdated speech. - Graceful Shutdown: Always call
stop()
to terminate threads cleanly. - Model Configuration: Ensure model-specific settings (e.g., voice selection for ElevenLabs) are defined in config files.
Architecture¶
The TextToSpeechAgent
interacts with the following core components:
- TTSModel: Converts text into audio (e.g., ElevenLabsTTS, OpenTTS)
- SoundDeviceConnector: Sends synthesized audio to output hardware
- ROS2HRIConnector: Handles incoming HRI and command messages
- Queues and Threads: Enable asynchronous and buffered audio processing
See Also¶
- BaseAgent: Abstract base for all agents in RAI
- SoundDeviceConnector: For details on speaker configuration and streaming
- Text-to-Speech Models: Supported TTS engines and usage
- ROS2 HRI Messaging: Interfacing with
/to_human
and/voice_commands