Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion api/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -538,10 +538,14 @@ func (s *Server) applyImportBundle(w http.ResponseWriter, bundle *library.Import
if incSettings && s.Menu != nil && len(bundle.Sorts) > 0 {
s.Menu.ImportSorts(bundle.Sorts)
}
// So is the root menu (djmdCategory).
// So is the root menu (djmdCategory), and so are the names the library
// gives its track colours (djmdColor).
if incSettings && s.Menu != nil && len(bundle.Categories) > 0 {
s.Menu.ImportCategories(bundle.Categories)
}
if incSettings && len(bundle.ColorNames) > 0 {
s.Library.SetColorNames(bundle.ColorNames)
}

writeJSON(w, result)
}
Expand Down
2 changes: 1 addition & 1 deletion api/tracks.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func (s *Server) libTrackToInfo(t *library.Track) TrackInfo {
TrackNum: t.TrackNum,
Rating: int(t.Rating),
ColorID: t.ColorID,
ColorName: library.TrackColorName(t.ColorID),
ColorName: s.Library.ColorName(t.ColorID),
PlayCount: t.PlayCount,
ArtID: t.ArtID,
ArtChecked: t.ArtChecked,
Expand Down
1 change: 1 addition & 0 deletions cmd/conformance-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ func main() {
second = api.ImportedTrackColumn(bundle.Sorts)
}
log.Printf("conformance: track-list second column %q", second)
lib.SetColorNames(bundle.ColorNames)
root := dbserver.DefaultRootMenu()
if len(bundle.Categories) > 0 {
root = dbserver.RootMenuFromCategories(bundle.Categories)
Expand Down
7 changes: 4 additions & 3 deletions dbserver/drilldown.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,13 +282,14 @@ func baseName(path string) string {
// to handleGetTracksByColor.
func (h *Handler) handleGetColors(msg *proto.DBMessage) []*proto.DBMessage {
dlog.Debugf("dbserver: COLOR list (0x100d)")
// rekordbox lists the eight named colours (Pink … Purple), title-cased,
// with no "no colour" row; each row type is 0x13 + the colour ID.
// rekordbox lists the eight named colours (Pink … Purple, or the names
// the library gives them), with no "no colour" row; each row type is
// 0x13 + the colour ID.
items := make([]*menuItem, 0, 8)
for id := uint8(1); id <= 8; id++ {
items = append(items, &menuItem{
ID: uint32(id),
Label1: library.TrackColorName(id),
Label1: h.colorName(id),
ItemType: uint32(0x13 + id),
})
}
Expand Down
11 changes: 10 additions & 1 deletion dbserver/menuitem.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,15 @@ func (h *Handler) catID(kind, name string) uint32 {
return categoryID(h.lib, kind, name)
}

// colorName is a track colour's name in the served library: the name the
// library gives it, else the stock name.
func (h *Handler) colorName(id uint8) string {
if h.lib != nil {
return h.lib.ColorName(id)
}
return library.TrackColorName(id)
}

// categoryID is catID for a library, which may be nil.
func categoryID(lib *library.Library, kind, name string) uint32 {
if lib != nil {
Expand Down Expand Up @@ -308,7 +317,7 @@ func (h *Handler) applyTrackDetail(m *menuItem, t *library.Track, ids *catIDs) {
// Label2 = colour name string, empty for no-colour.
if t.ColorID >= 1 && t.ColorID <= 8 {
m.ParentID = uint32(t.ColorID)
m.Label2 = library.TrackColorName(t.ColorID)
m.Label2 = h.colorName(t.ColorID)
m.ItemType = (uint32(0x13+t.ColorID) << 8) | 0x04
} else {
m.ParentID = 0x7fffffff
Expand Down
15 changes: 6 additions & 9 deletions dbserver/render_override.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

package dbserver

import (
"github.com/vynulldev/vynull/library"
"github.com/vynulldev/vynull/proto"
)
import "github.com/vynulldev/vynull/proto"

// render_override.go: an eight-argument track render can ask for a second
// column of its own (link-spec PROTOCOL_REFERENCE.md "Track render override
Expand Down Expand Up @@ -60,14 +57,14 @@ func capText(s string, n int) string {
}

// capRowText applies rekordbox's 127-character limit to a track row whose
// second column is a lookup or the key, the whole text included (KEY's
// "key - BPM" is cut too); m itself is left alone.
// second column is a lookup, the key or the colour, the whole text included
// (KEY's "key - BPM" is cut too); m itself is left alone.
func capRowText(m *menuItem) *menuItem {
if !m.libTrack || m.ItemType&0xff != 0x04 {
return m
}
switch byte(m.ItemType >> 8) {
case 0x07, 0x02, 0x06, 0x0e, 0x29, 0x28, 0x0f:
switch col := byte(m.ItemType >> 8); {
case col == 0x07, col == 0x02, col == 0x06, col == 0x0e, col == 0x29, col == 0x28, col == 0x0f, isColorColumn(col):
if capped := capText(m.Label2, lookupTextMax); capped != m.Label2 {
r := *m
r.Label2 = capped
Expand Down Expand Up @@ -138,7 +135,7 @@ func (h *Handler) overrideColumn(c *trackRows, m *menuItem, col byte) *menuItem
r.ParentID = uint32(v.plays)
case 0x13:
if v.color >= 1 && v.color <= 8 {
r.ParentID, r.Label2 = uint32(v.color), library.TrackColorName(uint8(v.color))
r.ParentID, r.Label2 = uint32(v.color), capText(h.colorName(uint8(v.color)), lookupTextMax)
r.ItemType = uint32(0x13+v.color)<<8 | 0x04
}
}
Expand Down
43 changes: 43 additions & 0 deletions dbserver/render_override_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,3 +169,46 @@ func TestDateAddedStoredText(t *testing.T) {
}
}
}

// A library's own colour names are served wherever a colour is named: the
// COLOR menu, a colour second column (configured or by the render override,
// cut to 127 characters) and TRACK INFO (link-spec custom-colors,
// secondary-string-boundaries and display-song-info-boundaries).
func TestCustomColorNames(t *testing.T) {
lib := library.New()
lib.AddTrack(&library.Track{ID: 1, Title: "One", ColorID: 1})
lib.AddTrack(&library.Track{ID: 2, Title: "Two", ColorID: 8})
lib.AddTrack(&library.Track{ID: 3, Title: "Three", ColorID: 2})
long := strings.Repeat("o", 128)
lib.SetColorNames(map[uint8]string{1: "Fixture Magenta", 8: long})
col := "color"
h := &Handler{lib: lib, menu: detailMenu{detail: &col}}

h.Handle(&proto.DBMessage{TxID: 1, Type: 0x100d, Args: []proto.DBArg{proto.ArgI32(overrideCtx), proto.ArgI32(0)}})
var colors []string
for _, m := range h.pendingItems {
colors = append(colors, m.Label1)
}
if colors[0] != "Fixture Magenta" || colors[1] != "Red" || colors[7] != long {
t.Errorf("COLOR menu %q", colors)
}

rows := listRows(t, h, 0)
if got := rowColumn(rows, 1).text; got != "Fixture Magenta" {
t.Errorf("colour column %q, want Fixture Magenta", got)
}
if got := rowColumn(rows, 2).text; got != long[:127] {
t.Errorf("colour column %d characters, want 127", len(got))
}
hk := &Handler{lib: lib, menu: keyColumnMenu{}}
if got := rowColumn(listRows(t, hk, 0, 1, 15), 2).text; got != long[:127] {
t.Errorf("override: %d characters, want 127", len(got))
}

h.Handle(&proto.DBMessage{TxID: 2, Type: proto.DBMsgGetMetadata, Args: []proto.DBArg{proto.ArgI32(0x01020401), proto.ArgI32(1)}})
for _, m := range h.pendingItems {
if m.ItemType == 0x14 && m.Label1 != "Fixture Magenta" {
t.Errorf("TRACK INFO colour %q, want Fixture Magenta", m.Label1)
}
}
}
2 changes: 1 addition & 1 deletion dbserver/songinfo_title.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (h *Handler) newSongInfoTitle(t *library.Track, plainArg0 uint32) *songInfo
12: {id("key", t.Key), keyName(t.Key, h.classicKeys()), 0x0f},
13: {uint32(t.Bitrate), "", 0x10},
14: {0, "", 0x00},
15: {color, library.TrackColorName(uint8(color)), 0x13 + color&0xff},
15: {color, h.colorName(uint8(color)), 0x13 + color&0xff},
16: {uint32(t.PlayCount), "", 0x2a},
17: {t.ID, date, 0x2e},
}}
Expand Down
2 changes: 1 addition & 1 deletion dbserver/track.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ func (h *Handler) handleGetMetadata(msg *proto.DBMessage) []*proto.DBMessage {
{ID: rating, ItemType: 0x000a, ParentID: on(6)}, // rating
// Colour: the ID is the colour (0 = none, 1 = Pink ... 8 = Purple) and
// the type is 0x13 + that ID, as in the COLOUR menu (0x14 = Pink).
{ID: colorID, Label1: library.TrackColorName(uint8(colorID)), ItemType: 0x13 + colorID&0xff, ParentID: on(13)},
{ID: colorID, Label1: h.colorName(uint8(colorID)), ItemType: 0x13 + colorID&0xff, ParentID: on(13)},
{ID: genreID, Label1: genre, ItemType: 0x0006, ParentID: on(1)}, // genre
{ID: tID, Label1: dateAdded, ItemType: 0x002e, ParentID: on(22)}, // date added
{ID: tID, Label1: comment, ItemType: 0x0023, ParentID: on(21)}, // comment
Expand Down
89 changes: 89 additions & 0 deletions library/colornames.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// SPDX-License-Identifier: GPL-3.0-or-later

package library

import (
"encoding/json"
"maps"
"os"

"github.com/vynulldev/vynull/internal/dlog"
"github.com/vynulldev/vynull/internal/fsutil"
)

// A rekordbox library can rename its eight track colours (djmdColor's
// Commnt), and rekordbox then serves those names wherever a colour is named:
// the COLOR menu, a colour second column, TRACK INFO (link-spec
// SORT_AND_COLOR_ORACLE.md, "Custom color labels"). A library keeps the names
// that differ from the stock ones (TrackColorName), persisted beside the
// track list.

type colorNamesFile struct {
Names map[uint8]string `json:"names"`
}

// ColorName is colour id's name in this library: its own name, else the
// stock one.
func (l *Library) ColorName(id uint8) string {
l.mu.RLock()
name, ok := l.colorNames[id]
l.mu.RUnlock()
if ok {
return name
}
return TrackColorName(id)
}

// SetColorNames replaces the library's own colour names with names (colour
// ID 1-8 to name; a stock or empty name, or another ID, is dropped), saves
// them, and moves the library's revision so served rows pick them up.
func (l *Library) SetColorNames(names map[uint8]string) {
own := make(map[uint8]string)
for id, name := range names {
if id >= 1 && id <= 8 && name != "" && name != TrackColorName(id) {
own[id] = name
}
}
l.mu.Lock()
l.colorNames = own
l.mu.Unlock()
l.rev.Add(1)
l.saveColorNames(own)
}

// colorNamesPath is where the colour names persist, beside the track list.
func (l *Library) colorNamesPath() string { return l.sidePath("colors.json") }

func (l *Library) saveColorNames(names map[uint8]string) {
path := l.colorNamesPath()
if path == "" {
return
}
data, err := json.Marshal(colorNamesFile{Names: names})
if err != nil {
dlog.Errorf("library: colour names: %v", err)
return
}
if err := fsutil.WriteFile(path, data, 0644); err != nil {
dlog.Errorf("library: write %s: %v", path, err)
}
}

func (l *Library) loadColorNames() {
path := l.colorNamesPath()
if path == "" {
return
}
data, err := os.ReadFile(path)
if err != nil {
return // none saved
}
var f colorNamesFile
if err := json.Unmarshal(data, &f); err != nil {
dlog.Errorf("library: parse %s: %v", path, err)
return
}
l.mu.Lock()
l.colorNames = maps.Clone(f.Names)
l.mu.Unlock()
}
36 changes: 36 additions & 0 deletions library/colornames_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// SPDX-License-Identifier: GPL-3.0-or-later

package library

import (
"path/filepath"
"testing"
)

// A library's own colour names replace the stock ones by colour ID, keep
// only real changes (a stock or empty name, or an ID outside 1-8, is
// dropped), move the library's revision, and survive a reload.
func TestColorNames(t *testing.T) {
dir := t.TempDir()
lib := New()
lib.SetDBPath(filepath.Join(dir, "library.json"))
if got := lib.ColorName(1); got != "Pink" {
t.Errorf("before: colour 1 %q, want Pink", got)
}
rev := lib.Rev()
lib.SetColorNames(map[uint8]string{1: "Fixture Magenta", 2: "Red", 3: "", 8: "Fixture Violet", 9: "Nine"})
if lib.Rev() == rev {
t.Errorf("the revision didn't move")
}
for id, want := range map[uint8]string{1: "Fixture Magenta", 2: "Red", 3: "Orange", 8: "Fixture Violet", 9: ""} {
if got := lib.ColorName(id); got != want {
t.Errorf("colour %d %q, want %q", id, got, want)
}
}

again := New()
again.SetDBPath(filepath.Join(dir, "library.json"))
if got := again.ColorName(8); got != "Fixture Violet" {
t.Errorf("after a reload: colour 8 %q, want Fixture Violet", got)
}
}
3 changes: 3 additions & 0 deletions library/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ type ImportBundle struct {
// Categories is the library's root menu (rekordbox master.db's
// djmdCategory), in menu order; empty when the format has none.
Categories []CategoryImport
// ColorNames are the library's names for its track colours by colour ID
// (rekordbox master.db's djmdColor); empty when the format has none.
ColorNames map[uint8]string
// ShareRoot is the share/ root for ANLZ + artwork (a backup-zip extract, or
// a master.db's own library folder). SettingsDir holds the *SETTING.DAT
// blobs. Both are empty when the format carries no such assets.
Expand Down
22 changes: 21 additions & 1 deletion library/import_masterdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,25 @@ type MasterDBDump struct {
Sorts []masterDBSort `json:"sorts"`
// Categories are the deck's root menu (djmdCategory).
Categories []masterDBCategory `json:"categories"`
// ColorNames are the track colours' names (djmdColor).
ColorNames []masterDBColorName `json:"color_names"`
}

// masterDBColorName is one djmdColor row: a track colour's ID and name.
type masterDBColorName struct {
ID string `json:"id"`
Name string `json:"name"`
}

// importColorNames maps a library's colour rows to their names by colour ID.
func importColorNames(rows []masterDBColorName) map[uint8]string {
names := make(map[uint8]string, len(rows))
for _, r := range rows {
if id := parseRBID(r.ID); id >= 1 && id <= 8 {
names[uint8(id)] = r.Name
}
}
return names
}

// masterDBHotCueBank is one djmdHotCueBanklist node; top-level nodes have
Expand Down Expand Up @@ -422,7 +441,8 @@ func ImportMasterDBDump(lib *Library, dump *MasterDBDump) (*ImportBundle, error)

lib.FinalizeBulk()
return &ImportBundle{Result: res, Playlists: playlists, Tags: tags, TagCategories: tagCategories, Colors: colors, Assets: assets, Cues: cues,
Sorts: importSorts(dump.Sorts), Categories: importCategories(dump.Categories)}, nil
Sorts: importSorts(dump.Sorts), Categories: importCategories(dump.Categories),
ColorNames: importColorNames(dump.ColorNames)}, nil
}

// masterDBPlaylistTree turns the flat djmdPlaylist rows into a nested
Expand Down
5 changes: 5 additions & 0 deletions library/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ type Library struct {
tagged map[uint32]struct{}
tagListMu sync.Mutex

// colorNames are the library's own names for its track colours, the
// ones that differ from the stock names (colornames.go).
colorNames map[uint8]string

// hotCueBanks holds rekordbox Hot Cue Banks from a master.db import,
// persisted beside the track list (hotcuebank.go).
hotCueBanks *hotCueBankSet
Expand Down Expand Up @@ -417,6 +421,7 @@ func (l *Library) SetDBPath(path string) {
l.loadFromDisk()
l.loadHotCueBanks()
l.loadTagList()
l.loadColorNames()
}

// IncrementPlayCount bumps the play count for trackID by 1 and persists the
Expand Down
1 change: 1 addition & 0 deletions library/overlay.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ func (l *Library) UseOverlay(path, musicDir string) {
}
l.mu.Unlock()
l.loadTagList()
l.loadColorNames()
}

// writeOverlay saves the library as an overlay over its music dir.
Expand Down
Loading