wave

package
v0.13.1 Latest Latest
Warning

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

Go to latest
Published: Jan 4, 2021 License: MIT Imports: 5 Imported by: 23

README

GoWave

This library is intended to help me with converting C code from "The Audio Programming Book" into Go code. The format for the wave file can be found: https://web.archive.org/web/20141213140451/https://ccrma.stanford.edu/courses/422/projects/WaveFormat/

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ChunkID          = []byte{0x52, 0x49, 0x46, 0x46} // RIFF
	BigEndianChunkID = []byte{0x52, 0x49, 0x46, 0x58} // RIFX
	WaveID           = []byte{0x57, 0x41, 0x56, 0x45} // WAVE
	Format           = []byte{0x66, 0x6d, 0x74, 0x20} // FMT
	Subchunk2ID      = []byte{0x64, 0x61, 0x74, 0x61} // DATA
)

Consts that appear in the .WAVE file format

Functions

func BatchSamples

func BatchSamples(data Wave, seconds float64) [][]Frame

BatchSamples batches the samples per requested timespan expressed in seconds

func WriteFrames

func WriteFrames(samples []Frame, wfmt WaveFmt, file string) error

WriteFrames writes the slice to disk as a .wav file the WaveFmt metadata needs to be correct WaveData and WaveHeader are inferred from the samples however..

Types

type Frame

type Frame float64

Frame is a single float64 value of raw audio data

func FloatsToFrames added in v0.11.0

func FloatsToFrames(fs []float64) []Frame

FloatsToFrames turns a slice of float64 to a slice of frames

type Wave

type Wave struct {
	WaveHeader
	WaveFmt
	WaveData
}

Wave represents an entire .wav audio file

func ReadWaveFile

func ReadWaveFile(f string) (Wave, error)

ReadWaveFile parses a .wave file into a Wave struct

type WaveData

type WaveData struct {
	Subchunk2ID   []byte // Identifier of subchunk
	Subchunk2Size int    // size of raw sound data
	RawData       []byte // raw sound data itself
	Frames        []Frame
}

WaveData contains the raw sound data

type WaveFmt

type WaveFmt struct {
	Subchunk1ID    []byte // should contain "fmt"
	Subchunk1Size  int    // 16 for PCM
	AudioFormat    int    // PCM = 1 (Linear Quantization), if not 1, compression was used.
	NumChannels    int    // Mono 1, Stereo = 2, ..
	SampleRate     int    // 44100 for CD-Quality, etc..
	ByteRate       int    // SampleRate * NumChannels * BitsPerSample / 8
	BlockAlign     int    // NumChannels * BitsPerSample / 8 (number of bytes per sample)
	BitsPerSample  int    // 8 bits = 8, 16 bits = 16, .. :-)
	ExtraParamSize int    // if not PCM, can contain extra params
	ExtraParams    []byte // the actual extra params.
}

WaveFmt describes the format of the sound-information in the data subchunks

func NewWaveFmt

func NewWaveFmt(format, channels, samplerate, bitspersample int, extraparams []byte) WaveFmt

NewWaveFmt can be used to generate a complete WaveFmt by calculating the remaining props

func (*WaveFmt) SetChannels

func (wfmt *WaveFmt) SetChannels(n uint)

SetChannels changes the FMT to adapt to a new amount of channels

type WaveHeader

type WaveHeader struct {
	ChunkID   []byte // should be RIFF on little-endian or RIFX on big-endian systems..
	ChunkSize int
	Format    string // sanity-check, should be WAVE (//TODO: keep as []byte?)
}

WaveHeader describes the header each WAVE file should start with

Jump to

Keyboard shortcuts

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