TR
apps/winzik / src / Model / Equalizer.php · commit 6576d6f
<?phpnamespace App\Model;/** * The equalizer of WinZik: ON / AUTO, a preamp and 10 bands (60 Hz to 16 kHz), -12..+12 dB, and its presets. * AUTO keeps the bands of each song (they come back with it). * * What it does to the sound: the preamp is a real gain on the volume. The 10 bands are kept and drawn, but JumPHP's * SDL_mixer binding has no effect callback (Mix_RegisterEffect) to filter the music: they will act once the engine * can apply filters to its music stream. */final class Equalizer{    public const BANDS = ['60', '170', '310', '600', '1K', '3K', '6K', '12K', '14K', '16K'];    public const MIN = -12.0;    public const MAX = 12.0;    /** name => 10 bands (dB) */    public const PRESETS = [        'Plat' => [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],        'Classique' => [0, 0, 0, 0, 0, 0, -4, -4, -4, -6],        'Club' => [0, 0, 5, 3, 3, 3, 2, 0, 0, 0],        'Dance' => [6, 4, 1, 0, 0, -3, -4, -4, 0, 0],        'Graves' => [6, 6, 6, 3, 1, -2, -5, -6, -7, -7],        'Graves et aigus' => [4, 3, 0, -4, -3, 1, 5, 7, 7, 7],        'Aigus' => [-6, -6, -6, -2, 1, 7, 10, 10, 10, 11],        'Casque' => [3, 7, 3, -2, -1, 1, 3, 6, 8, 9],        'Grande salle' => [6, 6, 3, 3, 0, -3, -3, -3, 0, 0],        'Live' => [-3, 0, 2, 3, 3, 3, 2, 1, 1, 1],        'Fête' => [4, 4, 0, 0, 0, 0, 0, 0, 4, 4],        'Pop' => [-1, 3, 4, 4, 3, 0, -1, -1, -1, -1],        'Reggae' => [0, 0, 0, -3, 0, 4, 4, 0, 0, 0],        'Rock' => [5, 3, -3, -5, -2, 2, 5, 7, 7, 7],        'Ska' => [-1, -3, -2, 0, 2, 3, 5, 6, 7, 6],        'Doux' => [3, 1, 0, -1, 0, 2, 5, 6, 7, 8],        'Techno' => [5, 3, 0, -3, -3, 0, 5, 6, 6, 5],    ];    public bool $on = true;    public bool $auto = false;    public float $preamp = 0.0;    /** @var list<float> */    public array $bands;    public function __construct()    {        $this->bands = array_fill(0, 10, 0.0);    }    /** @param array<string, mixed> $data */    public static function fromArray(array $data): self    {        $eq = new self();        $eq->on = (bool)($data['on'] ?? true);        $eq->auto = (bool)($data['auto'] ?? false);        $eq->preamp = self::clamp((float)($data['preamp'] ?? 0));        foreach (array_slice(array_values((array)($data['bands'] ?? [])), 0, 10) as $i => $v) {            $eq->bands[$i] = self::clamp((float)$v);        }        return $eq;    }    /** @return array{on: bool, auto: bool, preamp: float, bands: list<float>} */    public function toArray(): array    {        return ['on' => $this->on, 'auto' => $this->auto, 'preamp' => $this->preamp, 'bands' => $this->bands];    }    public function preset(string $name): bool    {        if (!isset(self::PRESETS[$name])) {            return false;        }        $this->bands = array_map('floatval', self::PRESETS[$name]);        return true;    }    public static function clamp(float $db): float    {        return round(max(self::MIN, min(self::MAX, $db)), 1);    }    /** The gain of the preamp (what is really applied): 0 dB when off. */    public function preampDb(): float    {        return $this->on ? $this->preamp : 0.0;    }}