Documentation
¶
Overview ¶
Package moho provides cross-platform, CGO-free native audio I/O powered by purego (CoreAudio on macOS, PulseAudio/ALSA on Linux, and WASAPI on Windows).
Audio callbacks exchange interleaved little-endian float32 PCM buffers via DataCallback. The package also provides PCMRing, a lock-free single-producer single-consumer frame-aligned ring buffer, and optional telemetry interfaces (OutputStatsProvider, ReconnectStatsProvider).
Building with -tags moho_noaudio compiles a stub backend that returns an error on device operations without loading OS audio libraries. Unsupported OS/architecture targets compile cleanly with a fallback backend.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Backend ¶
type Backend interface {
Init() error
ListDevices() (capture, playback []DeviceInfo, err error)
Open(DeviceConfig, DataCallback) (Device, error)
Close() error
}
Backend owns the device inventory and any native audio context. Close must be called after all devices it created have been closed.
func NewDefaultBackend ¶
func NewDefaultBackend() Backend
NewDefaultBackend returns the platform's native audio backend.
type DataCallback ¶
DataCallback is invoked by a backend with borrowed interleaved float32 PCM buffers that remain valid until the callback returns. frameCount may differ from the requested period and must not be assumed to be an Opus frame. Callbacks from one device must be serialized, and the backend must stop callbacks before Device.Stop or Device.Close returns.
type Device ¶
Device represents an opened audio stream pair (or playback-only stream). Implementations must guarantee that all DataCallback invocations have completed before Stop or Close returns.
type DeviceConfig ¶
type DeviceConfig struct {
InputID DeviceID
OutputID DeviceID
SampleRate uint32
Channels uint32
PeriodMillis uint32
// PlaybackOnly opens an output without requiring a capture device.
PlaybackOnly bool
// AppName identifies the application to OS audio servers (e.g. PulseAudio).
// Defaults to "moho" when empty.
AppName string
// CaptureStreamName and PlaybackStreamName label OS audio streams.
CaptureStreamName string
PlaybackStreamName string
// OnDisconnect is called when a running device loses its native stream and
// enters reconnect backoff. Callbacks must not block.
OnDisconnect func(err error)
}
DeviceConfig describes the PCM format requested from a backend. Buffers in DataCallback are interleaved float32 PCM in little-endian byte order.
type DeviceID ¶
type DeviceID string
DeviceID is an opaque identifier returned by a backend. The empty ID selects the system default. IDs are valid only for the backend that returned them.
type DeviceInfo ¶
DeviceInfo describes an audio capture or playback endpoint discovered by a Backend.
type OutputStatsProvider ¶
type OutputStatsProvider interface {
OutputStats() (callbacks, underruns, missingFrames uint64)
}
OutputStatsProvider is optionally implemented by native backends that track hardware render callbacks and output FIFO underruns.
type PCMRing ¶
type PCMRing struct {
// contains filtered or unexported fields
}
PCMRing has one writer and one reader. Both indices count bytes and only advance after their side has finished copying. Capacity and transfers are aligned to frameBytes (default 4 bytes = one mono float32 sample) so the reader never observes a split sample or swapped stereo channels.
func NewFramedPCMRing ¶
NewFramedPCMRing creates a ring sized for the given number of whole PCM frames, each frameBytes bytes wide (typically channels * 4 for float32 PCM).
func NewPCMRing ¶
NewPCMRing creates a ring sized for the given number of float32 samples (4 bytes each).
func (*PCMRing) Cap ¶
Cap returns the total capacity of the ring in bytes (a multiple of frameBytes).
func (*PCMRing) Discard ¶
Discard advances the read index by up to bytes (frame-aligned) without copying. Must only be called by the single reader.
func (*PCMRing) ReadBytes ¶
ReadBytes copies up to len(dst) buffered bytes (rounded down to whole PCM frames) into dst and returns the number of bytes read. Must only be called by the single reader.
func (*PCMRing) Reset ¶
func (r *PCMRing) Reset()
Reset requires the device callback and worker to have both stopped.
func (*PCMRing) WriteBytes ¶
WriteBytes copies up to len(src) bytes (rounded down to whole PCM frames) into the ring and returns the number of bytes written. Must only be called by the single writer.
func (*PCMRing) WriteSilence ¶
WriteSilence appends up to bytes of zeroed silence (rounded down to whole PCM frames) into the ring and returns the number of bytes written. Must only be called by the single writer.
type ReconnectStatsProvider ¶
type ReconnectStatsProvider interface {
ReconnectStats() (reconnects, openFailures uint64, lastError string)
}
ReconnectStatsProvider is optionally implemented by devices that recover from transient hardware disconnects.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
Package decode provides streaming WAV and MP3 audio decoders that produce raw interleaved PCM streams compatible with mixer.PCMFormat.
|
Package decode provides streaming WAV and MP3 audio decoders that produce raw interleaved PCM streams compatible with mixer.PCMFormat. |
|
Package mixer implements a playback-only game sound and music mixer built on top of moho, supporting voice priority, protected sounds, asynchronous streaming, 8/16/24/32-bit float PCM conversion, and optional slog diagnostics.
|
Package mixer implements a playback-only game sound and music mixer built on top of moho, supporting voice priority, protected sounds, asynchronous streaming, 8/16/24/32-bit float PCM conversion, and optional slog diagnostics. |
|
Package opus provides a voice.CodecFactory implementation backed by github.com/pion/opus with Packet Loss Concealment (PLC) support.
|
Package opus provides a voice.CodecFactory implementation backed by github.com/pion/opus with Packet Loss Concealment (PLC) support. |
|
Package voice provides a full-duplex real-time VoIP Engine, a bounded JitterBuffer with Packet Loss Concealment signaling, a lock-free PCM processing pipeline, and wall-time latency histograms.
|
Package voice provides a full-duplex real-time VoIP Engine, a bounded JitterBuffer with Packet Loss Concealment signaling, a lock-free PCM processing pipeline, and wall-time latency histograms. |
|
Package voicebuf manages pooled wire buffers for network audio packet ingress, jitter buffering, and egress without per-packet heap allocations.
|
Package voicebuf manages pooled wire buffers for network audio packet ingress, jitter buffering, and egress without per-packet heap allocations. |