Skip to main content
Version: v3.0.0 (current)

LyricsUtils

For ordinary playback lookups, use Node.getLyrics or Player.fetchCurrentLyrics.

Class Definition​

export abstract class LyricsUtils {}

All members are static; the class cannot be instantiated. Import LyricsUtils from magmastream.

Methods​

normalize​

public static normalize(
raw: Lyrics | NodeLinkGetLyrics | null,
origin: "lavalink" | "nodelink"
): NormalizedLyrics | null;

Normalizes a raw Lyrics or NodeLinkGetLyrics response without making requests. Pass the backend as origin; NodeLink load-type responses are detected by loadType and use origin: "nodelink".

  • Lavalink (LavaLyrics): uses sourceName, provider and text. Uses plain text when no parsed line contains text.
  • NodeLink: accepts the lyrics load type (time, duration, text lines); sourceName is the provider, else the name, and the synced flag is the provider hint. The NodeLink 2.x lyricsSingle and lyricsMultiple load types were removed in 3.0.0.
  • Returns NormalizedLyrics or null for absent/no usable lyrics, including NodeLink empty/error load types. Unlike Node.getLyrics, which throws when every lookup failed, this utility treats a raw error load type as null.

Text is trimmed, verse breaks are retained, and non-finite raw timing becomes null. A result is synced only if the provider hint is not explicitly false, it has at least two lines, all timestamps are finite numbers, and some timestamp is greater than zero. A true hint is not enough. Synced lines are sorted; unsynced lines have both timing fields cleared to null. See synchronization.

fetchLrclib​

public static async fetchLrclib(
track: Pick<Track, "title" | "author" | "duration" | "isStream">,
options: { userAgent?: string; timeoutMs?: number } = {}
): Promise<NormalizedLyrics | null>;

Directly queries LRCLIB; calling this helper is itself an opt-in request, independent of lrclibFallback. Cleans title/artist first and returns null without a request if the title is empty. For non-stream tracks with positive duration, converts milliseconds to rounded seconds. When artist and duration are available, tries /get, then /search; an empty artist/title search can fall back to a combined text search.

Search candidates must contain synced or plain lyrics. Excludes known duration differences over 15 seconds; closest duration wins, with synced lyrics preferred when differences are within 2 seconds. Tries candidates until one yields usable lyrics. Parses synced LRC first, then plain lyrics when needed. Returns normalized lyrics with origin: "lrclib", sourceName: "LRCLIB", and provider: "LRCLIB", or null for no match. Network, timeout, HTTP, or JSON errors reject the promise; unlike Node.getLyrics, this helper does not apply node fallback/error handling.

OptionDefaultMeaning
userAgentmagmastream (https://gitryx.com/MagmaStream/magmastream)User-Agent sent to LRCLIB.
timeoutMs10000Timeout in milliseconds for each request, not the complete lookup.
Third-Party Metadata

This method sends cleaned title, artist, and available length to lrclib.net directly from the client. Node/player lookups use it only when lrclibFallback: true; fallback is disabled by default. No lyrics plugin is required for a direct LRCLIB request.

lrclibRequest​

public static async lrclibRequest<T>(
path: string,
params: Record<string, string | number>,
options: { userAgent?: string; timeoutMs?: number } = {}
): Promise<T | null>;

Low-level GET against https://lrclib.net/api plus path (for example /get or /search). Encodes params as query parameters and sends Accept: application/json. Uses the same userAgent and per-request timeoutMs defaults as fetchLrclib. This is a direct third-party request; supplied query parameters are disclosed to LRCLIB.

Returns parsed JSON cast to T (no runtime schema validation), or null on HTTP 404. Other unsuccessful HTTP statuses throw MagmaStreamError with REST_REQUEST_FAILED; network, timeout, and JSON parse errors propagate.

parseLrc​

public static parseLrc(text: string): NormalizedLyricsLine[];

Parses timestamped LRC text into NormalizedLyricsLine entries, sorted by timestamp in milliseconds, with duration: null. Accepts fractional timestamps such as [mm:ss.xx] (also [mm:ss:xx]), with or without a space before text, and multiple leading timestamps per line. Ignores metadata tags such as [ar:Artist] and lines without timestamps. Returns [] if no timestamps are parsed. Preserves timestamped empty text as a verse break.

LyricsUtils.parseLrc("[00:01.25]First line\n[00:02.500][00:03.00]Again");
// [
// { timestamp: 1250, duration: null, line: "First line" },
// { timestamp: 2500, duration: null, line: "Again" },
// { timestamp: 3000, duration: null, line: "Again" }
// ]

This parser alone does not decide synced or clear single-line/all-zero timestamps; that happens when constructing a full normalized result.

cleanTrackInfo​

public static cleanTrackInfo(
track: Pick<Track, "title" | "author">
): { artist: string; title: string };

Returns cleaned artist/title strings for database lookup, without requests. Removes common author suffixes such as VEVO and - Topic, bracketed video/title noise, featured-artist suffixes, and trailing separator metadata. Extracts an Artist - Title prefix when it agrees with the author or the author is absent; reduces artist collaborations to the first artist.

LyricsUtils.cleanTrackInfo({ title: "Coldplay - Yellow (Official Video)", author: "ColdplayVEVO" });
// { artist: "Coldplay", title: "Yellow" }