From 541326358759f38952b93c99358f59065ed5a3a2 Mon Sep 17 00:00:00 2001 From: trav Date: Thu, 22 Oct 2026 12:43:34 -0700 Subject: [PATCH] v0.76.0: opening DJ mode with empty decks hands the playing song to Deck A without a gap, and closing goes back to it --- CLAUDE.md | 8 ++- lintunes/__init__.py | 2 +- lintunes/gui/dj_shelf.py | 30 ++++++++- lintunes/gui/main_window.py | 58 ++++++++++++++++- lintunes/player.py | 6 +- tests/test_round107.py | 123 ++++++++++++++++++++++++++++++++++++ 6 files changed, 220 insertions(+), 7 deletions(-) create mode 100644 tests/test_round107.py diff --git a/CLAUDE.md b/CLAUDE.md index 327a8b2..afbe101 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -384,7 +384,13 @@ persistence) → GUI (Qt widgets that read the manager and connect to its signal the main player is **suspended** while DJ mode is open (`Player.set_suspended`: paused, and nothing — double-click, Return, a media key — can start it; it played behind the decks, unstoppable, in - v0.63.0). "Sync to other track" is enabled whenever its own deck has a + v0.63.0). **Except** (Round 107) when both decks are empty and a song + is on: then it keeps sounding (`set_suspended(True, pause=False)`) while + Deck A loads it (`DjShelf.load(…, handoff=)`), the deck starts a sink + latency ahead (`DjShelf.cue`), the main player pauses as that arrives + and its spot is `MainWindow._dj_pin`; closing seeks back there and plays + (`_back_to_pin`). The crossfader goes to all-A. Casting/AirPlay keep the + old pause. "Sync to other track" is enabled whenever its own deck has a song; what it can't do yet it says in the status bar. Deck A is blue and Deck B orange, every control included; loaded songs get a DECK A/B tag in every track list (`track_table.deck_tag`). **Its own engine**, because Qt's diff --git a/lintunes/__init__.py b/lintunes/__init__.py index 4fee84d..69c60d3 100644 --- a/lintunes/__init__.py +++ b/lintunes/__init__.py @@ -1,3 +1,3 @@ """LinTunes — iTunes-style music library manager and player for Linux.""" -__version__ = "0.75.0" +__version__ = "0.76.0" diff --git a/lintunes/gui/dj_shelf.py b/lintunes/gui/dj_shelf.py index dc41915..63a4d4d 100644 --- a/lintunes/gui/dj_shelf.py +++ b/lintunes/gui/dj_shelf.py @@ -77,6 +77,7 @@ class DeckView: self.synced = False # set to the other deck's tempo, and still is self.synced_to = None # …the track it was set to self.loop_beats = 4.0 + self.handoff = None # called with this view (None: failed) once loaded @property def color(self) -> QColor: @@ -1569,6 +1570,7 @@ class DjShelf(QWidget): def stop(self): """DJ mode closed: both decks stop (kept loaded), the sink goes.""" for v in self.views: + v.handoff = None v.deck.playing = False v.deck.scratch_rate = None if self._output is not None: @@ -1641,9 +1643,12 @@ class DjShelf(QWidget): return self.load(view, track) - def load(self, view: DeckView, track): + def load(self, view: DeckView, track, handoff=None): + """Load ``track`` on this deck; ``handoff(view)`` runs once it's + playable (``handoff(None)`` if it never will be).""" view.deck.load(None) view.track = track + view.handoff = handoff view.peaks = None view.bpm = None view.offset = 0.0 @@ -1665,6 +1670,23 @@ class DjShelf(QWidget): self._refresh_deck(view) self._emit_decks() + def latency_ms(self) -> int: + """How far behind what's rendered the speakers are.""" + frames = self._output.latency_frames if self._output is not None else 0 + return int(round(frames * 1000 / RATE)) + + def cue(self, view: DeckView, seconds: float, play: bool): + """Put a loaded deck where ``seconds`` will be *heard* — a playing + deck renders a latency ahead of the speakers — playing or not.""" + deck = view.deck + if deck.source is None: + return + ahead = self.latency_ms() / 1000 if play else 0.0 + deck.seek((seconds + ahead) * RATE) + deck.ended = False + deck.playing = play + self._refresh_deck(view) + def _view(self, name: str) -> DeckView: return self.views[0] if name == "A" else self.views[1] @@ -1694,8 +1716,14 @@ class DjShelf(QWidget): self.vocal_sliders[view.name].setToolTip( "Set up Demucs to separate vocals:\n" + stems.setup_command()) self._refresh_deck(view) + handoff, view.handoff = view.handoff, None + if handoff is not None: + handoff(view) def _on_load_failed(self, view: DeckView, why: str): + handoff, view.handoff = view.handoff, None + if handoff is not None: + handoff(None) view.stem_note = "" self.message.emit(f"Couldn't load “{view.track.name if view.track else ''}” on " f"Deck {view.name}: {why}") diff --git a/lintunes/gui/main_window.py b/lintunes/gui/main_window.py index 6246bc3..3b997c8 100644 --- a/lintunes/gui/main_window.py +++ b/lintunes/gui/main_window.py @@ -164,6 +164,7 @@ class MainWindow(QMainWindow): self._dj = None self._dj_prep_worker = None self._dj_on = False + self._dj_pin = None # (track id, ms, was playing) to go back to self._dj_anim = None self._dj_resize_from = None self._transport.dj_toggled.connect(self.set_dj_mode) @@ -2660,7 +2661,9 @@ class MainWindow(QMainWindow): def set_dj_mode(self, on: bool): """Open or close the DJ shelf: the window parts under the row of transport boxes and the shelf shows in the gap; closing plays the - same thing backwards. The main player pauses while it's open.""" + same thing backwards. The main player pauses while it's open — + unless both decks are empty: then its song carries on and Deck A + takes it over without a gap, and closing goes back to it.""" on = bool(on) if on == self._dj_on: return @@ -2672,7 +2675,10 @@ class MainWindow(QMainWindow): from lintunes.gui import track_table if on: shelf = self._ensure_dj() - self.player.set_suspended(True) # pauses it, and keeps it so + handoff = (self.player.is_local and self.player.current_track is not None + and all(v.track is None for v in shelf.views)) + # pauses it (but not the song Deck A is about to take), and keeps it so + self.player.set_suspended(True, pause=not handoff) target = self._dj_target_height() self._transport.set_dj_mode(True, self._dj_titles()) track_table.deck_tag = self._deck_tag @@ -2683,6 +2689,8 @@ class MainWindow(QMainWindow): shelf.show() shelf.start() shelf.set_visualizer(self._transport._visualizer) + if handoff: + self._hand_to_deck(shelf) self._sidebar.art.hide() # room for the lists instead self._slide(split, before, shift=target + self._row_gap(), opening=True) else: @@ -2695,6 +2703,50 @@ class MainWindow(QMainWindow): self._slide(split, before, shift=shift, opening=False) self._refresh_tables() + def _hand_to_deck(self, shelf): + """Load the playing song on Deck A while the main player carries + on, then cross over: the deck starts where the song will be heard a + sink's latency from now and the main player pauses as that arrives. + Where it paused is the pin closing goes back to.""" + track = self.player.current_track + view = shelf.views[0] + shelf.crossfader.setValue(0) # Deck A alone, at full level + + def ready(loaded): + if not self._dj_on: + return + current = self.player.current_track + if loaded is None or current is None or current.track_id != track.track_id: + self.player.pause() # it failed, or the song moved on + return + position = self.player.position_ms() + if not self.player.is_playing(): + shelf.cue(loaded, position / 1000, play=False) + self._dj_pin = (track.track_id, position, False) + return + shelf.cue(loaded, position / 1000, play=True) + QTimer.singleShot(shelf.latency_ms(), lambda: self._pin_main(track.track_id)) + + shelf.load(view, track, handoff=ready) + + def _pin_main(self, track_id: int): + if not self._dj_on: + return # closed in the meantime: it never stopped + self.player.pause() + self._dj_pin = (track_id, self.player.position_ms(), True) + + def _back_to_pin(self): + """DJ mode closed: the main player goes back to where Deck A took + its song over, playing if it was.""" + pin, self._dj_pin = self._dj_pin, None + current = self.player.current_track + if pin is None or current is None or current.track_id != pin[0]: + return + _, position, playing = pin + self.player.seek(position) + if playing: + self.player.play() + def _ensure_dj(self): if self._dj is None: from lintunes.gui.dj_shelf import DjShelf @@ -2816,6 +2868,7 @@ class MainWindow(QMainWindow): self._dj.hide() self._sidebar.art.show() self.player.set_suspended(False) + self._back_to_pin() self._transport.set_dj_mode(False) def _dj_closed(self): @@ -2826,6 +2879,7 @@ class MainWindow(QMainWindow): self._dj.hide() self._sidebar.art.show() self.player.set_suspended(False) + self._back_to_pin() self._transport.set_dj_mode(False) def _on_dj_resize(self, dy: int): diff --git a/lintunes/player.py b/lintunes/player.py index efa8bac..5fe8d1e 100644 --- a/lintunes/player.py +++ b/lintunes/player.py @@ -591,9 +591,11 @@ class Player(QObject): # behind the decks with the main transport greyed out, unstoppable. suspended = False - def set_suspended(self, on: bool): + def set_suspended(self, on: bool, pause: bool = True): + """``pause=False`` keeps the current song sounding (nothing new can + start) while DJ mode's Deck A loads it to take over (Round 107).""" self.suspended = bool(on) - if on: + if on and pause: self.pause() def play_queue(self, track_ids: list[int], start_index: int): diff --git a/tests/test_round107.py b/tests/test_round107.py new file mode 100644 index 0000000..7e3ce96 --- /dev/null +++ b/tests/test_round107.py @@ -0,0 +1,123 @@ +"""Round 107: opening DJ mode with both decks empty doesn't stop the song — +Deck A loads it and takes over without a gap, and closing DJ mode goes back +to where the main player left off.""" + +import pytest + +from lintunes.dj import analysis, stems +from lintunes.dj.audio import RATE, Source +from lintunes.player import Player +from tests.test_round93 import _tone + + +@pytest.fixture +def window(qapp, tmp_path, monkeypatch): + from lintunes.gui import dj_shelf + from lintunes.gui.main_window import MainWindow + from lintunes.library_manager import LibraryManager + from lintunes.models import Track + 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) + monkeypatch.setattr(dj_shelf.DeckLoader, "request", lambda *a, **k: None) + monkeypatch.setattr(stems, "available", lambda: False) + monkeypatch.setattr(dj_shelf.tagging, "read_embedded_artwork", lambda p: None) + library = Library() + for tid in (1, 2): + library.tracks[tid] = Track(track_id=tid, name=f"Song {tid}", artist="Test", + location=f"/x{tid}.mp3", bpm=120) + made = MainWindow(LibraryManager(library, tmp_path), Preferences(tmp_path)) + # A main player "playing" song 1, 42 s in. + state = {"track": library.tracks[1], "playing": True, "ms": 42_000, "seeks": []} + monkeypatch.setattr(Player, "current_track", property(lambda self: state["track"])) + player = made.player + player.is_playing = lambda: state["playing"] + player.position_ms = lambda: state["ms"] + player.pause = lambda: state.update(playing=False) + player.play = lambda: state.update(playing=True) + player.seek = lambda ms: state["seeks"].append(ms) + made.state = state + made.library = library + yield made + made.close() + + +def _finish_loading(window, track): + from lintunes.dj.loader import Loaded + samples = _tone(seconds=60.0) + window._dj._on_loader_result("A", Loaded( + track_id=track.track_id, source=Source(samples), + peaks=analysis.peaks(samples, RATE), bpm=120.0, offset=0.0, detected=False)) + + +def _close(window, qapp): + window.set_dj_mode(False) + window._dj_anim.stop() + window._dj_closed() + + +def test_empty_decks_take_the_playing_song_over(window, qapp): + state = window.state + window.set_dj_mode(True) + shelf = window._dj + a = shelf.views[0] + assert state["playing"], "the song keeps playing while Deck A loads" + assert window.player.suspended + assert a.track is state["track"] + assert shelf.crossfader.value() == 0 # Deck A alone + _finish_loading(window, state["track"]) + assert a.deck.playing + assert a.deck.position / RATE == pytest.approx(42.0 + shelf.latency_ms() / 1000, abs=0.01) + qapp.processEvents() # the latency passes + assert not state["playing"] + assert window._dj_pin == (1, 42_000, True) + _close(window, qapp) + assert state["seeks"] == [42_000] and state["playing"] + assert window._dj_pin is None + + +def test_a_paused_song_is_cued_and_stays_paused(window, qapp): + state = window.state + state["playing"] = False + window.set_dj_mode(True) + a = window._dj.views[0] + _finish_loading(window, state["track"]) + assert not a.deck.playing + assert a.deck.position / RATE == pytest.approx(42.0, abs=0.01) + _close(window, qapp) + assert not state["playing"] + + +def test_closing_before_the_deck_loads_leaves_the_song_playing(window, qapp): + state = window.state + window.set_dj_mode(True) + _close(window, qapp) + assert state["playing"] and not window.player.suspended + assert window._dj.views[0].handoff is None + assert window._dj_pin is None + + +def test_a_loaded_deck_means_the_song_just_stops(window, qapp): + state = window.state + window.set_dj_mode(True) + _finish_loading(window, state["track"]) + qapp.processEvents() + _close(window, qapp) + window._dj.load(window._dj.views[1], window.library.tracks[2]) + window._dj.views[0].track = None # only Deck B holds something now + state["playing"] = True + window.set_dj_mode(True) + assert not state["playing"] + assert window._dj.views[0].track is None # nothing loaded for it + _close(window, qapp) + assert not state["playing"] + + +def test_a_failed_load_stops_the_song(window, qapp): + state = window.state + window.set_dj_mode(True) + window._dj._on_loader_failed("A", 1, "no decoder") + assert not state["playing"] + _close(window, qapp) + assert not state["playing"]