diff --git a/api/imports.go b/api/imports.go index 44f09fe..7ebda60 100644 --- a/api/imports.go +++ b/api/imports.go @@ -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) } diff --git a/api/tracks.go b/api/tracks.go index 4a8f1db..61882ba 100644 --- a/api/tracks.go +++ b/api/tracks.go @@ -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, diff --git a/cmd/conformance-server/main.go b/cmd/conformance-server/main.go index dcfe829..726bd34 100644 --- a/cmd/conformance-server/main.go +++ b/cmd/conformance-server/main.go @@ -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) diff --git a/dbserver/drilldown.go b/dbserver/drilldown.go index b81d646..0c0c2ab 100644 --- a/dbserver/drilldown.go +++ b/dbserver/drilldown.go @@ -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), }) } diff --git a/dbserver/menuitem.go b/dbserver/menuitem.go index adf408a..8c5f363 100644 --- a/dbserver/menuitem.go +++ b/dbserver/menuitem.go @@ -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 { @@ -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 diff --git a/dbserver/render_override.go b/dbserver/render_override.go index 0d380dd..cacff15 100644 --- a/dbserver/render_override.go +++ b/dbserver/render_override.go @@ -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 @@ -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 @@ -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 } } diff --git a/dbserver/render_override_test.go b/dbserver/render_override_test.go index 195c896..c2e4247 100644 --- a/dbserver/render_override_test.go +++ b/dbserver/render_override_test.go @@ -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) + } + } +} diff --git a/dbserver/songinfo_title.go b/dbserver/songinfo_title.go index 9948c1e..019ae3f 100644 --- a/dbserver/songinfo_title.go +++ b/dbserver/songinfo_title.go @@ -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}, }} diff --git a/dbserver/track.go b/dbserver/track.go index 2316d0d..74f346f 100644 --- a/dbserver/track.go +++ b/dbserver/track.go @@ -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 diff --git a/library/colornames.go b/library/colornames.go new file mode 100644 index 0000000..1c8411c --- /dev/null +++ b/library/colornames.go @@ -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() +} diff --git a/library/colornames_test.go b/library/colornames_test.go new file mode 100644 index 0000000..f3d5059 --- /dev/null +++ b/library/colornames_test.go @@ -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) + } +} diff --git a/library/import.go b/library/import.go index 91644e5..ffbfcc8 100644 --- a/library/import.go +++ b/library/import.go @@ -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. diff --git a/library/import_masterdb.go b/library/import_masterdb.go index b839ff9..92f4df6 100644 --- a/library/import_masterdb.go +++ b/library/import_masterdb.go @@ -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 @@ -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 diff --git a/library/index.go b/library/index.go index ae57a10..e1efc64 100644 --- a/library/index.go +++ b/library/index.go @@ -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 @@ -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 diff --git a/library/overlay.go b/library/overlay.go index 80f1077..f9bc8e0 100644 --- a/library/overlay.go +++ b/library/overlay.go @@ -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. diff --git a/library/taglist.go b/library/taglist.go index 0c7577d..39a2f88 100644 --- a/library/taglist.go +++ b/library/taglist.go @@ -109,16 +109,20 @@ func (l *Library) changeTagList(edit func(list []uint32) []uint32) bool { return true } -// tagListPath is where the tag list persists: beside the track list, or for -// a library built from a music dir, beside its overlay of edits. -func (l *Library) tagListPath() string { +// tagListPath is where the tag list persists, beside the track list. +func (l *Library) tagListPath() string { return l.sidePath("taglist.json") } + +// sidePath is where a file of library data persists: beside the track list, +// or for a library built from a music dir, beside its overlay of edits; "" +// for a library that isn't saved. +func (l *Library) sidePath(name string) string { l.mu.RLock() defer l.mu.RUnlock() switch { case l.dbPath != "": - return filepath.Join(filepath.Dir(l.dbPath), "taglist.json") + return filepath.Join(filepath.Dir(l.dbPath), name) case l.overlay != nil: - return filepath.Join(filepath.Dir(l.overlay.path), "taglist.json") + return filepath.Join(filepath.Dir(l.overlay.path), name) } return "" } diff --git a/tools/rekordbox_dump.py b/tools/rekordbox_dump.py index 361be68..d87b533 100644 --- a/tools/rekordbox_dump.py +++ b/tools/rekordbox_dump.py @@ -574,6 +574,15 @@ def main(): except sqlcipher3.dbapi2.OperationalError: pass # Older DBs may not have the djmdSort table + # The track colours' names (djmdColor's Commnt), which rekordbox lets the + # user change: one row per colour ID. + out["color_names"] = [] + try: + cur.execute("SELECT ID, Commnt FROM djmdColor WHERE COALESCE(rb_local_deleted, 0) = 0") + out["color_names"] = [{"id": str(i), "name": n or ""} for i, n in cur.fetchall()] + except sqlcipher3.dbapi2.OperationalError: + pass # Older DBs may not have the djmdColor table + # The deck's root menu (djmdCategory): one row per category, its # djmdMenuItems ID, Seq (menu order), and Disable (0 shown; MATCHING and # DATE ADDED use bit 1 for something else).