<?phpnamespace App\Media;/** * The visualizer's bars (the spectrum of what is heard, as the players of 1999 drew it): a 1024-point FFT of the * sound at the player's position, cut into bands spaced like the ear hears them (60 Hz to 16 kHz), each bar rising at * once and falling slowly, its peak held a moment then dropping. */final class Spectrum{ public const BANDS = 19; private const N = 1024; /** @var list<float> 0..1 */ public array $bars; /** @var list<float> 0..1 */ public array $peaks; /** @var list<float> how long each peak has been held */ private array $held; /** @var list<float> */ private array $window = []; /** @var list<float> the sound itself, 64 points -1..1 (the oscilloscope) */ public array $wave = []; /** @var list<list<float>> the last bars, the newest last (the waterfall) */ public array $history = []; public const HISTORY = 48; /** 0..1: the light between the volume and the balance, 1 at a beat of the bass, then fading */ public float $beat = 0.0; /** the energy of the bass now (40-160 Hz), its running mean and variance (this song's own level) */ private float $bass = 0.0; private float $bassMean = 0.0; private float $bassVar = 0.0; private float $sinceBeat = 9.0; private int $bassFrames = 0; /** A new song: the level of its bass is learnt again (a ballad and a techno track do not beat alike). */ public function newSong(): void { $this->bassMean = $this->bassVar = 0.0; $this->bassFrames = 0; $this->beat = 0.0; } /** * A beat: the bass clearly above what it has been for the last second and a half (mean + 1.5 deviations, and 40 % * above the mean), not sooner than 0.28 s after the last one (210 a minute at most). */ private function detectBeat(float $dt, bool $sound): void { $this->beat = max(0.0, $this->beat - $dt * 5); $this->sinceBeat += $dt; if (!$sound) { return; } $e = $this->bass; $a = $this->bassFrames < 15 ? 0.2 : 0.03; // learns fast at the start of a song, then slowly (~1.5 s) $deviation = sqrt(max(0.0, $this->bassVar)); if ($this->bassFrames >= 15 && $this->sinceBeat > 0.28 && $e > $this->bassMean + 1.5 * $deviation && $e > $this->bassMean * 1.4 && $e > 1e-4) { $this->beat = 1.0; $this->sinceBeat = 0.0; } $d = $e - $this->bassMean; $this->bassMean += $a * $d; $this->bassVar = (1 - $a) * ($this->bassVar + $a * $d * $d); $this->bassFrames++; } public function __construct() { $this->bars = $this->peaks = $this->held = array_fill(0, self::BANDS, 0.0); for ($i = 0; $i < self::N; $i++) { $this->window[] = 0.5 - 0.5 * cos(2 * M_PI * $i / (self::N - 1)); // Hann } } /** Each frame: the sound of the file at that moment (null: silence, the bars fall). */ public function update(float $dt, ?Wav $wav, float $seconds, bool $playing): void { $levels = array_fill(0, self::BANDS, 0.0); $this->wave = array_fill(0, 64, 0.0); $sound = false; if ($wav !== null && $playing && $wav->rate > 0 && $wav->channels > 0) { // a broken header (a rate of 0): silence $samples = $wav->samples($seconds, self::N); if (count($samples) === self::N) { $levels = $this->levels($samples, $wav->rate); $sound = true; for ($i = 0; $i < 64; $i++) { $this->wave[$i] = (float)$samples[$i * 4]; // 256 samples (about 6 ms): a few periods of the sound } } } $this->detectBeat($dt, $sound); $this->history[] = $levels; if (count($this->history) > self::HISTORY) { array_shift($this->history); } foreach ($levels as $i => $level) { // up at once, down slowly $this->bars[$i] = $level >= $this->bars[$i] ? $level : max($level, $this->bars[$i] - $dt * 1.6); if ($this->bars[$i] >= $this->peaks[$i]) { $this->peaks[$i] = $this->bars[$i]; $this->held[$i] = 0.0; } else { $this->held[$i] += $dt; if ($this->held[$i] > 0.35) { $this->peaks[$i] = max($this->bars[$i], $this->peaks[$i] - $dt * 0.9); // the tops fall } } } } /** @param list<float> $samples @return list<float> */ private function levels(array $samples, int $rate): array { $re = []; $im = array_fill(0, self::N, 0.0); foreach ($samples as $i => $s) { $re[] = $s * $this->window[$i]; } self::fft($re, $im); // the bass (40-160 Hz), summed: the beat of the light $bass = 0.0; for ($k = max(1, (int)floor(40 * self::N / $rate)); $k <= (int)ceil(160 * self::N / $rate); $k++) { $bass += $re[$k] * $re[$k] + $im[$k] * $im[$k]; } $this->bass = $bass / self::N; $out = []; $lo = 60.0; $hi = min(16000.0, $rate / 2 - 1); for ($b = 0; $b < self::BANDS; $b++) { $f0 = $lo * ($hi / $lo) ** ($b / self::BANDS); $f1 = $lo * ($hi / $lo) ** (($b + 1) / self::BANDS); $k0 = max(1, (int)floor($f0 * self::N / $rate)); $k1 = max($k0, (int)ceil($f1 * self::N / $rate)); $power = 0.0; for ($k = $k0; $k <= $k1 && $k < self::N / 2; $k++) { $power = max($power, $re[$k] * $re[$k] + $im[$k] * $im[$k]); } $db = 10 * log10($power + 1e-12); // a full scale sine: about +48 dB here $out[] = max(0.0, min(1.0, ($db + 12) / 60 + $b * 0.006)); // the highs a little lifted, as on the screens of that time } return $out; } /** @var array<int, array{0: list<int>, 1: list<float>, 2: list<float>}> per size: its bit reversal, its cosines, its sines */ private static array $tables = []; /** * In place, radix 2, with its tables made once (the bit reversal, the cosines and sines of every step): the * visualizer's FFT was a third of WinZik's own time. * @param array<int, float> $re @param array<int, float> $im * @param-out array<int, float> $re @param-out array<int, float> $im */ private static function fft(array &$re, array &$im): void { $n = count($re); if (!isset(self::$tables[$n])) { $rev = []; $bits = (int)round(log($n, 2)); for ($i = 0; $i < $n; $i++) { $r = 0; for ($b = 0, $x = $i; $b < $bits; $b++, $x >>= 1) { $r = ($r << 1) | ($x & 1); } $rev[] = $r; } $cos = $sin = []; for ($k = 0; $k < $n / 2; $k++) { $cos[] = cos(-2 * M_PI * $k / $n); $sin[] = sin(-2 * M_PI * $k / $n); } self::$tables[$n] = [$rev, $cos, $sin]; } [$rev, $cos, $sin] = self::$tables[$n]; for ($i = 0; $i < $n; $i++) { $j = $rev[$i]; if ($i < $j) { [$re[$i], $re[$j]] = [$re[$j], $re[$i]]; [$im[$i], $im[$j]] = [$im[$j], $im[$i]]; } } for ($len = 2; $len <= $n; $len <<= 1) { $half = $len >> 1; $step = intdiv($n, $len); for ($i = 0; $i < $n; $i += $len) { for ($k = 0, $t = 0; $k < $half; $k++, $t += $step) { $a = $i + $k; $b = $a + $half; $cr = $cos[$t]; $ci = $sin[$t]; $tr = $re[$b] * $cr - $im[$b] * $ci; $ti = $re[$b] * $ci + $im[$b] * $cr; $re[$b] = $re[$a] - $tr; $im[$b] = $im[$a] - $ti; $re[$a] += $tr; $im[$a] += $ti; } } } }}