Files
lintunes/tests/test_round95.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

187 lines
7.2 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_glows_while_it_holds(self, shelf):
a, b = self._pair(shelf)
shelf.sync(b)
assert b.synced and shelf.sync_buttons["B"].text() == "Synced"
shelf.speed["B"].setValue(0) # B moved by hand: unsynced
assert not b.synced
assert shelf.sync_buttons["B"].text() == "Sync" # Round 97: shorter
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
def test_the_bundled_horn_recording_loads(qapp):
# (Round 96 replaced "a tap plays the whole phrase" with the way a DJ
# horn really plays — see test_round96.TestHorn.)
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(200):
lanes.grab() # paused: eases out
assert lanes._window["A"] == pytest.approx(8.0 * 1.35, abs=0.05) # Round 97: gentler
a.deck.playing = True
for _ in range(200):
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