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: ...
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 ¶
Chunk creates a new chunk that has to be closed before creating a second one or closing the RIFF writer.
func (*Writer) Close ¶
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.