riff

package
Version: v0.0.0-...-b37319a Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 19, 2020 License: GPL-3.0 Imports: 4 Imported by: 0

Documentation

Overview

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

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Reader

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

Reader reads a RIFF file chunk by chunk.

Example
package main

import (
	"bytes"
	"fmt"
	"io"
	"log"

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

func exampleInt16WaveReader() io.ReadSeeker {

	return bytes.NewReader([]byte{

		0x52, 0x49, 0x46, 0x46, 0x24, 0x08, 0x00, 0x00, 0x57, 0x41, 0x56, 0x45,

		0x66, 0x6d, 0x74, 0x20, 0x10, 0x00, 0x00, 0x00, 0x01, 0x00, 0x02, 0x00,

		0x22, 0x56, 0x00, 0x00, 0x88, 0x58, 0x01, 0x00, 0x04, 0x00, 0x10, 0x00,

		0x73, 0x6c, 0x6e, 0x74, 0x04, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,

		0x64, 0x61, 0x74, 0x61, 0x1c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,

		0x24, 0x17, 0x1e, 0xf3, 0x3c, 0x13, 0x3c, 0x14, 0x16, 0xf9, 0x18, 0xf9,

		0x34, 0xe7, 0x23, 0xa6, 0x3c, 0xf2, 0x24, 0xf2, 0x11, 0xce, 0x1a, 0x0d,

		0x64, 0x61, 0x74, 0x61, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,

		0x24, 0x17, 0x1e, 0xf3, 0x3c, 0x13, 0x3c, 0x14, 0x16, 0xf9, 0x18, 0xf9,
	})
}

func main() {
	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

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

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

func (rr Reader) Error() error

Err returns the first non-EOF error.

func (*Reader) Next

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

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

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

Example
package main

import (
	"fmt"
	"io/ioutil"
	"log"

	"github.com/bake/wave/riff"
	"github.com/orcaman/writerseeker"
)

func main() {
	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

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

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

func (*Writer) Chunk

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

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

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

Seek in the writer.

func (*Writer) Write

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

Write to the chunk.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL