-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstream.py
52 lines (38 loc) · 1.34 KB
/
stream.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
44
45
46
47
48
49
50
51
52
import logging
import multiprocessing as mp
import torch
from torchaudio.io import StreamReader
logger = logging.getLogger(__file__)
def audio_stream(queue: mp.Queue):
"""
Learn more about how to install and use streaming audio here
https://pytorch.org/audio/stable/tutorials/streaming_api2_tutorial.html
"""
streamer = StreamReader(src=":0", format="avfoundation")
streamer.add_basic_audio_stream(frames_per_chunk=4000, sample_rate=16000)
stream_iterator = streamer.stream(-1, 1)
logger.info("Start audio streaming")
while True:
(chunk_,) = next(stream_iterator)
logger.info("Put chunk to queue")
queue.put(chunk_)
if __name__ == "__main__":
model = torch.jit.load("inference_model.pt").eval()
ctx = mp.get_context("spawn")
chunk_queue = ctx.Queue()
streaming_process = ctx.Process(target=audio_stream, args=(chunk_queue,))
streaming_process.start()
while True:
try:
chunk = chunk_queue.get()
chunk = chunk.view(1, -1)
print(f"{chunk.shape=}")
with torch.inference_mode():
result = model(chunk)
if result > 0.7:
print("DETECTED KEY WORD")
except KeyboardInterrupt:
break
except Exception as exc:
raise exc
streaming_process.join()