diff --git a/enc_test.go b/enc_test.go index a88cacb..de1acb6 100644 --- a/enc_test.go +++ b/enc_test.go @@ -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", diff --git a/internal/bits/unary.go b/internal/bits/unary.go index b7d7250..b4a81fc 100644 --- a/internal/bits/unary.go +++ b/internal/bits/unary.go @@ -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 } diff --git a/internal/bits/unary_test.go b/internal/bits/unary_test.go new file mode 100644 index 0000000..2da80fa --- /dev/null +++ b/internal/bits/unary_test.go @@ -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) + } + } +}