wave: github.com/bake/wave/riff Index | Examples | Files

package riff

import "github.com/bake/wave/riff"

Package riff contains implementations for a reader and a writer for RIFF files.

Index

Examples

Package Files

reader.go riff.go writer.go

type Reader Uses

type Reader struct {
    // contains filtered or unexported fields
}

Reader reads a RIFF file chunk by chunk.

Code:

r := exampleInt16WaveReader()
rr, riffType, err := riff.NewReader(r)
if err != nil {
    log.Fatal(err)
}
fmt.Printf("type: %s\n", riffType)
for rr.Next() {
    id, size, data := rr.Chunk()
    body := make([]byte, size)
    if _, err := data.Read(body); err != nil {
        log.Fatal(err)
    }
    switch id {
    case "fmt ", "slnt":
        fmt.Printf("%s: % x\n", id, body)
    default:
        fmt.Printf("%s: ...\n", id)
    }
}
if err := rr.Error(); err != nil {
    log.Fatal(err)
}

Output:

type: WAVE
fmt : 01 00 02 00 22 56 00 00 88 58 01 00 04 00 10 00
slnt: ff ff ff ff
data: ...
data: ...

func NewReader Uses

func NewReader(r io.Reader) (rr *Reader, riffType string, err error)

NewReader reads the initial RIFF header and returns a chunk reader and its type.

func (*Reader) Chunk Uses

func (rr *Reader) Chunk() (id string, size int64, data io.Reader)

Chunk returns the current chunk. This function can be called multiple times.

func (Reader) Error Uses

func (rr Reader) Error() error

Err returns the first non-EOF error.

func (*Reader) Next Uses

func (rr *Reader) Next() bool

Next returns true until the underlying reader returns an error like EOF. The caller is responsible to read or seek to the end of the chunk before calling Next again.

type Writer Uses

type Writer struct {
    // contains filtered or unexported fields
}

Writer extends an io.WriteSeeker by the ability to write in chunks.

Code:

ws := &writerseeker.WriterSeeker{}
rw, err := riff.NewWriter(ws, "WAVE")
if err != nil {
    log.Fatalf("could not create new riff writer: %v", err)
}

cw, err := rw.Chunk("foo1")
if err != nil {
    log.Fatalf("could not create chunk: %v", err)
}
if _, err = cw.Write([]byte{0xff, 0xff}); err != nil {
    log.Fatalf("could not write data: %v", err)
}
if err := cw.Close(); err != nil {
    log.Fatalf("could not close chunk: %v", err)
}

// Could (or should) be deferred.
if err := rw.Close(); err != nil {
    log.Fatalf("could not close reader: %v", err)
}

body, _ := ioutil.ReadAll(ws.Reader())
fmt.Printf("% x\n", body)

Output:

52 49 46 46 0e 00 00 00 57 41 56 45 66 6f 6f 31 02 00 00 00 ff ff

func NewWriter Uses

func NewWriter(ws io.WriteSeeker, riffType string) (*Writer, error)

NewWriter creates a new RIFF writer and writes the initial RIFF chunk.

func (*Writer) Chunk Uses

func (w *Writer) Chunk(chunkType string) (*Writer, error)

Chunk creates a new chunk that has to be closed before creating a second one or closing the RIFF writer.

func (*Writer) Close Uses

func (w *Writer) Close() error

Close seeks to the chunks beginning, writes its sice and seeks back to the writers end. The underlying io.WriteCloser has to be closed separately.

func (*Writer) Seek Uses

func (w *Writer) Seek(offset int64, whence int) (int64, error)

Seek in the writer.

func (*Writer) Write Uses

func (w *Writer) Write(p []byte) (n int, err error)

Write to the chunk.

Package riff imports 4 packages (graph) and is imported by 1 packages. Updated 2019-09-09. Refresh now. Tools for package owners.