Advertisement
Luzzo

Untitled

Jun 22nd, 2024 (edited)
508
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.76 KB | None | 0 0
  1. def is_windows_platform() -> bool:
  2.     return platform.system() == 'Windows'
  3.  
  4.  
  5. class SpeakerInitializeError(Exception):
  6.     """ Represent speaker initialization errors"""
  7.  
  8.  
  9. class JarvisStatus:
  10.     """initialize speaker and other settings"""
  11.     debugSwitchOffSpeaker = False
  12.     engine = None
  13.     isRunning = True
  14.     engineUsed = ""
  15.     countErrors = 0
  16.     wikifound = []
  17.     speak = None
  18.  
  19.     @staticmethod
  20.     def initializePyTTSSpeaker() -> bool:
  21.         try:
  22.             JarvisStatus.engine = pyttsx3.init()
  23.             voices = JarvisStatus.engine.getProperty('voices')
  24.             JarvisStatus.engine.setProperty('voice', voices[0].id)
  25.         except (RuntimeError, Exception):
  26.             print("Sorry, pyttsx3 is not working.")
  27.             traceback.print_exc(limit=2, file=sys.stdout)
  28.             JarvisStatus.engine = None
  29.             return False
  30.         return True
  31.  
  32.     @staticmethod
  33.     def initializeSpVoiceSpeaker() -> bool:
  34.         if not is_windows_platform():
  35.             raise SpeakerInitializeError("Cannot initialize SpVoice. Windows platform required")
  36.         from win32com.client import Dispatch
  37.         try:
  38.             JarvisStatus.speak = Dispatch("SAPI.SpVoice").Speak
  39.         except Exception:
  40.             traceback.print_exc(limit=2, file=sys.stdout)
  41.             raise SpeakerInitializeError("Cannot initialize SpVoice")
  42.         return True
  43.     #
  44.  
  45.     @staticmethod
  46.     def initializeSpeaker() -> bool:
  47.         """setup of speaking functionality"""
  48.         try:
  49.             # second member will be evaluated only if first will fail
  50.             return JarvisStatus.initializePyTTSSpeaker() or JarvisStatus.initializeSpVoiceSpeaker()
  51.         except SpeakerInitializeError:
  52.             JarvisStatus.debugSwitchOffSpeaker = True
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement