NL

Tutorial 4 — Geluid en muziek

$this->sound() (de SoundManager) speelt effecten en muziek af, op de desktop en in de browser. Zonder geluidskaart is alles stil, nooit een fout.

Geluiden gemaakt in PHP

Synth maakt retrogeluiden zonder enig bestand:

use JumPHP\Audio\Synth;

if (!$this->sound()->hasSfx('jump')) {                        // maar één keer
    $this->sound()->synth('jump', Synth::sweep(250, 600, 0.12));               // een stijgende frequentie
    $this->sound()->synth('coin', Synth::sequence([[988, 0.05], [1319, 0.12]])); // twee noten
    $this->sound()->synth('hit', Synth::noise(0.15));                          // een ruis
    $this->sound()->synth('bass', Synth::tone(110, 0.3, 'triangle'));          // square, triangle, sine, saw
}
$this->sound()->playSfx('jump');

Bestanden

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

Het volume

setSfxVolume(0.8), setMusicVolume(0.5), setMuted(true): deze instellingen worden opgeslagen met de opties van de speler. Een optiemenu maakt er regels van als ['VOLUME', fn () => ..., null, fn (int $dir) => ...] (zie de menu's).

3D-geluid (gepositioneerd in de ruimte) bestaat ook: $this->engine->audio3d().