From 97aacbbb937cfe23fdadcfbc97caf26793450aa9 Mon Sep 17 00:00:00 2001 From: Robin Eklind Date: Wed, 1 Nov 2023 00:44:39 +0100 Subject: [PATCH] flac: restore original samples after encode for subframes with wasted bits ref: https://github.com/mewkiz/flac/pull/66#issuecomment-1788175847 --- encode_subframe.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/encode_subframe.go b/encode_subframe.go index 30f0c20..f74b0d4 100644 --- a/encode_subframe.go +++ b/encode_subframe.go @@ -22,11 +22,16 @@ func encodeSubframe(bw *bitio.Writer, hdr frame.Header, subframe *frame.Subframe bps -= subframe.Wasted // Right shift to account for wasted bits-per-sample. - // TODO: figure out how to make this non-destructive (use defer to restore original samples?). if subframe.Wasted > 0 { for i, sample := range subframe.Samples { subframe.Samples[i] = sample >> subframe.Wasted } + // NOTE: use defer to restore original samples after encode. + defer func() { + for i, sample := range subframe.Samples { + subframe.Samples[i] = sample << subframe.Wasted + } + }() } // Encode audio samples.