reuse the stereo Opus encoder across connections

Every connect built a fresh stereo encoder for file playback. Each one holds
a little under a megabyte of encoder state, so on a flaky link that is close
to a megabyte of churn per reconnect for no benefit; the encoder is already
reset when file playback ends.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
Brandon McGinty
2026-08-24 12:32:30 -04:00
co-authored by Claude Opus 5
parent 5cdb2684b5
commit 0d62f745ca
2 changed files with 10 additions and 2 deletions
+4
View File
@@ -70,6 +70,10 @@ type Barnard struct {
// Added for file playback
FileStream *fileplayback.Player
FileStreamMutex sync.Mutex
// stereoEncoder is reused across connections. Each one holds a little
// under a megabyte of encoder state, so building a fresh one per
// reconnect is pure churn; it is reset when file playback ends.
stereoEncoder gumble.AudioEncoder
// Added for tone test mode (bypasses all soundcard/OpenAL)
ToneTest bool
+6 -2
View File
@@ -108,8 +108,12 @@ func (b *Barnard) connect(reconnect bool) bool {
}
})
// Initialize stereo encoder for file playback
b.Client.SetStereoEncoder(opus.NewStereoEncoder())
// Initialize stereo encoder for file playback, reusing the one built for
// the previous connection rather than allocating another.
if b.stereoEncoder == nil {
b.stereoEncoder = opus.NewStereoEncoder()
}
b.Client.SetStereoEncoder(b.stereoEncoder)
// Initialize file player
b.FileStreamMutex.Lock()