Files
lintunes/tests/test_round96.py
T
trav 279f51fba9 v0.66.0: DJ mode's djay layout, loops, a sync that sets once, a real horn, a smooth slide
- Records in the top corners, each deck's controls beside them facing the
  middle, the zoomed A-over-B waveforms in the centre, and a whole-song
  overview per deck below (click to jump, drag to move) with the
  crossfader between them.
- Loops per deck: Loop plus a 1/32 to 8 beat stepper; a loop starts on
  the next beat and shows on both waveforms.
- Sync sets this deck to the other's tempo once, and stays lit only while
  that still holds.
- The air horn plays like a real one: every press a fresh blast, held it
  carries the note, let go it stops.
- Opening and closing DJ mode slides a picture over the final layout
  instead of re-laying-out the window every frame (the flicker).
- Tests: garbage is collected on the main thread after each test, which
  stops a rare segfault in the full run.
2026-10-17 22:19:59 -07:00

157 lines
5.9 KiB
Python

"""Round 96: the DJ shelf's new layout (records in the corners, controls
facing the middle, whole-song overviews below the zoomed pair), loops, a
sync that sets once, the air horn as a real one plays, and the open/close
slide."""
import numpy as np
import pytest
from lintunes.dj.audio import RATE, Deck, Horn, Source
from tests.test_round93 import _clicks, _loaded, shelf # noqa: F401 — the fixture
def _pair(shelf):
a, b = shelf.views
_loaded(shelf, a, shelf.tracks[0], _clicks(124, 0.15, 20), 124.0, 0.15)
_loaded(shelf, b, shelf.tracks[1], _clicks(118, 0.05, 20), 118.0, 0.05)
return a, b
class TestSyncSetsOnce:
def test_the_other_deck_moving_leaves_this_tempo(self, shelf):
a, b = _pair(shelf)
shelf.sync(b)
speed = b.deck.speed
shelf.speed["A"].setValue(200) # A +2 %
shelf._tick()
assert b.deck.speed == pytest.approx(speed) # B stays where it was set
assert not b.synced # …and is no longer lit
def test_a_new_song_on_the_other_deck_unlights_it(self, shelf):
a, b = _pair(shelf)
shelf.sync(b)
_loaded(shelf, a, shelf.tracks[1], _clicks(118, 0.05, 20), 118.0, 0.05)
shelf._tick()
assert not b.synced and b.deck.speed == pytest.approx(124 / 118)
class TestLoops:
def test_engine_wraps_at_the_loop_end(self):
x = np.arange(RATE * 10)
tone = np.stack([(np.sin(x / RATE * 2 * np.pi * 200) * 10000).astype(np.int16)] * 2, 1)
deck = Deck("A")
deck.load(Source(tone))
deck.playing = True
deck.position = RATE * 1.0
deck.loop = (RATE, RATE + 11025)
for _ in range(100):
deck.render(1024)
assert RATE <= deck.position < RATE + 11025 + 1024
def test_starts_on_the_next_beat_and_shows_its_size(self, shelf):
a, _ = _pair(shelf)
a.deck.position = RATE * 3.0
shelf.set_loop(a, True)
start, end = a.deck.loop
beat = 60 / 124
k = (start / RATE - 0.15) / beat
assert k == pytest.approx(round(k), abs=1e-3) # on a beat
assert start / RATE >= 3.0 - 1e-3 # the next one
assert (end - start) / RATE == pytest.approx(4 * beat, abs=1e-3)
shelf.step_loop(a, -2) # 4 → 1 beat
assert shelf.loop_sizes["A"].text() == "1"
assert (a.deck.loop[1] - a.deck.loop[0]) / RATE == pytest.approx(beat, abs=1e-3)
shelf.step_loop(a, -5) # → 1/32
assert shelf.loop_sizes["A"].text() == "1/32"
shelf.set_loop(a, False)
assert a.deck.loop is None
def test_loading_a_song_drops_the_loop(self, shelf):
a, _ = _pair(shelf)
shelf.set_loop(a, True)
_loaded(shelf, a, shelf.tracks[1], _clicks(118, 0.05, 20), 118.0, 0.05)
assert a.deck.loop is None and not shelf.loop_buttons["A"].isChecked()
def test_the_overview_jumps(shelf):
from PyQt6.QtCore import QEvent, QPointF, Qt
from PyQt6.QtGui import QMouseEvent
a, _ = _pair(shelf)
ov = shelf.overviews["A"]
ov.resize(400, 64)
event = QMouseEvent(QEvent.Type.MouseButtonPress, QPointF(200, 30), QPointF(200, 30),
Qt.MouseButton.LeftButton, Qt.MouseButton.LeftButton,
Qt.KeyboardModifier.NoModifier)
ov.mousePressEvent(event)
assert a.deck.position / RATE == pytest.approx(a.deck.source.seconds / 2, abs=0.1)
ov.grab() # paints without trouble
class TestHorn:
def _horn(self):
x = (np.sin(np.arange(RATE * 2) / RATE * 2 * np.pi * 400) * 20000).astype(np.int16)
return Horn(np.stack([x, x], axis=1))
def _drain(self, horn):
total = 0
while True:
block = horn.render(1024)
if block is None:
return total / RATE
total += len(block)
def test_a_tap_is_short(self):
horn = self._horn()
horn.press()
horn.render(2205)
horn.release()
assert self._drain(horn) < 0.15
def test_holding_carries_the_note_even_after_taps(self):
horn = self._horn()
for _ in range(6):
horn.press()
horn.render(1024)
horn.release()
horn.render(1024)
horn.press()
for _ in range(int(3 * RATE / 1024)):
block = horn.render(1024)
assert np.abs(block).max() > 0.1 # still blaring at 3 s
horn.release()
assert self._drain(horn) < 0.15
def test_a_press_while_ringing_starts_a_new_blast(self):
horn = self._horn()
horn.press()
horn.render(4410)
horn.press()
assert len(horn._voices) == 2 and horn._voices[0]["fading"]
assert horn._voices[-1]["pos"] == 0
def test_dj_mode_slides(qapp, tmp_path, monkeypatch):
from lintunes.gui.main_window import MainWindow
from lintunes.library_manager import LibraryManager
from lintunes.models.library import Library
from lintunes.preferences import Preferences
monkeypatch.setattr("lintunes.gui.main_window.load_config", lambda: {})
monkeypatch.setattr("lintunes.gui.main_window.save_config", lambda c: None)
window = MainWindow(LibraryManager(Library(), tmp_path), Preferences(tmp_path))
try:
window.resize(1100, 760)
window.show()
qapp.processEvents()
window.set_dj_mode(True)
overlay = window._dj_overlay
assert overlay is not None and overlay.opening
# the real window is already in its final layout under the picture
assert window._dj.height() == window._dj.layout_height() > 0
window._dj_anim.stop()
window.set_dj_mode(False)
assert not window._dj.isVisible() # final layout at once
assert window._dj_overlay is not None and not window._dj_overlay.opening
window._dj_anim.stop()
finally:
window.close()