audio

package
v0.0.0-...-793ea6c Latest Latest
Warning

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

Go to latest
Published: Oct 24, 2021 License: BSD-3-Clause Imports: 5 Imported by: 13

Documentation

Overview

Package audio implements various audio types and interfaces.

This package aims to be like the 'image' package, except for audio.

Index

Constants

This section is empty.

Variables

View Source
var EOS = errors.New("end of stream")

EOS is the error returned by Read when no more input is available. Functions should return EOS only to signal a graceful end of input. If the EOS occurs unexpectedly in a structured data stream, the appropriate error is either ErrUnexpectedEOS or some other error giving more detail.

View Source
var ErrFormat = errors.New("audio: unknown format")

ErrFormat specifies an error where the format of the audio data is unknown of by the registered formats of this package.

View Source
var ErrInvalidData = errors.New("audio: input data is invalid or corrupt")

ErrInvalidData represents an error for decoding input data that is invalid or corrupted for some reason.

View Source
var ErrShortWrite = errors.New("short write")

ErrShortWrite means that a write accepted fewer bytes than requested but failed to return an explicit error.

View Source
var ErrUnexpectedEOS = errors.New("unexpected end of stream")

ErrUnexpectedEOS means that EOS was encountered in the middle of reading a fixed-size block or data structure.

Functions

func ALawToInt16

func ALawToInt16(s uint8) int16

ALawToInt16 converts an ALaw encoded audio sample to an Int16 encoded audio sample.

func Copy

func Copy(dst Writer, src Reader) (written int64, err error)

Copy copies from src to dst until either EOS is reached on src or an error occurs. It returns the number of samples copied and the first error encountered while copying, if any.

A successful Copy returns err == nil, not err == EOS. Because Copy is defined to read from src until EOS, it does not treat an EOS from Read as an error to be reported.

If src implements the WriterTo interface, the copy is implemented by calling src.WriteTo(dst). Otherwise, if dst implements the ReaderFrom interface, the copy is implemented by calling dst.ReadFrom(src).

func Float64ToInt16

func Float64ToInt16(s float64) int16

Float64ToInt16 converts a Float64 encoded audio sample to Int16.

func Float64ToInt32

func Float64ToInt32(s float64) int32

Float64ToInt32 converts a Float64 encoded audio sample to Int32.

func Float64ToUint8

func Float64ToUint8(s float64) uint8

Float64ToUint8 converts a Float64 encoded audio sample to Uint8.

func Int16ToALaw

func Int16ToALaw(s int16) uint8

Int16ToALaw converts an Int16 encoded audio sample to an ALaw encoded audio sample.

func Int16ToFloat64

func Int16ToFloat64(s int16) float64

Int16ToFloat64 converts a Int16 encoded audio sample to Float64.

func Int16ToMuLaw

func Int16ToMuLaw(s int16) uint8

Int16ToMuLaw converts from a Int16 encoded audio sample to an MuLaw encoded audio sample.

func Int32ToFloat64

func Int32ToFloat64(s int32) float64

Int32ToFloat64 converts a Int32 encoded audio sample to Float64.

func MuLawToInt16

func MuLawToInt16(s uint8) int16

MuLawToInt16 converts from an MuLaw encoded audio sample to an Int16 encoded audio sample.

func RegisterFormat

func RegisterFormat(name, magic string, newDecoder func(r interface{}) (Decoder, error))

RegisterFormat registers an image format for use by NewDecoder().

Name is the name of the format, like "wav" or "ogg".

Magic is the magic prefix that identifies the format's encoding. The magic string can contain "?" wildcards that each match any one byte.

newDecoder is the function that returns either [Decoder, nil] or [nil, ErrInvalidData] upon being called where the returned decoder is used to decode the io.Reader or io.ReadSeeker's encoded audio data.

func Uint8ToFloat64

func Uint8ToFloat64(s uint8) float64

Uint8ToFloat64 converts a Uint8 encoded audio sample to Float64.

Types

type ALaw

type ALaw []uint8

ALaw represents a slice of ALaw encoded audio samples.

func (ALaw) At

func (p ALaw) At(i int) float64

At implements the Slice interface.

func (ALaw) Cap

func (p ALaw) Cap() int

Cap implements the Slice interface.

func (ALaw) CopyTo

func (p ALaw) CopyTo(dst Slice) int

CopyTo implements the Slice interface.

func (ALaw) Len

func (p ALaw) Len() int

Len implements the Slice interface.

func (ALaw) Make

func (p ALaw) Make(length, capacity int) Slice

Make implements the Slice interface.

func (ALaw) Set

func (p ALaw) Set(i int, s float64)

Set implements the Slice interface.

func (ALaw) Slice

func (p ALaw) Slice(low, high int) Slice

Slice implements the Slice interface.

type Buffer

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

A Buffer is a variable-sized buffer of audio samples with Read and Write methods. Buffers must be allocated via the NewBuffer function.

func NewBuffer

func NewBuffer(buf Slice) *Buffer

NewBuffer creates and initializes a new Buffer using buf as its initial contents. The buffer will internally use the given slice, buf, which also defines the sample storage type. NewBuffer is intended to prepare a Buffer to read existing data. It can also be used to size the internal buffer for writing. To do that, buf should have the desired capacity but a length of zero.

func (*Buffer) Grow

func (b *Buffer) Grow(n int)

Grow grows the buffer's capacity, if necessary, to guarantee space for another n samples. After Grow(n), at least n samples can be written to the buffer without another allocation. If n is negative, Grow will panic.

func (*Buffer) Len

func (b *Buffer) Len() int

Len returns the number of samples of the unread portion of the buffer; b.Len() == b.Samples().Len().

func (*Buffer) Next

func (b *Buffer) Next(n int) Slice

Next returns a slice containing the next n samples from the buffer, advancing the buffer as if the samples had been returned by Read. If there are fewer than n samples in the buffer, Next returns the entire buffer. The slice is only valid until the next call to a read or write method.

func (*Buffer) Read

func (b *Buffer) Read(p Slice) (n int, err error)

Read reads the next p.Len() samples from the buffer or until the buffer is drained. The return value n is the number of samples read. If the buffer has no data to return, err is EOS (unless len(p) is zero); otherwise it is nil.

func (*Buffer) ReadFrom

func (b *Buffer) ReadFrom(r Reader) (n int64, err error)

ReadFrom reads data from r until EOS and appends it to the buffer, growing the buffer as needed. The return value n is the number of samples read. Any error except EOS encountered during the read is also returned.

func (*Buffer) ReadSample

func (b *Buffer) ReadSample() (c float64, err error)

ReadSample reads and returns the next sample from the buffer. If no sample is available, it returns error EOS.

func (*Buffer) Reset

func (b *Buffer) Reset()

Reset resets the buffer so it has no content. b.Reset() is the same as b.Truncate(0).

func (*Buffer) Samples

func (b *Buffer) Samples() Slice

Samples returns a slice of the unread portion of the buffer. If the caller changes the contents of the returned slice, the contents of the buffer will change, provided there are no intervening method calls on the Buffer.

func (*Buffer) Seek

func (b *Buffer) Seek(offset uint64) error

Seek seeks to the specified sample number, relative to the start of the stream. As such, subsequent Read() calls on the Buffer, begin reading at the specified sample.

If offset > b.Len(), then the offset is unchanged and the seek operation fails returning error == EOS.

func (*Buffer) Truncate

func (b *Buffer) Truncate(n int)

Truncate discards all but the first n unread samples from the buffer. It panics if n is negative or greater than the length of the buffer.

func (*Buffer) Write

func (b *Buffer) Write(p Slice) (n int, err error)

Write appends the contents of p to the buffer, growing the buffer as needed. The return value n is the length of p; err is always nil.

func (*Buffer) WriteSample

func (b *Buffer) WriteSample(c float64)

WriteSample appends the sample c to the buffer, growing the buffer as needed.

func (*Buffer) WriteTo

func (b *Buffer) WriteTo(w Writer) (n int64, err error)

WriteTo writes data to w until the buffer is drained or an error occurs. The return value n is the number of samples written; it always fits into an int, but it is int64 to match the WriterTo interface. Any error encountered during the write is also returned.

type Config

type Config struct {
	// SampleRate is the number of audio samples that the stream is played or
	// recorded at.
	//
	// E.g. 44100 would be compact disc quality.
	SampleRate int

	// Channels is the number of channels the stream contains.
	Channels int
}

Config represents an audio stream's configuration, like its sample rate and number of interleaved channels.

func (Config) String

func (c Config) String() string

String returns an string representation of this audio config.

type Decoder

type Decoder interface {
	ReadSeeker

	// Config returns the audio stream configuration of this decoder. It may
	// block until at least the configuration part of the stream has been read.
	Config() Config
}

Decoder is the generic audio decoder interface, for use with the RegisterFormat() function.

func NewDecoder

func NewDecoder(r interface{}) (Decoder, string, error)

NewDecoder returns a decoder which can be used to decode the encoded audio data stored in the io.Reader or io.ReadSeeker, 'r'.

The string returned is the format name used during format registration.

Format registration is typically done by the init method of the codec- specific package.

type Encoder

type Encoder interface {
	Writer

	// Close closes the audio encoder, and finalizes the encoding process.
	// It must be called, or else the encoding process may not finish, and
	// the encoded data may be incomplete.
	//
	// Writing data to a closed encoder causes an encoder-specific behavior,
	// but most often a panic will occur.
	Close() error
}

Encoder is the generic audio encoder interface.

type Float32

type Float32 []float32

Float32 represents a slice of 32-bit floating-point linear PCM encoded audio samples, in the range of -1 to +1.

func (Float32) At

func (p Float32) At(i int) float64

At implements the Slice interface.

func (Float32) Cap

func (p Float32) Cap() int

Cap implements the Slice interface.

func (Float32) CopyTo

func (p Float32) CopyTo(dst Slice) int

CopyTo implements the Slice interface.

func (Float32) Len

func (p Float32) Len() int

Len implements the Slice interface.

func (Float32) Make

func (p Float32) Make(length, capacity int) Slice

Make implements the Slice interface.

func (Float32) Set

func (p Float32) Set(i int, s float64)

Set implements the Slice interface.

func (Float32) Slice

func (p Float32) Slice(low, high int) Slice

Slice implements the Slice interface.

type Float64

type Float64 []float64

Float64 represents a slice of 64-bit floating-point linear PCM encoded audio samples, in the range of -1 to +1.

func (Float64) At

func (p Float64) At(i int) float64

At implements the Slice interface.

func (Float64) Cap

func (p Float64) Cap() int

Cap implements the Slice interface.

func (Float64) CopyTo

func (p Float64) CopyTo(dst Slice) int

CopyTo implements the Slice interface.

func (Float64) Len

func (p Float64) Len() int

Len implements the Slice interface.

func (Float64) Make

func (p Float64) Make(length, capacity int) Slice

Make implements the Slice interface.

func (Float64) Set

func (p Float64) Set(i int, s float64)

Set implements the Slice interface.

func (Float64) Slice

func (p Float64) Slice(low, high int) Slice

Slice implements the Slice interface.

type Int16

type Int16 []int16

Int16 represents a signed 16-bit linear PCM encoded audio sample.

func (Int16) At

func (p Int16) At(i int) float64

At implements the Slice interface.

func (Int16) Cap

func (p Int16) Cap() int

Cap implements the Slice interface.

func (Int16) CopyTo

func (p Int16) CopyTo(dst Slice) int

CopyTo implements the Slice interface.

func (Int16) Len

func (p Int16) Len() int

Len implements the Slice interface.

func (Int16) Make

func (p Int16) Make(length, capacity int) Slice

Make implements the Slice interface.

func (Int16) Set

func (p Int16) Set(i int, s float64)

Set implements the Slice interface.

func (Int16) Slice

func (p Int16) Slice(low, high int) Slice

Slice implements the Slice interface.

type Int32

type Int32 []int32

Int32 represents a signed 32-bit linear PCM encoded audio sample.

func (Int32) At

func (p Int32) At(i int) float64

At implements the Slice interface.

func (Int32) Cap

func (p Int32) Cap() int

Cap implements the Slice interface.

func (Int32) CopyTo

func (p Int32) CopyTo(dst Slice) int

CopyTo implements the Slice interface.

func (Int32) Len

func (p Int32) Len() int

Len implements the Slice interface.

func (Int32) Make

func (p Int32) Make(length, capacity int) Slice

Make implements the Slice interface.

func (Int32) Set

func (p Int32) Set(i int, s float64)

Set implements the Slice interface.

func (Int32) Slice

func (p Int32) Slice(low, high int) Slice

Slice implements the Slice interface.

type MuLaw

type MuLaw []uint8

MuLaw represents a slice of MuLaw encoded audio samples.

func (MuLaw) At

func (p MuLaw) At(i int) float64

At implements the Slice interface.

func (MuLaw) Cap

func (p MuLaw) Cap() int

Cap implements the Slice interface.

func (MuLaw) CopyTo

func (p MuLaw) CopyTo(dst Slice) int

CopyTo implements the Slice interface.

func (MuLaw) Len

func (p MuLaw) Len() int

Len implements the Slice interface.

func (MuLaw) Make

func (p MuLaw) Make(length, capacity int) Slice

Make implements the Slice interface.

func (MuLaw) Set

func (p MuLaw) Set(i int, s float64)

Set implements the Slice interface.

func (MuLaw) Slice

func (p MuLaw) Slice(low, high int) Slice

Slice implements the Slice interface.

type ReadSeeker

type ReadSeeker interface {
	Reader

	// Seek seeks to the specified sample number, relative to the start of the
	// stream. As such, subsequent Read() calls on the Reader, begin reading at
	// the specified sample.
	//
	// If any error is returned, it means it was impossible to seek to the
	// specified audio sample for some reason, and that the current playhead is
	// unchanged.
	Seek(sample uint64) error
}

ReadSeeker is the generic seekable audio reader interface.

type Reader

type Reader interface {
	// Read tries to read into the audio slice, b, filling it with at max
	// b.Len() audio samples.
	//
	// Returned is the number of samples that where read into the slice, and
	// an error if any occurred.
	//
	// It is possible for the number of samples read to be non-zero; and for an
	// error to be returned at the same time (E.g. read 300 audio samples, but
	// also encountered EOS).
	Read(b Slice) (read int, e error)
}

Reader is a generic interface which describes any type who can have audio samples read from it into an audio slice.

type ReaderFrom

type ReaderFrom interface {
	ReadFrom(r Reader) (n int64, err error)
}

ReaderFrom is the interface that wraps the ReadFrom method.

ReadFrom reads data from r until EOS or error. The return value n is the number of bytes read. Any error except EOS encountered during the read is also returned.

The Copy function uses ReaderFrom if available.

type Slice

type Slice interface {
	// Len returns the number of elements in the slice.
	//
	// Equivalent slice syntax:
	//
	//  len(b)
	Len() int

	// Cap returns the number of elements in the slice.
	//
	// Equivalent slice syntax:
	//
	//  cap(b)
	Cap() int

	// Set sets the specified index in the slice to the specified Float64
	// encoded audio sample, s.
	//
	// If the slice's audio samples are not stored in Float64 encoding, then
	// the sample should be converted to the slice's internal format and then
	// stored.
	//
	// Just like slices, slice indices must be non-negative; and no greater
	// than (Len() - 1), or else a panic may occur.
	//
	// Equivalent slice syntax:
	//
	//  b[index] = s
	//   -> b.Set(index, s)
	//
	Set(index int, s float64)

	// At returns the Float64 encoded audio sample at the specified index in
	// the slice.
	//
	// If the slice's audio samples are not stored in Float64 encoding, then
	// the sample should be converted to Float64 encoding, and subsequently
	// returned.
	//
	// Just like slices, slice indices must be non-negative; and no greater
	// than (Len() - 1), or else a panic may occur.
	//
	// Equivalent slice syntax:
	//
	//  b[index]
	//   -> b.At(index)
	//
	At(index int) float64

	// Slice returns a new slice of the slice, using the low and high
	// parameters.
	//
	// Equivalent slice syntax:
	//
	//  b[low:high]
	//   -> b.Slice(low, high)
	//
	//  b[2:]
	//   -> b.Slice(2, a.Len())
	//
	//  b[:3]
	//   -> b.Slice(0, 3)
	//
	//  b[:]
	//   -> b.Slice(0, a.Len())
	//
	Slice(low, high int) Slice

	// Make creates and returns a new slice of this slices type. This allows
	// allocating a new slice of exactly the same type for lossless copying of
	// data without knowing about the underlying type.
	//
	// It is exactly the same syntax as the make builtin:
	//
	//  make(MuLaw, len, cap)
	//
	// Where cap cannot be less than len.
	Make(length, capacity int) Slice

	// CopyTo operates exactly like the copy builtin, but this slice is always
	// the source operand. Equivalent slice syntax:
	//
	//  copy(dst, src)
	//   -> src.CopyTo(dst)
	//
	CopyTo(dst Slice) int
}

Slice is a generic audio slice, it can conceptually be thought of as a slice of some audio encoding type.

Conversion between two encoded audio slices is as simple as:

dst, ok := src.(MuLaw)
if !ok {
    // Create a new slice of the target encoding and copy the samples over
    // because src is not MuLaw encoded.
    dst = make(MuLaw, src.Len())
    src.CopyTo(dst)
}

type Uint8

type Uint8 []uint8

Uint8 represents a unsigned 8-bit linear PCM encoded audio sample.

func (Uint8) At

func (p Uint8) At(i int) float64

At implements the Slice interface.

func (Uint8) Cap

func (p Uint8) Cap() int

Cap implements the Slice interface.

func (Uint8) CopyTo

func (p Uint8) CopyTo(dst Slice) int

CopyTo implements the Slice interface.

func (Uint8) Len

func (p Uint8) Len() int

Len implements the Slice interface.

func (Uint8) Make

func (p Uint8) Make(length, capacity int) Slice

Make implements the Slice interface.

func (Uint8) Set

func (p Uint8) Set(i int, s float64)

Set implements the Slice interface.

func (Uint8) Slice

func (p Uint8) Slice(low, high int) Slice

Slice implements the Slice interface.

type Writer

type Writer interface {
	// Write attempts to write all, b.Len(), samples in the slice to the
	// writer.
	//
	// Returned is the number of samples from the slice that where wrote to
	// the writer, and an error if any occured.
	//
	// If the number of samples wrote is less than buf.Len() then the returned
	// error must be non-nil. If any error occurs it should be considered fatal
	// with regards to the writer: no more data can be subsequently wrote after
	// an error.
	Write(b Slice) (wrote int, err error)
}

Writer is a generic interface which describes any type who can have audio samples written from an audio slice into it.

type WriterTo

type WriterTo interface {
	WriteTo(w Writer) (n int64, err error)
}

WriterTo is the interface that wraps the WriteTo method.

WriteTo writes data to w until there's no more data to write or when an error occurs. The return value n is the number of samples written. Any error encountered during the write is also returned.

The Copy function uses WriterTo if available.

Directories

Path Synopsis
Package flac provides a FLAC audio decoder.
Package flac provides a FLAC audio decoder.
Package wav decodes and encodes wav audio files.
Package wav decodes and encodes wav audio files.

Jump to

Keyboard shortcuts

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