Skip to content

Audio

PHPolygon includes a multi-channel audio system with spatial audio support.

Audio Channels

ChannelTypical use
AudioChannel::MasterOverall volume control
AudioChannel::MusicBackground music
AudioChannel::SFXSound effects
AudioChannel::UIUI sounds
AudioChannel::VoiceDialogue

Playing Audio

php
use PHPolygon\Support\Facades\Audio;
use PHPolygon\Audio\AudioChannel;

// Play sounds on specific channels
Audio::playSfx('explosion');
Audio::playMusic('theme');
Audio::playUI('click');

// Adjust channel volume
Audio::setChannelVolume(AudioChannel::SFX, 0.8);
Audio::setChannelVolume(AudioChannel::Music, 0.5);
Audio::setMasterVolume(0.9);

AudioSource Component

Attach an AudioSource to an entity for spatial or looping audio:

php
use PHPolygon\Component\AudioSource;

$entity->attach(new AudioSource(
    clipId: 'campfire_loop',
    volume: 0.6,
    loop: true,
    playOnAwake: true,
));
PropertyTypeDefaultDescription
clipIdstring-Audio clip ID
volumefloat1.0Playback volume
loopboolfalseLoop playback
playOnAwakeboolfalseAuto-play when entity spawns

AudioSystem

The AudioSystem manages playback lifecycle and stops audio sources when their entity is destroyed. Register it like any other system:

php
$world->addSystem(new AudioSystem($engine->audio->getBackend()));

Audio Backends

BackendClassNotes
VioVioAudioBackendPrimary (via php-vio extension)
GLFWGLFWAudioBackendFallback via php-glfw
NullNullAudioBackendHeadless/testing

Released under the MIT License.