name => loaded (false: failed, never tried again) */ private array $flat = []; /** @var array */ private array $buffers = []; /** @var array */ private array $musics = []; private ?string $music = null; private ?string $pending = null; private float $fading = 0.0; public function __construct(Engine $engine) { $this->sound = $engine->sound; $this->a3 = $engine->audio3d(); $this->spatial = $this->a3->isAvailable(); if ($this->spatial) { // full volume up to 5 m, a third at 15 m, a sixth at 35 m: far howls stay audible $this->a3->setDistance(5.0, 0.8, 140.0); } } public function play(string $name): void { if (!$this->sound->isEnabled()) { return; } if (!isset($this->flat[$name])) { try { $this->sound->loadSfx('rv_' . $name, self::SFX . $name . '.wav'); $this->flat[$name] = true; } catch (\Throwable) { $this->flat[$name] = false; } } if ($this->flat[$name]) { $this->sound->playSfx('rv_' . $name, 0, false); } } public function playAt(string $name, array $pos, float $gain = 1.0, float $pitch = 1.0, bool $loop = false): int { if (!$this->spatial) { return 0; } if (!isset($this->buffers[$name])) { $this->buffers[$name] = $this->a3->load('rv_' . $name, self::SFX . $name . '.wav'); } return $this->buffers[$name] ? $this->a3->play('rv_' . $name, $pos, $gain, $pitch, $loop) : 0; } public function update(int $voice, ?array $pos, float $gain): void { if ($voice <= 0) { return; } if ($pos !== null) { $this->a3->move($voice, $pos); } $this->a3->setGain($voice, $gain); } public function stop(int $voice): void { if ($voice > 0) { $this->a3->stop($voice); } } public function playing(int $voice): bool { return $voice > 0 && $this->a3->isPlaying($voice); } public function listener(array $pos, array $forward): void { $this->a3->listener($pos, $forward); } public function music(?string $name): void { if ($name === $this->music && $this->pending === null) { return; } if ($name === $this->pending) { return; } $this->pending = $name; $this->fading = $this->music === null ? 0.0 : self::FADE; if ($this->music !== null && $this->sound->isEnabled() && function_exists('Mix_FadeOutMusic')) { \Mix_FadeOutMusic((int) (self::FADE * 1000)); } $this->music = null; } public function tick(float $dt): void { if (!is_finite($dt)) { $dt = 0.0; } if ($this->pending === null && $this->fading <= 0) { return; } $this->fading -= $dt; if ($this->fading > 0) { return; } $name = $this->pending; $this->pending = null; if ($name === null) { $this->sound->stopMusic(); return; } if (!isset($this->musics[$name])) { try { $this->sound->loadMusic('rv_' . $name, self::MUSIC . $name . '.ogg'); $this->musics[$name] = true; } catch (\Throwable) { $this->musics[$name] = false; } } if ($this->musics[$name]) { // halt the old loop first, whatever is left of its fade: SDL_mixer's Mix_PlayMusic WAITS (SDL_Delay loop) // while a music is fading out, and in the browser the audio callback that ends the fade runs on the same // (blocked) thread: the page froze for good (night falls at 20:30, the night music came in at ~20:35) $this->sound->stopMusic(); $this->sound->playMusic('rv_' . $name); $this->music = $name; } } public function stopAll(): void { if ($this->spatial) { $this->a3->stopAll(); } if ($this->sound->isEnabled() && function_exists('Mix_HaltChannel')) { \Mix_HaltChannel(-1); // the flat effects too (footsteps, breath...): nothing left to loop } if ($this->music !== null || $this->pending !== null) { $this->sound->stopMusic(); } [$this->music, $this->pending, $this->fading] = [null, null, 0.0]; } }