Files
lintunes/tests/test_round97.py
T
trav 0f535a4ca3 v0.67.0: the air horn done properly; djay-style loops; a gentler grab zoom
Air horn: the recording's first blast wobbles on a ~0.15 s cycle, so the
old 0.30 s loop jumped every lap (the repeating beat). Held, it now loops
exactly one cycle (found by self-similarity), levelled and crossfaded;
a release crossfades into the recording's own ending, so a click is a
short blast that trails off; each press cuts a still-sounding blast in
25 ms, so taps stay separate. Exact at any block size.

Loops start on the nearest beat (djay's Quantize), and resizing a running
loop keeps its start and keeps looping. Grabbing a waveform zooms out
only a little, gently, and drags at the zoom it was grabbed at. The sync
button just says Sync.
2026-10-17 22:44:45 -07:00

121 lines
4.8 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""Round 97: the air horn done properly (a click trails off; a hold
sustains one natural cycle, levelled, never a repeating beat; taps stay
separate), djay-style loops (the nearest beat; resizing keeps looping),
a gentler zoom-out when grabbing a waveform."""
import numpy as np
import pytest
from lintunes.dj.audio import RATE, Horn
from lintunes.dj.loader import decode
from tests.test_round93 import _clicks, _loaded, shelf # noqa: F401 — the fixture
@pytest.fixture(scope="module")
def recording():
from importlib import resources
with resources.as_file(resources.files("lintunes") / "sounds" / "airhorn.mp3") as path:
return decode(str(path))
def _play(sample, script):
horn = Horn(sample)
out = []
for action, seconds in script:
if action == "press":
horn.press()
elif action == "release":
horn.release()
n = int(seconds * RATE)
while n > 0:
block = horn.render(min(512, n))
out.append(block if block is not None else np.zeros((min(512, n), 2), np.float32))
n -= 512
while True:
block = horn.render(512)
if block is None:
break
out.append(block)
return np.concatenate(out).mean(axis=1), horn
def _envelope(mono, seconds=0.015):
win = int(seconds * RATE)
return np.sqrt(np.convolve(mono * mono, np.ones(win) / win, mode="same"))
class TestHorn:
def test_a_click_is_a_short_blast_that_trails_off(self, recording):
x, _ = _play(recording, [("press", 0.01), ("release", 0.0)])
assert 0.3 < len(x) / RATE < 0.45
env = _envelope(x)
assert env[-int(0.02 * RATE):].max() < 0.05 * env.max() # fades to nothing
# …gradually: the last 120 ms fall, they don't drop off a cliff
tail = env[-int(0.12 * RATE):]
assert tail[0] > 4 * tail[-1]
def test_a_hold_sustains_without_clicks_or_a_beat(self, recording):
x, _ = _play(recording, [("press", 3.0), ("release", 0.0)])
source = recording.astype(np.float32).mean(axis=1) / 32768
own_step = np.abs(np.diff(source[:int(0.6 * RATE)])).max()
assert np.abs(np.diff(x)).max() <= own_step + 1e-3 # no seam clicks
held = _envelope(x)[int(0.6 * RATE):int(2.6 * RATE)]
assert held.min() / held.max() > 0.7 # steady, no pulse
assert len(x) / RATE < 3.0 + 0.45 # stops on release
def test_taps_stay_separate_then_a_hold_carries(self, recording):
x, horn = _play(recording, [("press", 0.06), ("release", 0.12)] * 6
+ [("press", 2.5), ("release", 0.0)])
held = _envelope(x)[int(1.6 * RATE):int(3.2 * RATE)]
assert held.min() / held.max() > 0.7
assert not horn._voices
def test_a_retrigger_cuts_the_old_blast_quickly(self, recording):
horn = Horn(recording)
horn.press()
horn.render(4410)
horn.press()
states = [v["state"] for v in horn._voices]
assert states == ["cut", "held"]
horn.render(int(0.03 * RATE))
assert [v["state"] for v in horn._voices] == ["held"]
class TestLoops:
def _deck(self, shelf):
a, _ = shelf.views
_loaded(shelf, a, shelf.tracks[0], _clicks(120, 0.0, 20), 120.0, 0.0)
return a
def test_the_nearest_beat_may_be_behind(self, shelf):
a = self._deck(shelf)
a.deck.position = RATE * 2.1 # beats every 0.5 s: 2.0 is nearest
shelf.set_loop(a, True)
assert a.deck.loop[0] / RATE == pytest.approx(2.0, abs=1e-3)
a.deck.position = RATE * 2.4 # 2.5 is nearest
shelf.set_loop(a, True)
assert a.deck.loop[0] / RATE == pytest.approx(2.5, abs=1e-3)
def test_a_short_loop_snaps_to_its_own_grid(self, shelf):
a = self._deck(shelf)
shelf.step_loop(a, -4) # 4 → 1/4 beat
a.deck.position = RATE * 2.06 # quarter beats every 0.125 s
shelf.set_loop(a, True)
assert a.deck.loop[0] / RATE == pytest.approx(2.0, abs=1e-3)
def test_shrinking_a_running_loop_keeps_looping(self, shelf):
a = self._deck(shelf)
a.deck.position = RATE * 2.0
shelf.set_loop(a, True) # 4 beats: 2.0 – 4.0
a.deck.playing = True
a.deck.seek(RATE * 3.6) # late in the loop
shelf.step_loop(a, -2) # → 1 beat: 2.0 – 2.5
start, end = a.deck.loop
assert (start, end) == (RATE * 2, RATE * 2 + RATE // 2)
assert start <= a.deck.position < end # folded back inside
for _ in range(60):
a.deck.render(1024)
assert start <= a.deck.position < end + 1024
shelf.step_loop(a, 2) # back to 4 beats: same start
assert a.deck.loop == (RATE * 2, RATE * 4)