File size: 1,213 Bytes
c72e80d |
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 |
import threading
import sounddevice as sd
import numpy as np
import time
import logging
logger = logging.getLogger(__name__)
class LocalAudioStreamer:
def __init__(
self,
input_queue,
output_queue,
list_play_chunk_size=512,
):
self.list_play_chunk_size = list_play_chunk_size
self.stop_event = threading.Event()
self.input_queue = input_queue
self.output_queue = output_queue
def run(self):
def callback(indata, outdata, frames, time, status):
if self.output_queue.empty():
self.input_queue.put(indata.copy())
outdata[:] = 0 * outdata
else:
outdata[:] = self.output_queue.get()[:, np.newaxis]
logger.debug("Available devices:")
logger.debug(sd.query_devices())
with sd.Stream(
samplerate=16000,
dtype="int16",
channels=1,
callback=callback,
blocksize=self.list_play_chunk_size,
):
logger.info("Starting local audio stream")
while not self.stop_event.is_set():
time.sleep(0.001)
print("Stopping recording")
|