| 1 |
#!/usr/bin/env python3 |
| 2 |
|
| 3 |
from fenrirscreenreader.core import debug |
| 4 |
|
| 5 |
|
| 6 |
class DynamicVoiceCommand: |
| 7 |
"""Dynamic command class for voice selection""" |
| 8 |
|
| 9 |
def __init__(self, module, voice, env): |
| 10 |
self.module = module |
| 11 |
self.voice, separator, self.language = voice.partition("|") |
| 12 |
if not separator: |
| 13 |
self.language = "" |
| 14 |
self.env = env |
| 15 |
self.test_message = ( |
| 16 |
"This is a voice test. The quick brown fox jumps over the lazy dog." |
| 17 |
) |
| 18 |
self._test_generation = 0 |
| 19 |
|
| 20 |
def initialize(self, environment): |
| 21 |
self.env = environment |
| 22 |
|
| 23 |
def shutdown(self): |
| 24 |
self._test_generation += 1 |
| 25 |
|
| 26 |
def get_description(self): |
| 27 |
return f"Select voice: {self.voice}" |
| 28 |
|
| 29 |
def run(self): |
| 30 |
self._test_generation += 1 |
| 31 |
generation = self._test_generation |
| 32 |
try: |
| 33 |
self.env["runtime"]["OutputManager"].present_text( |
| 34 |
f"Testing voice {self.voice} from {self.module}. Please wait.", |
| 35 |
interrupt=True, |
| 36 |
) |
| 37 |
self.env["runtime"]["SpeechDiscoveryManager"].request_voice_test( |
| 38 |
self.module, |
| 39 |
self.voice, |
| 40 |
self.test_message, |
| 41 |
lambda succeeded, error: self._finish_voice_test( |
| 42 |
generation, succeeded, error |
| 43 |
), |
| 44 |
) |
| 45 |
|
| 46 |
except Exception as e: |
| 47 |
self.env["runtime"]["OutputManager"].present_text( |
| 48 |
f"Voice selection error: {str(e)}", |
| 49 |
interrupt=False, |
| 50 |
flush=False, |
| 51 |
) |
| 52 |
|
| 53 |
def _finish_voice_test(self, generation, succeeded, error): |
| 54 |
if generation != self._test_generation: |
| 55 |
return |
| 56 |
if not succeeded: |
| 57 |
self.env["runtime"]["OutputManager"].present_text( |
| 58 |
f"Voice test failed: {error}", |
| 59 |
interrupt=False, |
| 60 |
flush=False, |
| 61 |
) |
| 62 |
return |
| 63 |
|
| 64 |
self.env["commandBuffer"]["lastTestedModule"] = self.module |
| 65 |
self.env["commandBuffer"]["lastTestedVoice"] = self.voice |
| 66 |
self.env["commandBuffer"]["pendingVoiceModule"] = self.module |
| 67 |
self.env["commandBuffer"]["pendingVoiceVoice"] = self.voice |
| 68 |
if self.language: |
| 69 |
self.env["commandBuffer"]["pendingVoiceLanguage"] = self.language |
| 70 |
else: |
| 71 |
self.env["commandBuffer"].pop("pendingVoiceLanguage", None) |
| 72 |
self.env["commandBuffer"]["voiceTestCompleted"] = True |
| 73 |
self.env["runtime"]["OutputManager"].present_text( |
| 74 |
"Voice test completed successfully. " |
| 75 |
"Navigate to Apply Tested Voice to use this voice.", |
| 76 |
interrupt=False, |
| 77 |
flush=False, |
| 78 |
) |
| 79 |
|
| 80 |
def set_callback(self, callback): |
| 81 |
pass |
| 82 |
|
| 83 |
|
| 84 |
class DynamicApplyVoiceCommand: |
| 85 |
"""Command to apply the tested voice""" |
| 86 |
|
| 87 |
def __init__(self, env): |
| 88 |
self.env = env |
| 89 |
|
| 90 |
def initialize(self, environment): |
| 91 |
self.env = environment |
| 92 |
|
| 93 |
def shutdown(self): |
| 94 |
pass |
| 95 |
|
| 96 |
def get_description(self): |
| 97 |
return "Apply tested voice to Fenrir" |
| 98 |
|
| 99 |
def run(self): |
| 100 |
try: |
| 101 |
if not self.env["commandBuffer"].get("voiceTestCompleted", False): |
| 102 |
self.env["runtime"]["OutputManager"].present_text( |
| 103 |
"No voice has been tested yet", interrupt=True |
| 104 |
) |
| 105 |
return |
| 106 |
|
| 107 |
module = self.env["commandBuffer"]["pendingVoiceModule"] |
| 108 |
voice = self.env["commandBuffer"]["pendingVoiceVoice"] |
| 109 |
language = self.env["commandBuffer"].get("pendingVoiceLanguage") |
| 110 |
|
| 111 |
self.env["runtime"]["OutputManager"].present_text( |
| 112 |
f"Applying {voice} from {module}", interrupt=True |
| 113 |
) |
| 114 |
|
| 115 |
# Debug: Show current settings |
| 116 |
settings_manager = self.env["runtime"]["SettingsManager"] |
| 117 |
current_module = settings_manager.get_setting("speech", "module") |
| 118 |
current_voice = settings_manager.get_setting("speech", "voice") |
| 119 |
self.env["runtime"]["OutputManager"].present_text( |
| 120 |
f"Current: {current_voice} from {current_module}", |
| 121 |
interrupt=False, |
| 122 |
flush=False, |
| 123 |
) |
| 124 |
|
| 125 |
# Apply to runtime settings with fallback |
| 126 |
settings_manager = self.env["runtime"]["SettingsManager"] |
| 127 |
|
| 128 |
# Store old values for safety |
| 129 |
old_driver = settings_manager.get_setting("speech", "driver") |
| 130 |
old_module = settings_manager.get_setting("speech", "module") |
| 131 |
old_voice = settings_manager.get_setting("speech", "voice") |
| 132 |
old_language = settings_manager.get_setting("speech", "language") |
| 133 |
|
| 134 |
try: |
| 135 |
# Apply new settings to runtime only (use set_setting to update |
| 136 |
# settingArgDict) |
| 137 |
settings_manager.set_setting( |
| 138 |
"speech", "driver", "speechdDriver" |
| 139 |
) |
| 140 |
settings_manager.set_setting("speech", "module", module) |
| 141 |
settings_manager.set_setting("speech", "voice", voice) |
| 142 |
if language: |
| 143 |
settings_manager.set_setting( |
| 144 |
"speech", "language", language |
| 145 |
) |
| 146 |
|
| 147 |
# Apply settings to speech driver directly |
| 148 |
if "SpeechDriver" in self.env["runtime"]: |
| 149 |
speech_driver = self.env["runtime"]["SpeechDriver"] |
| 150 |
|
| 151 |
# Set module and voice on the driver instance. |
| 152 |
speech_driver.set_module(module) |
| 153 |
if language: |
| 154 |
speech_driver.set_language(language) |
| 155 |
speech_driver.set_voice(voice) |
| 156 |
|
| 157 |
# Debug: verify what was actually set |
| 158 |
self.env["runtime"]["OutputManager"].present_text( |
| 159 |
"Speech driver now has module: " |
| 160 |
f"{speech_driver.module}, voice: " |
| 161 |
f"{speech_driver.voice}", |
| 162 |
interrupt=True, |
| 163 |
) |
| 164 |
|
| 165 |
# Force application by speaking a test message |
| 166 |
self.env["runtime"]["OutputManager"].present_text( |
| 167 |
"Voice applied successfully! You should hear this in " |
| 168 |
"the new voice.", |
| 169 |
interrupt=True, |
| 170 |
) |
| 171 |
|
| 172 |
self.env["runtime"]["OutputManager"].present_text( |
| 173 |
"Use save settings to make permanent", interrupt=True |
| 174 |
) |
| 175 |
|
| 176 |
# Clear pending state |
| 177 |
self.env["commandBuffer"]["voiceTestCompleted"] = False |
| 178 |
|
| 179 |
# Exit vmenu after successful application |
| 180 |
self.env["runtime"]["VmenuManager"].set_active(False) |
| 181 |
|
| 182 |
except Exception as e: |
| 183 |
# Revert on failure |
| 184 |
settings_manager.set_setting("speech", "driver", old_driver) |
| 185 |
settings_manager.set_setting("speech", "module", old_module) |
| 186 |
settings_manager.set_setting("speech", "voice", old_voice) |
| 187 |
settings_manager.set_setting( |
| 188 |
"speech", "language", old_language |
| 189 |
) |
| 190 |
|
| 191 |
# Try to reinitialize with old settings |
| 192 |
if "SpeechDriver" in self.env["runtime"]: |
| 193 |
try: |
| 194 |
speech_driver = self.env["runtime"]["SpeechDriver"] |
| 195 |
speech_driver.shutdown() |
| 196 |
speech_driver.initialize(self.env) |
| 197 |
except Exception as e: |
| 198 |
self.env["runtime"]["DebugManager"].write_debug_out( |
| 199 |
"dynamicVoiceMenu: Error reinitializing speech driver: " |
| 200 |
+ str(e), |
| 201 |
debug.DebugLevel.ERROR, |
| 202 |
) |
| 203 |
|
| 204 |
self.env["runtime"]["OutputManager"].present_text( |
| 205 |
f"Failed to apply voice, reverted: {str(e)}", |
| 206 |
interrupt=False, |
| 207 |
flush=False, |
| 208 |
) |
| 209 |
|
| 210 |
except Exception as e: |
| 211 |
self.env["runtime"]["OutputManager"].present_text( |
| 212 |
f"Apply voice error: {str(e)}", interrupt=False, flush=False |
| 213 |
) |
| 214 |
|
| 215 |
def set_callback(self, callback): |
| 216 |
pass |
| 217 |
|
| 218 |
|
| 219 |
def add_dynamic_voice_menus(vmenu_manager): |
| 220 |
"""Populate cached voice menus and start non-blocking discovery if needed.""" |
| 221 |
try: |
| 222 |
env = vmenu_manager.env |
| 223 |
discovery = env["runtime"]["SpeechDiscoveryManager"] |
| 224 |
generation = getattr(vmenu_manager, "_voice_menu_generation", 0) + 1 |
| 225 |
vmenu_manager._voice_menu_generation = generation |
| 226 |
modules = discovery.get_cached_modules() |
| 227 |
if modules is None: |
| 228 |
_install_loading_menu(vmenu_manager) |
| 229 |
discovery.request_modules( |
| 230 |
lambda found, error: _request_dynamic_voice_lists( |
| 231 |
vmenu_manager, generation, found, error, False |
| 232 |
) |
| 233 |
) |
| 234 |
return |
| 235 |
_request_dynamic_voice_lists( |
| 236 |
vmenu_manager, generation, modules, "", True |
| 237 |
) |
| 238 |
except Exception as error: |
| 239 |
if "DebugManager" in env["runtime"]: |
| 240 |
env["runtime"]["DebugManager"].write_debug_out( |
| 241 |
f"Error creating dynamic voice menus: {error}", |
| 242 |
debug.DebugLevel.ERROR, |
| 243 |
) |
| 244 |
else: |
| 245 |
print(f"Error creating dynamic voice menus: {error}") |
| 246 |
|
| 247 |
|
| 248 |
def _request_dynamic_voice_lists( |
| 249 |
vmenu_manager, generation, modules, error, allow_active |
| 250 |
): |
| 251 |
if generation != getattr(vmenu_manager, "_voice_menu_generation", 0): |
| 252 |
return |
| 253 |
if error or not modules: |
| 254 |
if vmenu_manager.get_active() and not allow_active: |
| 255 |
vmenu_manager._voice_menu_refresh_pending = True |
| 256 |
return |
| 257 |
_install_discovery_error_menu(vmenu_manager) |
| 258 |
return |
| 259 |
|
| 260 |
discovery = vmenu_manager.env["runtime"]["SpeechDiscoveryManager"] |
| 261 |
selected_modules = modules[:8] |
| 262 |
voices_by_module = {} |
| 263 |
missing_modules = [] |
| 264 |
for module in selected_modules: |
| 265 |
voices = discovery.get_cached_voices(module) |
| 266 |
if voices is None: |
| 267 |
missing_modules.append(module) |
| 268 |
else: |
| 269 |
voices_by_module[module] = voices |
| 270 |
|
| 271 |
if not missing_modules: |
| 272 |
_install_dynamic_voice_menu( |
| 273 |
vmenu_manager, |
| 274 |
generation, |
| 275 |
selected_modules, |
| 276 |
voices_by_module, |
| 277 |
allow_active, |
| 278 |
) |
| 279 |
return |
| 280 |
|
| 281 |
_install_loading_menu(vmenu_manager) |
| 282 |
remaining_modules = set(missing_modules) |
| 283 |
|
| 284 |
def voices_ready(module, voices, _voice_error): |
| 285 |
if generation != getattr(vmenu_manager, "_voice_menu_generation", 0): |
| 286 |
return |
| 287 |
voices_by_module[module] = voices |
| 288 |
remaining_modules.discard(module) |
| 289 |
if not remaining_modules: |
| 290 |
_install_dynamic_voice_menu( |
| 291 |
vmenu_manager, |
| 292 |
generation, |
| 293 |
selected_modules, |
| 294 |
voices_by_module, |
| 295 |
False, |
| 296 |
) |
| 297 |
|
| 298 |
for module in missing_modules: |
| 299 |
discovery.request_voices(module, voices_ready) |
| 300 |
|
| 301 |
|
| 302 |
def _install_dynamic_voice_menu( |
| 303 |
vmenu_manager, |
| 304 |
generation, |
| 305 |
modules, |
| 306 |
voices_by_module, |
| 307 |
allow_active, |
| 308 |
): |
| 309 |
if generation != getattr(vmenu_manager, "_voice_menu_generation", 0): |
| 310 |
return |
| 311 |
if vmenu_manager.get_active() and not allow_active: |
| 312 |
vmenu_manager._voice_menu_refresh_pending = True |
| 313 |
return |
| 314 |
|
| 315 |
env = vmenu_manager.env |
| 316 |
voice_browser_menu = { |
| 317 |
"Apply Tested Voice Action": DynamicApplyVoiceCommand(env) |
| 318 |
} |
| 319 |
for module in modules: |
| 320 |
module_menu = {} |
| 321 |
voices = voices_by_module.get(module, []) |
| 322 |
for voice in voices: |
| 323 |
voice_name = voice.split("|", 1)[0] |
| 324 |
module_menu[f"{voice_name} Action"] = DynamicVoiceCommand( |
| 325 |
module, voice, env |
| 326 |
) |
| 327 |
if not module_menu: |
| 328 |
module_menu["No voices available Action"] = create_info_command( |
| 329 |
f"No voices found for {module}", env |
| 330 |
) |
| 331 |
voice_browser_menu[f"{module} Menu"] = module_menu |
| 332 |
vmenu_manager.menuDict["Voice Browser Menu"] = voice_browser_menu |
| 333 |
vmenu_manager._voice_menu_refresh_pending = False |
| 334 |
|
| 335 |
|
| 336 |
def _install_loading_menu(vmenu_manager): |
| 337 |
env = vmenu_manager.env |
| 338 |
vmenu_manager.menuDict["Voice Browser Menu"] = { |
| 339 |
"Loading voices Action": create_info_command( |
| 340 |
"Voice information is still loading", env |
| 341 |
) |
| 342 |
} |
| 343 |
|
| 344 |
|
| 345 |
def _install_discovery_error_menu(vmenu_manager): |
| 346 |
env = vmenu_manager.env |
| 347 |
vmenu_manager.menuDict["Voice Browser Menu"] = { |
| 348 |
"Voice discovery failed Action": create_info_command( |
| 349 |
"Voice discovery failed", env |
| 350 |
) |
| 351 |
} |
| 352 |
|
| 353 |
|
| 354 |
def create_info_command(message, env): |
| 355 |
"""Create a simple info command""" |
| 356 |
|
| 357 |
class InfoCommand: |
| 358 |
def __init__(self, message, env): |
| 359 |
self.message = message |
| 360 |
self.env = env |
| 361 |
|
| 362 |
def initialize(self, environment): |
| 363 |
pass |
| 364 |
|
| 365 |
def shutdown(self): |
| 366 |
pass |
| 367 |
|
| 368 |
def get_description(self): |
| 369 |
return self.message |
| 370 |
|
| 371 |
def run(self): |
| 372 |
self.env["runtime"]["OutputManager"].present_text( |
| 373 |
self.message, interrupt=True |
| 374 |
) |
| 375 |
|
| 376 |
def set_callback(self, callback): |
| 377 |
pass |
| 378 |
|
| 379 |
return InfoCommand(message, env) |