UK

Посібник 4 — Звуки та музика

$this->sound() (SoundManager) відтворює ефекти й музику — на комп'ютері так само, як у браузері. Без звукової карти все мовчить, але помилки ніколи не буде.

Звуки, створені на PHP

Synth створює ретро-звуки без жодного файлу:

use JumPHP\Audio\Synth;

if (!$this->sound()->hasSfx('jump')) {                        // лише один раз
    $this->sound()->synth('jump', Synth::sweep(250, 600, 0.12));               // частота, що зростає
    $this->sound()->synth('coin', Synth::sequence([[988, 0.05], [1319, 0.12]])); // дві ноти
    $this->sound()->synth('hit', Synth::noise(0.15));                          // шум
    $this->sound()->synth('bass', Synth::tone(110, 0.3, 'triangle'));          // square, triangle, sine, saw
}
$this->sound()->playSfx('jump');

Файли

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

Гучність

setSfxVolume(0.8), setMusicVolume(0.5), setMuted(true): ці налаштування зберігаються разом з опціями гравця. Меню налаштувань робить із них рядки ['VOLUME', fn () => ..., null, fn (int $dir) => ...] (див. меню).

Є також 3D-звук (позиційований у просторі): $this->engine->audio3d().