| 59 | |
| 60 | |
| 61 | def speak(text): |
| 62 | global TTS_DISABLED |
| 63 | if TTS_DISABLED: |
| 64 | return |
| 65 | if TTS_MODE == "none": |
| 66 | return |
| 67 | if TTS_ENGINE is not None: |
| 68 | try: |
| 69 | if hasattr(TTS_ENGINE, "Speak"): |
| 70 | TTS_ENGINE.Speak(str(text)) |
| 71 | else: |
| 72 | TTS_ENGINE.say(str(text)) |
| 73 | TTS_ENGINE.runAndWait() |
| 74 | return |
| 75 | except Exception as exc: |
| 76 | print(f"TTS engine failed: {exc}") |
| 77 | TTS_DISABLED = True |
| 78 | return |
| 79 | if gTTS is None or playsound is None: |
| 80 | return |
| 81 | temp_path = None |
| 82 | try: |
| 83 | with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as temp_audio: |
| 84 | temp_path = temp_audio.name |
| 85 | gTTS(text=str(text), lang="en", slow=False).save(temp_path) |
| 86 | playsound(temp_path) |
| 87 | except Exception as exc: |
| 88 | print(f"gTTS failed: {exc}") |
| 89 | TTS_DISABLED = True |
| 90 | finally: |
| 91 | if temp_path: |
| 92 | try: |
| 93 | os.remove(temp_path) |
| 94 | except OSError: |
| 95 | pass |
| 96 | |
| 97 | |
| 98 | def say(text): |