forked from suryanshsk/Python-Voice-Assistant-Suryanshsk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
enhanced_text_to_speech.py
35 lines (30 loc) · 1.13 KB
/
enhanced_text_to_speech.py
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
import pyttsx3
class TextToSpeech:
def __init__(self):
self.engine = pyttsx3.init()
self.set_voice(0) # Default to the first available voice
self.set_rate(150) # Default speech rate
self.set_volume(1.0) # Max volume
def set_voice(self, voice_index):
"""Set the voice by index (0 for first voice, 1 for second, etc.)."""
voices = self.engine.getProperty('voices')
if 0 <= voice_index < len(voices):
self.engine.setProperty('voice', voices[voice_index].id)
else:
print("Invalid voice index.")
def set_rate(self, rate):
"""Set the speech rate."""
self.engine.setProperty('rate', rate)
def set_volume(self, volume):
"""Set the volume (0.0 to 1.0)."""
self.engine.setProperty('volume', volume)
def speak(self, text):
"""Speak the given text."""
try:
self.engine.say(text)
self.engine.runAndWait()
except Exception as e:
print(f"Error during speech: {e}")
if __name__ == '__main__':
tts = TextToSpeech()
tts.speak("Hello, how can I assist you today?")