Tracklist API
import { applyEdits, parseTracklist } from "trackparse/tracklist";// CommonJS: const { applyEdits, parseTracklist } = require("trackparse/tracklist");from trackparse.tracklist import apply_edits, parse_tracklistuse trackparse::tracklist::{apply_edits, parse_tracklist, Edit};import "github.com/4matic/trackparse/go"
func ParseTracklist(text string) Tracklistfunc ParseTracklistWith(text string, options TracklistOptions) Tracklistfunc ApplyEdits(text string, edits []Edit) EditResultfunc ApplyEditsWith(text string, edits []Edit, options TracklistOptions) EditResult#include <trackparse.h>
trackparse_tracklist *trackparse_parse_tracklist(const char *s, size_t len);trackparse_tracklist *trackparse_parse_tracklist_with(const char *s, size_t len, const trackparse_tracklist_options *o);void trackparse_tracklist_free(trackparse_tracklist *t);trackparse_edit_result *trackparse_apply_edits(const char *s, size_t len, const trackparse_edits *e);void trackparse_edit_result_free(trackparse_edit_result *r);Neither function fails on string input. What to know for each language:
- JavaScript (
trackparseon npm). The functions live in the entrytrackparse/tracklist, so code that only usesparsenever loads them. Results are plain objects, andJSON.stringifygives the spec's JSON. - Python (
trackparseon PyPI). Results are frozen dataclasses with snake_case attributes (spec_version,first_time) and ato_dict()that returns the spec's JSON shape. Edits are plain dicts with the same keys as in JavaScript,"from"included. Atextthat isn't astrraisesTypeError. - Rust (
trackparse::trackliston docs.rs). Results are structs with public snake_case fields andto_json(). Edits are values of theEditenum (listed under Types). Options go through the_withfunctions, and nothing panics. - Go (pkg.go.dev). The functions are in the package
trackparseitself, with no separate import. Results are structs with exported fields (SpecVersion,FirstTime) whoseMarshalJSONwrites the spec's JSON. Edits areEditstructs made with a constructor per edit (listed under Types). Options go through theWithfunctions, and nothing panics. - C (
trackparse.h). The functions are in the same header astrackparse_parse. Text is UTF-8 with a byte length (orTRACKPARSE_NUL_TERMINATED), and results are read-only structs that you free withtrackparse_tracklist_freeandtrackparse_edit_result_free. Edits go in atrackparse_editshandle, filled by one function per edit (listed under Types).trackparse_apply_editstakes no options, since edits don't depend on them. Every function returnsNULLonly when memory runs out.
The guides Tracklists and Editing tracklists walk through examples. The rules are R13 in the spec.
parseTracklist(text, options?)
Section titled “parseTracklist(text, options?)”(Python: parse_tracklist. Rust: parse_tracklist(text) and
parse_tracklist_with(text, &options).
Go: ParseTracklist(text) and ParseTracklistWith(text, options).
C: trackparse_parse_tracklist(s, len) and trackparse_parse_tracklist_with(s, len, options))
Returns a Tracklist (Tracklist in Rust, Tracklist in Go, a trackparse_tracklist * in C). Every key is
always present (R13.8). The playground shows every entry of a list as a
table, with its JSON.
{ specVersion: string; entries: TracklistEntry[]; warnings: TracklistWarning[]; stats: { lines, tracks, markers, timed, firstTime, lastTime };}Tracklist( spec_version: str, entries: tuple[TracklistEntry, ...], warnings: tuple[TracklistWarning, ...], stats: Stats, # lines, tracks, markers, timed, first_time, last_time)pub struct Tracklist { pub spec_version: String, pub entries: Vec<TracklistEntry>, pub warnings: Vec<TracklistWarning>, pub stats: Stats, // lines, tracks, markers, timed, first_time, last_time}type Tracklist struct { SpecVersion string Entries []Entry Warnings []TracklistWarning Stats Stats // Lines, Tracks, Markers, Timed, FirstTime, LastTime}typedef struct trackparse_tracklist { trackparse_str spec_version; const trackparse_entry *entries; size_t entries_count; const trackparse_tracklist_warning *warnings; size_t warnings_count; trackparse_stats stats; /* lines, tracks, markers, timed, first_time, last_time */} trackparse_tracklist;TracklistEntry
Section titled “TracklistEntry”One per line, in order. The table uses the JavaScript and JSON types. Python has None for
null. In Rust (TracklistEntry), line is a usize, kind and eol are the
enums EntryKind and Eol (as_str() gives the strings below), time is
an Option<TimeValue> (a TimeValue) whose seconds is an f64, and track is an
Option<ParsedTrack>. Go calls the type Entry: Kind and Eol are string types
(EntryKind, Eol) whose values are the strings below, Time is a
*TracklistTime whose Seconds is a float64, and Track is a *Track,
nil where the table says null. C calls it trackparse_entry: line is an int, kind and
eol are the enums trackparse_entry_kind and trackparse_eol (their _name() functions give
the strings below), time is a const trackparse_tracklist_time * whose seconds is a
double, and track is a const trackparse_track *, NULL where the table says null.
| Field | Type | Description |
|---|---|---|
line |
number |
1-based |
kind |
"track" | "marker" | "header" | "link" | "note" | "blank" |
See line kinds (R13.3) |
eol |
"\n" | "\r\n" | "\r" | "" |
The line's own terminator. "" on a last line without one (R13.1) |
raw |
string |
The line exactly as written, never normalized |
parts |
{ lead, time, gap, rest } |
raw cut into four strings that join back into it (R13.1.3) |
time |
{ raw, seconds } | null |
The start time. See Times (R13.2) |
track |
ParsedTrack | null |
Parsed from rest in tracklist mode, without its Markdown emphasis, a leading w/ and a bare track number (R13.4). Only track and marker lines have one |
layered |
boolean |
The track text (after any time) starts with w/ and a space: a layered track, played over the previous one (R13.13) |
TracklistWarning
Section titled “TracklistWarning”{ code: TracklistWarningCode; line: number; severity: "info" | "warning"; suggestion: { line, raw, seconds } | null }TracklistWarning(code: TracklistWarningCode, line: int, severity: Severity, suggestion: Optional[Suggestion])# Suggestion(line: int, raw: str, seconds: float)pub struct TracklistWarning { pub code: WarningCode, pub line: usize, pub severity: Severity, // Info | Warning pub suggestion: Option<Suggestion>, // Suggestion { line: usize, raw: String, seconds: f64 }}type TracklistWarning struct { Code WarningCode Line int Severity Severity // SeverityInfo | SeverityWarning Suggestion *Suggestion // Suggestion{Line int, Raw string, Seconds float64}}typedef struct trackparse_tracklist_warning { trackparse_tracklist_warning_code code; int line; trackparse_severity severity; /* TRACKPARSE_SEVERITY_INFO | _WARNING */ const trackparse_suggestion *suggestion; /* { int line; trackparse_str raw; double seconds; } */} trackparse_tracklist_warning;Sorted by line (R13.7). The codes are listed under Warnings,
and Suggestions explains suggestion. In Rust the type
is TracklistWarning with a WarningCode, in Go
TracklistWarning with a WarningCode, in C trackparse_tracklist_warning
with a trackparse_tracklist_warning_code (TRACKPARSE_TL_TIME_OUT_OF_ORDER and so on).
Warnings from each line's own ParsedTrack stay on entries[i].track.warnings and aren't repeated
here.
| Field | |
|---|---|
lines |
number of entries |
tracks, markers |
entries of those kinds |
timed |
entries with a time |
firstTime, lastTime |
seconds of the first and last time, or null |
Python and Rust call the last two first_time and last_time. In Rust the counts are usize and
the two times are Option<f64> (Stats). In Go (Stats) the fields are
FirstTime and LastTime, of type *float64. In C (trackparse_stats) the counts are int and
first_time and last_time are const double *, NULL when there is no time.
applyEdits(text, edits, options?)
Section titled “applyEdits(text, edits, options?)”(Python: apply_edits. Rust: apply_edits(text, &edits) and
apply_edits_with(text, &edits, &options).
Go: ApplyEdits(text, edits) and ApplyEditsWith(text, edits, options).
C: trackparse_apply_edits(s, len, edits))
Returns { text, changed, warnings } (Python, Rust and Go: an EditResult; C: a
trackparse_edit_result *).
| Field | |
|---|---|
text |
the edited text |
changed |
final line numbers of every line an edit wrote, ascending |
warnings |
{ code, edit, line }[]: edit is the index in edits, line may be null |
In Rust (EditResult), changed is a Vec<usize> and each warning is an
EditWarning { code: EditWarningCode, edit: usize, line: Option<usize> }
(EditWarning). In Go (EditResult), Changed is an []int and each warning
is an EditWarning{Code EditWarningCode, Edit int, Line *int} (EditWarning). In C, changed is a
const long long * with changed_count, and each warning is a trackparse_edit_warning with a
code, a size_t edit and a const long long *line. The edit rules
are R13.9.
| JavaScript and Python | Effect |
|---|---|
{ op: "set-time", line, seconds } |
Rewrite the time on that line |
{ op: "shift", seconds } |
Add seconds to every timed line |
{ op: "shift", seconds, from } |
Add seconds to every timed line from from on |
{ op: "shift", seconds, lines } |
Add seconds to the listed lines |
{ op: "sort-by-time" } |
Stable sort by time. Untimed lines move with the timed line above them |
{ op: "set-rest", line, rest } |
Replace everything after the time |
{ op: "insert", after, text } |
Insert a line after after (0 = at the top) |
{ op: "delete", line } |
Remove a line |
Pass the objects in the table as an array: applyEdits(text, [{ op: "shift", seconds: 45, from: 5 }]).
An edit is a dict with the same keys, quoted: {"op": "shift", "seconds": 45, "from": 5}.
Each edit has a constructor on Edit; pass a slice of them.
| Edit | Rust |
|---|---|
set-time |
Edit::set_time(line, seconds) |
shift |
Edit::shift(seconds) |
shift with from |
Edit::shift_from(seconds, from) |
shift with lines |
Edit::shift_lines(seconds, lines) |
sort-by-time |
Edit::sort_by_time() |
set-rest |
Edit::set_rest(line, rest) |
insert |
Edit::insert(after, text) |
delete |
Edit::delete(line) |
Each edit has a constructor that returns an Edit; pass a []trackparse.Edit.
| Edit | Go |
|---|---|
set-time |
SetTime(line, seconds) |
shift |
Shift(seconds) |
shift with from |
ShiftFrom(seconds, from) |
shift with lines |
ShiftLines(seconds, lines...) |
sort-by-time |
SortByTime() |
set-rest |
SetRest(line, rest) |
insert |
Insert(after, text) |
delete |
Delete(line) |
Each edit is a function that appends it to a trackparse_edits list from trackparse_edits_new(). It returns false only when memory runs out.
| Edit | C |
|---|---|
set-time |
trackparse_edits_set_time(e, line, seconds) |
shift |
trackparse_edits_shift(e, seconds) |
shift with from |
trackparse_edits_shift_from(e, seconds, from) |
shift with lines |
trackparse_edits_shift_lines(e, seconds, lines, count) |
sort-by-time |
trackparse_edits_sort_by_time(e) |
set-rest |
trackparse_edits_set_rest(e, line, rest, len) |
insert |
trackparse_edits_insert(e, after, text, len) |
delete |
trackparse_edits_delete(e, line) |
With the serde feature, the Rust Edit reads and writes the JSON of the JavaScript objects. The Go Edit writes it with
json.Marshal.
Edits run in sequence, each on the result of the one before. Times are written in their line's style (R13.9.2), up to 100 hours (or 999 minutes for lists that never use hours). Editing tracklists has examples of each edit.
Edit warnings
Section titled “Edit warnings”| Code | When |
|---|---|
style-upgraded |
a time without hours passed one hour and gained them |
time-clamped |
a time would go below zero and was set to zero |
edit-invalid |
the edit could not apply and changed nothing |
In Rust the codes are EditWarningCode::StyleUpgraded, TimeClamped and
EditInvalid. In Go they are EditWarningCodeStyleUpgraded, EditWarningCodeTimeClamped and
EditWarningCodeEditInvalid. In C they are TRACKPARSE_EDIT_WARNING_STYLE_UPGRADED,
TRACKPARSE_EDIT_WARNING_TIME_CLAMPED and TRACKPARSE_EDIT_WARNING_EDIT_INVALID.
Warnings shows how to check them.
Options
Section titled “Options”Both functions take the same options (R13.10). applyEdits accepts them for symmetry; edits don't depend on
them, and the C trackparse_apply_edits has no options argument.
| JavaScript | Python and Rust Go |
Default | Description |
|---|---|---|---|
mode |
modeMode |
"tracklist" |
Mode for each line's parse(). See Tracklist mode |
knownArtists |
known_artistsKnownArtists |
empty | As for parse() |
splitAnd |
split_andSplitAnd |
"auto" |
As for parse() |
keywords |
keywordsKeywords |
none | As for parse() |
audioSeconds |
audio_secondsAudioSeconds |
none | Length of the audio. Enables time-after-audio-end |
markerWords |
marker_wordsMarkerWords |
empty | Extra marker words |
The C setters are named after the option, after trackparse_tracklist_options_: set_mode,
set_split_and and set_audio_seconds take a value, and the list options take one entry per call,
named in the singular: add_known_artist and add_marker_word. keywords is five setters, as on
the parse options: add_version_head, add_descriptor,
add_genre, add_junk and add_feat_marker.
Python uses keyword arguments. Rust has a setter of the same name for each on TracklistOptions,
for example TracklistOptions::new().audio_seconds(3600.0).marker_words(["drop"]), and passes it
to parse_tracklist_with or apply_edits_with. Go takes a TracklistOptions struct, for example
trackparse.TracklistOptions{AudioSeconds: trackparse.Ptr(3600.0), MarkerWords: []string{"drop"}},
in ParseTracklistWith or ApplyEditsWith. Its zero value is the defaults: an empty Mode means
ModeTracklist (ModeAuto asks for auto), and a nil AudioSeconds means no length, while
trackparse.Ptr(0.0) is an audio of length 0. C takes a trackparse_tracklist_options handle
from trackparse_tracklist_options_new() in trackparse_parse_tracklist_with; an option never set
keeps its default, so an audio length never set is none and
trackparse_tracklist_options_set_audio_seconds(o, 0) is an audio of length 0.
Tracklists has a full example.
Exported from trackparse/tracklist: Tracklist, TracklistEntry, EntryKind, EntryParts,
Eol, TracklistTime, TimeSuggestion, TracklistStats, TracklistWarning,
TracklistWarningCode, TracklistOptions, Edit, EditResult, EditWarning,
EditWarningCode.
Exported from trackparse.tracklist: the dataclasses Tracklist, TracklistEntry, Parts,
TimeValue, Suggestion, Stats, TracklistWarning, EditResult, EditWarning, and the
Literal aliases EntryKind, Eol, Severity, TracklistWarningCode, EditWarningCode.
Exported from trackparse::tracklist: the structs Tracklist, TracklistEntry, Parts,
TimeValue, Suggestion, Stats, TracklistWarning, TracklistOptions, EditResult and
EditWarning, the enums EntryKind, Eol, Severity, WarningCode and EditWarningCode, and
the edits (Edit on docs.rs):
#[non_exhaustive]pub enum Edit { SetTime { line: usize, seconds: f64 }, Shift { seconds: f64, lines: Option<Vec<usize>>, from: Option<usize> }, SortByTime, SetRest { line: usize, rest: String }, Insert { after: usize, text: String }, Delete { line: usize },}Each variant has a constructor: Edit::set_time(line, seconds), Edit::shift(seconds),
Edit::shift_from(seconds, from), Edit::shift_lines(seconds, lines), Edit::sort_by_time(),
Edit::set_rest(line, rest), Edit::insert(after, text) and Edit::delete(line).
Everything is in the package trackparse: the structs Tracklist, Entry, Parts,
TracklistTime, Suggestion, Stats, TracklistWarning, TracklistOptions, EditResult,
EditWarning and Edit, and the string types EntryKind, Eol, Severity, WarningCode,
EditWarningCode and EditOp, each with its constants (EntryKindTrack, EolCRLF,
WarningCodeTimeOutOfOrder). An edit is a struct (Edit) whose Op says which fields it reads:
type Edit struct { Op EditOp // EditOpSetTime, EditOpShift, EditOpSortByTime, EditOpSetRest, EditOpInsert, EditOpDelete Line int // set-time, set-rest, delete Seconds float64 // set-time, shift Lines []int // shift: nil means not given From *int // shift: nil means not given Rest string // set-rest After int // insert Text string // insert}Build one with a constructor: trackparse.SetTime(line, seconds), trackparse.Shift(seconds),
trackparse.ShiftFrom(seconds, from), trackparse.ShiftLines(seconds, lines...),
trackparse.SortByTime(), trackparse.SetRest(line, rest), trackparse.Insert(after, text) and
trackparse.Delete(line). json.Marshal writes an edit in the spec's JSON form.
Everything is in trackparse.h: the read-only structs trackparse_tracklist,
trackparse_entry, trackparse_parts, trackparse_tracklist_time, trackparse_suggestion,
trackparse_stats, trackparse_tracklist_warning, trackparse_edit_result and
trackparse_edit_warning; the enums trackparse_entry_kind, trackparse_eol,
trackparse_severity, trackparse_tracklist_warning_code, trackparse_edit_op and
trackparse_edit_warning_code, each with a _name() function (trackparse_eol_name gives
"\r\n" and so on); and two opaque handles, trackparse_tracklist_options and
trackparse_edits. An edit list is a handle filled by one function per edit, in the order the
edits run:
trackparse_edits *trackparse_edits_new(void);void trackparse_edits_free(trackparse_edits *e);bool trackparse_edits_set_time(trackparse_edits *e, long long line, double seconds);bool trackparse_edits_shift(trackparse_edits *e, double seconds);bool trackparse_edits_shift_from(trackparse_edits *e, double seconds, long long from);bool trackparse_edits_shift_lines(trackparse_edits *e, double seconds, const long long *lines, size_t count);bool trackparse_edits_sort_by_time(trackparse_edits *e);bool trackparse_edits_set_rest(trackparse_edits *e, long long line, const char *rest, size_t len);bool trackparse_edits_insert(trackparse_edits *e, long long after, const char *text, size_t len);bool trackparse_edits_delete(trackparse_edits *e, long long line);Each returns false only when memory runs out. The strings are copied, so they can be freed
after the call. trackparse_edit_result_to_json writes a result in the spec's JSON form.
The JSON Schema is
tracklist.schema.json.
The Rust items are also on
docs.rs, and the Go items on
pkg.go.dev.
See also
Section titled “See also”- Tracklists and Editing tracklists: the guides.
- Warnings: every code, and suggestions.
parseandParsedTrack: the type of each entry'strack, and serializing to JSON.- Options: the options shared with
parse. - Specification, R13: the exact tracklist rules.