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

fix: chunk.Size excludes padding #3

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
15 changes: 12 additions & 3 deletions chunk.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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)

Expand Down
7 changes: 0 additions & 7 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down