audio

package
v1.1.2 Latest Latest
Warning

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

Go to latest
Published: Oct 8, 2021 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const (
	BitDepthUndefined = BitDepth(0)
	BitDepth8         = BitDepth(8)
	BitDepth16        = BitDepth(16)
	BitDepth32        = BitDepth(32)
	BitDepth64        = BitDepth(64)
)

Possible values for BitDepth are 8, 16, 32 and 64.

Variables

View Source
var (
	// ErrInvalidBitDepth is returned when provided BitDepth value is not supported.
	ErrInvalidBitDepth = errors.New("unsupported bit depth")
)
View Source
var ErrInvalidBuffer = errors.New("invalid buffer")

ErrInvalidBuffer is returned when specified audio buffer is not valid for the operation.

Functions

This section is empty.

Types

type BitDepth

type BitDepth int8

BitDepth represents the bit depth of encoded audio.

type Buffer

type Buffer interface {
	// Size returns the size of the underlying slice.
	Size() int

	// BitDepth returns bit depth of the buffer.
	BitDepth() BitDepth

	// Data returns the pointer to the underlying slice.
	Data() interface{}

	// Clone clones the buffer, copying underlying slice.
	Clone() Buffer

	// Write writes provided int slice into the buffer.
	Write([]int, BitDepth) (int, error)

	// Read reads the contents of the buffer into provided slice.
	Read([]int, BitDepth) (int, error)

	// WriteTo writes the contents of the buffer into buf.
	// buf has to have matching bit depth.
	WriteTo(buf Buffer) (int, error)

	// ReadFrom reads the contents of buf into the buffer.
	// buf has to have matching bit depth.
	ReadFrom(buf Buffer) (int, error)

	// Encode writes the contents of the buffer into provided writer.
	// The buffer is responsible for performing resampling.
	Encode(binary.ByteOrder, io.Writer) (int, error)

	// Decode reads the contents of provided reader into the buffer.
	// The buffer is responsible for performing resampling.
	Decode(binary.ByteOrder, io.Reader) (int, error)
}

Buffer represents a chunk of audio data of a specific size and bit depth. TODO: need to make sure that Write / Read are balanced and do not overwrite.

func NewBuffer

func NewBuffer(d BitDepth, size int) (Buffer, error)

NewBuffer returns a new Buffer with specified bit depth and size.

type Container

type Container interface {
	Format() Format
	Buffer() Buffer
}

Container represents an audio container that holds audio data in a Buffer with a specific Format.

type Format

type Format struct {
	NumChannels     int32
	SampleRateHertz int32
	BitDepth        BitDepth
}

Format represents the format of audio data.

func NewFormat

func NewFormat(numChannels, sampleRate, depth int) (Format, error)

NewFormat returns new Format with specified number of channels, sample rate and bit depth. If provided depth is not valid, an error is returned.

type Player

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

Player is an audio player that reads data from specified audio source, and plays it using OS audio stack through default output device. It is based on portaudio, so it will use whatever audio stack implementation that portaudio implements for current OS. It is not safe for concurrent use.

func NewPlayer

func NewPlayer(src Source, log logger.Logger) (*Player, error)

NewPlayer returns a new Player that will use src as source of audio data.

func (*Player) Close

func (p *Player) Close() error

Close closes the player by closing the source, the audio stream and terminating the audio stack. Close MUST be called before program exits, otherwise the audio devices of the OS may be unusable until the audio system is restarted.

func (*Player) Play

func (p *Player) Play(ctx context.Context) error

Play plays audio from src within given context.

type RecordStream

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

RecordStream is an audio stream that implements io.WriterTo interface, by using an audio buffer and encoding it into binary data using specified audio format and byte order.

func NewRecordStream

func NewRecordStream(fmt Format, ord binary.ByteOrder, bufSize int, log logger.Logger) (*RecordStream, error)

NewRecordStream returns a new record stream with specified format, byte order and size of underlying buffer.

func (*RecordStream) Close

func (r *RecordStream) Close() error

Close closes RecordStream by closing the audio stream and terminating the audio stack. Close MUST be called before program exits, otherwise the audio devices of the OS may be unusable until the audio system is restarted.

func (*RecordStream) WriteTo

func (r *RecordStream) WriteTo(w io.Writer) (int64, error)

WriteTo implements io.WriterTo by reading from stream and then encoding and writing audio to w.

type Recorder

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

Recorder is an audio recorder that reads data from default OS audio input and writes data to specified destination. It is based on portaudio, so it will use whatever audio stack implementation that portaudio implements for current OS. It is not safe for concurrent use.

func NewRecorder

func NewRecorder(dst Sink, log logger.Logger) (*Recorder, error)

NewRecorder returns a new Recorder that will write audio to dst.

func (*Recorder) Close

func (r *Recorder) Close() error

Close closes the recorder by closing the destination, the audio stream and terminating the audio stack. Close MUST be called before program exits, otherwise the audio devices of the OS may be unusable until the audio system is restarted.

func (*Recorder) Record

func (r *Recorder) Record(ctx context.Context) error

Record records audio and writes it to dst within given context.

type Sink

type Sink interface {
	Container
	io.Closer
	WriteBuffer(Buffer) (int, error)
}

Sink is an audio container that can be written to using WriteBuffer.

type Source

type Source interface {
	Container
	io.Closer
	ReadBuffer(Buffer) (int, error)
}

Source is an audio container that can be read from using ReadBuffer.

type SourceSink

type SourceSink interface {
	Container
	Source
	Sink
}

SourceSink is an audio container that can be both written to and read from.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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