alsa

package
v1.0.0-rc.1 Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2026 License: MIT Imports: 19 Imported by: 1 module, 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 alsa discovers ALSA PCM endpoints and opens Linux playback streams.

Use List and Card.Devices for discovery, and close each card when done. OpenPlayback opens an int16 or float32 sink on Linux AMD64 and ARM64. Read the negotiated format with Playback.PCMFormat. Call Playback.Drain to play queued frames before releasing the device with Playback.Close.

Index

Examples

Constants

View Source
const (
	StandardSubformat  SubformatType = iota              // Standard PCM subformat.
	SubformatTypeFirst               = StandardSubformat // First PCM subformat.
	SubformatTypeLast                = StandardSubformat // Last PCM subformat.
)
View Source
const (
	Unknown sampleFormat = -1 // Unrecognized sample format.
)

Variables

View Source
var ErrPlaybackUnsupported = errors.New("alsa: playback requires Linux AMD64 or ARM64")

ErrPlaybackUnsupported indicates that ALSA playback is unavailable on this platform.

Functions

This section is empty.

Types

type Access

type Access int
const (
	MmapInterleaved    Access             = iota // Memory-mapped interleaved frames.
	MmapNonInterleaved                           // Memory-mapped channel planes.
	MmapComplex                                  // Memory-mapped complex channel layout.
	RWInterleaved                                // Interleaved read/write frames.
	RWNonInterleaved                             // Read/write channel planes.
	AccessTypeLast     = RWNonInterleaved        // Last access mode.
	AccessTypeFirst    = MmapInterleaved         // First access mode.
)

func (Access) String

func (a Access) String() string

type Card

type Card struct {
	Path    string
	ID      string
	Name    string
	Driver  string
	Index   int
	Version Version
	// contains filtered or unexported fields
}

func List

func List() ([]*Card, error)

List all available cards.

func Open

func Open(index int) (*Card, error)

Open a handle to a card by number.

Example
card, err := Open(0)
if err != nil {
	panic("error opening card 0: " + err.Error())
}
println("opened card ", card)

func OpenDriver

func OpenDriver(name string) (*Card, error)

OpenDriver opens a handle to a card by driver name.

Example
card, err := OpenDriver("snd_bcm2835")
if err != nil {
	panic("error opening snd_bcm2835: " + err.Error())
}
println("opened card ", card)

func (*Card) Close

func (card *Card) Close() error

func (*Card) Devices

func (card *Card) Devices() ([]*Device, error)

Devices lists one descriptor per available PCM direction on the open card. For an ALSA hardware PCM name, use hw:<card index>,<device index> with OpenPlayback. Read the negotiated format with Playback.PCMFormat.

type Device

type Device struct {
	Type      DeviceType
	Index     int
	Path      string // Kernel device node.
	Name      string
	CanPlay   bool
	CanRecord bool
}

Device describes one stream direction on an ALSA PCM device. To play PCM, open a Playback with OpenPlayback and read the negotiated format from Playback.PCMFormat.

func (Device) String

func (dev Device) String() string

type DeviceType

type DeviceType int
const (
	UnknownDeviceType DeviceType = iota // Unrecognized device type.
	PCM                                 // PCM device.
)

func (DeviceType) String

func (t DeviceType) String() string

type Flags

type Flags uint32
const (
	OpenMin Flags = 1 << iota // Exclusive lower bound.
	OpenMax                   // Exclusive upper bound.
	Integer                   // Integer values only.
	Empty                     // Empty range.
)

func (Flags) String

func (f Flags) String() string

type Playback added in v1.0.0

type Playback[T PlaybackSample] struct {
	// contains filtered or unexported fields
}

Playback is a planar sink whose channels follow the device's negotiated order. A Playback must be closed when it is no longer needed.

func OpenPlayback added in v1.0.0

func OpenPlayback[T PlaybackSample](ctx context.Context, config PlaybackConfig) (*Playback[T], error)

OpenPlayback opens a nonblocking ALSA playback PCM. The context governs both opening and subsequent writes and drains. The sample type fixes the native encoding. PCMFormat reports the negotiated rate and channel order. A nil context returns audio.ErrNilContext.

func (*Playback[T]) Close added in v1.0.0

func (p *Playback[T]) Close() error

Close cancels pending operations and releases the PCM. Use Drain first to wait for queued frames to play. Close is safe to call more than once.

func (*Playback[T]) Drain added in v1.0.0

func (p *Playback[T]) Drain() error

Drain waits for pending frames to play. The PCM can accept new frames afterward.

func (*Playback[T]) PCMFormat added in v1.0.0

func (p *Playback[T]) PCMFormat() audio.Format

PCMFormat returns the rate, channel order, and encoding selected for playback.

func (*Playback[T]) PreferredBlockFrames added in v1.0.0

func (p *Playback[T]) PreferredBlockFrames() int

PreferredBlockFrames returns the negotiated ALSA period in frames.

func (*Playback[T]) WriteFrames added in v1.0.0

func (p *Playback[T]) WriteFrames(src audio.Buffer[T]) (int, error)

WriteFrames writes complete frames from equal-length channel slices. On cancellation or device failure it returns the number accepted before the error.

type PlaybackConfig added in v1.0.0

type PlaybackConfig struct {
	Device       string // ALSA PCM name. An empty name selects "default".
	RateHz       int    // Requested sample rate. ALSA may select a nearby rate.
	Channels     int    // Required channel count, from 1 to 10.
	PeriodFrames int    // Requested period. Zero requests approximately 10 ms.
}

PlaybackConfig contains the requested ALSA PCM parameters.

type PlaybackSample added in v1.0.0

type PlaybackSample interface {
	~int16 | ~float32
}

PlaybackSample describes the native PCM sample types supported by ALSA playback.

Example
// A named int16 type selects the device's signed 16-bit encoding.
type sample int16

// This example cancels before opening an ALSA device.
ctx, cancel := context.WithCancel(context.Background())
cancel()
_, err := OpenPlayback[sample](ctx, PlaybackConfig{
	RateHz:   48000,
	Channels: 2,
})
if !errors.Is(err, context.Canceled) {
	panic("expected the canceled open to stop before accessing hardware")
}

type SubformatType

type SubformatType int

func (SubformatType) String

func (f SubformatType) String() string

type Timespec

type Timespec struct {
	Sec  int
	Nsec int
}

type Version

type Version uint32

func (Version) Major

func (v Version) Major() int

func (Version) Minor

func (v Version) Minor() int

func (Version) Patch

func (v Version) Patch() int

func (Version) String

func (v Version) String() string

Jump to

Keyboard shortcuts

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