SpeechRecognitionAgent¶
Overview¶
The SpeechRecognitionAgent
in the RAI framework is a specialized agent that performs voice activity detection (VAD), audio recording, and transcription. It integrates tightly with audio input sources and ROS2 messaging, allowing it to serve as a real-time voice interface for robotic systems.
This agent manages multiple pipelines for detecting when to start and stop recording, performs transcription using configurable models, and broadcasts messages to relevant ROS2 topics.
Class Definition¶
SpeechRecognitionAgent class definition
rai_s2s.asr.agents.asr_agent.SpeechRecognitionAgent
¶
Bases: BaseAgent
Agent responsible for voice recognition, transcription, and processing voice activity.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
microphone_config
|
SoundDeviceConfig
|
Configuration for the microphone device used for audio input. |
required |
ros2_name
|
str
|
Name of the ROS2 node. |
required |
transcription_model
|
BaseTranscriptionModel
|
Model used for transcribing audio input to text. |
required |
vad
|
BaseVoiceDetectionModel
|
Voice activity detection model used to determine when speech is present. |
required |
grace_period
|
float
|
Time in seconds to wait before stopping recording after speech ends, by default 1.0. |
1.0
|
logger
|
Optional[Logger]
|
Logger instance for logging messages, by default None. |
None
|
Source code in rai_s2s/asr/agents/asr_agent.py
49 50 51 52 53 54 55 56 57 58 59 60 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 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 |
|
add_detection_model(model, pipeline='record')
¶
Add a voice detection model to the specified processing pipeline.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model
|
BaseVoiceDetectionModel
|
The voice detection model to be added. |
required |
pipeline
|
str
|
The pipeline where the model should be added, either 'record' or 'stop'. Default is 'record'. |
'record'
|
Raises:
Type | Description |
---|---|
ValueError
|
If the specified pipeline is not 'record' or 'stop'. |
Source code in rai_s2s/asr/agents/asr_agent.py
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 |
|
run()
¶
Start the voice recognition agent, initializing the microphone and handling incoming audio samples.
Source code in rai_s2s/asr/agents/asr_agent.py
192 193 194 195 196 197 198 199 200 201 202 203 204 |
|
stop()
¶
Clean exit the voice recognition agent, ensuring all transcription threads finish before termination.
Source code in rai_s2s/asr/agents/asr_agent.py
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 |
|
Purpose¶
The SpeechRecognitionAgent
class enables real-time voice processing with the following responsibilities:
- Detecting speech through VAD
- Managing recording state and grace periods
- Buffering and threading transcription processes
- Publishing transcriptions and control messages to ROS2 topics
- Supporting multiple VAD and transcription model types
Initialization Parameters¶
Parameter | Type | Description |
---|---|---|
microphone_config |
SoundDeviceConfig |
Configuration for the microphone input. |
ros2_name |
str |
Name of the ROS2 node. |
transcription_model |
BaseTranscriptionModel |
Model instance for transcribing speech. |
vad |
BaseVoiceDetectionModel |
Model for detecting voice activity. |
grace_period |
float |
Time (in seconds) to continue buffering after speech ends. Defaults to 1.0 . |
logger |
Optional[logging.Logger] |
Logger instance. If None , defaults to module logger. |
Key Methods¶
from_config()
¶
Creates a SpeechRecognitionAgent
instance from a YAML config file. Dynamically loads the required transcription and VAD models.
run()
¶
Starts the microphone stream and handles incoming audio samples.
stop()
¶
Stops the agent gracefully, joins all running transcription threads, and shuts down ROS2 connectors.
add_detection_model(model, pipeline="record")
¶
Adds a custom VAD model to a processing pipeline.
pipeline
can be either'record'
or'stop'
'stop'
pipeline
The 'stop'
pipeline is present for forward compatibility. It currently doesn't affect Agent's functioning.
Best Practices¶
- Graceful Shutdown: Always call
stop()
to ensure transcription threads complete. - Model Compatibility: Ensure all transcription and VAD models are compatible with the sample rate (typically 16 kHz).
- Thread Safety: Use provided locks for shared state, especially around the transcription model.
- Logging: Utilize
self.logger
for debug and info logs to aid in tracing activity. - Config-driven Design: Use
from_config()
to ensure modular and portable deployment.
Architecture¶
The SpeechRecognitionAgent
typically interacts with the following components:
- SoundDeviceConnector: Interfaces with microphone audio input.
- BaseVoiceDetectionModel: Determines whether speech is present.
- BaseTranscriptionModel: Converts speech audio into text.
- ROS2Connector / ROS2HRIConnector: Publishes transcription and control messages to ROS2 topics.
- Config Loader: Dynamically creates agent from structured config files.
See Also¶
- BaseAgent: Abstract agent class providing lifecycle and logging support.
- ROS2 Connectors: Communication layer for ROS2 topics.
- Models: For available voice based models and instructions for creating new ones.
- TextToSpeech: For TextToSpeechAgent meant for distributed deployment.