"""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")) # The horn's tests moved to test_round98 with its new recording. 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)