Most of the pep8 changes finished. Be careful, things may be horribly broken.
This commit is contained in:
@ -22,12 +22,12 @@ class DynamicVoiceCommand:
|
||||
def shutdown(self):
|
||||
pass
|
||||
|
||||
def getDescription(self):
|
||||
def get_description(self):
|
||||
return f"Select voice: {self.voice}"
|
||||
|
||||
def run(self):
|
||||
try:
|
||||
self.env['runtime']['outputManager'].presentText(
|
||||
self.env['runtime']['OutputManager'].present_text(
|
||||
f"Testing voice {
|
||||
self.voice} from {
|
||||
self.module}. Please wait.",
|
||||
@ -37,9 +37,9 @@ class DynamicVoiceCommand:
|
||||
time.sleep(0.5)
|
||||
|
||||
# Test voice
|
||||
testResult, errorMsg = self.testVoice()
|
||||
testResult, errorMsg = self.test_voice()
|
||||
if testResult:
|
||||
self.env['runtime']['outputManager'].presentText(
|
||||
self.env['runtime']['OutputManager'].present_text(
|
||||
"Voice test completed successfully. Navigate to Apply Tested Voice to use this voice.",
|
||||
interrupt=False,
|
||||
flush=False)
|
||||
@ -52,14 +52,14 @@ class DynamicVoiceCommand:
|
||||
self.env['commandBuffer']['pendingVoiceVoice'] = self.voice
|
||||
self.env['commandBuffer']['voiceTestCompleted'] = True
|
||||
else:
|
||||
self.env['runtime']['outputManager'].presentText(
|
||||
self.env['runtime']['OutputManager'].present_text(
|
||||
f"Voice test failed: {errorMsg}", interrupt=False, flush=False)
|
||||
|
||||
except Exception as e:
|
||||
self.env['runtime']['outputManager'].presentText(
|
||||
self.env['runtime']['OutputManager'].present_text(
|
||||
f"Voice selection error: {str(e)}", interrupt=False, flush=False)
|
||||
|
||||
def testVoice(self):
|
||||
def test_voice(self):
|
||||
"""Test voice with spd-say"""
|
||||
try:
|
||||
cmd = [
|
||||
@ -84,7 +84,7 @@ class DynamicVoiceCommand:
|
||||
except Exception as e:
|
||||
return False, f"Error running voice test: {str(e)}"
|
||||
|
||||
def setCallback(self, callback):
|
||||
def set_callback(self, callback):
|
||||
pass
|
||||
|
||||
|
||||
@ -100,163 +100,169 @@ class DynamicApplyVoiceCommand:
|
||||
def shutdown(self):
|
||||
pass
|
||||
|
||||
def getDescription(self):
|
||||
def get_description(self):
|
||||
return "Apply tested voice to Fenrir"
|
||||
|
||||
def run(self):
|
||||
try:
|
||||
if not self.env['commandBuffer'].get('voiceTestCompleted', False):
|
||||
self.env['runtime']['outputManager'].presentText(
|
||||
self.env['runtime']['OutputManager'].present_text(
|
||||
"No voice has been tested yet", interrupt=True)
|
||||
return
|
||||
|
||||
module = self.env['commandBuffer']['pendingVoiceModule']
|
||||
voice = self.env['commandBuffer']['pendingVoiceVoice']
|
||||
|
||||
self.env['runtime']['outputManager'].presentText(
|
||||
self.env['runtime']['OutputManager'].present_text(
|
||||
f"Applying {voice} from {module}", interrupt=True)
|
||||
|
||||
# Debug: Show current settings
|
||||
settingsManager = self.env['runtime']['settingsManager']
|
||||
currentModule = settingsManager.getSetting('speech', 'module')
|
||||
currentVoice = settingsManager.getSetting('speech', 'voice')
|
||||
self.env['runtime']['outputManager'].presentText(
|
||||
f"Current: {currentVoice} from {currentModule}", interrupt=False, flush=False)
|
||||
settings_manager = self.env['runtime']['SettingsManager']
|
||||
current_module = settings_manager.get_setting('speech', 'module')
|
||||
current_voice = settings_manager.get_setting('speech', 'voice')
|
||||
self.env['runtime']['OutputManager'].present_text(
|
||||
f"Current: {current_voice} from {current_module}", interrupt=False, flush=False)
|
||||
|
||||
# Apply to runtime settings with fallback
|
||||
settingsManager = self.env['runtime']['settingsManager']
|
||||
settings_manager = self.env['runtime']['SettingsManager']
|
||||
|
||||
# Store old values for safety
|
||||
oldDriver = settingsManager.getSetting('speech', 'driver')
|
||||
oldModule = settingsManager.getSetting('speech', 'module')
|
||||
oldVoice = settingsManager.getSetting('speech', 'voice')
|
||||
old_driver = settings_manager.get_setting('speech', 'driver')
|
||||
old_module = settings_manager.get_setting('speech', 'module')
|
||||
old_voice = settings_manager.get_setting('speech', 'voice')
|
||||
|
||||
try:
|
||||
# Apply new settings to runtime only (use setSetting to update
|
||||
# Apply new settings to runtime only (use set_setting to update
|
||||
# settingArgDict)
|
||||
settingsManager.setSetting('speech', 'driver', 'speechdDriver')
|
||||
settingsManager.setSetting('speech', 'module', module)
|
||||
settingsManager.setSetting('speech', 'voice', voice)
|
||||
settings_manager.set_setting('speech', 'driver', 'speechdDriver')
|
||||
settings_manager.set_setting('speech', 'module', module)
|
||||
settings_manager.set_setting('speech', 'voice', voice)
|
||||
|
||||
# Apply settings to speech driver directly
|
||||
if 'speechDriver' in self.env['runtime']:
|
||||
speechDriver = self.env['runtime']['speechDriver']
|
||||
if 'SpeechDriver' in self.env['runtime']:
|
||||
SpeechDriver = self.env['runtime']['SpeechDriver']
|
||||
|
||||
# Get current module to see if we're changing modules
|
||||
currentModule = settingsManager.getSetting(
|
||||
current_module = settings_manager.get_setting(
|
||||
'speech', 'module')
|
||||
moduleChanging = (currentModule != module)
|
||||
module_changing = (current_module != module)
|
||||
|
||||
# Set module and voice on driver instance first
|
||||
speechDriver.setModule(module)
|
||||
speechDriver.setVoice(voice)
|
||||
SpeechDriver.set_module(module)
|
||||
SpeechDriver.set_voice(voice)
|
||||
|
||||
if moduleChanging:
|
||||
if module_changing:
|
||||
# Module change requires reinitializing the speech
|
||||
# driver
|
||||
self.env['runtime']['outputManager'].presentText(
|
||||
f"Switching from {currentModule} to {module} module", interrupt=True)
|
||||
speechDriver.shutdown()
|
||||
speechDriver.initialize(self.env)
|
||||
self.env['runtime']['OutputManager'].present_text(
|
||||
f"Switching from {current_module} to {module} module", interrupt=True)
|
||||
SpeechDriver.shutdown()
|
||||
SpeechDriver.initialize(self.env)
|
||||
# Re-set after initialization
|
||||
speechDriver.setModule(module)
|
||||
speechDriver.setVoice(voice)
|
||||
self.env['runtime']['outputManager'].presentText(
|
||||
SpeechDriver.set_module(module)
|
||||
SpeechDriver.set_voice(voice)
|
||||
self.env['runtime']['OutputManager'].present_text(
|
||||
"Speech driver reinitialized", interrupt=True)
|
||||
|
||||
# Debug: verify what was actually set
|
||||
self.env['runtime']['outputManager'].presentText(
|
||||
self.env['runtime']['OutputManager'].present_text(
|
||||
f"Speech driver now has module: {
|
||||
speechDriver.module}, voice: {
|
||||
speechDriver.voice}", interrupt=True)
|
||||
SpeechDriver.module}, voice: {
|
||||
SpeechDriver.voice}", interrupt=True)
|
||||
|
||||
# Force application by speaking a test message
|
||||
self.env['runtime']['outputManager'].presentText(
|
||||
self.env['runtime']['OutputManager'].present_text(
|
||||
"Voice applied successfully! You should hear this in the new voice.", interrupt=True)
|
||||
|
||||
# Brief pause then more speech to test
|
||||
time.sleep(1)
|
||||
self.env['runtime']['outputManager'].presentText(
|
||||
self.env['runtime']['OutputManager'].present_text(
|
||||
"Use save settings to make permanent", interrupt=True)
|
||||
|
||||
# Clear pending state
|
||||
self.env['commandBuffer']['voiceTestCompleted'] = False
|
||||
|
||||
# Exit vmenu after successful application
|
||||
self.env['runtime']['vmenuManager'].setActive(False)
|
||||
self.env['runtime']['VmenuManager'].set_active(False)
|
||||
|
||||
except Exception as e:
|
||||
# Revert on failure
|
||||
settingsManager.settings['speech']['driver'] = oldDriver
|
||||
settingsManager.settings['speech']['module'] = oldModule
|
||||
settingsManager.settings['speech']['voice'] = oldVoice
|
||||
settings_manager.settings['speech']['driver'] = old_driver
|
||||
settings_manager.settings['speech']['module'] = old_module
|
||||
settings_manager.settings['speech']['voice'] = old_voice
|
||||
|
||||
# Try to reinitialize with old settings
|
||||
if 'speechDriver' in self.env['runtime']:
|
||||
if 'SpeechDriver' in self.env['runtime']:
|
||||
try:
|
||||
speechDriver = self.env['runtime']['speechDriver']
|
||||
speechDriver.shutdown()
|
||||
speechDriver.initialize(self.env)
|
||||
SpeechDriver = self.env['runtime']['SpeechDriver']
|
||||
SpeechDriver.shutdown()
|
||||
SpeechDriver.initialize(self.env)
|
||||
except Exception as e:
|
||||
self.env['runtime']['debug'].writeDebugOut(
|
||||
self.env['runtime']['DebugManager'].write_debug_out(
|
||||
'dynamicVoiceMenu: Error reinitializing speech driver: ' + str(e),
|
||||
debug.debugLevel.ERROR)
|
||||
debug.DebugLevel.ERROR)
|
||||
|
||||
self.env['runtime']['outputManager'].presentText(
|
||||
self.env['runtime']['OutputManager'].present_text(
|
||||
f"Failed to apply voice, reverted: {
|
||||
str(e)}", interrupt=False, flush=False)
|
||||
|
||||
except Exception as e:
|
||||
self.env['runtime']['outputManager'].presentText(
|
||||
self.env['runtime']['OutputManager'].present_text(
|
||||
f"Apply voice error: {str(e)}", interrupt=False, flush=False)
|
||||
|
||||
def setCallback(self, callback):
|
||||
def set_callback(self, callback):
|
||||
pass
|
||||
|
||||
|
||||
def addDynamicVoiceMenus(vmenuManager):
|
||||
def add_dynamic_voice_menus(VmenuManager):
|
||||
"""Add dynamic voice menus to vmenu system"""
|
||||
try:
|
||||
env = vmenuManager.env
|
||||
env = VmenuManager.env
|
||||
|
||||
# Get speech modules
|
||||
modules = getSpeechdModules()
|
||||
modules = get_speechd_modules()
|
||||
if not modules:
|
||||
return
|
||||
|
||||
# Create voice browser submenu
|
||||
voiceBrowserMenu = {}
|
||||
voice_browser_menu = {}
|
||||
|
||||
# Add apply voice command
|
||||
applyCommand = DynamicApplyVoiceCommand(env)
|
||||
voiceBrowserMenu['Apply Tested Voice Action'] = applyCommand
|
||||
apply_command = DynamicApplyVoiceCommand(env)
|
||||
voice_browser_menu['Apply Tested Voice Action'] = apply_command
|
||||
|
||||
# Add modules as submenus
|
||||
for module in modules[:8]: # Limit to 8 modules to keep menu manageable
|
||||
moduleMenu = {}
|
||||
module_menu = {}
|
||||
|
||||
# Get voices for this module
|
||||
voices = getModuleVoices(module)
|
||||
voices = get_module_voices(module)
|
||||
if voices:
|
||||
|
||||
# Add voice commands
|
||||
for voice in voices:
|
||||
voiceCommand = DynamicVoiceCommand(module, voice, env)
|
||||
moduleMenu[f"{voice} Action"] = voiceCommand
|
||||
voice_command = DynamicVoiceCommand(module, voice, env)
|
||||
module_menu[f"{voice} Action"] = voice_command
|
||||
else:
|
||||
moduleMenu['No voices available Action'] = createInfoCommand(
|
||||
module_menu['No voices available Action'] = create_info_command(
|
||||
f"No voices found for {module}", env)
|
||||
|
||||
voiceBrowserMenu[f"{module} Menu"] = moduleMenu
|
||||
voice_browser_menu[f"{module} Menu"] = module_menu
|
||||
|
||||
# Add to main menu dict
|
||||
vmenuManager.menuDict['Voice Browser Menu'] = voiceBrowserMenu
|
||||
VmenuManager.menuDict['Voice Browser Menu'] = voice_browser_menu
|
||||
|
||||
except Exception as e:
|
||||
print(f"Error creating dynamic voice menus: {e}")
|
||||
# Use debug manager instead of print for error logging
|
||||
if 'DebugManager' in env['runtime']:
|
||||
env['runtime']['DebugManager'].write_debug_out(
|
||||
f"Error creating dynamic voice menus: {e}",
|
||||
debug.DebugLevel.ERROR)
|
||||
else:
|
||||
print(f"Error creating dynamic voice menus: {e}")
|
||||
|
||||
|
||||
def createInfoCommand(message, env):
|
||||
def create_info_command(message, env):
|
||||
"""Create a simple info command"""
|
||||
class InfoCommand:
|
||||
def __init__(self, message, env):
|
||||
@ -265,18 +271,18 @@ def createInfoCommand(message, env):
|
||||
|
||||
def initialize(self, environment): pass
|
||||
def shutdown(self): pass
|
||||
def getDescription(self): return self.message
|
||||
def get_description(self): return self.message
|
||||
|
||||
def run(self):
|
||||
self.env['runtime']['outputManager'].presentText(
|
||||
self.env['runtime']['OutputManager'].present_text(
|
||||
self.message, interrupt=True)
|
||||
|
||||
def setCallback(self, callback): pass
|
||||
def set_callback(self, callback): pass
|
||||
|
||||
return InfoCommand(message, env)
|
||||
|
||||
|
||||
def getSpeechdModules():
|
||||
def get_speechd_modules():
|
||||
"""Get available speech modules"""
|
||||
try:
|
||||
result = subprocess.run(
|
||||
@ -289,7 +295,7 @@ def getSpeechdModules():
|
||||
return []
|
||||
|
||||
|
||||
def getModuleVoices(module):
|
||||
def get_module_voices(module):
|
||||
"""Get voices for a module"""
|
||||
try:
|
||||
result = subprocess.run(
|
||||
@ -301,7 +307,7 @@ def getModuleVoices(module):
|
||||
if not line.strip():
|
||||
continue
|
||||
if module.lower() == 'espeak-ng':
|
||||
voice = processEspeakVoice(line)
|
||||
voice = process_espeak_voice(line)
|
||||
if voice:
|
||||
voices.append(voice)
|
||||
else:
|
||||
@ -312,14 +318,14 @@ def getModuleVoices(module):
|
||||
return []
|
||||
|
||||
|
||||
def processEspeakVoice(voiceLine):
|
||||
def process_espeak_voice(voiceLine):
|
||||
"""Process espeak voice format"""
|
||||
try:
|
||||
parts = [p for p in voiceLine.split() if p]
|
||||
if len(parts) < 2:
|
||||
return None
|
||||
langCode = parts[-2].lower()
|
||||
lang_code = parts[-2].lower()
|
||||
variant = parts[-1].lower()
|
||||
return f"{langCode}+{variant}" if variant and variant != 'none' else langCode
|
||||
return f"{lang_code}+{variant}" if variant and variant != 'none' else lang_code
|
||||
except Exception:
|
||||
return None
|
||||
|
Reference in New Issue
Block a user