audio

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Apr 9, 2018 License: MIT Imports: 8 Imported by: 2

Documentation

Overview

Package audio contains structs and functions to allow operating on audio data

Index

Constants

View Source
const (
	BufSize      = 1 << 10
	SilentThresh = 1 << 10
	SampleRate   = 16000
	NumChannels  = 1
)
View Source
const (
	// DefaultNumChannels is 1 (mono audio)
	DefaultNumChannels uint16 = 1
	// DefaultSampleRate is 16KHz
	DefaultSampleRate uint32 = 16000
	// DefaultAudioFormat is 1 (raw, uncompressed PCM waveforms)
	DefaultAudioFormat uint16 = 1
	// DefaultBitsPerSample is 16 (2 bytes per sample).
	DefaultBitsPerSample uint16 = 16
)

WAV-related constants.

Variables

This section is empty.

Functions

func NewRecordingStream

func NewRecordingStream(length float64, silenceLen float64) io.Reader

NewRecordingStream records audio data according to the given parameters (just like `NewFileFromRecording`), however instead of creating an *audio.File, it streams the data to a Reader, which can be read as soon as data is available. It emits a WAV header before the actual data, but the length of the data is not correct. Therefore, the resulting WAV should be read until EOF.

Types

type File

type File struct {
	AudioData *WAV
	// contains filtered or unexported fields
}

File represents an audio file. It wraps the raw WAV data and allows you to operate with it using high-level operations, such as padding, trimming, playback, writing to file, recording, etc.

func NewFileFromBytes

func NewFileFromBytes(b []byte) (*File, error)

NewFileFromBytes creates a new audio.File from WAV data

func NewFileFromFile

func NewFileFromFile(f *os.File) (*File, error)

NewFileFromFile creates a new audio.File from an os.File

func NewFileFromFileName

func NewFileFromFileName(f string) (*File, error)

NewFileFromFileName creates a new audio.File from the given filename

func NewFileFromReader

func NewFileFromReader(r io.Reader) (*File, error)

NewFileFromReader creates a new audio.File from an io.Reader

func NewFileFromRecording

func NewFileFromRecording(length float64, silenceLen float64) (*File, error)

NewFileFromRecording creates a new audio.File by recording from the default input stream. `length` specifies the maximum length of the recording in seconds. `silenceLen` specifies in seconds how much consecutive silence to wait for before ending the recording.

func (*File) Pad

func (f *File) Pad(seconds float64)

Pad adds silence to both the beginning and end of the audio data. Silence is specified in seconds.

func (*File) PadLeft

func (f *File) PadLeft(seconds float64)

PadLeft adds silence to the beginning of the audio data.

func (*File) PadRight

func (f *File) PadRight(seconds float64)

PadRight adds silence to the end of the audio data.

func (*File) Play

func (f *File) Play() error

Play the audio file to the default output

func (*File) Stop

func (f *File) Stop()

Stop the audio playback immediately.

func (*File) TrimSilence

func (f *File) TrimSilence()

TrimSilence trims silence from both ends of the audio data.

func (*File) WAVData

func (f *File) WAVData() []byte

WAVData returns the wav data (header + audio data) contained in the audio file

func (*File) WriteToFile

func (f *File) WriteToFile(filename string) error

WriteToFile writes the audio data to a file.

type WAV

type WAV struct {
	// NumChannels is the number of channels the WAV file has. 1 = mono,
	// 2 = stereo, etc. This affects the block align and also number of
	// bytes per sample: (BitsPerSample / 8) * NumChannels.
	NumChannels uint16
	// SampleRate is the number of samples taken per second.
	SampleRate uint32
	// AudioFormat is the type of Audio that is encoded in the WAV file.
	// In most scenarios, this will be 1 (1 = raw, uncompressed PCM audio),
	// since WAV doesn't support compression.
	AudioFormat uint16
	// BitsPerSample is the width of each sample. 16 bits means each sample
	// is two bytes.
	BitsPerSample uint16
	// contains filtered or unexported fields
}

WAV represents a PCM audio file in the WAV container format. It keeps a high-level description of the parameters of the file, along with the raw audio bytes, until it needs to be written to a file, stream, or array. It is based on the WAV formatting as specified: http://soundfile.sapp.org/doc/WaveFormat/

func NewWAV

func NewWAV() *WAV

NewWAV returns a new, empty WAV file using the default parameters.

func NewWAVFromData

func NewWAVFromData(data []byte) (*WAV, error)

NewWAVFromData creates a WAV format struct from the given data buffer The buffer is broken up into its respective information and that

func NewWAVFromParams

func NewWAVFromParams(params *WAVParams) *WAV

NewWAVFromParams returns a new WAV file from the passed in parameters If any of the parameters are 0, it will be given the default value.

func NewWAVFromReader

func NewWAVFromReader(reader io.Reader) (*WAV, error)

NewWAVFromReader takes in a reader and creates a new WAV file.

func (*WAV) AddAudioData

func (w *WAV) AddAudioData(d []byte)

AddAudioData adds the passed-in audio bytes to the WAV struct

func (*WAV) AudioData

func (w *WAV) AudioData() []byte

AudioData returns the raw audio data

func (*WAV) Data

func (w *WAV) Data() []byte

Data creates the header and data based on the WAV struct and returns a fully formatted WAV file

func (*WAV) TrimSilent

func (w *WAV) TrimSilent(threshold float64, padding float64)

TrimSilent is called on a WAV struct to trim the silent portions from the ends of the file while leaving a certain amount of padding. The padding input is specified in seconds. The threshold input is a decimal (between 0 and 1) and is relative to the maximum amplitude of the waveform

type WAVParams

type WAVParams struct {
	NumChannels   uint16
	SampleRate    uint32
	BitsPerSample uint16
	AudioData     []byte
}

WAVParams are a set of parameters used to create a WAV file. Its fields correspond directly to the WAV file.

Jump to

Keyboard shortcuts

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