aego/audio
Sounds are presentation: they never change the game state, and a rewind or replay is silent. Wait for game logic with a timer, not with a Sound. The engine ships four clips for tests and sketches, audio.Builtin("Beep"), Click, Hum and Melody; the last two loop without a seam. The example plays a hum and music, then fades both out. See Audio.
Example
package demo
import (
"aego/audio"
"aego/log"
)
script Chime {
var tone audio.Sound
var frames int
func Ready() {
tone = audio.PlayWith(audio.Builtin("Hum"), audio.PlayOptions{Volume: 0.5, Pitch: 1.5, Loop: true})
audio.Music(audio.Builtin("Melody"), 0.5)
audio.SetBusVolume(audio.Bus.Music, 0.6)
log.Info("the hum plays: " + string(tone.Playing()))
}
func Update(dt float) {
frames++
if frames == 60 {
tone.StopFade(0.2)
audio.StopMusic(0.5)
log.Info("the hum fades out: " + string(!tone.Playing()))
}
}
}
Functions
| Builtin | (name string) AssetRef<audio> | Returns a clip that ships with the engine: Beep, Click, Hum (loopable) or Melody (loopable). |
| BusVolume | (bus aego/audio.Bus) float | Returns the session volume of a bus set by SetBusVolume; the player's settings are separate. |
| ListenerPosition | () Vec2 | Returns the last effective listener position in world space, or zero outside Play. It includes the camera fallback when no listener is active. |
| Music | (clip AssetRef<audio>, seconds float) Handle<aego/audio.Sound> | Loops a clip on the music bus, crossfading from the music that played over seconds. |
| Mute | (bus aego/audio.Bus, on bool) | Silences a bus for this session. |
| Pause | (on bool) | Pauses or resumes every sound of this world. |
| Play | (clip AssetRef<audio>) Handle<aego/audio.Sound> | Plays a clip on its bus. The sound stops when the node of the script is destroyed. |
| PlayAt | (clip AssetRef<audio>, position Vec2) Handle<aego/audio.Sound> | Plays a clip at a world position, fading and panning with the distance to the listener. |
| PlayEvent | (event AssetRef<data:AudioEvent>) Handle<aego/audio.Sound> | Plays an audio event; a spatial event follows the node of the script. |
| PlayEventAt | (event AssetRef<data:AudioEvent>, position Vec2) Handle<aego/audio.Sound> | Plays an audio event at a world position. |
| PlayOptions | | PlayOptions tune one play. Volume and Pitch 0 mean 1; Bus Master plays on the bus of the clip; Unscaled keeps playing while the game is paused. |
| PlayWith | (clip AssetRef<audio>, options aego/audio.PlayOptions) Handle<aego/audio.Sound> | Plays a clip with options. |
| SetBusVolume | (bus aego/audio.Bus, volume float) | Sets the session volume of a bus, on top of the player's settings; it is not saved. |
| Sound | | Sound is a playing sound. yield s waits until it ends; a nil or finished sound is done. It is a presentation event: wait for game logic with a timer, not a sound. |
| StopMusic | (seconds float) | Fades the music out over seconds. |
| WorldBusGain | (bus aego/audio.Bus) float | Returns the last zone-selected world-layer target for a bus, or 1 outside Play. It is not the ramped mixer output or the player's settings. |
3 constants and enum values are listed below.
Builtin function
func Builtin(name string) AssetRef<audio>
Returns a clip that ships with the engine: Beep, Click, Hum (loopable) or Melody (loopable).
Example
audio.Play(audio.Builtin("Beep"))
Bus enum
enum Bus
Bus is a mixer bus: Master, Music, Sfx, UI, Ambience or Voice.
BusVolume function
func BusVolume(bus aego/audio.Bus) float
Returns the session volume of a bus set by SetBusVolume; the player's settings are separate.
See the module example for this function in use.
ListenerPosition function
func ListenerPosition() Vec2
Returns the last effective listener position in world space, or zero outside Play. It includes the camera fallback when no listener is active.
See the module example for this function in use.
Music function
func Music(clip AssetRef<audio>, seconds float) Handle<aego/audio.Sound>
Loops a clip on the music bus, crossfading from the music that played over seconds.
Example
audio.Music(Theme, 1.5)
Mute function
func Mute(bus aego/audio.Bus, on bool)
Silences a bus for this session.
See the module example for this function in use.
Pause function
func Pause(on bool)
Pauses or resumes every sound of this world.
See the module example for this function in use.
PauseWith enum
enum PauseWith
PauseWith decides whether a sound pauses with the game or keeps playing.
Play function
func Play(clip AssetRef<audio>) Handle<aego/audio.Sound>
Plays a clip on its bus. The sound stops when the node of the script is destroyed.
Example
audio.Play(Coin)
PlayAt function
func PlayAt(clip AssetRef<audio>, position Vec2) Handle<aego/audio.Sound>
Plays a clip at a world position, fading and panning with the distance to the listener.
See the module example for this function in use.
PlayEvent function
func PlayEvent(event AssetRef<data:AudioEvent>) Handle<aego/audio.Sound>
Plays an audio event; a spatial event follows the node of the script.
Example
audio.PlayEvent(Jump)
PlayEventAt function
func PlayEventAt(event AssetRef<data:AudioEvent>, position Vec2) Handle<aego/audio.Sound>
Plays an audio event at a world position.
See the module example for this function in use.
PlayWith function
func PlayWith(clip AssetRef<audio>, options aego/audio.PlayOptions) Handle<aego/audio.Sound>
Plays a clip with options.
Example
audio.PlayWith(Step, audio.PlayOptions{Volume: 0.5, Pitch: 1.1})
SetBusVolume function
func SetBusVolume(bus aego/audio.Bus, volume float)
Sets the session volume of a bus, on top of the player's settings; it is not saved.
See the module example for this function in use.
Steal enum
enum Steal
Steal is what an audio event does at Max Voices: stop the oldest voice, stop the quietest, or skip the new play.
StopMusic function
func StopMusic(seconds float)
Fades the music out over seconds.
See the module example for this function in use.
WorldBusGain function
func WorldBusGain(bus aego/audio.Bus) float
Returns the last zone-selected world-layer target for a bus, or 1 outside Play. It is not the ramped mixer output or the player's settings.
See the module example for this function in use.
- PlayOptions value type PlayOptions tune one play. Volume and Pitch 0 mean 1; Bus Master plays on the bus of the clip; Unscaled keeps playing while the game is paused.
- Sound type Sound is a playing sound. yield s waits until it ends; a nil or finished sound is done. It is a presentation event: wait for game logic with a timer, not a sound.