| 1 |
#!/usr/bin/env python3 |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Fenrir TTY screen reader |
| 5 |
# By Chrys, Storm Dragon, and contributors. |
| 6 |
# speech-dispatcher driver |
| 7 |
|
| 8 |
from fenrirscreenreader.core import debug |
| 9 |
from fenrirscreenreader.core.speechDriver import speech_driver |
| 10 |
|
| 11 |
|
| 12 |
class driver(speech_driver): |
| 13 |
"""Speech-dispatcher driver for Fenrir screen reader. |
| 14 |
|
| 15 |
This driver provides text-to-speech functionality through |
| 16 |
speech-dispatcher, |
| 17 |
which acts as a common interface to various TTS engines. It supports voice |
| 18 |
selection, speech parameters (rate, pitch, volume), and multiple TTS |
| 19 |
modules. |
| 20 |
|
| 21 |
Features: |
| 22 |
- Dynamic voice switching and parameter adjustment |
| 23 |
- Support for multiple speech-dispatcher modules (espeak, festival, etc.) |
| 24 |
- Real-time speech cancellation and queueing |
| 25 |
- Language and voice selection |
| 26 |
- Speech callbacks for synchronization |
| 27 |
|
| 28 |
Attributes: |
| 29 |
_sd: Speech-dispatcher client connection |
| 30 |
language (str): Current speech language |
| 31 |
voice (str): Current voice name |
| 32 |
module (str): Current TTS module name |
| 33 |
""" |
| 34 |
def __init__(self): |
| 35 |
speech_driver.__init__(self) |
| 36 |
|
| 37 |
def initialize(self, environment): |
| 38 |
"""Initialize the speech-dispatcher connection. |
| 39 |
|
| 40 |
Establishes connection to speech-dispatcher daemon and configures |
| 41 |
initial speech parameters. Sets up callbacks and prepares the |
| 42 |
speech subsystem for use. |
| 43 |
|
| 44 |
Args: |
| 45 |
environment: Fenrir environment dictionary with settings and managers |
| 46 |
|
| 47 |
Note: |
| 48 |
Gracefully handles cases where speech-dispatcher is not available. |
| 49 |
""" |
| 50 |
self._sd = None |
| 51 |
self.env = environment |
| 52 |
self._is_initialized = False |
| 53 |
|
| 54 |
# Only set these if they haven't been set yet (preserve existing |
| 55 |
# values) |
| 56 |
if not hasattr(self, "language") or self.language is None: |
| 57 |
self.language = "" |
| 58 |
if not hasattr(self, "voice") or self.voice is None: |
| 59 |
self.voice = "" |
| 60 |
if not hasattr(self, "module") or self.module is None: |
| 61 |
self.module = "" |
| 62 |
|
| 63 |
try: |
| 64 |
import speechd |
| 65 |
|
| 66 |
self._sd = speechd.SSIPClient("fenrir-dev") |
| 67 |
self._punct = speechd.PunctuationMode() |
| 68 |
self._is_initialized = True |
| 69 |
|
| 70 |
configured_module = self.env["runtime"][ |
| 71 |
"SettingsManager" |
| 72 |
].get_setting("speech", "module") |
| 73 |
if not configured_module: |
| 74 |
try: |
| 75 |
active_module = self._sd.get_output_module() |
| 76 |
if active_module: |
| 77 |
self.module = active_module |
| 78 |
self.env["runtime"]["SettingsManager"].set_setting( |
| 79 |
"speech", "module", active_module |
| 80 |
) |
| 81 |
except Exception as e: |
| 82 |
self.env["runtime"]["DebugManager"].write_debug_out( |
| 83 |
"SpeechDriver get_output_module:" + str(e), |
| 84 |
debug.DebugLevel.ERROR, |
| 85 |
) |
| 86 |
except Exception as e: |
| 87 |
self.env["runtime"]["DebugManager"].write_debug_out( |
| 88 |
"SpeechDriver initialize:" + str(e), debug.DebugLevel.ERROR |
| 89 |
) |
| 90 |
|
| 91 |
def shutdown(self): |
| 92 |
"""Shutdown the speech-dispatcher connection. |
| 93 |
|
| 94 |
Cleanly closes the connection to speech-dispatcher and releases |
| 95 |
any allocated resources. |
| 96 |
""" |
| 97 |
if not self._is_initialized: |
| 98 |
return |
| 99 |
self.cancel() |
| 100 |
try: |
| 101 |
self._sd.close() |
| 102 |
except Exception as e: |
| 103 |
pass |
| 104 |
self._is_initialized = False |
| 105 |
|
| 106 |
def speak(self, text, queueable=True, ignore_punctuation=False): |
| 107 |
"""Speak the given text through speech-dispatcher. |
| 108 |
|
| 109 |
Args: |
| 110 |
text (str): Text to speak |
| 111 |
queueable (bool): Whether speech can be queued with other speech |
| 112 |
ignore_punctuation (bool): Whether to ignore punctuation settings |
| 113 |
|
| 114 |
Note: |
| 115 |
Handles text preprocessing and manages speech queue based on parameters. |
| 116 |
""" |
| 117 |
if not queueable: |
| 118 |
self.cancel() |
| 119 |
if not self._is_initialized: |
| 120 |
self.initialize(self.env) |
| 121 |
if not queueable: |
| 122 |
self.cancel() |
| 123 |
if not self._is_initialized: |
| 124 |
return |
| 125 |
|
| 126 |
try: |
| 127 |
if self.module != "": |
| 128 |
self._sd.set_output_module(self.module) |
| 129 |
except Exception as e: |
| 130 |
self.env["runtime"]["DebugManager"].write_debug_out( |
| 131 |
"SpeechDriver set_module:" + str(e), debug.DebugLevel.ERROR |
| 132 |
) |
| 133 |
|
| 134 |
try: |
| 135 |
if self.language != "": |
| 136 |
self._sd.set_language(self.language) |
| 137 |
except Exception as e: |
| 138 |
self.env["runtime"]["DebugManager"].write_debug_out( |
| 139 |
"SpeechDriver set_language:" + str(e), debug.DebugLevel.ERROR |
| 140 |
) |
| 141 |
|
| 142 |
try: |
| 143 |
if self.voice != "": |
| 144 |
self._sd.set_synthesis_voice(self.voice) |
| 145 |
except Exception as e: |
| 146 |
self.env["runtime"]["DebugManager"].write_debug_out( |
| 147 |
"SpeechDriver set_voice:" + str(e), debug.DebugLevel.ERROR |
| 148 |
) |
| 149 |
|
| 150 |
try: |
| 151 |
if ignore_punctuation: |
| 152 |
self._sd.set_punctuation(self._punct.ALL) |
| 153 |
else: |
| 154 |
self._sd.set_punctuation(self._punct.NONE) |
| 155 |
except Exception as e: |
| 156 |
self.env["runtime"]["DebugManager"].write_debug_out( |
| 157 |
"SpeechDriver set_punctuation:" + str(e), |
| 158 |
debug.DebugLevel.ERROR, |
| 159 |
) |
| 160 |
|
| 161 |
try: |
| 162 |
# Check if read-all is active and add callback if needed |
| 163 |
if ("ReadAllManager" in self.env["runtime"] and |
| 164 |
self.env["runtime"]["ReadAllManager"] and |
| 165 |
self.env["runtime"]["ReadAllManager"].is_active()): |
| 166 |
|
| 167 |
import speechd |
| 168 |
|
| 169 |
def read_all_callback(msg_id, client_id, event_type, *args): |
| 170 |
if (event_type == speechd.CallbackType.END and |
| 171 |
self.env["runtime"]["ReadAllManager"] and |
| 172 |
self.env["runtime"]["ReadAllManager"].is_active()): |
| 173 |
self.env["runtime"]["ReadAllManager"].speech_completed() |
| 174 |
|
| 175 |
self._sd.speak(text, callback=read_all_callback, |
| 176 |
event_types=(speechd.CallbackType.END,)) |
| 177 |
else: |
| 178 |
self._sd.speak(text) |
| 179 |
except Exception as e: |
| 180 |
self.env["runtime"]["DebugManager"].write_debug_out( |
| 181 |
"SpeechDriver speak:" + str(e), debug.DebugLevel.ERROR |
| 182 |
) |
| 183 |
self._is_initialized = False |
| 184 |
|
| 185 |
def cancel(self): |
| 186 |
"""Cancel all pending and current speech. |
| 187 |
|
| 188 |
Immediately stops speech output and clears the speech queue. |
| 189 |
""" |
| 190 |
if not self._is_initialized: |
| 191 |
self.initialize(self.env) |
| 192 |
if not self._is_initialized: |
| 193 |
return |
| 194 |
try: |
| 195 |
self._sd.cancel() |
| 196 |
except Exception as e: |
| 197 |
self.env["runtime"]["DebugManager"].write_debug_out( |
| 198 |
"SpeechDriver cancel:" + str(e), debug.DebugLevel.ERROR |
| 199 |
) |
| 200 |
self._is_initialized = False |
| 201 |
|
| 202 |
def set_pitch(self, pitch): |
| 203 |
"""Set the speech pitch. |
| 204 |
|
| 205 |
Args: |
| 206 |
pitch (float): Speech pitch (0.0 to 1.0, where 0.5 is normal) |
| 207 |
""" |
| 208 |
if not self._is_initialized: |
| 209 |
return |
| 210 |
try: |
| 211 |
self._sd.set_pitch(int(-100 + pitch * 200)) |
| 212 |
except Exception as e: |
| 213 |
self.env["runtime"]["DebugManager"].write_debug_out( |
| 214 |
"SpeechDriver set_pitch:" + str(e), debug.DebugLevel.ERROR |
| 215 |
) |
| 216 |
|
| 217 |
def set_rate(self, rate): |
| 218 |
"""Set the speech rate. |
| 219 |
|
| 220 |
Args: |
| 221 |
rate (float): Speech rate (0.0 to 1.0, where 0.5 is normal) |
| 222 |
""" |
| 223 |
if not self._is_initialized: |
| 224 |
return |
| 225 |
try: |
| 226 |
self._sd.set_rate(int(-100 + rate * 200)) |
| 227 |
except Exception as e: |
| 228 |
self.env["runtime"]["DebugManager"].write_debug_out( |
| 229 |
"SpeechDriver set_rate:" + str(e), debug.DebugLevel.ERROR |
| 230 |
) |
| 231 |
|
| 232 |
def set_volume(self, volume): |
| 233 |
"""Set the speech volume. |
| 234 |
|
| 235 |
Args: |
| 236 |
volume (float): Volume level (0.0 to 1.0) |
| 237 |
""" |
| 238 |
if not self._is_initialized: |
| 239 |
return |
| 240 |
try: |
| 241 |
self._sd.set_volume(int(-100 + volume * 200)) |
| 242 |
except Exception as e: |
| 243 |
self.env["runtime"]["DebugManager"].write_debug_out( |
| 244 |
"SpeechDriver set_volume:" + str(e), debug.DebugLevel.ERROR |
| 245 |
) |