playback

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Feb 13, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package playback provides an interface for audio playback, allowing audio to be sent to various drivers and devices.

Fun fact: the first audio played using this package was the song Parasol Cider by MORE MORE JUMP!.

Example
package main

import (
	"os"

	"github.com/SladkyCitron/resona/codec"
	_ "github.com/SladkyCitron/resona/codec/wav"
	"github.com/SladkyCitron/resona/playback"

	_ "github.com/SladkyCitron/resona/playback/driver/oto"
)

func main() {
	// Open the audio file
	f, err := os.Open("file.wav")
	if err != nil {
		panic(err)
	}
	defer f.Close()

	// Decode audio file into an aio.SampleReader
	decoder, _, err := codec.Decode(f)
	if err != nil {
		panic(err)
	}

	// Create audio playback context with Oto as the driver
	ctx, err := playback.NewContext(decoder.Format(), playback.WithDriver("oto"))
	if err != nil {
		panic(err)
	}
	defer ctx.Close()

	// Create player and start playback
	player := ctx.NewPlayer(decoder)
	player.PlayAndWait() // Play and block until done
}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Drivers

func Drivers() []string

Drivers returns a sorted list of the names of the registered drivers.

func Register

func Register(name string, drv driver.Driver)

Register registers a playback driver with the given name.

Types

type Context

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

Context represents the playback context.

func NewContext

func NewContext(format afmt.Format, opts ...ContextOption) (*Context, error)

NewContext creates a new Context with the specified format and options. If no driver is specified, the default driver (first one registered) is used.

func (*Context) Close

func (ctx *Context) Close() error

Close closes the underlying playback driver and the context.

func (*Context) NewPlayer

func (ctx *Context) NewPlayer(src aio.SampleReader) *Player

NewPlayer creates a new Player.

type ContextOption

type ContextOption func(*Context)

ContextOption represents an option for configuring Context.

func WithBufferSize

func WithBufferSize(size int) ContextOption

WithBufferSize sets the buffer size. Bigger buffer size means lower CPU usage and more reliable playback. Lower buffer size means better responsiveness and less delay.

func WithBufferSizeBytes

func WithBufferSizeBytes(size int) ContextOption

WithBufferSizeBytes sets the buffer size in bytes. Bigger buffer size means lower CPU usage and more reliable playback. Lower buffer size means better responsiveness and less delay.

func WithDriver

func WithDriver(name string) ContextOption

WithDriver sets the playback driver.

type Player

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

Player represents an audio player.

func (*Player) Play

func (p *Player) Play()

Play starts the playback.

func (*Player) PlayAndWait

func (p *Player) PlayAndWait()

PlayAndWait starts the playback and blocks until the player has finished playing and drained.

func (*Player) PlayWithDone

func (p *Player) PlayWithDone() chan struct{}

PlayWithDone starts the playback and returns a channel that closes when the player has finished playing and drained.

Directories

Path Synopsis
Package driver provides the interface for playback drivers.
Package driver provides the interface for playback drivers.
ffplay
Package ffplay provides a FFplay-based playback driver.
Package ffplay provides a FFplay-based playback driver.
oto
Package oto provides a cross-platform [Oto]-based playback driver.
Package oto provides a cross-platform [Oto]-based playback driver.

Jump to

Keyboard shortcuts

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