From c9d71576f54ad5ec7e0f6794944265373e2a37f0 Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Fri, 16 Sep 2022 11:36:25 +0200 Subject: [PATCH] Fix support for mono and 5.1 Vorbis files The code no longer assumes Vorbis files are stereo (they may have up to 255 channels), which caused e.g. mono files to play at twice their speed. The current strategy for files with more than 2 channels is to only play the first and last channels, and drop the rest. --- vorbis/decode.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/vorbis/decode.go b/vorbis/decode.go index 3419211..a9ac694 100644 --- a/vorbis/decode.go +++ b/vorbis/decode.go @@ -10,7 +10,6 @@ import ( ) const ( - govorbisNumChannels = 2 govorbisPrecision = 2 ) @@ -31,16 +30,18 @@ func Decode(rc io.ReadCloser) (s beep.StreamSeekCloser, format beep.Format, err } format = beep.Format{ SampleRate: beep.SampleRate(d.SampleRate()), - NumChannels: govorbisNumChannels, + NumChannels: d.Channels(), Precision: govorbisPrecision, } - return &decoder{rc, d, format, nil}, format, nil + buffer := make([]float32, d.Channels()) + return &decoder{rc, d, format, buffer, nil}, format, nil } type decoder struct { closer io.Closer d *oggvorbis.Reader f beep.Format + buf []float32 err error } @@ -48,11 +49,10 @@ func (d *decoder) Stream(samples [][2]float64) (n int, ok bool) { if d.err != nil { return 0, false } - var tmp [2]float32 for i := range samples { - dn, err := d.d.Read(tmp[:]) - if dn == 2 { - samples[i][0], samples[i][1] = float64(tmp[0]), float64(tmp[1]) + dn, err := d.d.Read(d.buf[:]) + if dn == d.f.NumChannels { + samples[i][0], samples[i][1] = float64(d.buf[0]), float64(d.buf[len(d.buf) - 1]) n++ ok = true }