mirror of
https://github.com/MrPatben8/konduktor.git
synced 2026-12-03 18:32:51 +01:00
Added support for coloring Rekordbox hotcues
This commit is contained in:
1 parent
9be2c029b2
commit
08c53e1150
19 files changed
+337
-45
No files matched your search
@@ -21,13 +21,13 @@ from __future__ import annotations
|
||||
|
||||
from pathlib import Path
|
||||
|
||||
from ...core.adapter import Unsupported
|
||||
from ...core.adapter import InvalidCommand, Unsupported
|
||||
from ...core.capabilities import Capabilities
|
||||
from ...core.model import GridMarker, PlaylistNode, Track, TrackCues
|
||||
from ...core.pathmap import PathMapping, common_dir_prefix
|
||||
from ...core.query import TrackIndex
|
||||
from . import capabilities as caps
|
||||
from . import projection
|
||||
from . import palette, projection
|
||||
from .store import RekordboxStore
|
||||
|
||||
|
||||
@@ -297,6 +297,18 @@ class RekordboxAdapter:
|
||||
self._store.set_cue_type(track_id, slot, cue_type)
|
||||
return self._refresh_cues(track_id)
|
||||
|
||||
def set_cue_color(self, track_id: str, slot: int, color: str | None) -> TrackCues:
|
||||
self._require_writable("Editing cues")
|
||||
code = None
|
||||
if color is not None:
|
||||
code = palette.swatch_code(color)
|
||||
if code is None:
|
||||
raise InvalidCommand(
|
||||
f"{color!r} is not one of Rekordbox's hot cue colours"
|
||||
)
|
||||
self._store.set_cue_color(track_id, slot, code)
|
||||
return self._refresh_cues(track_id)
|
||||
|
||||
def delete_cue(self, track_id: str, slot: int) -> TrackCues:
|
||||
self._require_writable("Deleting cues")
|
||||
self._store.delete_cue(track_id, slot)
|
||||
|
||||
@@ -30,6 +30,7 @@ from ...core.capabilities import (
|
||||
SaveCapabilities,
|
||||
TrackCapabilities,
|
||||
)
|
||||
from . import palette
|
||||
from .cue_types import WRITABLE_CUE_TYPES
|
||||
|
||||
# Rekordbox's addressable hot cue bank. The pads are labelled A-H; the stored
|
||||
@@ -68,8 +69,10 @@ def capabilities_for(
|
||||
max_memory_cues=None, # unlimited
|
||||
# Both cue types write; Rekordbox has no fade/load types.
|
||||
types=WRITABLE_CUE_TYPES,
|
||||
# A hot cue's colour is one of rekordbox's swatches (the 15 that
|
||||
# were measured, see `palette`), offered in rekordbox's own order.
|
||||
color="palette",
|
||||
palette=[], # TODO milestone 2: the built-in cue colour table
|
||||
palette=[palette.hex_for(code) for code in palette.SWATCHES],
|
||||
named=True, # djmdCue.Comment
|
||||
loops="cue_type",
|
||||
),
|
||||
|
||||
@@ -91,3 +91,28 @@ def code_for(color: str | None) -> tuple[int, tuple[int, int, int]]:
|
||||
return min(d, 1 - d)
|
||||
best = min((c for c in PALETTE if c != 0x2B), key=hue_gap)
|
||||
return best, PALETTE[best]
|
||||
|
||||
|
||||
#: The swatches a user can pick, in rekordbox's own order — what
|
||||
#: `capabilities.cues.palette` offers. 15 of rekordbox's 16: the unmeasured
|
||||
#: teal-green is left out rather than offered with a guessed RGB, and 0x2B is
|
||||
#: not a swatch at all (see the module note).
|
||||
SWATCHES: list[int] = [code for code in PALETTE if code != 0x2B]
|
||||
|
||||
|
||||
def hex_for(code: int | None) -> str | None:
|
||||
"""`#RRGGBB` for a stored palette code; None for uncoloured or unknown.
|
||||
|
||||
An unknown code (the unmeasured teal-green) projects as no colour rather
|
||||
than a guessed one; it is still kept on the row, since editing a cue's
|
||||
position, type or name never rewrites its colour.
|
||||
"""
|
||||
rgb = PALETTE.get(code) if code else None
|
||||
return None if rgb is None else f"#{rgb[0]:02X}{rgb[1]:02X}{rgb[2]:02X}"
|
||||
|
||||
|
||||
def swatch_code(color: str) -> int | None:
|
||||
"""The code of an EXACT palette colour, else None — for a user's pick, which
|
||||
must be a swatch, unlike `code_for`'s nearest-hue mapping for exports."""
|
||||
rgb = _parse(color)
|
||||
return next((code for code, value in PALETTE.items() if value == rgb), None)
|
||||
@@ -8,7 +8,7 @@ from __future__ import annotations
|
||||
import re
|
||||
|
||||
from ...core.model import CuePoint, GridMarker, HotcueChip, Track, TrackCues
|
||||
from . import beatgrid, timebase
|
||||
from . import beatgrid, palette, timebase
|
||||
from .cue_types import cue_type, role_and_slot
|
||||
|
||||
# Rekordbox names keys musically ("Abm", "F", "Ebm") rather than in Camelot or
|
||||
@@ -100,7 +100,7 @@ def _name_of(related) -> str | None:
|
||||
def to_track(row, cue_kinds=()) -> Track:
|
||||
"""Project one ``DjmdContent`` row.
|
||||
|
||||
`cue_kinds` is this track's ``(Kind, OutMsec)`` pairs from the store's single
|
||||
`cue_kinds` is this track's ``(Kind, OutMsec, ColorTableIndex)`` from the store's single
|
||||
query — passed in rather than read off the row, which would be a per-track
|
||||
round trip. "Is this a hot cue, and on which pad?" is `role_and_slot`, the
|
||||
same definition `to_track_cues` uses, so the table's count and dots agree
|
||||
@@ -118,12 +118,11 @@ def to_track(row, cue_kinds=()) -> Track:
|
||||
BPM are precisely the 22 with no grid.
|
||||
"""
|
||||
chips: list[HotcueChip] = []
|
||||
for kind, out_msec in cue_kinds:
|
||||
for kind, out_msec, color_code in cue_kinds:
|
||||
role, slot = role_and_slot(kind)
|
||||
if role == "hotcue" and slot is not None:
|
||||
# Rekordbox's colour is a palette index nobody has decoded yet (see
|
||||
# `_cue_color`), so no colour is claimed and the UI uses the type's.
|
||||
chips.append(HotcueChip(slot=slot, type=cue_type(out_msec), color=None))
|
||||
chips.append(HotcueChip(slot=slot, type=cue_type(out_msec),
|
||||
color=palette.hex_for(color_code)))
|
||||
chips.sort(key=lambda chip: chip.slot)
|
||||
key_name = _name_of(getattr(row, "Key", None))
|
||||
wheel, mode = parse_key(key_name)
|
||||
@@ -210,11 +209,6 @@ def to_track_cues(cue_rows: list, grid: tuple | None, time_offset: float = 0.0)
|
||||
|
||||
|
||||
def _cue_color(row) -> str | None:
|
||||
"""Rekordbox stores a PALETTE INDEX, not an RGB value.
|
||||
|
||||
The generic model carries ``#RRGGBB``, and the built-in palette that index
|
||||
refers to is not yet known, so no colour is claimed rather than a wrong one
|
||||
being invented. `capabilities.cues.color` already says "palette", so the UI
|
||||
knows not to offer a free colour picker.
|
||||
"""
|
||||
return None
|
||||
"""Rekordbox stores a PALETTE CODE (`ColorTableIndex`), not an RGB value;
|
||||
`Color` is -1 on a coloured cue. A loop's uncoloured convention is code 0."""
|
||||
return palette.hex_for(getattr(row, "ColorTableIndex", None))
|
||||
@@ -192,8 +192,9 @@ class RekordboxStore:
|
||||
raise NotFound(f"No track {track_id!r}")
|
||||
return row
|
||||
|
||||
def cue_kinds(self) -> dict[str, list[tuple[int | None, int | None]]]:
|
||||
"""``{track_id: [(Kind, OutMsec), ...]}`` for every live cue, in one query.
|
||||
def cue_kinds(self) -> dict[str, list[tuple[int | None, int | None, int | None]]]:
|
||||
"""``{track_id: [(Kind, OutMsec, ColorTableIndex), ...]}`` for every live
|
||||
cue, in one query.
|
||||
|
||||
Just enough per cue for the library table's count and hotcue dots,
|
||||
without a per-track round trip. What a ``Kind`` means is decided in the
|
||||
@@ -202,12 +203,13 @@ class RekordboxStore:
|
||||
"""
|
||||
t = self._tables
|
||||
rows = self._db.session.execute(
|
||||
select(t.DjmdCue.ContentID, t.DjmdCue.Kind, t.DjmdCue.OutMsec)
|
||||
select(t.DjmdCue.ContentID, t.DjmdCue.Kind, t.DjmdCue.OutMsec,
|
||||
t.DjmdCue.ColorTableIndex)
|
||||
.where(t.DjmdCue.rb_local_deleted == 0)
|
||||
).all()
|
||||
out: dict[str, list[tuple[int | None, int | None]]] = {}
|
||||
for content_id, kind, out_msec in rows:
|
||||
out.setdefault(str(content_id), []).append((kind, out_msec))
|
||||
out: dict[str, list[tuple[int | None, int | None, int | None]]] = {}
|
||||
for content_id, kind, out_msec, color_code in rows:
|
||||
out.setdefault(str(content_id), []).append((kind, out_msec, color_code))
|
||||
return out
|
||||
|
||||
def cues(self, track_id: str) -> list:
|
||||
@@ -651,13 +653,17 @@ class RekordboxStore:
|
||||
) -> None:
|
||||
"""Create or replace the cue in `slot`. A loop is a cue with a length.
|
||||
|
||||
`color_code` is a rekordbox palette code (`palette.PALETTE`); None or 0
|
||||
writes the uncoloured convention, which is what every edit through the
|
||||
adapter does — only the exporter passes a colour.
|
||||
`color_code` is a rekordbox palette code (`palette.PALETTE`). None KEEPS
|
||||
the colour of the cue already in the slot — the colour belongs to the
|
||||
pad, so a rename, a type change or an Auto Hotcues Replace leaves it as
|
||||
the user set it — and a new cue is uncoloured. Only `set_cue_color`
|
||||
clears one.
|
||||
"""
|
||||
if int(slot) < 0:
|
||||
raise InvalidCommand(f"Hot cue slot cannot be negative, got {slot}")
|
||||
existing = self._cue_row(track_id, slot)
|
||||
if color_code is None and existing is not None:
|
||||
color_code = existing.ColorTableIndex or None
|
||||
self._write_cue(
|
||||
track_id, existing, kind=kind_for("hotcue", slot), start_sec=start_sec,
|
||||
cue_type=cue_type, length_sec=length_sec, name=name, color_code=color_code,
|
||||
@@ -730,26 +736,44 @@ class RekordboxStore:
|
||||
cue.BeatLoopSize = beat_loop_size(beats) if beats else None
|
||||
cue.ActiveLoop = 0
|
||||
cue.CueMicrosec = 0
|
||||
cue.Color = 255
|
||||
cue.ColorTableIndex = 0
|
||||
else:
|
||||
cue.BeatLoopSize = None
|
||||
cue.ActiveLoop = None
|
||||
cue.CueMicrosec = None
|
||||
cue.Color = -1
|
||||
cue.ColorTableIndex = None
|
||||
if color_code:
|
||||
# A palette-coloured cue, as the real library stores one: the code in
|
||||
# ColorTableIndex (22 = green), Color -1. How rekordbox stores a
|
||||
# COLOURED LOOP is not in the reference library; the same column is
|
||||
# assumed and is what the smoke test checks.
|
||||
cue.ColorTableIndex = int(color_code)
|
||||
cue.Color = -1
|
||||
self._apply_color(cue, color_code, is_loop)
|
||||
cue.Comment = name or None
|
||||
|
||||
self._db.flush()
|
||||
self._sync_content_cue(track_id)
|
||||
|
||||
@staticmethod
|
||||
def _apply_color(cue, color_code: int | None, is_loop: bool) -> None:
|
||||
"""A row's two colour columns — the one definition of both conventions."""
|
||||
if color_code:
|
||||
# A palette-coloured cue, as the real library stores one: the code in
|
||||
# ColorTableIndex (22 = green), Color -1. Coloured loops store the
|
||||
# same; verified in Rekordbox 7 through the master.db export.
|
||||
cue.ColorTableIndex = int(color_code)
|
||||
cue.Color = -1
|
||||
elif is_loop:
|
||||
# What Rekordbox writes on the loops and auto-cues it creates.
|
||||
cue.Color = 255
|
||||
cue.ColorTableIndex = 0
|
||||
else:
|
||||
cue.Color = -1
|
||||
cue.ColorTableIndex = None
|
||||
|
||||
def set_cue_color(self, track_id: str, slot: int, color_code: int | None) -> None:
|
||||
"""Recolour the cue in `slot`; None makes it uncoloured. Touches only the
|
||||
colour columns, so the position does not take a time-base round trip."""
|
||||
cue = self._cue_row(track_id, slot)
|
||||
if cue is None:
|
||||
raise NotFound(f"No cue in slot {slot}")
|
||||
self._apply_color(cue, color_code, (cue.OutMsec or 0) > 0)
|
||||
self._db.flush()
|
||||
self._sync_content_cue(track_id)
|
||||
self._journal.record("cue", "modify", track_id, f"slot:{slot}")
|
||||
|
||||
def set_cue_type(self, track_id: str, slot: int, cue_type: str) -> None:
|
||||
cue = self._cue_row(track_id, slot)
|
||||
if cue is None:
|
||||
|
||||
Reference in new issue
Block a user