driver

package
v1.0.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: 5 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 driver opens the default audio playback device for the host platform.

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrPlaybackUnsupported = errors.New("driver: default playback is unsupported on this platform")

ErrPlaybackUnsupported reports that the host has no supported playback backend.

Functions

This section is empty.

Types

type Playback

type Playback interface {
	audio.Sink[float32]
	audio.BlockSizeHint
	// Drain waits for the device to play frames it already accepted.
	Drain() error
	// Close releases the device. Call Drain first to let queued audio finish.
	Close() error
}

Playback is a device-backed float32 PCM sink. Call Drain to play queued frames and Close to release the device.

Example
package main

import "github.com/BeatGlow/audio"

// capturedPlayback records the calls a device would receive.
type capturedPlayback struct {
	samples []float32
	steps   []string
}

func (*capturedPlayback) PCMFormat() audio.Format {
	return audio.Format{
		RateHz: 48000,
		Layout: audio.MonoLayout,
		Encoding: audio.Encoding{
			Kind: audio.FloatPCM,
			Bits: 32,
		},
	}
}

func (*capturedPlayback) PreferredBlockFrames() int { return 2 }

func (p *capturedPlayback) WriteFrames(frames audio.Buffer[float32]) (int, error) {
	p.samples = append(p.samples, frames[0]...)
	return frames.Frames(), nil
}

func (p *capturedPlayback) Drain() error {
	p.steps = append(p.steps, "drain")
	return nil
}

func (p *capturedPlayback) Close() error {
	p.steps = append(p.steps, "close")
	return nil
}

func main() {
	// OpenPlayback supplies the device in an application.
	device := new(capturedPlayback)
	var output Playback = device
	format := output.PCMFormat()
	block := make(audio.Buffer[float32], len(format.Layout))
	for ch := range block {
		block[ch] = make([]float32, output.PreferredBlockFrames())
	}
	block[0][0] = 0.25
	n, err := output.WriteFrames(block)
	if err != nil || n != block.Frames() {
		panic("expected the device to accept one complete block")
	}
	if err := output.Drain(); err != nil {
		panic(err)
	}
	if err := output.Close(); err != nil {
		panic(err)
	}
	if len(device.samples) != 2 || device.samples[0] != 0.25 ||
		len(device.steps) != 2 || device.steps[0] != "drain" || device.steps[1] != "close" {
		panic("expected the sample block to play before closing")
	}

}

func OpenPlayback

func OpenPlayback(ctx context.Context, rateHz, channels int) (Playback, error)

OpenPlayback opens the platform's default output using CoreAudio on macOS or ALSA on Linux AMD64 and ARM64. Request one channel for mono or two for stereo. Use the returned PCMFormat to build sources at the negotiated sample rate and channel order. The context governs the lifetime of playback. A nil context returns audio.ErrNilContext.

Directories

Path Synopsis
Package alsa discovers ALSA PCM endpoints and opens Linux playback streams.
Package alsa discovers ALSA PCM endpoints and opens Linux playback streams.
Package coreaudio plays PCM through the macOS default output device.
Package coreaudio plays PCM through the macOS default output device.

Jump to

Keyboard shortcuts

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