튜토리얼 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()를 사용해요.