From dcc3d4561d3bf17100296bcb2051327f870ccff7 Mon Sep 17 00:00:00 2001 From: trav Date: Sat, 28 Nov 2026 13:14:14 -0800 Subject: [PATCH] =?UTF-8?q?v0.104.0:=20Kaleidostrobe's=20cones=20push=20on?= =?UTF-8?q?=20the=20hits=20you=20hear=20=E2=80=94=20kicks'=20punch,=20snar?= =?UTF-8?q?es,=20the=20music's=20body=20(100=E2=80=931600=20Hz)=20?= =?UTF-8?q?=E2=80=94=20instead=20of=20pumping=20on=20every=20sub-bass=20no?= =?UTF-8?q?te,=20and=20a=20held=20note=20no=20longer=20flickers=20them?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CLAUDE.md | 9 +++- lintunes/__init__.py | 2 +- lintunes/gui/visualizer_kaleido.py | 58 +++++++++++++++++++- tests/test_round136.py | 87 ++++++++++++++++++++++++++++++ tests/test_round84.py | 3 +- 5 files changed, 154 insertions(+), 5 deletions(-) create mode 100644 tests/test_round136.py diff --git a/CLAUDE.md b/CLAUDE.md index 629d9b2..decfa76 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -424,7 +424,14 @@ persistence) → GUI (Qt widgets that read the manager and connect to its signal were Apple SDK leftovers its code never read, so the style has none. It reads only iTunes' **spectrum** bytes (never the waveform); iTunes' byte scale was never published, so `itunes_spectrum` (√magnitude, 43 Hz bins) - is the one calibrated stand-in. **On screen it is 60 fps anyway** + is the one calibrated stand-in. **Except bins 0–3** (Round 136), the + beat bins that drive the cones' depth: four octave bands 100–1600 Hz + (`BEAT_BANDS`, a 2048-point FFT) with a fast attack and a 0.12 s + release (`_enveloped`) — trav's 2000s video of the original, measured + frame by frame, showed its depth following 120–500 Hz onsets and hardly + anything under 120 Hz; the literal 0–170 Hz pumped on every sub-bass + note. The goal trav set is "smooth and reacts to what people hear", not + a frame-exact copy of iTunes' unknown spectrum. **On screen it is 60 fps anyway** (Round 85): between the 25 Hz frames `_compose` turns the cones by a fraction of a frame and crossfades the textures — smoothing only. The cones' height/depth change **only on the plugin's frames**: Round 85 also diff --git a/lintunes/__init__.py b/lintunes/__init__.py index 38ec9a8..3e99cf1 100644 --- a/lintunes/__init__.py +++ b/lintunes/__init__.py @@ -1,3 +1,3 @@ """LinTunes — iTunes-style music library manager and player for Linux.""" -__version__ = "0.103.1" +__version__ = "0.104.0" diff --git a/lintunes/gui/visualizer_kaleido.py b/lintunes/gui/visualizer_kaleido.py index 96d5887..bc5912b 100644 --- a/lintunes/gui/visualizer_kaleido.py +++ b/lintunes/gui/visualizer_kaleido.py @@ -18,6 +18,19 @@ way its OpenGL did. - **Input.** iTunes handed plugins 512 spectrum bins per channel as bytes; how it scaled them was never documented, so ``itunes_spectrum`` is a calibrated stand-in (``SPECTRUM_GAIN``). +- **The beat bins** (Round 136). The plugin's depth — the cones' push + towards you — is bins 0–3, each against its own recent peak. Taken + literally (0–170 Hz) they made the cones pump on sub-bass: every bass + note and 808 swell, which the ear hears as a steady floor, not as hits. + trav's 2000s video of the original, measured frame by frame, shows its + depth followed onsets at 120–500 Hz (and 2–4 kHz) and hardly any below + 120 Hz — so iTunes' bins were never what a 1024-point FFT gives. Here + they are four octave bands, ``BEAT_BANDS`` (100–1600 Hz, where kicks' + punch, snares and the music's body sit), each with a fast attack and a + ``BEAT_RELEASE_S`` release (the ear integrates loudness over ~100 ms): + a hit lands at once, a held note can't flicker. On that song the depth + then tracks heard onsets 0.64 against 0.43, sub-bass 0.22 against 0.51, + with the same frequency profile as the original's, only stronger. Drawing: ``paint`` is QPainter — each GL triangle is planar, so its texture → screen mapping is a homography, drawn in texture space through a @@ -44,9 +57,13 @@ STEP_S = ks.FRAME_US / 1e6 # one plugin frame MAX_CATCH_UP = 3 # frames run at once after a stall, at most FFT_AT_44K = 1024 # iTunes' 512 bins are a 1024-point FFT's half SPECTRUM_GAIN = 320.0 # byte = √magnitude × this (full-scale sine = 1) +BEAT_FFT_AT_44K = 2048 # the beat bands' FFT (the 100 Hz octave needs it) +BEAT_BANDS = ((100, 200), (200, 400), (400, 800), (800, 1600)) # Hz → bins 0–3 +BEAT_RELEASE_S = 0.12 # the beat bins fall back with this time constant _PROJ_F = 1.0 / math.tan(math.radians(ks.FOVY) / 2) _WINDOWS = {} +_BEAT_MASKS = {} # GL enums the replay needs GL_TEXTURE_2D, GL_TRIANGLES = 0x0DE1, 0x0004 GL_COLOR_BUFFER_BIT, GL_DEPTH_BUFFER_BIT = 0x4000, 0x0100 @@ -81,10 +98,31 @@ def itunes_spectrum(samples: np.ndarray, rate: float) -> np.ndarray: mag = np.abs(np.fft.rfft(seg * window))[:512] / (n / 4) if len(mag) < 512: mag = np.concatenate([mag, np.zeros(512 - len(mag))]) - row = np.clip(np.sqrt(mag) * SPECTRUM_GAIN, 0, 255).astype(np.uint8) + row = np.sqrt(mag) * SPECTRUM_GAIN + row[:4] = beat_bands(samples, rate) + row = np.clip(row, 0, 255).astype(np.uint8) return np.stack([row, row]) +def beat_bands(samples: np.ndarray, rate: float) -> np.ndarray: + """Bins 0–3 (see the module docstring): each ``BEAT_BANDS`` octave's + magnitude (√ of its summed power) on the same byte scale.""" + n = max(64, int(round(BEAT_FFT_AT_44K * rate / 44100.0))) + masks = _BEAT_MASKS.get(n) + if masks is None: + freqs = np.fft.rfftfreq(n, 1.0 / rate) + masks = _BEAT_MASKS[n] = [(freqs >= lo) & (freqs < hi) for lo, hi in BEAT_BANDS] + window = _WINDOWS.get(n) + if window is None: + window = _WINDOWS[n] = np.hanning(n).astype(np.float32) + seg = samples[-n:] + if len(seg) < n: + seg = np.concatenate([np.zeros(n - len(seg), np.float32), seg]) + power = np.abs(np.fft.rfft(seg * window)) ** 2 / (n / 4) ** 2 + mag = np.sqrt(np.array([power[m].sum() for m in masks])) + return np.sqrt(mag) * SPECTRUM_GAIN + + def _rotz(deg): r = np.radians(np.asarray(deg, float)) c, s = np.cos(r), np.sin(r) @@ -133,6 +171,7 @@ class KaleidostrobeRenderer: self._images = {} self._ink = QColor(0, 0, 0) self._gl_uploaded = None + self._beat = None # bins 0–3 after attack/release, as floats self.version = 0 # bumped whenever _shown changes # ---- the renderer contract ---- @@ -145,8 +184,22 @@ class KaleidostrobeRenderer: did across iTunes' pause.""" self._frame = None self._shown = None + self._beat = None self.version += 1 + def _enveloped(self, spectrum, dt): + """Bins 0–3 rise at once and fall back over ``BEAT_RELEASE_S``.""" + raw = spectrum[:, :4].astype(np.float32) + if self._beat is None or self._beat.shape != raw.shape: + self._beat = raw + else: + keep = math.exp(-max(0.0, dt) / BEAT_RELEASE_S) + fall = self._beat * keep + raw * (1.0 - keep) + self._beat = np.where(raw > self._beat, raw, fall) + spectrum = spectrum.copy() + spectrum[:, :4] = (self._beat + 0.5).astype(np.uint8) + return spectrum + def advance(self, frame) -> bool: if self._engine is None: # srand(TickCount()) at 'init': any clock reading will do @@ -154,7 +207,8 @@ class KaleidostrobeRenderer: self._engine = ks.Kaleidostrobe(seed, self._ticks()) self._behind = min(self._behind + max(0.0, frame.dt), STEP_S * MAX_CATCH_UP) - spectrum = itunes_spectrum(frame.samples, frame.rate) + spectrum = self._enveloped(itunes_spectrum(frame.samples, frame.rate), + frame.dt) while self._behind >= STEP_S: self._behind -= STEP_S self._clock += STEP_S diff --git a/tests/test_round136.py b/tests/test_round136.py new file mode 100644 index 0000000..1822827 --- /dev/null +++ b/tests/test_round136.py @@ -0,0 +1,87 @@ +"""Round 136: Kaleidostrobe's beat bins hear what people hear. + +The plugin's cone depth comes from spectrum bins 0–3. They are now four +octave bands, 100–1600 Hz, with a fast attack and a slow release, instead +of the 0–170 Hz a literal 1024-point FFT gives — which pumped the cones on +every sub-bass note. These pin the shape: which frequencies reach those +bins, and how the envelope moves. +""" +import math + +import numpy as np +import pytest + +from lintunes.gui import visualizer_kaleido as vk + +RATE = 44100 + + +def _tone(hz, seconds=0.2, amp=0.5, rate=RATE): + t = np.arange(int(seconds * rate)) / rate + return (amp * np.sin(2 * np.pi * hz * t)).astype(np.float32) + + +@pytest.mark.parametrize("hz, band", [(150, 0), (300, 1), (600, 2), (1200, 3)]) +def test_each_octave_lands_in_its_own_beat_bin(hz, band): + bins = vk.itunes_spectrum(_tone(hz), RATE)[0, :4] + assert int(bins.argmax()) == band + assert bins[band] > 100 + + +def test_sub_bass_barely_reaches_the_beat_bins(): + sub = vk.itunes_spectrum(_tone(45), RATE)[0, :4] + body = vk.itunes_spectrum(_tone(300), RATE)[0, :4] + assert sub.max() < body.max() / 3 + + +@pytest.mark.parametrize("rate", [44100, 48000, 96000]) +def test_the_bands_follow_hertz_at_any_rate(rate): + bins = vk.itunes_spectrum(_tone(600, rate=rate), rate)[0, :4] + assert int(bins.argmax()) == 2 + + +def test_the_rest_of_the_spectrum_is_unchanged(): + tone = _tone(1000) + n = vk.FFT_AT_44K + mag = np.abs(np.fft.rfft(tone[-n:] * np.hanning(n)))[:512] / (n / 4) + expect = np.clip(np.sqrt(mag) * vk.SPECTRUM_GAIN, 0, 255).astype(np.uint8) + assert (vk.itunes_spectrum(tone, RATE)[0, 4:] == expect[4:]).all() + + +def _spec(level): + s = np.zeros((2, 512), np.uint8) + s[:, :4] = level + s[:, 4:] = 77 + return s + + +def test_a_hit_lands_at_once(): + r = vk.KaleidostrobeRenderer() + r._enveloped(_spec(10), 0.016) + assert (r._enveloped(_spec(220), 0.016)[:, :4] == 220).all() + + +def test_it_falls_back_with_the_release(): + r = vk.KaleidostrobeRenderer() + r._enveloped(_spec(200), 0.016) + out = r._enveloped(_spec(0), vk.BEAT_RELEASE_S) + assert abs(int(out[0, 0]) - round(200 / math.e)) <= 1 + assert (out[:, 4:] == 77).all() # only the beat bins are held + + +def test_the_release_follows_time_not_calls(): + a, b = vk.KaleidostrobeRenderer(), vk.KaleidostrobeRenderer() + a._enveloped(_spec(200), 0.0) + b._enveloped(_spec(200), 0.0) + for _ in range(6): + a._enveloped(_spec(0), 1 / 60) + for _ in range(12): + b._enveloped(_spec(0), 1 / 120) + assert abs(int(a._beat[0, 0]) - int(b._beat[0, 0])) <= 1 + + +def test_pause_forgets_the_envelope(): + r = vk.KaleidostrobeRenderer() + r._enveloped(_spec(250), 0.016) + r.clear() + assert (r._enveloped(_spec(10), 0.016)[:, :4] == 10).all() diff --git a/tests/test_round84.py b/tests/test_round84.py index 9c0526a..76d71a0 100644 --- a/tests/test_round84.py +++ b/tests/test_round84.py @@ -269,7 +269,8 @@ class TestRenderer: tone = (0.5 * np.sin(2 * np.pi * 1000 * t)).astype(np.float32) spec = itunes_spectrum(tone, rate) assert spec.shape == (2, 512) and spec.dtype == np.uint8 - assert abs(int(spec[0].argmax()) - round(1000 / (44100 / 1024))) <= 1 + # bins 0–3 are the beat bands since Round 136; the rest are 43 Hz + assert abs(4 + int(spec[0, 4:].argmax()) - round(1000 / (44100 / 1024))) <= 1 assert (spec[0] == spec[1]).all() def test_silence_is_zero(self):