Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Encode lpc fix for unary #68

Merged
merged 3 commits into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions enc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,11 @@ func TestEncode(t *testing.T) {
"testdata/flac-test-files/subset/41 - 6 channels (5.1).flac",
"testdata/flac-test-files/subset/42 - 7 channels (6.1).flac",
"testdata/flac-test-files/subset/43 - 8 channels (7.1).flac",
// TODO: check why there is an audio sample diff (they look identical,
// except for using `sample_rate: 0b1100 (end of header (8 bit*1000))`
// for orig and `sample_rate: 192000 (0b11)` for mewkiz/flac); orig md5:
// cdf531d4d4b95233986bc499518a89db, output md5:
// 678a704da087bf8e98a19f1ad47ca359.
// NOTE: the only diff is that "44 - ...flac" uses `0b1100 (end of header
// (8 bit*1000))` to encode the sample rate at the end of the header,
// whereas mewkiz/flac encodes it directly `192000 (0b11)`. Notably, the
// computed md5 hash of the decoded audio samples is identical
// (MD5: cdf531d4d4b95233986bc499518a89db). Thus, ignore the test case.
//"testdata/flac-test-files/subset/44 - 8-channel surround, 192kHz, 24 bit, using only 32nd order predictors.flac",
"testdata/flac-test-files/subset/45 - no total number of samples set.flac",
"testdata/flac-test-files/subset/46 - no min-max framesize set.flac",
Expand Down
11 changes: 7 additions & 4 deletions internal/bits/unary.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,14 @@ func (br *Reader) ReadUnary() (x uint64, err error) {
// 5 => 000001
// 6 => 0000001
func WriteUnary(bw *bitio.Writer, x uint64) error {
bits := uint64(1)
n := byte(1)
for ; x > 0; x-- {
n++
for ; x > 8; x -= 8 {
if err := bw.WriteByte(0x0); err != nil {
return err
}
}

bits := uint64(1)
n := byte(x + 1)
if err := bw.WriteBits(bits, n); err != nil {
return err
}
Expand Down
36 changes: 36 additions & 0 deletions internal/bits/unary_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package bits_test

import (
"bytes"
"github.com/icza/bitio"
"github.com/mewkiz/flac/internal/bits"
"testing"
)

func TestUnary(t *testing.T) {
w := new(bytes.Buffer)
bw := bitio.NewWriter(w)

var want uint64
for ; want < 1000; want++ {
// Write unary
if err := bits.WriteUnary(bw, want); err != nil {
t.Fatalf("unable to write unary; %v", err)
}
// Flush buffer
if err := bw.Close(); err != nil {
t.Fatalf("unable to close (flush) the bit buffer; %v", err)
}

// Read written unary
r := bits.NewReader(w)
got, err := r.ReadUnary()
if err != nil {
t.Fatalf("unable to read unary; %v", err)
}

if got != want {
t.Fatalf("the written and read unary doesn't match the original value, got: %v, expected: %v", got, want)
}
}
}