Skip to content

Commit

Permalink
flac: Add option to see sample details like residuals and verbatim va…
Browse files Browse the repository at this point in the history
…lues
  • Loading branch information
wader committed Oct 30, 2023
1 parent 7c8fa05 commit 824ad24
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 13 deletions.
64 changes: 52 additions & 12 deletions format/flac/flac_frame.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
})
}
Expand Down Expand Up @@ -466,25 +467,48 @@ 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)
n++
}
}
} 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<<riceParameter | low)
n++
if ffi.SampleDetails {
d.FieldArray("residuals", func(d *decode.D) {
for i := 0; i < count; i++ {
d.FieldStruct("residual", func(d *decode.D) {
high := d.FieldUnary("high", 0)
low := d.FieldU("low", riceParameter)
residual := mathex.ZigZag[uint64, int64](high<<riceParameter | low)
d.FieldValueSint("value", residual)
samples[n] = residual
})
n++
}
})
} 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<<riceParameter | low)
n++
}
samplesStop := d.Pos()
d.RangeFn(samplesStart, samplesStop-samplesStart, func(d *decode.D) {
d.FieldRawLen("residuals", d.BitsLeft())
})
}
samplesStop := d.Pos()
d.RangeFn(samplesStart, samplesStop-samplesStart, func(d *decode.D) {
d.FieldRawLen("samples", d.BitsLeft())
})
}
})
}
Expand Down Expand Up @@ -516,7 +540,15 @@ func frameDecode(d *decode.D) any {
// <n*i> 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++ {
Expand Down Expand Up @@ -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)
})
}
Expand Down
3 changes: 2 additions & 1 deletion format/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 824ad24

Please sign in to comment.