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 ¶
const ( StandardSubformat SubformatType = iota // Standard PCM subformat. SubformatTypeFirst = StandardSubformat // First PCM subformat. SubformatTypeLast = StandardSubformat // Last PCM subformat. )
const (
Unknown sampleFormat = -1 // Unrecognized sample format.
)
Variables ¶
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. )
type Card ¶
type Card struct {
Path string
ID string
Name string
Driver string
Index int
Version Version
// contains filtered or unexported fields
}
func Open ¶
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 ¶
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) Devices ¶
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.
type DeviceType ¶
type DeviceType int
const ( UnknownDeviceType DeviceType = iota // Unrecognized device type. PCM // PCM device. )
func (DeviceType) String ¶
func (t DeviceType) 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
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
Drain waits for pending frames to play. The PCM can accept new frames afterward.
func (*Playback[T]) PCMFormat ¶ added in v1.0.0
PCMFormat returns the rate, channel order, and encoding selected for playback.
func (*Playback[T]) PreferredBlockFrames ¶ added in v1.0.0
PreferredBlockFrames returns the negotiated ALSA period in frames.
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
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