wav

package module
v0.0.0-...-56127fb Latest Latest
Warning

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

Go to latest
Published: Aug 20, 2017 License: MIT Imports: 5 Imported by: 29

README

go-wav

MIT License CircleCI

Package go-wav reads and writes WAV file.

Example

The following example concatinates input1.wav and input2.wav into output.wav.

package main

import (
	"io"
	"io/ioutil"

	"github.com/moutend/go-wav"
)

func main() {
	// Read input1.wav and input2.wav
	i1, _ := ioutil.ReadFile("input1.wav")
	i2, _ := ioutil.ReadFile("input2.wav")

	// Create wav.File.
	a := &wav.File{}
	b := &wav.File{}

	// Unmarshal input1.wav and input2.wav.
	wav.Unmarshal(i1, a)
	wav.Unmarshal(i2, b)

	// Add input2.wav to input1.wav.
	c, _ := wav.New(a.SamplesPerSec(), a.BitsPerSample(), a.Channels())
	io.Copy(c, a)
	io.Copy(c, b)

	// Marshal input1.wav and save result.
	file, _ := wav.Marshal(c)
	ioutil.WriteFile("output.wav", file, 0644)
}

Note that the example assumes that the two input files have same sample rate, bit depth and channels.

Contributing

  1. Fork (https://github.com/moutend/go-wca/fork)
  2. Create a feature branch
  3. Add changes
  4. Run go fmt and go test
  5. Commit your changes
  6. Open a new Pull Request

Author

Yoshiyuki Koyanagi

Documentation

Overview

Package wav reads and writes wave (.wav) file.

Index

Constants

View Source
const (
	WAVE_FORMAT_PCM        = 0x1
	WAVE_FORMAT_EXTENSIBLE = 0xFFFE
)

Variables

This section is empty.

Functions

func Marshal

func Marshal(v *File) (stream []byte, err error)

Marshal returns audio data as WAV formatted data.

func Unmarshal

func Unmarshal(stream []byte, audio *File) (err error)

Unmarshal parses WAV formatted audio and store data into *File.

Types

type File

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

File represents WAV audio file.

func New

func New(samplesPerSec, bitsPerSample, channels int) (*File, error)

New creates an empty File.

func (*File) AvgBytesPerSec

func (v *File) AvgBytesPerSec() int

AvgBytesPerSec returns average bytes per second.

func (*File) BitsPerSample

func (v *File) BitsPerSample() int

BitsPerSample returns bits per sample.

func (*File) BlockAlign

func (v *File) BlockAlign() int

BlockAlign returns block align size in byte.

func (*File) Bytes

func (v *File) Bytes() []byte

Bytes returns audio samples as byte slice.

func (*File) Channels

func (v *File) Channels() int

Channels returns number of channels.

func (*File) Duration

func (v *File) Duration() time.Duration

Duration returns playback time in second.

func (*File) Float64s

func (v *File) Float64s() []float64

Float64s returns audio samples as slice of float64.

func (*File) FormatTag

func (v *File) FormatTag() uint16

FormatTag returns either 0x1 (WAVE_FORMAT_PCM) or 0xFFFE (WAVE_FORMAT_EXTENSIBLE).

func (*File) Int32s

func (v *File) Int32s() []int32

Int32s returns audio samples as slice of int32.

func (*File) Length

func (v *File) Length() int

Length returns size of the audio except for headers in bytes. The returned value is same as len(v.Bytes()).

func (*File) Read

func (v *File) Read(p []byte) (int, error)

Read reads audio samples byte by byte.

func (*File) S16

func (v *File) S16() []byte

S16 returns audio samples as byte slice which is encoded 16 bit signed integer.

func (*File) S24

func (v *File) S24() []byte

S24 returns audio samples as byte slice which is encoded 24 bit signed integer.

func (*File) S32

func (v *File) S32() []byte

S32 returns audio samples as byte slice which is encoded 32 bit signed integer.

func (*File) S8

func (v *File) S8() []byte

S8 returns audio samples as byte slice which is encoded 8 bit signed integer.

func (*File) Samples

func (v *File) Samples() int

Samples returns number of the samples that the audio contains. For example, 10 seconds of the stereo audio which is encoded 16 bit / 44.1 kHz contains 882000 samples.

func (*File) SamplesPerSec

func (v *File) SamplesPerSec() int

SamplesPerSec returns number of samples per second. For example, CD quality audio is encoded as 44100 samples per second.

func (*File) String

func (v *File) String() string

String returns textual representation of audio.

func (*File) Write

func (v *File) Write(b []byte) (n int, err error)

Write writes audio samples byte by byte.

Jump to

Keyboard shortcuts

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