wave: github.com/bakerolls/wave Index | Examples | Files

package wave

import "github.com/bakerolls/wave"

Package wave contains an encode and a decoder for wave files.



Package Files

io.go reader.go wave.go writer.go

type Config Uses

type Config struct {
    RIFF   *riffHeader
    Format *formatChunk

Config contains the RIFF and FMT chunks but no samples.

func New Uses

func New(numChans, sampleRate, bitsPerSample int) *Config

New generates a new wave config containing relevant header fields.

type Reader Uses

type Reader struct {
    // contains filtered or unexported fields

Reader decodes and reads a wave.

func NewReader Uses

func NewReader(r io.Reader) (*Reader, error)

NewReader decodes RIFF, WAVE and fmt headers and creates a Reader.

Decode wave audio from an io.Reader.


// This example is borrowed from http://soundfile.sapp.org/doc/WaveFormat/.
r := bytes.NewReader([]byte{
    // R,    I,    F,    F,                   2084,    W,    A,    V,    E,
    0x52, 0x49, 0x46, 0x46, 0x24, 0x08, 0x00, 0x00, 0x57, 0x41, 0x56, 0x45,

    // F,    M,    T,    ␣,                     16,          1,          2,
    0x66, 0x6d, 0x74, 0x20, 0x10, 0x00, 0x00, 0x00, 0x01, 0x00, 0x02, 0x00,
    //               22050,                  88200,          4,         16,
    0x22, 0x56, 0x00, 0x00, 0x88, 0x58, 0x01, 0x00, 0x04, 0x00, 0x10, 0x00,

    // s,    l,    n,    t,                      4,
    0x73, 0x6c, 0x6e, 0x74, 0x04, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,

    // d,    a,    t,    a,                     26,          0,          0,
    0x64, 0x61, 0x74, 0x61, 0x1a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    //    5924,      -3298,       4924,       5180,      -1770,      -1768,
    0x24, 0x17, 0x1e, 0xf3, 0x3c, 0x13, 0x3c, 0x14, 0x16, 0xf9, 0x18, 0xf9,
    //   -6348,     -23005,      -3524,      -3548,     -12783,       3354,
    0x34, 0xe7, 0x23, 0xa6, 0x3c, 0xf2, 0x24, 0xf2, 0x11, 0xce, 0x1a, 0x0d,

wavr, err := wave.NewReader(r)
if err != nil {
cfg := wavr.Config()
fmt.Printf("%+v\n", cfg.RIFF)
fmt.Printf("%+v\n", cfg.Format)

var samples []int
for {
    sample, err := wavr.ReadSample()
    if err != nil {
        if err == io.EOF {
    samples = append(samples, sample)


&{chunk:{ID:RIFF Size:2084} Format:WAVE}
&{chunk:{ID:fmt  Size:16} AudioFormat:1 NumChans:2 SampleRate:22050 ByteRate:88200 BlockAlign:4 BitsPerSample:16}
[0 0 5924 -3298 4924 5180 -1770 -1768 -6348 -23005 -3524 -3548 -12783 3354]

func (*Reader) Config Uses

func (r *Reader) Config() *Config

Config returns the wave files config.

func (*Reader) Read16 Uses

func (r *Reader) Read16() (int16, error)

Read16 reads a 16 bit sample.

func (*Reader) Read32 Uses

func (r *Reader) Read32() (int32, error)

Read32 reads a 32 bit sample.

func (*Reader) Read8 Uses

func (r *Reader) Read8() (int8, error)

Read8 reads a 8 bit sample.

func (*Reader) ReadSample Uses

func (r *Reader) ReadSample() (int, error)

ReadSample chooses the correct reader and returns its value as an int.

type Writer Uses

type Writer struct {
    // contains filtered or unexported fields

Writer encodes and writes a wave.

func NewWriter Uses

func NewWriter(ws io.WriteSeeker, cfg *Config) (*Writer, error)

NewWriter returns a new Writer and encodes the config.

Generate a new wave encoder with one channel (mono) at 44.1kHz and 2 bytes per sample. Then create a low sine wave.


var w io.WriteSeeker

numChans := 1
sampleRate := 44100
wav := wave.New(numChans, sampleRate, 16)
wavw, err := wave.NewWriter(w, wav)
if err != nil {
defer wavw.Close()

for i := 0; i < sampleRate*numChans; i += numChans {
    sample := int16(math.MaxInt16 * math.Sin(float64(i)/100.0))
    for c := 0; c < numChans; c++ {

func (*Writer) Close Uses

func (w *Writer) Close() error

Close saves the file size and closes the writer.

func (*Writer) Write Uses

func (w *Writer) Write(sample []byte) error

Write writes to the data chunk.

func (*Writer) Write16 Uses

func (w *Writer) Write16(sample int16) error

Write16 writes a 16 bit number to the data chunk.

func (*Writer) Write32 Uses

func (w *Writer) Write32(sample int32) error

Write32 writes a 32 bit number to the data chunk.

func (*Writer) Write8 Uses

func (w *Writer) Write8(sample int8) error

Write8 writes a 8 bit number to the data chunk.

func (*Writer) WriteSample Uses

func (w *Writer) WriteSample(sample int) error

WriteSample chooses the correct writer.

Package wave imports 3 packages (graph). Updated 2019-02-26. Refresh now. Tools for package owners.