-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaudio.py
More file actions
31 lines (26 loc) · 909 Bytes
/
audio.py
File metadata and controls
31 lines (26 loc) · 909 Bytes
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
import speech_recognition as sr
import pyttsx3
class SpeechToText:
def __init__(self):
self.recognizer = sr.Recognizer()
def listen_command(self):
with sr.Microphone() as source:
print("Listening for command...")
audio_data = self.recognizer.listen(source, phrase_time_limit=5)
try:
command = self.recognizer.recognize_google(audio_data)
print(f"You said: {command}")
return command
except sr.UnknownValueError:
print("Sorry, I couldn't understand.")
return None
except sr.RequestError as e:
print(f"Speech recognition error: {e}")
return None
class TextToSpeech:
def __init__(self):
self.engine = pyttsx3.init()
def speak(self, text):
print("Speaking:", text)
self.engine.say(text)
self.engine.runAndWait()