Modes
The same string can mean different things depending on where it came from.
Blink-182-All The Small Things is clearly artist and title on YouTube. In a music library, a
tight dash like that is more likely part of a name. Modes tell trackparse how much guessing the
source can take.
| Mode | For | What it adds |
|---|---|---|
clean |
store metadata, ID3 tags | Only a spaced dash separates artist and title |
youtube |
video titles, scrobbles | Pipe segments, "Title" by Artist, Artist: "Title", unspaced dashes, trailing junk, the uploader fallback |
filename |
files on disk | Strips the extension, reads _ as a space, accepts 01-Track numbering |
tracklist |
one line of a tracklist | The first spaced dash always separates, and the words after it are never read as a version or genre suffix |
auto |
anything (the default) | Picks filename, youtube or clean from the string. It never picks tracklist |
The resolved mode is in track.mode, so you can always see which one ran. You pick one with the
mode option. In Rust the option is the enum
ModeOption and the resolved mode is Mode,
which has no Auto. In Go both are the string type Mode:
trackparse.ModeAuto ("auto") asks for auto, an empty Mode in ParseOptions
means auto too, and Track.Mode is never ModeAuto. In C both are the enum trackparse_mode:
TRACKPARSE_MODE_AUTO is what a new options handle uses until trackparse_parse_options_set_mode
changes it, and a result's
mode is never TRACKPARSE_MODE_AUTO; trackparse_mode_name gives the name. The mode rules are
R2 in the spec.
How auto decides
Section titled “How auto decides”- A known audio or video extension at the end (
.mp3,.flac,.wav,.mp4…) meansfilename. - Junk (a genre bracket doesn't count), a
|, or a platform suffix like- YouTubemeansyoutube. - Anything else is
clean.
That order is R2.1. Open the file name or the video title in the playground to see the mode it picks.
import { parse } from "trackparse";
const file = parse("07_Burial_-_Archangel.mp3");[file.mode, file.position, file.artists[0].name, file.title];// [ 'filename', { raw: '07', number: 7 }, 'Burial', 'Archangel' ]
const video = parse("Noisia - Stigma | Official Video");[video.mode, video.title, video.junk];// [ 'youtube', 'Stigma', [ { raw: 'Official Video', kind: 'video' } ] ]from trackparse import parse
file = parse("07_Burial_-_Archangel.mp3")(file.mode, file.position, file.artists[0].name, file.title)# ('filename', Position(raw='07', number=7), 'Burial', 'Archangel')
video = parse("Noisia - Stigma | Official Video")(video.mode, video.title, video.junk)# ('youtube', 'Stigma', (Junk(raw='Official Video', kind='video'),))use trackparse::parse;
let file = parse("07_Burial_-_Archangel.mp3");(file.mode, &file.position, &file.artists[0].name, &file.title);// (Filename, Some(Position { raw: "07", number: 7 }), "Burial", "Archangel")
let video = parse("Noisia - Stigma | Official Video");(video.mode, &video.title, &video.junk);// (Youtube, "Stigma", [Junk { raw: "Official Video", kind: Video }])import ( "fmt"
"github.com/4matic/trackparse/go")
t := trackparse.Parse("07_Burial_-_Archangel.mp3")fmt.Println(t.Mode, t.Position.Number, t.Artists[0].Name, "/", t.Title)// filename 7 Burial / Archangel
t = trackparse.Parse("Noisia - Stigma | Official Video")fmt.Printf("%s %q %+v\n", t.Mode, t.Title, t.Junk)// youtube "Stigma" [{Raw:Official Video Kind:video}]#include <trackparse.h>
trackparse_track *t = trackparse_parse("07_Burial_-_Archangel.mp3", TRACKPARSE_NUL_TERMINATED);printf("%s %d %s / %s\n", trackparse_mode_name(t->mode), t->position->number, t->artists[0].name.ptr, t->title.ptr);// filename 7 Burial / Archangeltrackparse_track_free(t);
t = trackparse_parse("Noisia - Stigma | Official Video", TRACKPARSE_NUL_TERMINATED);printf("%s %s %s: %s\n", trackparse_mode_name(t->mode), t->title.ptr, t->junk[0].kind_name.ptr, t->junk[0].raw.ptr);// youtube Stigma video: Official Videotrackparse_track_free(t);youtube mode
Section titled “youtube mode”Set it yourself when you know the strings are video titles. It takes guesses that clean
won't, and every guess shows up in warnings. The
three below are R6.6,
R6.5 and R6.7; pipes are
R2.4:
const show = (t) => [t.artists.map((a) => a.name), t.title, t.warnings];
show(parse('"Numb" by Linkin Park', { mode: "youtube" }));// [ [ 'Linkin Park' ], 'Numb', [ 'bySplit' ] ]
show(parse('Linkin Park: "Numb"', { mode: "youtube" }));// [ [ 'Linkin Park' ], 'Numb', [ 'quotedTitleSplit' ] ]
show(parse("Blink-182-All The Small Things", { mode: "youtube" }));// [ [ 'Blink-182' ], 'All The Small Things', [ 'unspacedDashSplit' ] ]def show(t): return [a.name for a in t.artists], t.title, t.warnings
show(parse('"Numb" by Linkin Park', mode="youtube"))# (['Linkin Park'], 'Numb', ('bySplit',))
show(parse('Linkin Park: "Numb"', mode="youtube"))# (['Linkin Park'], 'Numb', ('quotedTitleSplit',))
show(parse("Blink-182-All The Small Things", mode="youtube"))# (['Blink-182'], 'All The Small Things', ('unspacedDashSplit',))use trackparse::{parse_with, ModeOption, ParseOptions, ParsedTrack};
let show = |t: ParsedTrack| (t.artists.into_iter().map(|a| a.name).collect::<Vec<_>>(), t.title, t.warnings);let youtube = ParseOptions::new().mode(ModeOption::Youtube);
show(parse_with("\"Numb\" by Linkin Park", &youtube));// (["Linkin Park"], "Numb", [BySplit])
show(parse_with("Linkin Park: \"Numb\"", &youtube));// (["Linkin Park"], "Numb", [QuotedTitleSplit])
show(parse_with("Blink-182-All The Small Things", &youtube));// (["Blink-182"], "All The Small Things", [UnspacedDashSplit])youtube := trackparse.ParseOptions{Mode: trackparse.ModeYouTube}
t = trackparse.ParseWith(`"Numb" by Linkin Park`, youtube)fmt.Printf("%q %q %v\n", t.Artists[0].Name, t.Title, t.Warnings)// "Linkin Park" "Numb" [bySplit]
t = trackparse.ParseWith(`Linkin Park: "Numb"`, youtube)fmt.Printf("%q %q %v\n", t.Artists[0].Name, t.Title, t.Warnings)// "Linkin Park" "Numb" [quotedTitleSplit]
t = trackparse.ParseWith("Blink-182-All The Small Things", youtube)fmt.Printf("%q %q %v\n", t.Artists[0].Name, t.Title, t.Warnings)// "Blink-182" "All The Small Things" [unspacedDashSplit]trackparse_parse_options *youtube = trackparse_parse_options_new();trackparse_parse_options_set_mode(youtube, TRACKPARSE_MODE_YOUTUBE);
t = trackparse_parse_with("\"Numb\" by Linkin Park", TRACKPARSE_NUL_TERMINATED, youtube);printf("%s / %s / %s\n", t->artists[0].name.ptr, t->title.ptr, trackparse_warning_name(t->warnings[0]));// Linkin Park / Numb / bySplittrackparse_track_free(t);
t = trackparse_parse_with("Linkin Park: \"Numb\"", TRACKPARSE_NUL_TERMINATED, youtube);printf("%s / %s / %s\n", t->artists[0].name.ptr, t->title.ptr, trackparse_warning_name(t->warnings[0]));// Linkin Park / Numb / quotedTitleSplittrackparse_track_free(t);
t = trackparse_parse_with("Blink-182-All The Small Things", TRACKPARSE_NUL_TERMINATED, youtube);printf("%s / %s / %s\n", t->artists[0].name.ptr, t->title.ptr, trackparse_warning_name(t->warnings[0]));// Blink-182 / All The Small Things / unspacedDashSplittrackparse_track_free(t);In clean mode the last one comes back with no artist, the whole string as the title and a
noSeparator warning. That's the safe answer when you can't be sure. Compare the two in the
playground:
youtube and
clean.
The uploader fallback
Section titled “The uploader fallback”Many music videos are titled with only the song name, and the artist is the channel. Pass the
channel as uploader and youtube mode uses it when there's no separator. Auto-generated suffixes
like - Topic and VEVO are dropped (R6.8;
try it):
show(parse("Around the World", { mode: "youtube", uploader: "Daft Punk - Topic" }));// [ [ 'Daft Punk' ], 'Around the World', [] ]show(parse("Around the World", mode="youtube", uploader="Daft Punk - Topic"))# (['Daft Punk'], 'Around the World', ())let options = ParseOptions::new().mode(ModeOption::Youtube).uploader("Daft Punk - Topic");show(parse_with("Around the World", &options));// (["Daft Punk"], "Around the World", [])options := trackparse.ParseOptions{Mode: trackparse.ModeYouTube, Uploader: "Daft Punk - Topic"}t = trackparse.ParseWith("Around the World", options)fmt.Printf("%q %q\n", t.Artists[0].Name, t.Title)// "Daft Punk" "Around the World"trackparse_parse_options_set_uploader(youtube, "Daft Punk - Topic", TRACKPARSE_NUL_TERMINATED);t = trackparse_parse_with("Around the World", TRACKPARSE_NUL_TERMINATED, youtube);printf("%s / %s / %zu warnings\n", t->artists[0].name.ptr, t->title.ptr, t->warnings_count);// Daft Punk / Around the World / 0 warningstrackparse_track_free(t);trackparse_parse_options_free(youtube);filename mode
Section titled “filename mode”The extension comes off first, and a name without spaces gets its underscores turned into spaces
(R2.3). A leading number then
counts as a track number in more forms than elsewhere, such as 311 - and 01-Track (R4.2). Try
the first line in the playground.
parse("02_Aphex_Twin_-_Xtal.flac").artists[0].name; // 'Aphex Twin'parse("311 - Amber.mp3").position; // { raw: '311', number: 311 }parse("311 - Amber").artists[0].name; // '311'parse("02_Aphex_Twin_-_Xtal.flac").artists[0].name # 'Aphex Twin'parse("311 - Amber.mp3").position # Position(raw='311', number=311)parse("311 - Amber").artists[0].name # '311'&parse("02_Aphex_Twin_-_Xtal.flac").artists[0].name; // "Aphex Twin"parse("311 - Amber.mp3").position; // Some(Position { raw: "311", number: 311 })&parse("311 - Amber").artists[0].name; // "311"fmt.Printf("%q\n", trackparse.Parse("02_Aphex_Twin_-_Xtal.flac").Artists[0].Name) // "Aphex Twin"fmt.Printf("%+v\n", *trackparse.Parse("311 - Amber.mp3").Position) // {Raw:311 Number:311}fmt.Printf("%q\n", trackparse.Parse("311 - Amber").Artists[0].Name) // "311"t = trackparse_parse("02_Aphex_Twin_-_Xtal.flac", TRACKPARSE_NUL_TERMINATED);printf("%s\n", t->artists[0].name.ptr); // Aphex Twintrackparse_track_free(t);
t = trackparse_parse("311 - Amber.mp3", TRACKPARSE_NUL_TERMINATED);printf("%s %d\n", t->position->raw.ptr, t->position->number); // 311 311trackparse_track_free(t);
t = trackparse_parse("311 - Amber", TRACKPARSE_NUL_TERMINATED);printf("%s\n", t->artists[0].name.ptr); // 311trackparse_track_free(t);The 311 lines show why the mode matters. In a file name a leading number with a dash is a track
number. Anywhere else, 311 could be a band.
tracklist mode
Section titled “tracklist mode”Inside a tracklist almost every line is Artist - Title, so the first spaced dash can be trusted
(R6.10, R13.6).
A single string doesn't get that benefit of the doubt:
const plain = parse("Electrosoul System - Liquid Metal");[plain.artists, plain.title, plain.junk];// [ [], 'Electrosoul System', [ { raw: 'Liquid Metal', kind: 'genre' } ] ]
show(parse("Electrosoul System - Liquid Metal", { mode: "tracklist" }));// [ [ 'Electrosoul System' ], 'Liquid Metal', [] ]plain = parse("Electrosoul System - Liquid Metal")(plain.artists, plain.title, plain.junk)# ((), 'Electrosoul System', (Junk(raw='Liquid Metal', kind='genre'),))
show(parse("Electrosoul System - Liquid Metal", mode="tracklist"))# (['Electrosoul System'], 'Liquid Metal', ())let plain = parse("Electrosoul System - Liquid Metal");(&plain.artists, &plain.title, &plain.junk);// ([], "Electrosoul System", [Junk { raw: "Liquid Metal", kind: Genre }])
show(parse_with("Electrosoul System - Liquid Metal", &ParseOptions::new().mode(ModeOption::Tracklist)));// (["Electrosoul System"], "Liquid Metal", [])t = trackparse.Parse("Electrosoul System - Liquid Metal")fmt.Printf("%q %+v\n", t.Title, t.Junk)// "Electrosoul System" [{Raw:Liquid Metal Kind:genre}]
t = trackparse.ParseWith("Electrosoul System - Liquid Metal", trackparse.ParseOptions{Mode: trackparse.ModeTracklist})fmt.Printf("%q %q\n", t.Artists[0].Name, t.Title)// "Electrosoul System" "Liquid Metal"t = trackparse_parse("Electrosoul System - Liquid Metal", TRACKPARSE_NUL_TERMINATED);printf("%zu artists / %s / %s: %s\n", t->artists_count, t->title.ptr, t->junk[0].kind_name.ptr, t->junk[0].raw.ptr);// 0 artists / Electrosoul System / genre: Liquid Metaltrackparse_track_free(t);
trackparse_parse_options *tracklist = trackparse_parse_options_new();trackparse_parse_options_set_mode(tracklist, TRACKPARSE_MODE_TRACKLIST);t = trackparse_parse_with("Electrosoul System - Liquid Metal", TRACKPARSE_NUL_TERMINATED, tracklist);printf("%s / %s / %zu junk\n", t->artists[0].name.ptr, t->title.ptr, t->junk_count);// Electrosoul System / Liquid Metal / 0 junktrackparse_track_free(t);trackparse_parse_options_free(tracklist);parseTracklist (parse_tracklist in Python and Rust, ParseTracklist in Go, trackparse_parse_tracklist in C) uses this
mode for every line. You only need to set it yourself when you parse tracklist lines one at a time.
The playground can show this line in tracklist mode.
Tracklist mode in the tracklist guide has more
lines that need it.
See also
Section titled “See also”- Options for
mode,uploaderand the other options. - Warnings for what each guess reports.
- Specification, R2 for the exact mode rules.
- How it works for how the separator search differs between modes.