fix: fix double channel-counting in Opus decoder buffer allocation
AudioMaximumFrameSize (5760) already accounts for stereo by including the AudioChannels factor. The Decode method was multiplying by AudioChannels again, allocating 11520 int16s per decode call instead of the needed 5760. This wasted ~21KB per audio packet (2MB/s at 100 pps). Use frameSize directly and derive total sample count from the decoder's configured channel count.
This commit is contained in:
committed by
Brandon McGinty
parent
80580d2a3c
commit
cd968d7530
+9
-5
@@ -97,17 +97,21 @@ func (*Decoder) ID() int {
|
||||
}
|
||||
|
||||
func (d *Decoder) Decode(data []byte, frameSize int) ([]int16, error) {
|
||||
// Allocate buffer for stereo - frameSize is per channel
|
||||
pcm := make([]int16, frameSize*gumble.AudioChannels)
|
||||
// frameSize is the maximum number of PCM samples (all channels
|
||||
// combined). The underlying Opus decoder output is interleaved
|
||||
// stereo, so the buffer holds left+right pairs.
|
||||
pcm := make([]int16, frameSize)
|
||||
|
||||
// Decode the data
|
||||
// Decode the data. If data is nil/empty, the decoder performs
|
||||
// Packet Loss Concealment and produces a concealed frame.
|
||||
n, err := d.Decoder.Decode(data, pcm)
|
||||
if err != nil {
|
||||
return []int16{}, err
|
||||
}
|
||||
|
||||
// Return the exact number of samples decoded
|
||||
return pcm[:n*gumble.AudioChannels], nil
|
||||
// n is the number of samples per channel; stereo interleaved
|
||||
// output means total samples = n * channels.
|
||||
return pcm[:n*d.channels], nil
|
||||
}
|
||||
|
||||
func (d *Decoder) Reset() {
|
||||
|
||||
Reference in New Issue
Block a user