add endframe to transport receive queue

This commit is contained in:
Chad Bailey
2024-04-04 20:45:23 +00:00
parent 03ea208361
commit 27cef7cd70

View File

@@ -32,12 +32,15 @@ from dailyai.transports.abstract_transport import AbstractTransport
def int2float(sound): def int2float(sound):
abs_max = np.abs(sound).max() try:
sound = sound.astype("float32") abs_max = np.abs(sound).max()
if abs_max > 0: sound = sound.astype("float32")
sound *= 1 / 32768 if abs_max > 0:
sound = sound.squeeze() # depends on the use case sound *= 1 / 32768
return sound sound = sound.squeeze() # depends on the use case
return sound
except ValueError:
return sound
class VADState(Enum): class VADState(Enum):
@@ -266,17 +269,21 @@ class ThreadedTransport(AbstractTransport):
pass pass
def _silero_vad_analyze(self): def _silero_vad_analyze(self):
audio_chunk = self.read_audio_frames(self._vad_samples) try:
audio_int16 = np.frombuffer(audio_chunk, np.int16) audio_chunk = self.read_audio_frames(self._vad_samples)
audio_float32 = int2float(audio_int16) audio_int16 = np.frombuffer(audio_chunk, np.int16)
new_confidence = self.model( audio_float32 = int2float(audio_int16)
torch.from_numpy(audio_float32), 16000).item() new_confidence = self.model(
# yeses = int(new_confidence * 20.0) torch.from_numpy(audio_float32), 16000).item()
# nos = 20 - yeses # yeses = int(new_confidence * 20.0)
# out = "!" * yeses + "." * nos # nos = 20 - yeses
# print(f"!!! confidence: {out}") # out = "!" * yeses + "." * nos
speaking = new_confidence > 0.5 # print(f"!!! confidence: {out}")
return speaking speaking = new_confidence > 0.5
return speaking
except BaseException:
# This comes from an empty audio array
return False
def _vad(self): def _vad(self):
@@ -426,6 +433,10 @@ class ThreadedTransport(AbstractTransport):
asyncio.run_coroutine_threadsafe( asyncio.run_coroutine_threadsafe(
self.completed_queue.put(frame), self._loop self.completed_queue.put(frame), self._loop
) )
# Also send the EndFrame to the pipeline so it can stop
asyncio.run_coroutine_threadsafe(
self.receive_queue.put(frame), self._loop
)
return return
# if interrupted, we just pull frames off the queue and # if interrupted, we just pull frames off the queue and