TR

Eğitim 4 — Ses ve müzik

$this->sound() (SoundManager), efektleri ve müziği masaüstünde de tarayıcıda da çalar. Ses kartı yoksa her şey sessizdir, asla bir hata olmaz.

PHP ile üretilen sesler

Synth, hiçbir dosya olmadan retro sesler oluşturur:

use JumPHP\Audio\Synth;

if (!$this->sound()->hasSfx('jump')) {                        // yalnızca bir kez
    $this->sound()->synth('jump', Synth::sweep(250, 600, 0.12));               // yükselen bir frekans
    $this->sound()->synth('coin', Synth::sequence([[988, 0.05], [1319, 0.12]])); // iki nota
    $this->sound()->synth('hit', Synth::noise(0.15));                          // bir gürültü
    $this->sound()->synth('bass', Synth::tone(110, 0.3, 'triangle'));          // square, triangle, sine, saw
}
$this->sound()->playSfx('jump');

Dosyalar

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

Ses seviyesi

setSfxVolume(0.8), setMusicVolume(0.5), setMuted(true): bu ayarlar oyuncunun seçenekleriyle birlikte kaydedilir. Bir ayarlar menüsü bunları ['VOLUME', fn () => ..., null, fn (int $dir) => ...] satırlarına dönüştürür (menülere bakın).

3D ses (uzayda konumlandırılmış) de vardır: $this->engine->audio3d().