Skip to content

Commit

Permalink
frame: Fix decoding of FLAC files with wasted bits-per-sample.
Browse files Browse the repository at this point in the history
Fixes issue #12.
  • Loading branch information
mewmew committed Feb 2, 2016
1 parent be53387 commit 154b8f6
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 13 deletions.
5 changes: 5 additions & 0 deletions frame/frame_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ var golden = []struct {
{
name: "../testdata/189983.flac",
},

// i=3
{
name: "../testdata/love.flac",
},
}

func TestFrameHash(t *testing.T) {
Expand Down
14 changes: 7 additions & 7 deletions frame/subframe.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ func (frame *Frame) parseSubframe(bps uint) (subframe *Subframe, err error) {
if err != nil {
return subframe, err
}
// Adjust bps of subframe for wasted bits-per-sample.
bps -= subframe.Wasted

// Decode subframe audio samples.
subframe.NSamples = int(frame.BlockSize)
Expand All @@ -52,8 +54,7 @@ func (frame *Frame) parseSubframe(bps uint) (subframe *Subframe, err error) {

// Left shift to accout for wasted bits-per-sample.
for i, sample := range subframe.Samples {
// TODO: Verify after a FLAC file with wasted bits-per-sample has been found.
subframe.Samples[i] = sample << subframe.WastedBits
subframe.Samples[i] = sample << subframe.Wasted
}

return subframe, err
Expand All @@ -69,7 +70,7 @@ type SubHeader struct {
// Prediction order used by fixed and FIR linear prediction decoding.
Order int
// Wasted bits-per-sample.
WastedBits uint
Wasted uint
}

// parseHeader reads and parses the header of a subframe.
Expand Down Expand Up @@ -138,14 +139,13 @@ func (subframe *Subframe) parseHeader(br *bits.Reader) error {
return unexpected(err)
}
if x != 0 {
// The number of wasted bits-per-sample is unary coded.
// k wasted bits-per-sample in source subblock, k-1 follows, unary coded;
// e.g. k=3 => 001 follows, k=7 => 0000001 follows.
x, err = br.ReadUnary()
if err != nil {
return unexpected(err)
}
subframe.WastedBits = uint(x)
log.Printf("frame.Subframe.parseHeader: Never seen a FLAC file contain wasted-bits-per-sample before. I want to dissect one of those files. Please send it to me :)")
return nil
subframe.Wasted = uint(x) + 1
}

return nil
Expand Down
11 changes: 5 additions & 6 deletions testdata/README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
public domain
-------------
## Public domain

The testcase sounds have all been released into the *[public domain][]*.
The testcase sounds have all been released into the [public domain].

[public domain]: https://creativecommons.org/publicdomain/zero/1.0/

The source of each sound is listed below:

* [59996.flac][]
* [172960.flac][]
* [189983.flac][]
* [59996.flac]
* [172960.flac]
* [189983.flac]

[59996.flac]: http://www.freesound.org/people/qubodup/sounds/59996/
[172960.flac]: http://www.freesound.org/people/qubodup/sounds/172960/
Expand Down
Binary file added testdata/love.flac
Binary file not shown.

0 comments on commit 154b8f6

Please sign in to comment.