-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlisten.py
More file actions
50 lines (48 loc) · 1.48 KB
/
Copy pathlisten.py
File metadata and controls
50 lines (48 loc) · 1.48 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
import os
import tempfile
import sounddevice as sd
from config import LANG, AUDIO_DIR, FS, RECORD_SECONDS, DEBUG, START_SOUND, SUCCESS_SOUND, ERROR_SOUND
from scipy.io.wavfile import write
import speech_recognition as sr
from sound import play
def listen():
fs = FS
seconds = RECORD_SECONDS
if DEBUG:
print("Tôi đang nghe đây")
os.makedirs(AUDIO_DIR, exist_ok=True)
play(START_SOUND)
try:
recording = sd.rec(int(seconds * fs), samplerate=fs, channels=1, dtype='int16')
sd.wait()
except Exception as e:
print("Lỗi khi thu âm:", e)
return None
tmp = None
try:
with tempfile.NamedTemporaryFile(suffix=".wav", dir=AUDIO_DIR, delete=False) as f:
tmp = f.name
write(tmp, fs, recording)
if DEBUG:
print("đang nhận diện")
r = sr.Recognizer()
with sr.AudioFile(tmp) as source:
audio = r.record(source)
try:
text = r.recognize_google(audio, language=LANG)
play(SUCCESS_SOUND)
except sr.UnknownValueError:
play(ERROR_SOUND)
print("Tôi chưa nghe rõ")
return None
except sr.RequestError:
play(ERROR_SOUND)
print("Có lỗi kết nối mạng đã xảy ra")
return None
return text
finally:
try:
if tmp and os.path.exists(tmp):
os.remove(tmp)
except Exception:
pass