Skip to content

Commit

Permalink
Revert "Revert "only append audio if speech has started""
Browse files Browse the repository at this point in the history
This reverts commit 992ca8f.
  • Loading branch information
matthewkennedy5 committed Apr 4, 2024
1 parent 992ca8f commit d9db0ca
Showing 1 changed file with 19 additions and 16 deletions.
35 changes: 19 additions & 16 deletions openduck-py/openduck_py/response_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ def __init__(
self.response_task: Optional[asyncio.Task] = None
self.interrupt_event = asyncio.Event()
self.system_prompt = system_prompt
self.speech_has_started = False

if context is None:
context = {}
Expand Down Expand Up @@ -308,7 +309,6 @@ async def receive_audio(self, message: bytes):
audio_16k_np = audio_16k_np.astype(np.float32)

# TODO: Only append to audio_data if start of speech has been detected.
self.audio_data.append(audio_16k_np)
if self.record:
if self._time_of_last_record is None:
self._time_of_last_record = time()
Expand All @@ -326,29 +326,32 @@ async def receive_audio(self, message: bytes):
vad_result = self.vad(audio_16k_chunk)
if vad_result:
async with SessionAsync() as db:

transcription = ""
if "start" in vad_result or "end" in vad_result:
self.time_of_last_activity = time()
audio_data = np.concatenate(self.audio_data)

if "start" in vad_result:
self.speech_has_started = True
print("Detected start of speech", flush=True)
await log_event(
db,
self.session_id,
"detected_start_of_speech",
audio=audio_data,
)
# await log_event(
# db,
# self.session_id,
# "detected_start_of_speech",
# audio=audio_data,
# )
else:
self.speech_has_started = False
print("Detected end of speech", flush=True)
await log_event(
db,
self.session_id,
"detected_end_of_speech",
audio=audio_data,
)
# await log_event(
# db,
# self.session_id,
# "detected_end_of_speech",
# audio=audio_data,
# )

if self.speech_has_started:
self.audio_data.append(audio_16k_np)

audio_data = np.concatenate(self.audio_data)
transcription = await _transcribe(audio_data)
if not transcription:
continue
Expand Down

0 comments on commit d9db0ca

Please sign in to comment.