Skip to content

Commit

Permalink
Fix potential infinite loop if <1s of audio buffered in receiver
Browse files Browse the repository at this point in the history
  • Loading branch information
dharmab committed Aug 31, 2024
1 parent 7f93500 commit 03c1f83
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions pkg/simpleradio/audio/receive.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,15 @@ func (c *audioClient) receiveVoice(ctx context.Context, in <-chan []byte, out ch
// Check if there is enough in the buffer and that we've consumed all queued packets. Then check if we've passed the receive deadline.
// If so, we have a tranmission ready to publish for audio decoding.
// 25 packets * 40ms = 1s which is the minimum whisper can transcribe anyway.
if len(buf) > 25 && len(in) == 0 && time.Now().After(c.lastRx.deadline) {
if len(in) == 0 && time.Now().After(c.lastRx.deadline) {
log.Trace().Int("bufferLength", len(buf)).Uint64("lastPacketID", c.lastRx.packetNumber).Str("lastOrigin", string(c.lastRx.origin)).Msg("passed receive deadline with packets in buffer")
audio := make([]voice.VoicePacket, len(buf))
copy(audio, buf)
out <- audio
if len(buf) > 25 {
audio := make([]voice.VoicePacket, len(buf))
copy(audio, buf)
out <- audio
} else {
log.Warn().Int("bufferLength", len(buf)).Msg("audio in buffer is too short to publish, discarding buffer")
}
// Reset receiver state
buf = make([]voice.VoicePacket, 0)
c.resetLastRx()
Expand Down

0 comments on commit 03c1f83

Please sign in to comment.