"""Round 122: Lint Oontz, the stop-motion lint-ball visualizer.""" import numpy as np from PyQt6.QtCore import QRectF from PyQt6.QtGui import QColor, QImage, QPainter from lintunes.gui import visualizer_styles as vs from lintunes.gui.visualizer_lint import FPS, LintRenderer RATE = 44100 def _song(bpm, seconds, full): rng = np.random.default_rng(3) t = np.arange(int(seconds * RATE)) / RATE x = np.sin(2 * np.pi * 55 * t) * np.exp(-(t % (60 / bpm)) * 18) * 0.8 if full: x = x + rng.normal(0, 0.12, t.size) + 0.2 * np.sin(2 * np.pi * 440 * t) else: x = x + 0.05 * np.sin(2 * np.pi * 330 * t) return x.astype(np.float32) def _play(r, audio): hist = np.zeros(vs.HISTORY, np.float32) n = RATE // 60 steps = 0 for i in range(0, len(audio) - n, n): chunk = audio[i:i + n] hist = np.concatenate([hist, chunk])[-vs.HISTORY:] steps += r.advance(vs.Frame(hist, RATE, chunk, 1 / 60)) return steps def test_registered(): assert vs.STYLE_LINT in vs.STYLES assert vs.STYLE_NAMES[vs.STYLE_LINT] == "Lint Oontz" assert isinstance(vs.make_renderer(vs.STYLE_LINT), LintRenderer) def test_stop_motion_frame_rate(): r = LintRenderer() steps = _play(r, _song(100, 3, False)) assert abs(steps - 3 * FPS) <= 2 # 8 drawings a second, not 60 def test_fast_full_music_dances(): r = LintRenderer() _play(r, _song(128, 6, True)) assert not r.asleep and r.busy assert 120 <= r.tempo <= 136 def test_slow_sparse_music_only_bobs(): r = LintRenderer() _play(r, _song(80, 6, False)) assert not r.asleep and not r.busy assert 74 <= r.tempo <= 86 def test_quiet_puts_them_to_sleep_and_music_wakes_them(): r = LintRenderer() _play(r, _song(128, 3, True)) assert not r.asleep _play(r, np.zeros(RATE * 2, np.float32)) assert not r.asleep # a short break isn't sleep _play(r, np.zeros(RATE * 3, np.float32)) assert r.asleep _play(r, _song(128, 1, True)) assert not r.asleep def test_cleared_is_asleep(): r = LintRenderer() _play(r, _song(128, 2, True)) r.clear() assert r.asleep def test_three_balls_gray_highlight_black(qapp): r = LintRenderer() r.panel = QColor(205, 205, 205) gray, highlight, black = r.colors(QColor(40, 110, 220)) assert highlight == QColor(40, 110, 220) assert black == QColor(0, 0, 0) assert gray.red() == gray.green() == gray.blue() < 205 for asleep in (False, True): r.asleep = asleep for w, h in ((110, 44), (900, 450)): img = QImage(w, h, QImage.Format.Format_ARGB32) img.fill(r.panel) p = QPainter(img) r.paint(p, QRectF(0, 0, w, h), highlight) p.end() blue = sum(img.pixelColor(x, y).blue() - img.pixelColor(x, y).red() > 80 for x in range(w) for y in range(0, h, 2)) assert blue > 0 def test_every_frame_wobbles(qapp): r = LintRenderer() r.asleep = False def draw(): img = QImage(200, 100, QImage.Format.Format_ARGB32) img.fill(QColor(205, 205, 205)) p = QPainter(img) r.paint(p, QRectF(0, 0, 200, 100), QColor(40, 110, 220)) p.end() return img first = draw() assert draw() == first # the same step draws the same r.step += 1 assert draw() != first # the next one is re-wobbled