From 824ad247ec3d3312078ca0dfd492892d2e169148 Mon Sep 17 00:00:00 2001 From: Mattias Wadman Date: Mon, 30 Oct 2023 12:31:18 +0100 Subject: [PATCH] flac: Add option to see sample details like residuals and verbatim values --- format/flac/flac_frame.go | 64 +++++++++++++++++++++++++++++++-------- format/format.go | 3 +- 2 files changed, 54 insertions(+), 13 deletions(-) diff --git a/format/flac/flac_frame.go b/format/flac/flac_frame.go index 2e63f4d493..bb0ea31335 100644 --- a/format/flac/flac_frame.go +++ b/format/flac/flac_frame.go @@ -20,6 +20,7 @@ func init() { DecodeFn: frameDecode, DefaultInArg: format.FLAC_Frame_In{ BitsPerSample: 16, // TODO: maybe should not have a default value? + SampleDetails: false, }, }) } @@ -466,7 +467,15 @@ func frameDecode(d *decode.D) any { n += count } else { d.RangeFn(d.Pos(), int64(count*escapeSampleSize), func(d *decode.D) { - d.FieldRawLen("samples", int64(count*escapeSampleSize)) + if ffi.SampleDetails { + d.FieldArray("residuals", func(d *decode.D) { + for i := 0; i < count; i++ { + d.FieldS("residual", escapeSampleSize) + } + }) + } else { + d.FieldRawLen("residuals", int64(count*escapeSampleSize)) + } }) for j := 0; j < count; j++ { samples[n] = d.S(escapeSampleSize) @@ -474,17 +483,32 @@ func frameDecode(d *decode.D) any { } } } else { - samplesStart := d.Pos() - for j := 0; j < count; j++ { - high := d.Unary(0) - low := d.U(riceParameter) - samples[n] = mathex.ZigZag[uint64, int64](high< Unencoded subblock; n = frame's bits-per-sample, i = frame's blocksize. // TODO: refactor into some kind of FieldBitBufLenFn? d.RangeFn(d.Pos(), int64(blockSize*subframeSampleSize), func(d *decode.D) { - d.FieldRawLen("samples", d.BitsLeft()) + if ffi.SampleDetails { + d.FieldArray("samples", func(d *decode.D) { + for i := 0; i < subframeSampleSize; i++ { + d.FieldS("sample", subframeSampleSize) + } + }) + } else { + d.FieldRawLen("samples", d.BitsLeft()) + } }) for i := 0; i < blockSize; i++ { @@ -565,6 +597,14 @@ func frameDecode(d *decode.D) any { } } + if ffi.SampleDetails { + d.FieldArray("samples", func(d *decode.D) { + for i := 0; i < len(samples); i++ { + d.FieldValueSint("sample", samples[i]) + } + }) + } + channelSamples = append(channelSamples, samples) }) } diff --git a/format/format.go b/format/format.go index b87fa4b212..bb4cef9ea7 100644 --- a/format/format.go +++ b/format/format.go @@ -199,7 +199,8 @@ type CAFF_In struct { type FLAC_Frame_In struct { SamplesBuf []byte - BitsPerSample int `doc:"Bits per sample"` + BitsPerSample int `doc:"Bits per sample"` + SampleDetails bool `doc:"Decode more sample details like residuals etc"` } type FLAC_Frame_Out struct {