forked from advaitpaliwal/insight
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwake_word_detection.py
43 lines (38 loc) · 1.17 KB
/
wake_word_detection.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
36
37
38
39
40
41
42
43
import pvporcupine
import pyaudio
import struct
from config import PICOVOICE_ACCESS_KEY
def detect_wake_word():
porcupine = None
pa = None
audio_stream = None
try:
porcupine = pvporcupine.create(
access_key=PICOVOICE_ACCESS_KEY,
keywords=["hey google"]
)
pa = pyaudio.PyAudio()
audio_stream = pa.open(
rate=porcupine.sample_rate,
channels=1,
format=pyaudio.paInt16,
input=True,
frames_per_buffer=porcupine.frame_length
)
print("Listening for 'Hey Google'...")
while True:
pcm = audio_stream.read(porcupine.frame_length)
pcm = struct.unpack_from("h" * porcupine.frame_length, pcm)
keyword_index = porcupine.process(pcm)
if keyword_index >= 0:
print("Wake word detected")
return True
except Exception as e:
print(f"An error occurred: {e}")
finally:
if porcupine is not None:
porcupine.delete()
if audio_stream is not None:
audio_stream.close()
if pa is not None:
pa.terminate()