From 523a4e49d05be09ed4b29ac56aad14a364dab80b Mon Sep 17 00:00:00 2001 From: Mark Kremer Date: Sat, 3 Aug 2024 19:56:45 +0200 Subject: [PATCH] Simplify flac decoding --- flac/decode.go | 41 +++++++++++------------------------------ 1 file changed, 11 insertions(+), 30 deletions(-) diff --git a/flac/decode.go b/flac/decode.go index 339f2e0..5bdc5ca 100644 --- a/flac/decode.go +++ b/flac/decode.go @@ -1,7 +1,6 @@ package flac import ( - "fmt" "io" "github.com/mewkiz/flac" @@ -100,39 +99,21 @@ func (d *decoder) decodeFrameRangeInto(frame *frame.Frame, start, num int, into numChannels := d.stream.Info.NChannels s := 1 << (bps - 1) q := 1 / float64(s) - switch { - case bps == 8 && numChannels == 1: - for i := 0; i < num; i++ { - into[i][0] = float64(frame.Subframes[0].Samples[start+i]) * q - into[i][1] = into[i][0] - } - case bps == 16 && numChannels == 1: - for i := 0; i < num; i++ { - into[i][0] = float64(frame.Subframes[0].Samples[start+i]) * q - into[i][1] = into[i][0] - } - case bps == 24 && numChannels == 1: - for i := 0; i < num; i++ { - into[i][0] = float64(frame.Subframes[0].Samples[start+i]) * q - into[i][1] = into[i][0] - } - case bps == 8 && numChannels >= 2: - for i := 0; i < num; i++ { - into[i][0] = float64(frame.Subframes[0].Samples[start+i]) * q - into[i][1] = float64(frame.Subframes[1].Samples[start+i]) * q - } - case bps == 16 && numChannels >= 2: + + if numChannels == 1 { + samples1 := frame.Subframes[0].Samples[start:] for i := 0; i < num; i++ { - into[i][0] = float64(frame.Subframes[0].Samples[start+i]) * q - into[i][1] = float64(frame.Subframes[1].Samples[start+i]) * q + v := float64(samples1[i]) * q + into[i][0] = v + into[i][1] = v } - case bps == 24 && numChannels >= 2: + } else { + samples1 := frame.Subframes[0].Samples[start:] + samples2 := frame.Subframes[1].Samples[start:] for i := 0; i < num; i++ { - into[i][0] = float64(frame.Subframes[0].Samples[start+i]) * q - into[i][1] = float64(frame.Subframes[1].Samples[start+i]) * q + into[i][0] = float64(samples1[i]) * q + into[i][1] = float64(samples2[i]) * q } - default: - panic(fmt.Errorf("flac: support for %d bits-per-sample and %d channels combination not yet implemented", bps, numChannels)) } }