Documentation
¶
Index ¶
- Constants
- func CodecEnabled(c Codec) bool
- func CodecEnabledByName(name string) bool
- func CodecSetEnabled(name string, enabled bool)
- func CodecsSetEnabled(codecs map[string]bool)
- func DumpFramesPCM16(w io.WriteCloser, sampleRate int, frames []PCM16Sample) error
- func MonoToStereo(dst, src PCM16Sample)
- func OnRegister(fnc func(c Codec))
- func Pipe[T ~[]E, E comparable](sampleRate int) (*PipeReader[T, E], *PipeWriter[T, E])
- func PlayAudio[T any](ctx context.Context, w Writer[T], sampleDur time.Duration, frames []T) error
- func RegisterCodec(c Codec)
- func StereoToMono(dst, src PCM16Sample)
- type AudioCodec
- type AudioDecodeFunc
- type AudioEncodeFunc
- type AudioFrameCodec
- type BytesFrame
- type BytesWriter
- type Codec
- type CodecInfo
- type CodecSet
- type Frame
- type MediaSampleWriter
- type MultiWriter
- type PCM16GainWriter
- type PCM16Processor
- type PCM16Sample
- type PCM16Writer
- func DumpFilePCM16(name string, sampleRate int) PCM16Writer
- func DumpWriterPCM16(name string, w PCM16Writer) PCM16Writer
- func NewPCM16BufferWriter(buf *PCM16Sample, sampleRate int) PCM16Writer
- func NewPCM16FrameWriter(buf *[]PCM16Sample, sampleRate int) PCM16Writer
- func ResampleWriter(w PCM16Writer, sampleRate int) (w2 PCM16Writer)
- type PipeReader
- type PipeWriter
- type Processor
- type ReadCloser
- type Reader
- type SwitchWriter
- func (s *SwitchWriter) Close() error
- func (s *SwitchWriter) Disable()
- func (s *SwitchWriter) Enable()
- func (s *SwitchWriter) Get() PCM16Writer
- func (s *SwitchWriter) SampleRate() int
- func (s *SwitchWriter) SetSampleRate(rate int)
- func (s *SwitchWriter) String() string
- func (s *SwitchWriter) Swap(w PCM16Writer) PCM16Writer
- func (s *SwitchWriter) WriteSample(sample PCM16Sample) error
- type WriteCloser
- func DumpFile[T Frame](name, ext string, sampleRate int) WriteCloser[T]
- func DumpWriter[T Frame](ext string, name string, w WriteCloser[T]) WriteCloser[T]
- func EncodeBytes[S Frame](w BytesWriter, sampleRate int) WriteCloser[S]
- func FromSampleWriter[T ~[]byte](w MediaSampleWriter, sampleRate int, sampleDur time.Duration) WriteCloser[T]
- func FullFrames[T ~[]S, S sample](w WriteCloser[T], frameSize int) WriteCloser[T]
- func NewFileWriter[T Frame](w io.WriteCloser, sampleRate int) WriteCloser[T]
- func NewFrameWriter[T ~[]S, S sample](buf *[]T, sampleRate int) WriteCloser[T]
- func NopCloser[T any](w Writer[T]) WriteCloser[T]
- type Writer
Constants ¶
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 ¶
CodecEnabled checks if the codec is enabled in the GlobalCodecs set.
func CodecEnabledByName ¶
CodecEnabledByName checks if the codec name is enabled in the GlobalCodecs set.
func CodecSetEnabled ¶
CodecSetEnabled enables or disables a codec in the GlobalCodecs set.
func CodecsSetEnabled ¶
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 ¶
PlayAudio into a given writer. It assumes that frames are already at the writer's sample rate.
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 BytesWriter ¶
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 EnabledCodecs ¶
func EnabledCodecs() []Codec
EnabledCodecs lists all codecs enabled in the GlobalCodecs set.
type CodecSet ¶
type CodecSet struct {
// contains filtered or unexported fields
}
CodecSet represents a set of codecs that can be enabled or disabled.
func NewCodecSet ¶
func NewCodecSet() *CodecSet
NewCodecSet creates an empty codec set. All codecs are disabled, unless enabled explicitly.
func (*CodecSet) IsEnabledByName ¶
IsEnabledByName checks if a given codec is enabled by its name.
func (*CodecSet) ListEnabled ¶
ListEnabled lists all enabled codecs.
func (*CodecSet) NewSet ¶
NewSet creates a codec set that overlays the current codec set. It will inherit all codecs enabled in the parent set.
func (*CodecSet) SetEnabled ¶
SetEnabled enables or disables a given codec.
func (*CodecSet) SetEnabledMap ¶
SetEnabledMap is the same as SetEnabled, but accepts a map with multiple codecs.
type MediaSampleWriter ¶
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) 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 Reader ¶
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 ¶
func (s *SwitchWriter) Swap(w PCM16Writer) PCM16Writer
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 ¶
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]