media

package module
v0.0.0-...-88258bf Latest Latest
Warning

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

Go to latest
Published: May 20, 2026 License: Apache-2.0 Imports: 18 Imported by: 32

Documentation

Index

Constants

View Source
const (
	// DefFrameDur is a default duration of an audio frame.
	DefFrameDur = 20 * time.Millisecond
	// DefFramesPerSec is a default number of audio frames per second.
	DefFramesPerSec = int(time.Second / DefFrameDur)
)

Variables

This section is empty.

Functions

func CodecEnabled

func CodecEnabled(c Codec) bool

CodecEnabled checks if the codec is enabled in the GlobalCodecs set.

func CodecEnabledByName

func CodecEnabledByName(name string) bool

CodecEnabledByName checks if the codec name is enabled in the GlobalCodecs set.

func CodecSetEnabled

func CodecSetEnabled(name string, enabled bool)

CodecSetEnabled enables or disables a codec in the GlobalCodecs set.

func CodecsSetEnabled

func CodecsSetEnabled(codecs map[string]bool)

CodecsSetEnabled enables or disables multiple codecs in the GlobalCodecs set.

func DumpFramesPCM16

func DumpFramesPCM16(w io.WriteCloser, sampleRate int, frames []PCM16Sample) error

DumpFramesPCM16 writes PCM16 frames in a little-endian format (s16le). It is compatible with the following ffmpeg options: -f s16le -ar <rate> -ac 1

func MonoToStereo

func MonoToStereo(dst, src PCM16Sample)

MonoToStereo converts mono PCM from src to stereo PCM in dst. It panics if size of dst is too small. Src and dst must not overlap.

func OnRegister

func OnRegister(fnc func(c Codec))

func Pipe

func Pipe[T ~[]E, E comparable](sampleRate int) (*PipeReader[T, E], *PipeWriter[T, E])

Pipe creates a synchronous in-memory pipe. It can be used to connect code expecting a Reader with code expecting a Writer.

func PlayAudio

func PlayAudio[T any](ctx context.Context, w Writer[T], sampleDur time.Duration, frames []T) error

PlayAudio into a given writer. It assumes that frames are already at the writer's sample rate.

func RegisterCodec

func RegisterCodec(c Codec)

RegisterCodec registers the codec.

func StereoToMono

func StereoToMono(dst, src PCM16Sample)

StereoToMono converts stereo PCM from src to mono PCM in dst. It panics if size of dst is too small. Src and dst may overlap.

Types

type AudioCodec

type AudioCodec interface {
	Codec
	// EncodeBytes creates a PCM->bytes encoder using the codec.
	EncodeBytes(w BytesWriter) PCM16Writer
	// DecodeBytes creates a bytes->PCM decoder using the codec.
	DecodeBytes(w PCM16Writer) BytesWriter
}

AudioCodec is an audio codec implementation that can encode/decode bytes<->PCM.

Each audio codec also implements AudioFrameCodec that can encode/decode bytes to a native Frame format of the codec.

type AudioDecodeFunc

type AudioDecodeFunc[S Frame] func(w PCM16Writer) WriteCloser[S]

type AudioEncodeFunc

type AudioEncodeFunc[S Frame] func(w WriteCloser[S]) PCM16Writer

type AudioFrameCodec

type AudioFrameCodec[S BytesFrame] interface {
	AudioCodec
	// Encode creates a Frame->bytes encoder using the codec.
	// The frame is in a native format of the codec.
	Encode(w WriteCloser[S]) PCM16Writer
	// Decode creates a bytes->Frame decoder using the codec.
	// The frame is in a native format of the codec.
	Decode(w PCM16Writer) WriteCloser[S]
}

AudioFrameCodec is an audio codec implementation that can encode/decode bytes to/from a native Frame format of the codec.

func NewAudioCodec

func NewAudioCodec[S BytesFrame](
	info CodecInfo,
	decode AudioDecodeFunc[S],
	encode AudioEncodeFunc[S],
) AudioFrameCodec[S]

NewAudioCodec creates an audio codec with a given encode and decode implementations.

type BytesFrame

type BytesFrame interface {
	~[]byte
	Frame
}

type BytesWriter

type BytesWriter interface {
	String() string
	WriteRaw(frame []byte) error
	Close() error
}

BytesWriter is similar to io.WriteCloser, but intentionally breaks API compatibility.

This is done to emphasize that BytesWriter implementations are aware of the frame boundaries, and will only use buffer sizes that match the frame size.

func DecodeBytes

func DecodeBytes[S BytesFrame](w WriteCloser[S]) BytesWriter

DecodeBytes creates a writer that converts every binary write from a standard io.WriteCloser to a frame write.

Note that directly reading from a file is not possible in this case, as the frame boundaries are unknown.

func NewBytesWriter

func NewBytesWriter(w io.WriteCloser) BytesWriter

NewBytesWriter creates a BytesWriter that writes to a standard io.WriteCloser.

This process will erase the frame boundaries. Implement BytesWriter directly to preserve frame boundaries.

type Codec

type Codec interface {
	Info() CodecInfo
}

func Codecs

func Codecs() []Codec

Codecs lists all registered codecs.

func EnabledCodecs

func EnabledCodecs() []Codec

EnabledCodecs lists all codecs enabled in the GlobalCodecs set.

func NewCodec

func NewCodec(info CodecInfo) Codec

NewCodec creates a generic codec definition without a specific implementation.

type CodecInfo

type CodecInfo struct {
	SDPName      string
	SampleRate   int
	RTPClockRate int
	RTPDefType   byte
	RTPIsStatic  bool
	Priority     int
	Disabled     bool // codec is disabled in GlobalCodecs by default
	Hidden       bool // codec should not appear in SDP offer, but can be used in the answer
	FileExt      string
}

type CodecSet

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

CodecSet represents a set of codecs that can be enabled or disabled.

func GlobalCodecs

func GlobalCodecs() *CodecSet

GlobalCodecs returns a shared codec set.

func NewCodecSet

func NewCodecSet() *CodecSet

NewCodecSet creates an empty codec set. All codecs are disabled, unless enabled explicitly.

func (*CodecSet) IsEnabled

func (s *CodecSet) IsEnabled(c Codec) bool

IsEnabled checks if a given codec is enabled.

func (*CodecSet) IsEnabledByName

func (s *CodecSet) IsEnabledByName(name string) bool

IsEnabledByName checks if a given codec is enabled by its name.

func (*CodecSet) ListEnabled

func (s *CodecSet) ListEnabled() []Codec

ListEnabled lists all enabled codecs.

func (*CodecSet) NewSet

func (s *CodecSet) NewSet() *CodecSet

NewSet creates a codec set that overlays the current codec set. It will inherit all codecs enabled in the parent set.

func (*CodecSet) SetEnabled

func (s *CodecSet) SetEnabled(name string, enabled bool)

SetEnabled enables or disables a given codec.

func (*CodecSet) SetEnabledMap

func (s *CodecSet) SetEnabledMap(codecs map[string]bool)

SetEnabledMap is the same as SetEnabled, but accepts a map with multiple codecs.

type Frame

type Frame interface {
	// Size of the frame in bytes.
	Size() int
	// CopyTo copies the frame content to the destination bytes slice.
	// It returns io.ErrShortBuffer is the buffer size is less than frame's Size.
	CopyTo(dst []byte) (int, error)
}

type MediaSampleWriter

type MediaSampleWriter interface {
	WriteSample(sample media.Sample) error
}

type MultiWriter

type MultiWriter[T any] []WriteCloser[T]

func (MultiWriter[T]) Close

func (s MultiWriter[T]) Close() error

func (MultiWriter[T]) SampleRate

func (s MultiWriter[T]) SampleRate() int

func (MultiWriter[T]) String

func (s MultiWriter[T]) String() string

func (MultiWriter[T]) WriteSample

func (s MultiWriter[T]) WriteSample(sample T) error

type PCM16GainWriter

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

PCM16GainWriter scales every PCM16 sample by a configurable gain before forwarding it to the underlying writer.

It is intended for runtime volume/ducking control on a PCM16 audio path: callers update the gain concurrently with WriteSample via SetGain and the next WriteSample picks up the new value atomically.

func NewPCM16GainWriter

func NewPCM16GainWriter(w PCM16Writer) *PCM16GainWriter

func (*PCM16GainWriter) Close

func (g *PCM16GainWriter) Close() error

func (*PCM16GainWriter) SampleRate

func (g *PCM16GainWriter) SampleRate() int

func (*PCM16GainWriter) SetGain

func (g *PCM16GainWriter) SetGain(v float32) error

func (*PCM16GainWriter) String

func (g *PCM16GainWriter) String() string

func (*PCM16GainWriter) WriteSample

func (g *PCM16GainWriter) WriteSample(sample PCM16Sample) error

type PCM16Processor

type PCM16Processor = Processor[PCM16Sample]

type PCM16Sample

type PCM16Sample []int16

func Resample

func Resample(dst PCM16Sample, dstSampleRate int, src PCM16Sample, srcSampleRate int) PCM16Sample

Resample the source sample into the destination sample rate. It appends resulting samples to dst and returns the result.

func (PCM16Sample) Clear

func (s PCM16Sample) Clear()

func (PCM16Sample) Close

func (s PCM16Sample) Close() error

func (PCM16Sample) CopyTo

func (s PCM16Sample) CopyTo(dst []byte) (int, error)

func (PCM16Sample) Size

func (s PCM16Sample) Size() int

func (*PCM16Sample) WriteSample

func (s *PCM16Sample) WriteSample(data PCM16Sample) error

type PCM16Writer

type PCM16Writer = WriteCloser[PCM16Sample]

func DumpFilePCM16

func DumpFilePCM16(name string, sampleRate int) PCM16Writer

DumpFilePCM16 writes PCM16 frames in a little-endian format (s16le) to a file '<name>_ar<rate>.s16le'. It is compatible with the following ffmpeg options: -f s16le -ar <rate> -ac 1

func DumpWriterPCM16

func DumpWriterPCM16(name string, w PCM16Writer) PCM16Writer

DumpWriterPCM16 wraps the provided audio writer and dumps all processed frames to a file with DumpFilePCM16. It is useful when debugging different audio processing stages.

func NewPCM16BufferWriter

func NewPCM16BufferWriter(buf *PCM16Sample, sampleRate int) PCM16Writer

func NewPCM16FrameWriter

func NewPCM16FrameWriter(buf *[]PCM16Sample, sampleRate int) PCM16Writer

func ResampleWriter

func ResampleWriter(w PCM16Writer, sampleRate int) (w2 PCM16Writer)

ResampleWriter returns a new writer that expects samples of a given sample rate and resamples then for the destination writer.

type PipeReader

type PipeReader[T ~[]E, E comparable] struct {
	// contains filtered or unexported fields
}

A PipeReader is the read half of a pipe.

func (*PipeReader[T, E]) Close

func (r *PipeReader[T, E]) Close() error

Close closes the reader; subsequent writes to the write half of the pipe will return the error io.ErrClosedPipe.

func (*PipeReader[T, E]) CloseWithError

func (r *PipeReader[T, E]) CloseWithError(err error) error

CloseWithError closes the reader; subsequent writes to the write half of the pipe will return the error err.

CloseWithError never overwrites the previous error if it exists and always returns nil.

func (*PipeReader[T, E]) ReadSample

func (r *PipeReader[T, E]) ReadSample(data T) (n int, err error)

ReadSample implements the Reader interface.

type PipeWriter

type PipeWriter[T ~[]E, E comparable] struct {
	// contains filtered or unexported fields
}

A PipeWriter is the write half of a pipe.

func (*PipeWriter[T, E]) Close

func (w *PipeWriter[T, E]) Close() error

Close closes the writer; subsequent reads from the read half of the pipe will return no bytes and EOF.

func (*PipeWriter[T, E]) CloseWithError

func (w *PipeWriter[T, E]) CloseWithError(err error) error

CloseWithError closes the writer; subsequent reads from the read half of the pipe will return no bytes and the error err, or EOF if err is nil.

CloseWithError never overwrites the previous error if it exists and always returns nil.

func (*PipeWriter[T, E]) SampleRate

func (w *PipeWriter[T, E]) SampleRate() int

SampleRate implements the Writer interface.

func (*PipeWriter[T, E]) String

func (w *PipeWriter[T, E]) String() string

func (*PipeWriter[T, E]) WriteSample

func (w *PipeWriter[T, E]) WriteSample(data T) (err error)

WriteSample implements the Writer interface.

type Processor

type Processor[T any] func(w WriteCloser[T]) WriteCloser[T]

type ReadCloser

type ReadCloser[T any] interface {
	Reader[T]
	Close() error
}

type Reader

type Reader[T any] interface {
	ReadSample(buf T) (int, error)
}

func NewPCM16BufferReader

func NewPCM16BufferReader(buf PCM16Sample) Reader[PCM16Sample]

type SwitchWriter

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

func NewSwitchWriter

func NewSwitchWriter(sampleRate int) *SwitchWriter

func (*SwitchWriter) Close

func (s *SwitchWriter) Close() error

func (*SwitchWriter) Disable

func (s *SwitchWriter) Disable()

func (*SwitchWriter) Enable

func (s *SwitchWriter) Enable()

func (*SwitchWriter) Get

func (s *SwitchWriter) Get() PCM16Writer

func (*SwitchWriter) SampleRate

func (s *SwitchWriter) SampleRate() int

SampleRate returns an expected sample rate for this writer. It panics if the sample rate is not specified.

func (*SwitchWriter) SetSampleRate

func (s *SwitchWriter) SetSampleRate(rate int)

SetSampleRate sets a new sample rate for the switch. For this to work, NewSwitchWriter(-1) must be called. The code will panic if sample rate is unset when a writer is attached, or if this method is called twice.

func (*SwitchWriter) String

func (s *SwitchWriter) String() string

func (*SwitchWriter) Swap

Swap sets an underlying writer and returns the old one. Caller is responsible for closing the old writer.

func (*SwitchWriter) WriteSample

func (s *SwitchWriter) WriteSample(sample PCM16Sample) error

type WriteCloser

type WriteCloser[T any] interface {
	Writer[T]
	Close() error
}

func DumpFile

func DumpFile[T Frame](name, ext string, sampleRate int) WriteCloser[T]

DumpFile writes frames in its native byte format, without any container to a file '<name>_ar<rate>.<ext>'

This is probably not the way you want to save the audio, unless you know what you are doing. Consider using webm package instead if you want to play the audio file directly.

func DumpWriter

func DumpWriter[T Frame](ext string, name string, w WriteCloser[T]) WriteCloser[T]

DumpWriter wraps the provided audio writer and dumps all processed frames to a file with DumpFile. It is useful when debugging different audio processing stages.

func EncodeBytes

func EncodeBytes[S Frame](w BytesWriter, sampleRate int) WriteCloser[S]

EncodeBytes creates a writer that converts every frame write to a single binary Write call on a standard io.WriteCloser.

If preserving frame boundaries is not required, using NewFileWriter would be more efficient.

func FromSampleWriter

func FromSampleWriter[T ~[]byte](w MediaSampleWriter, sampleRate int, sampleDur time.Duration) WriteCloser[T]

func FullFrames

func FullFrames[T ~[]S, S sample](w WriteCloser[T], frameSize int) WriteCloser[T]

FullFrames creates a writer that only writes full frames of a given size to the underlying writer (except the last one).

func NewFileWriter

func NewFileWriter[T Frame](w io.WriteCloser, sampleRate int) WriteCloser[T]

NewFileWriter creates a new frame writer that encodes frame to a binary stream.

This process will erase the frame boundaries. Use EncodeBytes to preserve frame boundaries.

func NewFrameWriter

func NewFrameWriter[T ~[]S, S sample](buf *[]T, sampleRate int) WriteCloser[T]

NewFrameWriter creates a writer that appends a copy of all written frames to a slice.

func NopCloser

func NopCloser[T any](w Writer[T]) WriteCloser[T]

type Writer

type Writer[T any] interface {
	String() string
	SampleRate() int
	WriteSample(sample T) error
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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