From 31772fdae1fd7a6cf5ba52b7ba8277869a12fc0f Mon Sep 17 00:00:00 2001 From: Steven Hartland Date: Thu, 6 Apr 2023 21:12:22 +0100 Subject: [PATCH 1/2] fix: chunk.Size excludes padding Fix chunk.Size to match the definition, removing the additional padding byte which was previously present. This allows consumers to create cleaner parsing which relies on io.EOF instead of having to also check for a padding byte. Also: * Fix some typos. * Clarify that IsFullyRead doesn't include the padding byte. Fixes #2 --- chunk.go | 15 ++++++++++++--- parser.go | 7 ------- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/chunk.go b/chunk.go index 9189281..7774beb 100644 --- a/chunk.go +++ b/chunk.go @@ -9,7 +9,7 @@ import ( "sync" ) -// Chunk represents the header and containt of a sub block +// Chunk represents the header and content of a sub block // See https://tech.ebu.ch/docs/tech/tech3285.pdf to see how // audio content is stored in a BWF/WAVE file. type Chunk struct { @@ -47,7 +47,7 @@ func (ch *Chunk) DecodeWavHeader(p *Parser) error { } // if we aren't dealing with a PCM file, we advance to reader to the - // end of the chunck. + // end of the chunk. if ch.Size > 16 { extra := make([]byte, ch.Size-16) ch.ReadLE(&extra) @@ -67,7 +67,9 @@ func (ch *Chunk) Done() { } } -// IsFullyRead checks if we're finished reading the chunk +// IsFullyRead checks if we're finished reading the chunk data. +// This doesn't include any padding byte. Drain should be used to +// ensure that has also been read. func (ch *Chunk) IsFullyRead() bool { if ch == nil || ch.R == nil { return true @@ -119,6 +121,13 @@ func (ch *Chunk) ReadByte() (byte, error) { // Drain discards the rest of the chunk func (ch *Chunk) Drain() { bytesAhead := ch.Size - ch.Pos + // all RIFF chunks (including WAVE "data" chunks) must be word aligned. + // If the data has an odd number of bytes a padding byte with a value + // of zero must be placed at the end of the sample data. + // The "data" chunk header's size does not include this byte. + if ch.Size%2 == 1 { + bytesAhead++ + } for bytesAhead > 0 { readSize := int64(bytesAhead) diff --git a/parser.go b/parser.go index ee11584..44f3765 100644 --- a/parser.go +++ b/parser.go @@ -133,13 +133,6 @@ func (c *Parser) NextChunk() (*Chunk, error) { return nil, err } - // all RIFF chunks (including WAVE "data" chunks) must be word aligned. - // If the data uses an odd number of bytes, a padding byte with a value of zero must be placed at the end of the sample data. - // The "data" chunk header's size should not include this byte. - if size%2 == 1 { - size++ - } - ch := &Chunk{ ID: id, Size: int(size), From eaa6baa0e4ead6ea4f80780dcf0a535136e1df5c Mon Sep 17 00:00:00 2001 From: Steven Hartland Date: Thu, 6 Apr 2023 21:30:15 +0100 Subject: [PATCH 2/2] fix: godoc link Fix the godoc link in README.md. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index c291c42..8fa15b3 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Resource Interchange File Format parser -[![GoDoc](http://godoc.org/github.com/go-audio/riff?status.svg)](http://godoc.org/go-audio/riff) +[![GoDoc](http://godoc.org/github.com/go-audio/riff?status.svg)](http://godoc.org/github.com/go-audio/riff) [![Build Status](https://travis-ci.org/go-audio/riff.png)](https://travis-ci.org/go-audio/riff)