wave

package module
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

README

wave

GoDoc Go Report Card codecov

Package wave offers a simplified API to read and write WAVE files by only allowing to work with samples directly. The underlying package riff contains implementations for a reader and a writer for RIFF files.

Reader example

Create a new WAVE reader by wrapping it around an io.Reader, optionally a buffered one.

r, err := os.Open("audio.wav")
if err != nil {
  log.Fatalf("could not open file: %v", err)
}
buf := bufio.NewReader(r)
wavr, err := wave.NewReader(buf)
if err != nil {
  log.Fatalf("could not create wave reader: %v", err)
}

Read the samples one by one or into a slice of integers. The wave.Reader skips all non-data chunks.

for {
  sample, err := wavr.Sample()
  if err == io.EOF {
    break
  }
  if err != nil {
    log.Fatalf("could not read sample: %v", err)
  }
  fmt.Println(sample)
}
samples, err := wavr.Samples()
if err != nil {
  log.Fatalf("could not read samples: %v", err)
}

Writer example

Create a new WAVE writer by wrapping it around an io.WriteSeeker. This one is automatically buffered.

format := wave.Format{
  AudioFormat:   1,
  NumChans:      2,
  SampleRate:    44100,
  ByteRate:      176400,
  BlockAlign:    4,
  BitsPerSample: 16,
}
samples := []int{
  0, 0, 5924, -3298, 4924, 5180, -1770, -1768,
  -6348, -23005, -3524, -3548, -12783, 3354,
  0, 0, 5924, -3298, 4924, 5180, -1770, -1768,
}
w, err := os.Create("audio.wav")
if err != nil {
  log.Fatalf("could not create file: %v", err)
}
defer w.Close()
wavw, err := wave.NewWriter(w, format)
if err != nil {
  log.Fatalf("could not create wave writer: %v", err)
}
defer wavw.Close()

Write the samples one by one or as a slice of integers.

for _, s := range samples {
  if err := wavw.Sample(s); err != nil {
    log.Fatalf("could not write sample %d: %v", s, err)
  }
}
if err := wavw.Samples(samples); err != nil {
  log.Fatalf("could not write samples: %v", err)
}

Before creating a new chunk, the current one has to be closed which automatically writes its size.

Documentation

Overview

Package wave offers a simplified API to read and write WAVE files by only allowing to work with samples directly. The underlying 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 Format

type Format struct {
	AudioFormat   uint16 // 1 if PCM is used.
	NumChans      uint16 // Number of channels (1 = mono, 2 = stereo, ...)
	SampleRate    uint32 // Samples per second (44100, ...).
	ByteRate      uint32 // Average bytes per second.
	BlockAlign    uint16 // Bytes per sample.
	BitsPerSample uint16 // Bits per sample.
}

Format holds configuration about the WAVE.

type Reader

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

Reader reads samples from a WAVE file.

Example
package main

import (
	"bytes"
	"fmt"
	"log"

	"github.com/bake/wave"
)

func main() {
	r := bytes.NewReader([]byte{
		// R,    I,    F,    F,                     76,    W,    A,    V,    E,
		0x52, 0x49, 0x46, 0x46, 0x50, 0x00, 0x00, 0x00, 0x57, 0x41, 0x56, 0x45,

		// f,    m,    t,    ␣,                     16,          1,          2,
		0x66, 0x6d, 0x74, 0x20, 0x10, 0x00, 0x00, 0x00, 0x01, 0x00, 0x02, 0x00,
		//               44100,                 176400,          4,         16,
		0x44, 0xac, 0x00, 0x00, 0x10, 0xb1, 0x02, 0x00, 0x04, 0x00, 0x10, 0x00,

		// d,    a,    t,    a,                     44,          0,          0,
		0x64, 0x61, 0x74, 0x61, 0x2c, 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,
		//       0,          0,       5924,      -3298,       4924,       5180,
		0x00, 0x00, 0x00, 0x00, 0x24, 0x17, 0x1e, 0xf3, 0x3c, 0x13, 0x3c, 0x14,
		//   -1770,      -1768,
		0x16, 0xf9, 0x18, 0xf9,
	})
	wavr, err := wave.NewReader(r)
	if err != nil {
		log.Fatalf("could not create new wave reader: %v", err)
	}
	fmt.Printf("SampleRate: %d\n", wavr.Format.SampleRate)
	samples, err := wavr.Samples()
	if err != nil {
		log.Fatalf("could not read samples: %v", err)
	}
	fmt.Printf("Samples: %v\n", samples[:10])

}
Output:

SampleRate: 44100
Samples: [0 0 5924 -3298 4924 5180 -1770 -1768 -6348 -23005]

func NewReader

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

NewReader reads the initial chunks from a WAVE file and returns a new reader.

func (*Reader) Sample

func (wavr *Reader) Sample() (int, error)

Sample returns the next sample from the wave file. Chunks that don't contain samples are skipped.

func (*Reader) Samples

func (wavr *Reader) Samples() ([]int, error)

Samples reads the whole file and returns all samples.

type Writer

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

Writer writes samples to an io.WriteSeeker.

Example
package main

import (
	"log"

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

func main() {
	format := wave.Format{
		AudioFormat:   1,
		NumChans:      2,
		SampleRate:    44100,
		ByteRate:      176400,
		BlockAlign:    4,
		BitsPerSample: 16,
	}
	samples := []int{
		0, 0, 5924, -3298, 4924, 5180, -1770, -1768,
		-6348, -23005, -3524, -3548, -12783, 3354,
		0, 0, 5924, -3298, 4924, 5180, -1770, -1768,
	}

	ws := &writerseeker.WriterSeeker{}
	wavw, err := wave.NewWriter(ws, format)
	if err != nil {
		log.Fatalf("could not create wave writer: %v", err)
	}
	defer wavw.Close()
	for _, s := range samples {
		if err := wavw.Sample(s); err != nil {
			log.Fatalf("could not write sample %d: %v", s, err)
		}
	}
}
Output:

func NewWriter

func NewWriter(ws io.WriteSeeker, format Format) (*Writer, error)

NewWriter creates a new WAVE Writer.

func (*Writer) Close

func (wavw *Writer) Close() error

Close the underlying RIFF writers. The file writer needs to be closed separately.

func (*Writer) Sample

func (wavw *Writer) Sample(s int) error

Sample writes a sample.

func (*Writer) Samples

func (wavw *Writer) Samples(samples []int) error

Samples writes a slice of samples.

Directories

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

Jump to

Keyboard shortcuts

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