pcmoutput

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Nov 28, 2026 License: Apache-2.0 Imports: 5 Imported by: 0 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.

Documentation

Overview

Package pcmoutput drives pull-based PCM playback over a frame-oriented audio device such as Linux ALSA.

Index

Constants

View Source
const (
	MinLatencyMicros      = 4_000
	DefaultLatencyMicros  = 40_000
	FallbackLatencyMicros = 100_000
	MaxLatencyMicros      = 500_000
)

DefaultLatencyMicros and fallback bounds used when negotiating a device buffer duration in microseconds from an optional latency hint.

Variables

View Source
var (
	// ErrAgain reports that the device buffer is full or a call was
	// interrupted by a signal and should be retried after waiting.
	ErrAgain = errors.New("nsgo: audio: PCM device temporarily busy")
	// ErrXRun reports a hardware or server underrun (such as ALSA -EPIPE)
	// that requires Prepare before writing resumes.
	ErrXRun = errors.New("nsgo: audio: PCM buffer underrun")
	// ErrSuspended reports that the audio device was suspended (such as
	// ALSA -ESTRPIPE).
	ErrSuspended = errors.New("nsgo: audio: PCM device suspended")
	// ErrDisconnected reports that the audio device was disconnected or
	// became unusable (such as ALSA -ENODEV).
	ErrDisconnected = errors.New("nsgo: audio: PCM device disconnected")
)

Functions

func Duration

func Duration(frames int64, sampleRate int) time.Duration

Duration returns the time spanned by frames at sampleRate Hz.

func LatencyCandidates

func LatencyCandidates(hint time.Duration) []uint32

LatencyCandidates returns ordered ALSA-style buffer latencies in microseconds to try for hint, falling back to safe defaults if the device rejects a tight hint.

Types

type Device

type Device interface {
	// BufferFrames and PeriodFrames return the negotiated ring buffer and
	// per-callback period lengths in frames.
	BufferFrames() int
	PeriodFrames() int
	// Prepare transitions the device into a ready-to-write state.
	Prepare() error
	// AvailDelay returns the frames currently available to write and the
	// frames still queued ahead of playback. delayOK is false if the device
	// cannot report delay.
	AvailDelay() (avail int, delay int64, delayOK bool, err error)
	// Wait blocks up to timeout until the device can accept more frames,
	// reporting whether space became available.
	Wait(timeout time.Duration) (ready bool, err error)
	// Write writes interleaved Float32 samples and returns the frames accepted.
	Write(samples []float32) (frames int, err error)
	// Drop immediately stops playback and discards queued frames.
	Drop()
	// Close releases the underlying device handle.
	Close()
}

Device is the low-level PCM sink owned by one Stream. After New, Device methods are called only under the stream's control lock while stopped or exclusively from the pull goroutine while running.

type Stream

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

Stream implements driver.AudioOutput by pulling PCM periods on a dedicated goroutine and feeding them to a Device.

func New

func New(dev Device, conf driver.AudioOutputConfig) *Stream

New constructs a paused Stream over dev. Call Start to begin pulling samples.

func (*Stream) Close

func (s *Stream) Close()

Close stops rendering and releases the device. Calling it again has no effect.

func (*Stream) Flush

func (s *Stream) Flush() int

Flush stops rendering, discards queued frames not yet played, reports the number of discarded client-rendered frames and restarts if active.

func (*Stream) Latency

func (s *Stream) Latency() time.Duration

Latency returns the most recent device buffer latency, or zero before the first render.

func (*Stream) Pause

func (s *Stream) Pause()

Pause stops rendering, discards queued frames and freezes Position.

func (*Stream) Position

func (s *Stream) Position() (int64, time.Time)

Position returns the monotonic count of frames that reached the device and the host timestamp of the most recent advance.

func (*Stream) Start

func (s *Stream) Start() error

Start prepares the device and launches the pull goroutine.

func (*Stream) Underruns

func (s *Stream) Underruns() int

Underruns counts short Render callbacks and device xruns.

Jump to

Keyboard shortcuts

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