Kern-API¶
Diese Seite enthält die Referenz für die Kernkomponenten von text2speech.
Text2Speech¶
text2speech.text2speech.Text2Speech
¶
Text-to-Speech (TTS) class with configurable settings.
This class provides text-to-speech functionality using either ElevenLabs or Kokoro model with configuration support via YAML files.
Source code in text2speech/text2speech.py
36 37 38 39 40 41 42 43 44 45 46 47 48 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 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 | |
__init__(el_api_key=None, verbose=None, config_path=None, config=None, enable_queue=True, max_queue_size=DEFAULT_QUEUE_SIZE, duplicate_timeout=DEFAULT_DUPLICATE_TIMEOUT)
¶
Initialize the Text2Speech instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
el_api_key
|
Optional[str]
|
API key for ElevenLabs. |
None
|
verbose
|
Optional[bool]
|
If True, prints debug info. Overrides config if set. |
None
|
config_path
|
Optional[str]
|
Path to config.yaml file. |
None
|
config
|
Optional[Config]
|
Pre-loaded Config object. |
None
|
enable_queue
|
bool
|
If True, uses AudioQueueManager for thread-safe playback. |
True
|
max_queue_size
|
int
|
Maximum queued messages. |
DEFAULT_QUEUE_SIZE
|
duplicate_timeout
|
float
|
Skip duplicate messages within this window (seconds). |
DEFAULT_DUPLICATE_TIMEOUT
|
Source code in text2speech/text2speech.py
speak(text, priority=0, blocking=False)
¶
Queue text for speech synthesis.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
Text to speak. |
required |
priority
|
int
|
Priority level (0-100). |
0
|
blocking
|
bool
|
If True, wait for speech to complete. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if successfully queued/spoken. |
Source code in text2speech/text2speech.py
speak_sync(text)
¶
Synchronous TTS call.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
Text to speak. |
required |
Source code in text2speech/text2speech.py
set_voice(voice)
¶
Set the voice for TTS.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
voice
|
str
|
Name of the voice to use. |
required |
Source code in text2speech/text2speech.py
set_speed(speed)
¶
Set the speech speed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
speed
|
float
|
Speech speed (0.5 to 2.0). |
required |
Source code in text2speech/text2speech.py
set_volume(volume)
¶
Set the playback volume.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
volume
|
float
|
Playback volume (0.0 to 1.0). |
required |
Source code in text2speech/text2speech.py
shutdown(timeout=5.0)
¶
Shutdown the TTS system.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
timeout
|
float
|
Maximum seconds to wait for shutdown. |
5.0
|
get_available_devices()
¶
Get available audio devices.
Returns:
| Type | Description |
|---|---|
List[Dict[str, Any]]
|
List[Dict[str, Any]]: List of available audio devices. |
Source code in text2speech/text2speech.py
get_queue_stats()
¶
Get queue statistics.
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
Dict[str, Any]: Dictionary containing queue statistics. |
AudioQueueManager¶
text2speech.audio_queue.AudioQueueManager
¶
Thread-safe audio queue manager that serializes TTS playback.
Features
- Single worker thread for sequential audio playback
- Priority queue for urgent messages
- Automatic cleanup on shutdown
- Skip duplicate messages within timeout
- Non-blocking queueing
Source code in text2speech/audio_queue.py
44 45 46 47 48 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 | |
__init__(tts_callable, max_queue_size=50, duplicate_timeout=2.0, logger=None)
¶
Initialize the audio queue manager.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tts_callable
|
Callable[[str], None]
|
Synchronous function that performs TTS (blocks until done). |
required |
max_queue_size
|
int
|
Maximum queued messages (older discarded if full). |
50
|
duplicate_timeout
|
float
|
Skip duplicate messages within this window (seconds). |
2.0
|
logger
|
Optional[Logger]
|
Optional logger instance (creates one if None). |
None
|
Source code in text2speech/audio_queue.py
start()
¶
Start the worker thread.
Source code in text2speech/audio_queue.py
shutdown(timeout=5.0)
¶
Stop the worker thread and wait for completion.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
timeout
|
float
|
Maximum seconds to wait for shutdown. |
5.0
|
Source code in text2speech/audio_queue.py
enqueue(text, priority=0)
¶
Queue a message for audio playback (non-blocking).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
Message to speak. |
required |
priority
|
int
|
Priority (higher = more urgent, range 0-100). |
0
|
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if queued successfully, False if skipped/failed. |
Source code in text2speech/audio_queue.py
clear_queue()
¶
Clear all pending messages from queue.
Source code in text2speech/audio_queue.py
get_stats()
¶
Config¶
text2speech.config.Config
¶
Configuration manager for text2speech settings.
Source code in text2speech/config.py
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 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 | |
audio_output_device
property
¶
Get audio output device ID.
audio_volume
property
¶
Get default audio volume.
sample_rate
property
¶
Get sample rate.
__init__(config_path=None)
¶
Initialize configuration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config_path
|
Optional[str]
|
Path to config.yaml file. If None, searches in common locations. |
None
|
Source code in text2speech/config.py
load_from_file(config_path)
¶
Load configuration from YAML file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config_path
|
str
|
Path to the YAML configuration file. |
required |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If config file doesn't exist. |
YAMLError
|
If config file is invalid YAML. |
Source code in text2speech/config.py
get(key_path, default=None)
¶
Get configuration value using dot notation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key_path
|
str
|
Dot-separated path to config value (e.g., 'audio.output_device'). |
required |
default
|
Any
|
Default value if key not found. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
Any |
Any
|
Configuration value or default. |
Source code in text2speech/config.py
set(key_path, value)
¶
Set configuration value using dot notation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key_path
|
str
|
Dot-separated path to config value (e.g., 'audio.output_device'). |
required |
value
|
Any
|
Value to set. |
required |
Source code in text2speech/config.py
save_to_file(config_path)
¶
Save current configuration to YAML file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config_path
|
str
|
Path where to save the configuration. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If path is outside allowed directories. |