moho

package module
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Nov 30, 2026 License: MIT Imports: 11 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

moho

Go Reference CI

Cross-platform, CGO-free Go audio library powered by purego for native OS audio I/O (CoreAudio on macOS, PulseAudio/ALSA on Linux, WASAPI on Windows).

Packages

Package Purpose External Dependencies
github.com/banditmoscow1337/moho Native float32 PCM I/O (Backend, Device), lock-free frame-aligned PCMRing, reconnect/output stats interfaces purego
github.com/banditmoscow1337/moho/voice Full-duplex VoIP Engine, JitterBuffer (PopWithStatus), PCM worker pipeline, timing histograms moho, moho/voicebuf
github.com/banditmoscow1337/moho/opus Optional voice.CodecFactory backed by pion/opus pion/opus
github.com/banditmoscow1337/moho/mixer Playback-only sound/music Driver with voice priority, streaming, 8/16/24/32-bit float PCM and slog diagnostics moho
github.com/banditmoscow1337/moho/decode WAV (8/16/24-bit PCM, IEEE float32, WAVE_FORMAT_EXTENSIBLE) and MP3 file decoders moho/mixer, go-mp3
github.com/banditmoscow1337/moho/voicebuf Pooled wire buffers for zero-copy P2P ingress/egress stdlib

Quick Start

Game Sound Mixer
driver, err := mixer.New(32,
	mixer.WithAppName("my-game"),
	mixer.WithStreamName("Game Audio"),
)
if err != nil {
	log.Fatal(err)
}
defer driver.Close()

// Call driver.Update() once per game tick to reclaim finished voices.
Real-Time VoIP Engine
engine := voice.NewEngine(opus.NewFactory())
defer engine.Cleanup()

if err := engine.AddPeer("peer-1"); err != nil {
	log.Fatal(err)
}
if err := engine.Start(func(packet []byte) {
	// Copy packet before returning and send over your transport.
}); err != nil {
	log.Fatal(err)
}

Build Tags & Testing

  • -tags moho_noaudio: compiles stub backend and codec factory for headless environments or tests without OS audio libraries.
  • Unsupported platforms (e.g. freebsd, linux/arm, linux/riscv64) compile cleanly with a fallback backend returning an unsupported-platform error.
go test -race ./...
go test -tags moho_noaudio ./...
go test -bench=. -benchmem ./voice ./mixer

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

type DataCallback func(output, input []byte, frameCount uint32)

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

type Device interface {
	Start() error
	Stop() error
	Close() error
}

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

type DeviceInfo struct {
	ID   DeviceID
	Name string
}

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

func NewFramedPCMRing(frames, frameBytes int) PCMRing

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

func NewPCMRing(samples int) PCMRing

NewPCMRing creates a ring sized for the given number of float32 samples (4 bytes each).

func (*PCMRing) Cap

func (r *PCMRing) Cap() int

Cap returns the total capacity of the ring in bytes (a multiple of frameBytes).

func (*PCMRing) Discard

func (r *PCMRing) Discard(bytes int) int

Discard advances the read index by up to bytes (frame-aligned) without copying. Must only be called by the single reader.

func (*PCMRing) Len

func (r *PCMRing) Len() int

Len returns the number of unread bytes currently buffered in the ring.

func (*PCMRing) ReadBytes

func (r *PCMRing) ReadBytes(dst []byte) int

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

func (r *PCMRing) WriteBytes(src []byte) int

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

func (r *PCMRing) WriteSilence(bytes int) int

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.

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.

Jump to

Keyboard shortcuts

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