Skip to content
@deckyard/mp3

Cheatsheet

Every export of @deckyard/mp3, by topic, called through the import alias: import "@deckyard/mp3/" as mp3; then mp3.open(path). tools/check-docs.sh checks each signature against the source.

Version

ConstantValueMeaning
VERSION"0.1.0"The deck version, stamped by tools/inject-version.sh.

Errors

ConstantValueMeaning
ERROR_KIND"mp3"The kind of every Error the deck raises; fs errors keep their own.
SYNCSAFE_MAX268435455The largest value a 28-bit synchsafe integer can hold.

Genres

The ID3v1 genre table and the ID3v2 TCON resolver.

ConstantValueMeaning
GENRESa list of stringEvery genre an ID3v1 genre byte can name, in numeric order.
GENRE_COUNT192Assigned genre numbers; GENRES holds 0 to GENRE_COUNT - 1.
GENRE_NONE255The genre byte an ID3v1 tag uses for "no genre set".
SignatureReturnsDoes
genreName(number)stringThe name of genre 0-191, else "".
genreNumber(name)intThe number of a genre name, ignoring case and outer whitespace; -1 if unknown.
resolveGenre(raw)stringTCON text (Rock, 17, (17), (17)Surf, (RX), (CR)) to a name; unresolvable text comes back trimmed.

Text encodings

ConstantValueMeaning
TEXT_LATIN10ID3v2 text encoding 0: ISO-8859-1, one byte per character.
TEXT_UTF161ID3v2 text encoding 1: UTF-16 with a byte-order mark. All versions.
TEXT_UTF16BE2ID3v2 text encoding 2: UTF-16BE with no byte-order mark. ID3v2.4 only.
TEXT_UTF83ID3v2 text encoding 3: UTF-8. ID3v2.4 only.
SignatureReturnsDoes
decodeText(data, enc)stringDecode unterminated ID3v2 text; invalid UTF-8 falls back to ISO-8859-1. Throws for enc outside 0-3.
encodeText(s, enc)bytesEncode without terminator; TEXT_UTF16 writes an ff fe BOM. Throws for enc outside 0-3 or non-Latin-1 text with TEXT_LATIN1.

MPEG frame headers

MpegVersion

Which MPEG audio generation a frame was coded to. The zero value is Mpeg25.

VariantMeaning
Mpeg25The unofficial MPEG-2.5 extension: 11025, 12000 and 8000 Hz
Mpeg2MPEG-2: 22050, 24000 and 16000 Hz
Mpeg1MPEG-1: 44100, 48000 and 32000 Hz

Layer

Which MPEG audio layer a frame was coded to. MP3 is Layer III. The zero value is LayerI.

VariantMeaning
LayerILayer I, 384 samples per frame, four-byte slots
LayerIILayer II, 1152 samples per frame
LayerIIILayer III, what MP3 means: 1152 samples on MPEG-1, 576 below it

ChannelMode

How a frame carries its channels. The zero value is Stereo.

VariantMeaning
StereoTwo independently coded channels
JointStereoTwo channels sharing coded information
DualChannelTwo unrelated mono channels in one stream
MonoOne channel

FrameHeader

One decoded MPEG audio frame header.

FieldMeaning
offset as intwhere the header starts in the buffer it was read from
version as MpegVersionMPEG-1, MPEG-2 or MPEG-2.5
layer as LayerLayer I, II or III
bitrate as intbits per second; 0 for a free-format frame
sampleRate as intsamples per second
channelMode as ChannelModestereo, joint stereo, dual channel or mono
channels as int1 for mono, 2 for everything else
modeExtension as intthe joint-stereo mode extension bits, 0-3
padding as boolwhether the frame carries one extra slot
crc as boolwhether a 16-bit CRC follows the header
copyright as boolthe copyright bit
original as boolthe original-media bit
emphasis as intthe de-emphasis field, 0-3
frameSize as intthe whole frame in bytes, header included
samples as intPCM samples per channel the frame decodes to
SignatureReturnsDoes
parseFrameHeader(data, offset)FrameHeaderDecode the header at offset; throws when there is no legal header there.
versionName(version)stringThe name of an MPEG generation.
layerName(layer)stringThe name of an MPEG audio layer.
channelModeName(mode)stringThe name of a channel mode.

The audio stream

VbrHeader

The encoder-written header that opens a variable-bitrate stream.

FieldMeaning
kind as string"Xing", "Info", "VBRI", or "" when the stream carries no such header
frames as inthow many frames the stream holds, or 0 when unstated
streamBytes as inthow many bytes of audio, or 0 when unstated
quality as intthe encoder quality 0-100, or -1 when unstated
hasToc as boolwhether a seek table is present

AudioInfo

Everything the audio stream itself says about a file.

FieldMeaning
present as boolwhether any MPEG audio frame was found at all
offset as intwhere the first frame starts in the file
length as inthow many bytes of audio follow it
version as MpegVersionthe generation the first frame was coded to
layer as Layerthe layer the first frame was coded to
channelMode as ChannelModethe first frame's channel mode
channels as int1 or 2
sampleRate as intsamples per second
bitrate as intbits per second: declared for CBR, measured average for VBR
vbr as boolwhether the bitrate varies between frames
vbrHeader as VbrHeaderthe encoder-written header, if any
frames as inthow many frames the stream holds, or 0 when unknown
durationMs as inthow long the stream plays, in milliseconds
exact as boolwhether frames and durationMs were counted or stated, not estimated
SignatureReturnsDoes
readAudio(data, start, end)AudioInfoDescribe the MPEG audio between two offsets, walking every frame when no VBR header states the length.
readAudioWith(data, start, end, scan)AudioInforeadAudio; with scan false, estimate instead of walking.
formatDuration(durationMs)stringm:ss, or h:mm:ss from one hour; seconds truncated, negative as 0:00.

ID3v1

ConstantValueMeaning
ID3V1_SIZE128How long an ID3v1 tag is: TAG and 125 bytes of fixed-width fields.

Id3v1

An ID3v1 or ID3v1.1 tag.

FieldMeaning
present as boolwhether the file carried a tag at all
title as stringup to 30 characters
artist as stringup to 30 characters
album as stringup to 30 characters
year as stringfour characters, conventionally YYYY
comment as stringup to 30 characters, or 28 on ID3v1.1
track as intthe ID3v1.1 track number, or 0
genre as intthe genre byte as stored; GENRE_NONE when absent
version11 as boolwhether the tag uses the ID3v1.1 track field
SignatureReturnsDoes
readId3v1(data)Id3v1Read the tag in the last 128 bytes; present false when there is none.
renderId3v1(tag)bytes128 bytes; ID3v1.1 when track is 1-255; text above U+00FF becomes ?.

ID3v2

ConstantValueMeaning
ID3V2_HEADER_SIZE10How long an ID3v2 header is, and an ID3v2.4 footer with it.

Id3v2Frame

One ID3v2 frame, as stored.

FieldMeaning
id as stringthe frame identifier - three characters on ID3v2.2, four on ID3v2.3 and ID3v2.4
flags as intthe two frame flag bytes, status high and format low; 0 on ID3v2.2, which has none
data as bytesthe frame body, de-unsynchronised and decompressed

Id3v2

An ID3v2 tag.

FieldMeaning
present as boolwhether the file opened with an ID3v2 tag
major as intthe ID3v2 major version: 2, 3 or 4
minor as intthe revision number, almost always 0
flags as intthe header flag byte
size as inthow many bytes the whole tag occupies in the file, header and footer included
frames as list of Id3v2Frameevery readable frame, in stored order; encrypted frames are dropped
SignatureReturnsDoes
id3v2Size(data)intTag size with header and footer, from the header alone; 0 without a tag.
readId3v2(data)Id3v2Read the tag at the start of data; a damaged tag yields the frames before the damage.
hasFrame(tag, id)boolWhether a tag carries a frame with this identifier.
frame(tag, id)Id3v2FrameThe first frame with this identifier, or one with id "".
framesWithId(tag, id)list of Id3v2FrameEvery frame with this identifier, in the order stored.
frameText(f)stringThe first value of a text or URL frame, else "".
frameValues(f)list of stringEvery non-empty value of a text frame.
frameDescription(f)stringThe description of TXXX / WXXX, else "".
renderId3v2(frames, major, padding)bytesA plain tag (no unsync, footer or compression) with padding NUL bytes.

Tags

ConstantValueMeaning
PICTURE_TYPESa list of stringThe 21 APIC picture type names, 0-20.
PICTURE_FRONT_COVER3The APIC picture type for the front cover, the one players show.

Picture

One embedded picture.

FieldMeaning
mimeType as stringthe media type, "image/jpeg" or "image/png" in practice; ID3v2.2's three-letter image format is mapped onto it
kind as intthe picture type, an index into PICTURE_TYPES
description as stringa short label, often empty
data as bytesthe image itself

Comment

One comment.

FieldMeaning
language as stringthe ISO-639-2 language code, "eng" by default
description as stringwhat distinguishes this comment from the others
text as stringthe comment itself

Tag

Everything a tag says, independent of which version said it.

FieldMeaning
title as stringthe track title
artist as stringthe performing artist
album as stringthe album title
albumArtist as stringthe album's artist, when it differs
composer as stringwho wrote it
genre as stringthe genre as text, with numeric references resolved
year as stringthe four-digit year
date as stringthe full recording time when the tag carried one
track as intthe track number, or 0
trackTotal as inthow many tracks the release has, or 0
disc as intthe disc number, or 0
discTotal as inthow many discs the release has, or 0
bpm as intbeats per minute, or 0
publisher as stringthe label
encodedBy as stringthe encoding software or person
copyright as stringthe copyright notice
lyrics as stringthe unsynchronised lyrics
comments as list of Commentevery comment, in the order stored
pictures as list of Pictureevery embedded picture
extra as map of string to stringthe TXXX frames, by description
SignatureReturnsDoes
emptyTag()TagA tag with every field empty; the same as the zero value.
canonicalFrameId(id)stringThe ID3v2.3 name of an ID3v2.2 identifier; others unchanged.
tagFromId3v2(id3)TagThe unified view of an ID3v2 tag.
pictureTypeName(kind)stringThe name of picture type 0-20, else "".
coverArt(tag)PictureThe front cover, else the first picture, else an empty one.
commentText(tag)stringThe first comment with no description, else the first comment, else "".
tagFromId3v1(v1)TagThe unified view of an ID3v1 tag.
mergeTags(primary, fallback)Tagprimary with empty fields filled from fallback; extra is not merged.
framesFromTag(tag, major)list of Id3v2FrameID3v2 frames for every set field.
textFrame(id, value, major)Id3v2FrameA text frame in the narrowest encoding, id mapped for ID3v2.2.
id3v1FromTag(tag)Id3v1The ID3v1 subset of a tag; an unknown genre becomes GENRE_NONE.

Whole files

ConstantValueMeaning
DEFAULT_PADDING1024NUL bytes defaultOptions leaves after the frames.
DEFAULT_MAJOR4The ID3v2 version defaultOptions writes.

Track

One MP3 file, read.

FieldMeaning
path as stringwhere the file was read from, or "" for a buffer
tag as Tagthe unified view, ID3v2 merged over ID3v1
audio as AudioInfowhat the MPEG stream says
id3v2 as Id3v2the ID3v2 tag, with every frame as stored
id3v1 as Id3v1the ID3v1 tag

WriteOptions

What to write, and how.

FieldMeaning
major as intthe ID3v2 version to write: 2, 3 or 4
padding as inthow many NUL bytes to leave after the frames
id3v2 as boolwhether to write an ID3v2 tag at the front
id3v1 as boolwhether to write an ID3v1 tag at the end

AudioRange

Where the audio starts and ends within a file.

FieldMeaning
start as intthe first audio byte, past any ID3v2 tag
end as intone past the last audio byte, before any ID3v1 tag
SignatureReturnsDoes
defaultOptions()WriteOptionsThe default write options: ID3v2.4, 1 KiB of padding, no ID3v1 tag.
optionsFor(track)WriteOptionsThe file's ID3v2 version (else DEFAULT_MAJOR), ID3v1 only if it had one.
audioRange(data)AudioRangeThe bytes between the ID3v2 and ID3v1 tags.
strip(data)bytesdata without its ID3v2 and ID3v1 tags.
parse(data)TrackRead a buffer; path is "".
parseWith(data, scan)Trackparse; with scan false, skip the frame walk.
open(path)TrackRead a file; path is set.
withTag(track, tag)TrackA copy of track with tag.
write(data, tag, options)bytesThe audio of data with new tags; old tags are discarded.
writeFile(source, target, tag, options)nullwrite from file to file, atomically by rename.
save(track, target)nullwriteFile from track.path with optionsFor(track); throws when path is empty.
describe(track)stringOne line: names, duration, bitrate, rate, channel mode, separated by two spaces.

Playlists

ConstantValueMeaning
DURATION_UNKNOWN-1An entry duration that is not known.

PlaylistEntry

One entry of a playlist.

FieldMeaning
path as stringthe file, as written in the playlist
title as stringwhat to show, conventionally artist - title
duration as inthow long it plays in seconds, or DURATION_UNKNOWN
group as stringthe #EXTGRP group the entry belongs to
album as stringthe #EXTALB album
artist as stringthe #EXTART artist

Playlist

A playlist.

FieldMeaning
title as stringthe #PLAYLIST name, or ""
extended as boolwhether the file opened with #EXTM3U
entries as list of PlaylistEntrythe entries, in order
SignatureReturnsDoes
newPlaylist()PlaylistAn empty extended playlist.
playlistEntry(path, title, duration)PlaylistEntryBuild one playlist entry.
addEntry(playlist, entry)PlaylistAppend one entry; copies the list, so quadratic in a loop.
addEntries(playlist, entries)PlaylistAppend a list of entries with one copy.
entryFromTrack(track)PlaylistEntryAn entry for track.path, titled from the tag, duration rounded to seconds.
parsePlaylist(text)PlaylistRead M3U or extended M3U text; never throws.
buildPlaylist(playlist)stringPlaylist text, \n line endings.
playlistDuration(playlist)intTotal seconds; unknown durations count as 0.
resolvePlaylist(playlist, baseDir)PlaylistJoin relative paths onto baseDir; does not confine them.
openPlaylist(playlistPath)PlaylistRead a file as UTF-8, or ISO-8859-1 when not valid UTF-8.
savePlaylist(playlist, playlistPath)nullWrite .m3u8 as UTF-8, others as ISO-8859-1 when possible.