Tutorial 4 — Sons e música
$this->sound() (o SoundManager) toca os efeitos e a música, tanto no desktop quanto no navegador.
Sem placa de som, tudo fica em silêncio, nunca um erro.
Sons fabricados em PHP
O Synth cria sons retrô sem nenhum arquivo:
use JumPHP\Audio\Synth;
if (!$this->sound()->hasSfx('jump')) { // uma única vez
$this->sound()->synth('jump', Synth::sweep(250, 600, 0.12)); // uma frequência que sobe
$this->sound()->synth('coin', Synth::sequence([[988, 0.05], [1319, 0.12]])); // duas notas
$this->sound()->synth('hit', Synth::noise(0.15)); // um ruído
$this->sound()->synth('bass', Synth::tone(110, 0.3, 'triangle')); // square, triangle, sine, saw
}
$this->sound()->playSfx('jump');
Arquivos
$this->sound()->loadSfx('explosion', 'assets/sfx/explosion.wav'); // wav, ogg, mp3
$this->sound()->loadMusic('level', 'assets/music/level.ogg');
$this->sound()->playMusic('level');
$this->sound()->stopMusic();
O volume
setSfxVolume(0.8), setMusicVolume(0.5), setMuted(true): esses ajustes são gravados junto com as
opções do jogador. Um menu de opções os transforma em linhas ['VOLUME', fn () => ..., null, fn (int $dir) => ...]
(veja os menus).
O som em 3D (posicionado no espaço) também existe: $this->engine->audio3d().