Skip to content

bake/wave

Repository files navigation

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.

Releases

No releases published

Packages

No packages published

Languages