wav

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Apr 13, 2024 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package wav defines WAV audio struct and Chunk struct. You can decode or encode WAV audio with this handy package.

WAV audio format reference: http://www.topherlee.com/software/pcm-tut-wavformat.html.

The header of WAV(RIFF) file is 44 bytes long and has following format:

| Positions | Sample Value | Description | |-----------|----------------|-------------------------------------------------------------| | 1 - 4 | "RIFF" | Marks the file as a RIFF file | | 5 - 8 | File size (u32)| Size of the overall file (fill this in after creation) | | 9 - 12 | "WAVE" | File type header | | 13 -16 | "fmt " | Format chunk marker | | 17 - 20 | 16 (u32) | Length of format data listed above | | 21 - 22 | 1 (u16) | Type of format (1 is PCM) | | 23 - 24 | 2 (u16) | Number channels | | 25 - 28 | 44100 (u32) | Sample rate | | 29 - 32 | 176400 (u32) | (Sample Rate * BitsPerSample * Channels) / 8 | | 33 - 34 | 4 (u16) | Sound: 1 - mono, 2 - stereo: (BitsPerSample * Channels) / 8 | | 35 - 36 | 16 (u16) | Bits per sample | | 37 - 40 | "data" | Data chunk header | | 41 - 44 | File size (u32)| Size of the data section |

Index

Constants

View Source
const (
	AudioFormatPCM = 1
)

Variables

View Source
var (
	WaveHeader = []byte{'W', 'A', 'V', 'E'}
	RiffHeader = []byte{'R', 'I', 'F', 'F'}
	FmtHeader  = []byte{'f', 'm', 't', ' '}
	DataHeader = []byte{'d', 'a', 't', 'a'}
)

Functions

func Encode

func Encode(w io.Writer, audio *WaveAudio) error

Encode encodes wave audio to a given writer. WAV file ref: http://www.topherlee.com/software/pcm-tut-wavformat.html

Types

type Chunk

type Chunk struct {
	Header   []byte
	Position int
	Size     uint32
}

type DecodeError

type DecodeError string

func (DecodeError) Error

func (e DecodeError) Error() string

type Decoder

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

func NewDecoder

func NewDecoder(r io.Reader) (*Decoder, error)

func (*Decoder) Decode

func (d *Decoder) Decode() (*WaveAudio, error)

type WaveAudio

type WaveAudio struct {
	Format        uint16
	Channels      uint16
	SampleRate    uint32
	BitsPerSample uint16
	RawData       []byte
}

func Decode

func Decode(r io.Reader) (*WaveAudio, error)

func (*WaveAudio) DataSize

func (w *WaveAudio) DataSize() uint32

func (*WaveAudio) SampleFreq

func (w *WaveAudio) SampleFreq() uint32

func (*WaveAudio) Sound

func (w *WaveAudio) Sound() uint16

Jump to

Keyboard shortcuts

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