Files
lintunes/tests/test_round95.py
T
trav e00aafefba v0.65.0: DJ mode round three, and the small things
- The shelf is revealed in place: the decks don't stretch or squeeze as
  the window parts.
- The DJ button is the top row's last box (where bpm was), so it stays
  put; bpm moved under it.
- Tempo, vocals and the crossfader snap to their middle and show it. The
  vocals slider has icons, lined up with its ends and middle.
- Sync sticks: a synced deck follows the other's tempo, glows and says
  Synced until its own tempo fader moves. The ½×/1×/2× switch re-syncs,
  and the BPM line says when it's counted half or double.
- A back-to-the-start button per deck.
- The air horn is a real recording (CC0): tap for the phrase, hold to
  stretch the first blast.
- The zoomed waveforms ease out while a deck is paused or held.
- DJ audio follows a change of output device instead of going silent.
- A new playlist folder is named in place, scrolled into view.
- Visualizer bars: choose how many, 1 to 1024 (one per pixel at most).
- The volume slider is as tall as the scrubber.
2026-10-17 04:00:34 -07:00

216 lines
8.1 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 95: DJ mode, round three — the shelf revealed in place, the DJ
button where bpm was, snapping sliders (tempo, vocals, crossfader), sticky
sync that glows and re-syncs on ½×/2×, back to the start, the real air
horn, zoom-out when paused or held, a sink that follows the system's output
— and outside DJ mode: a new folder named in place, the bars' count, a
taller volume slider."""
from unittest.mock import MagicMock
import numpy as np
import pytest
from PyQt6.QtCore import QEvent, QPointF, Qt
from PyQt6.QtGui import QMouseEvent
from lintunes.dj.audio import RATE, Horn
from tests.test_round93 import _clicks, _loaded, shelf # noqa: F401 — the fixture
def _mouse(kind, x, y):
return QMouseEvent(kind, QPointF(x, y), QPointF(x, y), Qt.MouseButton.LeftButton,
Qt.MouseButton.LeftButton, Qt.KeyboardModifier.NoModifier)
class TestSnapSlider:
def test_vertical_tempo_snaps_to_zero(self, qapp):
from lintunes.gui.dj_shelf import SnapSlider
slider = SnapSlider(True, -800, 800, snap=30)
slider.resize(slider.width(), 400)
a, b = slider._span() # bottom, top
y_near = a + (b - a) * (830 / 1600) # +30 → snaps
slider.mousePressEvent(_mouse(QEvent.Type.MouseButtonPress, 10, y_near))
assert slider.value() == 0
slider.mouseMoveEvent(_mouse(QEvent.Type.MouseMove, 10, b))
assert slider.value() == 800 # top is +8 %
slider.mouseReleaseEvent(_mouse(QEvent.Type.MouseButtonRelease, 10, b))
slider.mouseDoubleClickEvent(None)
assert slider.value() == 0
def test_vocals_icons_sit_on_the_ends_and_middle(self, qapp):
from PyQt6.QtGui import QColor
from lintunes.gui.dj_shelf import VocalSlider
slider = VocalSlider(QColor("#5a9be0"))
slider.resize(220, slider.height())
lo, hi = slider._span()
assert slider._pos_of(-100) == pytest.approx(lo)
assert slider._pos_of(0) == pytest.approx((lo + hi) / 2)
assert slider._pos_of(100) == pytest.approx(hi)
slider.setValue(5)
slider.mouseReleaseEvent(None)
assert slider.value() == 0 # within the snap
class TestSync:
def _pair(self, 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
def test_sync_sticks_glows_and_follows(self, shelf):
a, b = self._pair(shelf)
shelf.sync(b)
assert b.synced and shelf.sync_buttons["B"].text() == "Synced"
shelf.speed["A"].setValue(200) # A +2 %: B follows
shelf._tick()
assert b.deck.speed == pytest.approx(124 * 1.02 / 118)
shelf.speed["B"].setValue(0) # B moved by hand: unsynced
assert not b.synced
assert shelf.sync_buttons["B"].text() == "Sync to other track"
def test_only_one_deck_follows(self, shelf):
a, b = self._pair(shelf)
shelf.sync(b)
shelf.sync(a)
assert a.synced and not b.synced
def test_half_time_resyncs_a_playing_pair(self, shelf):
a, b = self._pair(shelf)
a.deck.playing = b.deck.playing = True
a.deck.position, b.deck.position = RATE * 3.0, RATE * 7.31
shelf.sync(b)
before = b.deck.position
shelf.set_mult(b, 0.5)
assert "counted ½×" in shelf.bpm_notes["B"].text()
assert b.synced and b.deck.speed == pytest.approx(124 / 118)
assert b.deck.position != before # re-lined up on the new grid
def test_back_to_the_start(self, shelf):
a, _ = self._pair(shelf)
a.deck.seek(RATE * 5)
a.deck.ended = True
shelf.to_start(a)
assert a.deck.position == 0 and not a.deck.ended
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(4096)
if block is None:
return total / RATE
total += len(block)
def test_a_tap_plays_the_whole_recording(self):
horn = self._horn()
horn.press()
horn.render(441)
horn.release()
assert 0.01 + self._drain(horn) == pytest.approx(2.0, abs=0.1)
def test_holding_stretches_it(self):
horn = self._horn()
horn.press()
held = sum(len(horn.render(4096)) for _ in range(int(4 * RATE / 4096))) / RATE
horn.release()
assert held > 3.9
assert self._drain(horn) <= 2.0 # then the rest plays out
def test_the_bundled_recording_loads(self, qapp):
from lintunes.gui.dj_shelf import _load_horn
horn = Horn()
_load_horn(horn)
assert horn._src is not None and len(horn._src) > RATE
def test_the_waveform_zooms_out_while_paused(shelf):
a, _ = shelf.views
_loaded(shelf, a, shelf.tracks[0], _clicks(124, 0.1, 10), 124.0, 0.1)
lanes = shelf.lanes
lanes.resize(600, 120)
for _ in range(80):
lanes.grab() # paused: eases out
assert lanes._window["A"] == pytest.approx(16.0)
a.deck.playing = True
for _ in range(80):
lanes.grab()
assert lanes._window["A"] == pytest.approx(8.0)
def test_revealed_in_place(shelf):
shelf.set_layout_height(330)
shelf.setFixedHeight(40) # mid-animation
assert shelf._body.height() == 330
assert shelf.sync_buttons["A"].isVisibleTo(shelf) # laid out full, not compact
def test_the_sink_follows_the_default_output(monkeypatch):
from lintunes.dj import output
pump = output._Pump(MagicMock())
opened = []
pump._open = lambda: opened.append(1) or True
pump.sink = MagicMock()
pump.sink.error.return_value = output.QAudio.Error.NoError
pump.io = MagicMock()
pump.device_id = b"old"
other = MagicMock()
other.isNull.return_value = False
other.id.return_value = b"new"
monkeypatch.setattr(output.QMediaDevices, "defaultAudioOutput", lambda: other)
pump._fill()
assert opened == [1]
def test_bars_count_and_one_per_pixel(qapp):
from PyQt6.QtCore import QRectF
from PyQt6.QtGui import QColor, QImage, QPainter
from lintunes.gui.visualizer_styles import BarsRenderer, Frame, settings
config = settings(None)
config["visualizer_bars_count"] = 1024
renderer = BarsRenderer(config)
x = (np.random.default_rng(1).standard_normal(4096) * 0.3).astype(np.float32)
renderer.advance(Frame(x, 44100, x[:735], 1 / 60))
assert len(renderer.bars) == 1024
image = QImage(206, 60, QImage.Format.Format_RGB32)
image.fill(QColor("black"))
painter = QPainter(image)
renderer.paint(painter, QRectF(0, 0, 206, 60), QColor("white"))
painter.end()
renderer.configure(dict(config, visualizer_bars_count=1))
assert len(renderer.bars) == 1
def test_new_folder_is_named_in_place(qapp, tmp_path):
from lintunes.gui.sidebar import PlaylistTree
from lintunes.library_manager import LibraryManager
from lintunes.models.library import Library
manager = LibraryManager(Library(), tmp_path)
tree = PlaylistTree(manager)
tree.show()
tree.create_folder_interactive()
item = tree.currentItem()
assert item is not None and item.text(0) == "untitled folder"
assert tree.state() == tree.State.EditingState
tree.close()
def test_dj_button_up_top_bpm_below_and_a_tall_volume(qapp, tmp_path):
import tempfile
from pathlib import Path
from lintunes.gui.transport import TransportBar
from lintunes.preferences import Preferences
player = MagicMock()
player.repeat_mode.return_value = "off"
with tempfile.TemporaryDirectory() as d:
bar = TransportBar(player, MagicMock(), Preferences(Path(d)))
assert bar.dj_button.parentWidget() is bar._dj_box
assert bar._dj_box in bar._fixed_controls
bar._place_header() # what a resize does
assert bar._header_row.right is bar._bpm_button
assert bar._volume_slider.property("tall") is True