wav

package module
v0.0.0-...-42cf4c4 Latest Latest
Warning

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

Go to latest
Published: May 25, 2019 License: BSD-2-Clause Imports: 5 Imported by: 8

README

wav

wav is a dead-simple API for dealing with WAV audio files.

Examples

I have created many examples in the form of command line utilities. These utilities are as follows:

Documentation

Full documentation is here

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrDone       = errors.New("Done reading audio data.")
	ErrSampleSize = errors.New("Unsupported sample size.")
	ErrInvalid    = errors.New("The input data was invalid.")
)

Functions

func Append

func Append(dest Sound, sounds ...Sound)

Append appends sounds to a sound. This method adds and removes channels and modifies sample rates as needed.

func AppendSilence

func AppendSilence(s Sound, t time.Duration)

AppendSilence appends a certain amount of silence to a Sound.

func Crop

func Crop(s Sound, start, end time.Duration)

Crop isolates a time segment in a sound. If the end time is past the sound's duration, it will be clamped to the end of the sound.

func Gradient

func Gradient(s Sound, start, end time.Duration)

Gradient creates a linear fade-in gradient for an audio file. The voume is 0% at start and 100% at end.

func Overlay

func Overlay(s, o Sound, delay time.Duration)

Overlay overlays a sound over another sound at a certain offset. The overlaying sound will be converted to match the destination's sample rate and channel count.

func Volume

func Volume(s Sound, scale float64)

Volume scales all the samples in a Sound.

func WriteFile

func WriteFile(s Sound, path string) error

WriteFile saves a sound to a file.

Types

type ChunkHeader

type ChunkHeader struct {
	ID   uint32
	Size uint32
}

ChunkHeader is the generic 64-bit header in WAV files.

type FileHeader

type FileHeader struct {
	ChunkHeader
	Format uint32
}

FileHeader is the "RIFF" chunk

func (FileHeader) Valid

func (h FileHeader) Valid() bool

Valid returns true only if the ID and format match the expected values for WAVE files.

type FormatHeader

type FormatHeader struct {
	ChunkHeader
	AudioFormat   uint16
	NumChannels   uint16
	SampleRate    uint32
	ByteRate      uint32
	BlockAlign    uint16
	BitsPerSample uint16
}

FormatHeader is the "fmt" sub-chunk

func (FormatHeader) BlockSize

func (f FormatHeader) BlockSize() uint16

BlockSize returns the number of bytes per sample-channel.

func (FormatHeader) Valid

func (f FormatHeader) Valid() bool

Returns true only if the ID, size, and audio format match those of a PCM WAV audio file.

type Header struct {
	File   FileHeader
	Format FormatHeader
	Data   ChunkHeader
}

Header is the canonical header for all WAV files

func NewHeader

func NewHeader() *Header

NewHeader creates a header with some reasonable defaults.

func ReadHeader

func ReadHeader(r io.Reader) (*Header, error)

ReadHeader reads a header from a reader. This does basic verification to make sure the header is valid.

func (*Header) Duration

func (h *Header) Duration() time.Duration

Duration returns the duration of the WAV file.

func (*Header) Valid

func (h *Header) Valid() bool

Valid returns true only if the header is for a valid WAV PCM audio file.

func (*Header) Write

func (h *Header) Write(w io.Writer) error

Write writes the header to a writer.

type Reader

type Reader interface {
	// Header returns the reader's WAV header.
	Header() *Header

	// Read reads as many as len(out) samples.
	// The samples are signed values ranging between -1.0 and 1.0.
	// Channels are packed side-by-side, so for a stereo track it'd be LRLRLR...
	// If the end of stream is reached, ErrDone will be returned.
	Read(out []Sample) (int, error)

	// Remaining returns the number of samples left to read.
	Remaining() int
}

func NewReader

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

NewReader wraps an io.Reader with a Reader.

type Sample

type Sample float64

type Sound

type Sound interface {
	Channels() int
	Clone() Sound
	Duration() time.Duration
	SampleRate() int
	Samples() []Sample
	SetSamples([]Sample)
	Write(io.Writer) error
}

Sound represents and abstract list of samples which can be encoded to a file.

func NewPCM16Sound

func NewPCM16Sound(channels int, sampleRate int) Sound

NewPCM16Sound creates a new empty Sound with given parameters.

func NewPCM8Sound

func NewPCM8Sound(channels int, sampleRate int) Sound

NewPCM8Sound creates a new empty Sound with given parameters.

func ReadSound

func ReadSound(f io.Reader) (Sound, error)

ReadSound reads a sound from an io.Reader.

func ReadSoundFile

func ReadSoundFile(path string) (Sound, error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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