-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvoice.py
More file actions
57 lines (44 loc) · 1.22 KB
/
voice.py
File metadata and controls
57 lines (44 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# voice.py — Jarvis X Speech Engine
import pyttsx3
import threading
import sys
_engine = None
_engine_lock = threading.Lock()
def _init_engine():
global _engine
if _engine is not None:
return
try:
_engine = pyttsx3.init()
_engine.setProperty("rate", 170)
_engine.setProperty("volume", 1.0)
print("🔊 Voice engine initialized.")
except Exception as e:
print("❌ TTS init failed:", e, file=sys.stderr)
_engine = None
def speak(text):
if not text:
return
def _run():
global _engine
with _engine_lock:
_init_engine()
if _engine is None:
print("[TTS unavailable]", text)
return
try:
print(f"🤖 Speaking: {text}")
_engine.stop()
_engine.say(text)
_engine.runAndWait()
except Exception as e:
print("TTS error:", e)
# 🔥 Run speech in separate thread
threading.Thread(target=_run, daemon=True).start()
def stop_speak():
global _engine
try:
if _engine:
_engine.stop()
except Exception as e:
print("stop_speak error:", e)