Documentation
¶
Overview ¶
Package audio provides backend-independent playback. Player components select tracks from a Source, while a Mixer controls global volume and pause state. NewAudioSystem reconciles them with a runner's Controller and emits Finished signals. OneShot creates a temporary player that the system destroys when its sequence finishes.
Index ¶
- Constants
- func NewAudioSystem(controller Controller) core.SystemFunc
- func NewPreferences() (*settings.Group[Preferences], error)
- func OneShot(world *core.World, src Source, s Settings) (core.Entity, error)
- func OneShotAt(world *core.World, src Source, s Settings, pos geom.Vector2) (core.Entity, error)
- type Backend
- type BusName
- type Controller
- type Finished
- type Listener
- type ListenerMode
- type LoadMode
- type LoopMode
- type Mixer
- func (m *Mixer) AllPaused() bool
- func (m *Mixer) BusMuted(name BusName) bool
- func (m *Mixer) BusPaused(name BusName) bool
- func (m *Mixer) BusVolume(name BusName) float64
- func (m *Mixer) Buses() []BusName
- func (m *Mixer) Fade(name BusName, to float64, over time.Duration) error
- func (m *Mixer) FadeMaster(to float64, over time.Duration) error
- func (m *Mixer) HasBus(name BusName) bool
- func (m *Mixer) MasterMuted() bool
- func (m *Mixer) MasterVolume() float64
- func (m *Mixer) PauseAll()
- func (m *Mixer) RemoveBus(name BusName)
- func (m *Mixer) ResumeAll()
- func (m *Mixer) SetBusMuted(name BusName, muted bool)
- func (m *Mixer) SetBusPaused(name BusName, paused bool)
- func (m *Mixer) SetBusVolume(name BusName, volume float64) error
- func (m *Mixer) SetMasterVolume(volume float64) error
- func (m *Mixer) ToggleAllPaused()
- func (m *Mixer) ToggleMasterMuted()
- type PauseMode
- type Playback
- type Player
- type Preferences
- type Settings
- type Source
Constants ¶
const SystemName = "engine.audio"
SystemName is the name of the game's audio reconciliation system.
Variables ¶
This section is empty.
Functions ¶
func NewAudioSystem ¶
func NewAudioSystem(controller Controller) core.SystemFunc
NewAudioSystem returns a system that advances mixer fades and reconciles players. It requires a *Mixer resource. With a non-nil controller, it also requires a *signal.Hub resource and validates each player's settings before syncing. A nil controller advances fade advancement but skips player reconciliation.
func NewPreferences ¶ added in v0.4.0
func NewPreferences() (*settings.Group[Preferences], error)
NewPreferences returns a validated group with unity mixer levels.
func OneShot ¶
OneShot creates a temporary entity for a non-looping play. The audio system destroys the entity when the sequence finishes. It returns an error if the source or settings is invalid or settings requests looping or spatial playback. Use OneShotAt for spatial playback.
func OneShotAt ¶ added in v0.4.0
OneShotAt creates a temporary entity at pos. Set Settings.Spatial to enable listener-relative playback. It returns an error if the source or settings is invalid or settings requests looping.
Types ¶
type Backend ¶ added in v0.4.0
type Backend struct {
// contains filtered or unexported fields
}
Backend holds the controller used by the game's audio system. Its zero value has no controller and is suitable for headless games.
func (*Backend) Attach ¶ added in v0.4.0
func (b *Backend) Attach(controller Controller) error
Attach installs a controller. It returns an error if controller is nil or a controller is already attached. An attachment during the frame phase takes effect on the next frame pass.
Attach must not run concurrently with game updates.
func (*Backend) Controller ¶ added in v0.4.0
func (b *Backend) Controller() Controller
Controller returns the attached controller, or nil if none is attached.
type Controller ¶
type Controller interface {
// Sync applies want to the player for entityID and source. live reports
// whether the play has not finished, not whether it is currently playing:
// a paused play is live, while a finished [LoopNone] play is not.
//
// A finished play must not resume merely because want.Playing is true;
// want.Restart requests an explicit restart. The audio system calls Sync
// for each [Player] on every run.
Sync(entityID core.EntityID, source asset.ID, want Playback) (live bool, err error)
// Sweep releases players for entities not passed to Sync since the
// previous sweep. The audio system calls it after each reconciliation
// pass in which all players were synced, including spatial-error passes.
Sweep() error
}
Controller applies resolved Playback state to backend players. Runners implement it and attach it to the game's Backend.
type Finished ¶ added in v0.3.0
type Finished struct {
// Entity is the entity whose play finished. A [OneShot]'s entity is
// destroyed on the same update, so its ID identifies the play only.
Entity core.Entity
// Tick is the fixed tick of the update that observed the finish.
Tick uint64
// Audio is the asset ID of the finished track.
Audio asset.ID
}
Finished is the signal the audio system emits on the update that observes a track finishing. Player.Completed reports when the player has no next track. A LoopTrack play does not finish.
The game reconciles audio after frame systems when a controller is attached. Read Finished in the post-frame phase before the frame window closes, or poll Player.Completed when the exact frame does not matter.
type Listener ¶ added in v0.4.0
type Listener struct {
// Enabled enables spatialization for sources that opt in.
Enabled bool
// Mode selects camera-following or a fixed position.
Mode ListenerMode
// Position is the world-space listener position in [FixedPosition] mode.
Position geom.Vector2
// Range is the positive finite distance in world units at which sound is
// silent. Lateral displacement is divided by Range before it offsets
// Balance.
Range float64
}
Listener controls spatial playback for sources in the world. The zero value disables spatialization.
type ListenerMode ¶ added in v0.4.0
type ListenerMode int
ListenerMode selects how a listener is positioned.
const ( // FollowCamera uses the primary camera entity's transform position and rotation. FollowCamera ListenerMode = iota // FixedPosition uses the listener's position and world axes. FixedPosition )
type Mixer ¶
type Mixer struct {
// contains filtered or unexported fields
}
Mixer holds master and bus volume and mute state, and global and bus pause state. Its zero value has a master volume of zero; use NewMixer for unity.
func NewMixer ¶
func NewMixer() *Mixer
NewMixer returns a mixer with unity master volume and no configured buses.
func (*Mixer) BusMuted ¶ added in v0.4.0
BusMuted reports whether a bus is muted, independently of master mute. An unconfigured bus is not muted.
func (*Mixer) BusPaused ¶ added in v0.4.0
BusPaused reports whether a bus is locally paused or the mixer is globally paused.
func (*Mixer) BusVolume ¶ added in v0.4.0
BusVolume returns a bus's volume, independently of mute state. An unconfigured bus has unity volume.
func (*Mixer) Fade ¶ added in v0.4.0
Fade starts a linear bus volume fade to the target over the given duration. It creates the bus if needed and replaces its active fade. The target is clamped to [0, 1]; NaN or a non-positive duration returns an error without creating or changing a bus.
func (*Mixer) FadeMaster ¶ added in v0.4.0
FadeMaster starts a linear master volume fade to the target over the given duration. It replaces any active master fade and clamps the target to [0, 1]. It returns an error for NaN or a non-positive duration without changing the fade.
func (*Mixer) MasterMuted ¶ added in v0.4.0
MasterMuted reports whether the master is muted.
func (*Mixer) MasterVolume ¶ added in v0.4.0
MasterVolume returns the master volume, independently of mute state.
func (*Mixer) RemoveBus ¶ added in v0.4.0
RemoveBus removes a configured bus and its local state. It does nothing if the bus is not configured.
func (*Mixer) ResumeAll ¶
func (m *Mixer) ResumeAll()
ResumeAll clears the global pause state without changing local bus pauses.
func (*Mixer) SetBusMuted ¶ added in v0.4.0
SetBusMuted sets a bus's mute state, creating the bus if needed.
func (*Mixer) SetBusPaused ¶ added in v0.4.0
SetBusPaused sets a bus's local pause state, creating the bus if needed.
func (*Mixer) SetBusVolume ¶ added in v0.4.0
SetBusVolume sets a bus's volume, clamped to [0, 1]. It creates the bus if needed and cancels its fade. NaN returns an error without creating or changing a bus.
func (*Mixer) SetMasterVolume ¶ added in v0.4.0
SetMasterVolume sets the master volume, clamped to [0, 1]. It cancels any master fade and returns an error for NaN without changing the volume or fade.
func (*Mixer) ToggleAllPaused ¶ added in v0.4.0
func (m *Mixer) ToggleAllPaused()
ToggleAllPaused toggles the global pause state.
func (*Mixer) ToggleMasterMuted ¶ added in v0.4.0
func (m *Mixer) ToggleMasterMuted()
ToggleMasterMuted toggles the master mute state.
type Playback ¶
type Playback struct {
// Playing reports whether playback should advance. False holds its position.
Playing bool
// Restart requests a fresh player from the beginning. The audio system
// sets it on the first sync, a track selection change, or
// [Player.Restart].
Restart bool
// Volume is the final mix, including spatial attenuation, in [0, 1].
// Zero is silence: runtime fade targets are safe here.
Volume float64
// Rate is the resolved playback multiplier; 1 is the normal rate.
Rate float64
// Loop is LoopTrack to repeat this track, or LoopNone to play it once.
// Sequence repetition is managed by the audio system.
Loop LoopMode
// Load selects eager decode or per-play streaming.
Load LoadMode
// Balance is the resolved channel attenuation, independent of Volume.
Balance float64
}
Playback is the resolved state that a Controller applies to a play. NewAudioSystem derives it from a Player, a Mixer, and optionally a Listener.
type Player ¶ added in v0.4.0
type Player struct {
// Settings controls playback and may be updated while the player is active.
Settings Settings `json:"settings"`
// Paused holds playback independently of the mixer's pause.
Paused bool `json:"paused"`
// Index is the zero-based index of the selected track in [Player.Source].
// Changing it restarts the selected track on the next sync. An out-of-range
// index is rejected by [Player.Validate].
Index int `json:"index"`
// Source is the track sequence. Prefer [Player.SetSource], which validates
// the replacement and restarts a completed play; a field write takes
// effect on the next sync.
Source Source `json:"source"`
// contains filtered or unexported fields
}
Player describes playback of a Source on an entity. The audio system reconciles it with a Controller.
Construct a player with NewPlayer; its zero value is not valid.
Player serializes its desired state: settings, pause, selection, and source. Runtime playback state rebuilds on the next sync after a load.
func NewPlayer ¶ added in v0.4.0
NewPlayer returns a player with the first track selected. It returns an error if src or settings is invalid.
func (Player) Completed ¶ added in v0.4.0
Completed reports whether the sequence has finished without repeating. Restart clears it immediately; selecting a track clears it on the next sync.
func (Player) Current ¶ added in v0.4.0
Current returns the selected audio ID, whether or not it has been synced. It returns an empty ID for a player with no tracks or an out-of-range selection instead of panicking.
func (*Player) Next ¶ added in v0.4.0
func (p *Player) Next()
Next selects the next track, wrapping to the first. Changing the selection restarts the track on the next sync. It does nothing for a player with no tracks.
func (*Player) Prev ¶ added in v0.4.0
func (p *Player) Prev()
Prev selects the previous track, wrapping to the last. Changing the selection restarts the track on the next sync. It does nothing for a player with no tracks.
func (*Player) Restart ¶ added in v0.4.0
func (p *Player) Restart()
Restart selects the first track, clears completion and the player's pause, and requests a restart on the next audio system update. A mixer-wide pause can still hold playback.
func (*Player) SetSource ¶ added in v0.4.0
SetSource replaces the source and selects its first track. Playback continues from its current position when the new source starts with the track the audio system last synced; otherwise the play restarts from the beginning. A completed play always restarts. It returns an error without changing the player if src is invalid.
type Preferences ¶ added in v0.4.0
type Preferences struct {
// Master is the master volume in [0, 1].
Master float64 `json:"master"`
// Buses maps bus names to desired volumes in [0, 1].
Buses map[string]float64 `json:"buses"`
}
Preferences holds JSON-serializable desired mixer levels.
It does not apply them to a mixer.
func (Preferences) Clone ¶ added in v0.4.0
func (value Preferences) Clone() Preferences
Clone returns a copy of the preferences and their bus volumes.
func (Preferences) Validate ¶ added in v0.4.0
func (value Preferences) Validate() error
Validate reports an error if a mixer level is not finite or outside [0, 1].
type Settings ¶ added in v0.4.0
type Settings struct {
// Bus selects the mixer volume bus; the zero value selects the empty-named bus.
Bus BusName `json:"bus"`
// Volume is the finite per-player volume in [0, 1]. Zero is silence;
// use mixer levels for muting.
Volume float64 `json:"volume"`
// Loop selects how the player advances and repeats tracks. The ebitrun
// runner restarts the current track when its track-repeat setting changes.
Loop LoopMode `json:"loop"`
// Pause selects how the player responds to a mixer-wide pause.
Pause PauseMode `json:"pause"`
// Load selects eager decoding or streaming. The ebitrun runner restarts
// the current track when this changes.
Load LoadMode `json:"load"`
// PlaybackMultiplier is the finite non-negative playback rate; zero means
// normal speed. A runner may reject unsupported rates.
PlaybackMultiplier float64 `json:"playbackMultiplier"`
// Balance attenuates the opposite stereo channel; it must be finite and
// from -1 to 1.
Balance float64 `json:"balance"`
// Spatial enables listener-relative volume and balance. It requires a
// [Listener] resource; an enabled listener also requires a [core.Transform]
// on the player entity.
Spatial bool `json:"spatial"`
// Attenuation names the geom ease that maps normalized distance to
// volume falloff. Empty is linear; unknown names are rejected by
// [Settings.Validate] so the curve round-trips through serialization.
Attenuation string `json:"attenuation"`
}
Settings controls how a Player plays its source.
func DefaultSettings ¶ added in v0.4.0
func DefaultSettings() Settings
DefaultSettings returns settings with unity volume and zero-valued options.
type Source ¶
type Source struct {
// contains filtered or unexported fields
}
Source is a sequence of audio asset IDs for a Player.
func (Source) MarshalJSON ¶ added in v0.4.0
MarshalJSON encodes the source as its track sequence.
func (*Source) UnmarshalJSON ¶ added in v0.4.0
UnmarshalJSON decodes a track sequence without validating it.