IT

Tutorial 4 — Suoni e musica

$this->sound() (il SoundManager) riproduce effetti e musica, sul desktop come nel browser. Senza scheda audio, tutto resta silenzioso, mai un errore.

Suoni fabbricati in PHP

Synth crea suoni retro senza alcun file:

use JumPHP\Audio\Synth;

if (!$this->sound()->hasSfx('jump')) {                        // una sola volta
    $this->sound()->synth('jump', Synth::sweep(250, 600, 0.12));               // una frequenza che sale
    $this->sound()->synth('coin', Synth::sequence([[988, 0.05], [1319, 0.12]])); // due note
    $this->sound()->synth('hit', Synth::noise(0.15));                          // un rumore
    $this->sound()->synth('bass', Synth::tone(110, 0.3, 'triangle'));          // square, triangle, sine, saw
}
$this->sound()->playSfx('jump');

File

$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();

Il volume

setSfxVolume(0.8), setMusicVolume(0.5), setMuted(true): queste impostazioni vengono salvate con le opzioni del giocatore. Un menu delle opzioni ne fa delle righe ['VOLUME', fn () => ..., null, fn (int $dir) => ...] (vedi i menu).

Esiste anche il suono in 3D (posizionato nello spazio): $this->engine->audio3d().