Documentation
¶
Overview ¶
Package driver opens the default audio playback device for the host platform.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
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")
}
}
Output:
func OpenPlayback ¶
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. |