The moves come in three tiers picked by how full the music is now against the last ~12 s, so a build or a drop changes the move; some use the legs. It waits standing for 10 s before sitting and sleeps at 30 s, and skipping songs no longer sits it down.
165 lines
5.2 KiB
Python
165 lines
5.2 KiB
Python
"""Round 122: Lint Oontz, the stop-motion lint-ball visualizer: one ball in
|
|
headphones that dances, sits when nothing plays and falls asleep; its
|
|
colour modes are highlight, gray and black."""
|
|
import numpy as np
|
|
from PyQt6.QtCore import QRectF
|
|
from PyQt6.QtGui import QColor, QImage, QPainter
|
|
|
|
from lintunes.gui import visualizer_lint as vl
|
|
from lintunes.gui import visualizer_styles as vs
|
|
from lintunes.gui.visualizer import (
|
|
MODE_BLACK, MODE_DIM, MODE_OFF, MODE_ON)
|
|
from lintunes.gui.visualizer_lint import FPS, LintRenderer, SIT, SLEEP, STAND
|
|
|
|
from test_round86 import _tile
|
|
|
|
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 _silence(seconds):
|
|
return np.zeros(int(RATE * seconds), np.float32)
|
|
|
|
|
|
def _draw(r, w, h, color=QColor(40, 110, 220)):
|
|
img = QImage(w, h, QImage.Format.Format_ARGB32)
|
|
img.fill(QColor(205, 205, 205))
|
|
p = QPainter(img)
|
|
r.paint(p, QRectF(0, 0, w, h), color)
|
|
p.end()
|
|
return img
|
|
|
|
|
|
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 r.posture == STAND and r.busy
|
|
assert 120 <= r.tempo <= 136
|
|
|
|
|
|
def test_slow_sparse_music_only_bobs():
|
|
r = LintRenderer()
|
|
_play(r, _song(80, 6, False))
|
|
assert r.posture == STAND and not r.busy
|
|
assert 74 <= r.tempo <= 86
|
|
|
|
|
|
def test_it_starts_sitting_then_falls_asleep():
|
|
r = LintRenderer()
|
|
assert r.posture == SIT
|
|
_play(r, _silence(vl.SLEEP_AFTER_S - vl.SIT_AFTER_S - 1))
|
|
assert r.posture == SIT
|
|
_play(r, _silence(2))
|
|
assert r.posture == SLEEP
|
|
|
|
|
|
def test_quiet_sits_then_sleeps_and_music_wakes_it():
|
|
r = LintRenderer()
|
|
_play(r, _song(128, 3, True))
|
|
assert r.posture == STAND
|
|
_play(r, _silence(vl.SIT_AFTER_S + 0.5))
|
|
assert r.posture == SIT
|
|
_play(r, _silence(vl.SLEEP_AFTER_S))
|
|
assert r.posture == SLEEP
|
|
_play(r, _song(128, 1, True))
|
|
assert r.posture == STAND
|
|
|
|
|
|
def test_pausing_lets_the_quiet_clock_decide():
|
|
r = LintRenderer()
|
|
_play(r, _song(128, 2, True))
|
|
r.clear()
|
|
assert r.posture == STAND # Round 123: it waits before sitting
|
|
|
|
|
|
def test_one_ball_in_the_given_colour(qapp):
|
|
r = LintRenderer()
|
|
for posture in (STAND, SIT, SLEEP):
|
|
r.posture = posture
|
|
for w, h in ((134, 38), (900, 450)):
|
|
img = _draw(r, w, h)
|
|
cols = [x for x in range(w)
|
|
if any(img.pixelColor(x, y).blue() - img.pixelColor(x, y).red() > 80
|
|
for y in range(0, h, 2))]
|
|
assert cols
|
|
# One figure, in the middle: nothing in the outer sixths.
|
|
assert min(cols) > w / 6 and max(cols) < w * 5 / 6
|
|
|
|
|
|
def test_every_frame_wobbles(qapp):
|
|
r = LintRenderer()
|
|
r.posture = STAND
|
|
first = _draw(r, 200, 100)
|
|
assert _draw(r, 200, 100) == first # the same step draws the same
|
|
r.step += 1
|
|
assert _draw(r, 200, 100) != first # the next one is re-wobbled
|
|
|
|
|
|
def test_the_wheel_adds_black_for_lint_only(qapp, tmp_path):
|
|
prefs, vis = _tile(tmp_path, "lint")
|
|
seen = []
|
|
for _ in range(4):
|
|
vis.cycle_mode()
|
|
seen.append(vis._mode)
|
|
assert seen == [MODE_DIM, MODE_BLACK, MODE_OFF, MODE_ON]
|
|
vis.cycle_mode()
|
|
vis.cycle_mode()
|
|
assert vis._mode == MODE_BLACK
|
|
assert vis.paint_color() == QColor(0, 0, 0)
|
|
prefs.set("visualizer_style", "bars") # black means nothing there
|
|
assert vis.paint_color() == vis.palette().highlight().color()
|
|
vis.cycle_mode()
|
|
assert vis._mode == MODE_OFF
|
|
prefs2, bars = _tile(tmp_path / "b", "bars")
|
|
seen = []
|
|
for _ in range(3):
|
|
bars.cycle_mode()
|
|
seen.append(bars._mode)
|
|
assert seen == [MODE_DIM, MODE_OFF, MODE_ON]
|
|
|
|
|
|
def test_it_keeps_its_clock_with_nothing_playing(qapp, tmp_path):
|
|
_, vis = _tile(tmp_path, "lint")
|
|
assert vis._timer.isActive() # sits, and will fall asleep
|
|
_, bars = _tile(tmp_path / "b", "bars")
|
|
assert not bars._timer.isActive()
|
|
vis._renderer.posture = STAND
|
|
for _ in range(int(vl.SIT_AFTER_S * 60) + 30):
|
|
vis._advance_frame() # fed silence, not the old song
|
|
assert vis._renderer.posture == SIT
|
|
vis.cycle_mode(); vis.cycle_mode(); vis.cycle_mode()
|
|
assert vis._mode == MODE_OFF and not vis._timer.isActive()
|