media

package module
v0.0.0-...-67dd4ed Latest Latest
Warning

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

Go to latest
Published: Dec 30, 2025 License: Apache-2.0 Imports: 16 Imported by: 31

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

func CodecEnabledByName

func CodecEnabledByName(name string) bool

func CodecSetEnabled

func CodecSetEnabled(name string, enabled bool)

func CodecsSetEnabled

func CodecsSetEnabled(codecs map[string]bool)

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)

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 Codec

type Codec interface {
	Info() CodecInfo
}

func Codecs

func Codecs() []Codec

func EnabledCodecs

func EnabledCodecs() []Codec

func NewCodec

func NewCodec(info CodecInfo) Codec

type CodecInfo

type CodecInfo struct {
	SDPName      string
	SampleRate   int
	RTPClockRate int
	RTPDefType   byte
	RTPIsStatic  bool
	Priority     int
	Disabled     bool
	FileExt      string
}

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 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 DumpWriterPCM16

func DumpWriterPCM16(name string, w PCM16Writer) PCM16Writer

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 DumpWriter

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

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]

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