Skip to content

Commit

Permalink
Trim audio frames
Browse files Browse the repository at this point in the history
  • Loading branch information
beastoin committed Sep 22, 2024
1 parent bd95410 commit 8465de6
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions backend/routers/transcribe.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,8 @@ async def receive_audio(dg_socket1, dg_socket2, soniox_socket, speechmatics_sock
# audio_file = open(path, "a")
try:
while websocket_active:
data = await websocket.receive_bytes()
raw_data = await websocket.receive_bytes()
data = raw_data[:]
# audio_buffer.extend(data)
if codec == 'opus' and sample_rate == 16000:
data = decoder.decode(bytes(data), frame_size=160)
Expand All @@ -283,7 +284,7 @@ async def receive_audio(dg_socket1, dg_socket2, soniox_socket, speechmatics_sock
dg_socket2.send(data)

# stream
stream_audio(data)
stream_audio(raw_data)

# audio_buffer = bytearray()

Expand Down Expand Up @@ -407,10 +408,10 @@ async def _post_process_memory(memory: Memory):
# Remove audio frames [start, end]
left = 0
if segment_start:
left = max(left, math.floor(segment_start * audio_frames_per_sec))
left = max(left, math.floor(segment_start) * audio_frames_per_sec)
right = processing_audio_frame_synced
if segment_end:
right = min(math.ceil(segment_end * audio_frames_per_sec), right)
right = min(math.ceil(segment_end) * audio_frames_per_sec, right)

file_path = f"_temp/{memory.id}_{uuid.uuid4()}_be"
create_wav_from_bytes(file_path=file_path, frames=processing_audio_frames[left:right],
Expand Down Expand Up @@ -585,8 +586,8 @@ async def _try_flush_new_memory(time_validate: bool = True):
# Longer 120s
now = time.time()
should_create_memory_time = True
timer_segment_start = timer_start + segment_start
if time_validate:
timer_segment_start = timer_start + segment_start
should_create_memory_time = timer_segment_start + segment_end + min_seconds_limit < now

# 1 words at least
Expand Down

0 comments on commit 8465de6

Please sign in to comment.