--!strict -- codecs/audio/VorbisComment: Vorbis comment fields ("KEY=value", the tag block of Vorbis, Opus and FLAC) <-> Types.Tags. -- Tag keys are lower case everywhere in formats-luau (the same names ID3 / MP4 / RIFF INFO map to): title, artist, -- album, albumartist, date, genre, track, tracktotal, disc, disctotal, comment, composer, ...; TRACKNUMBER becomes -- track, DISCNUMBER disc, DESCRIPTION comment, other fields their lower-cased name. Repeated fields are joined with -- "; " in tags; the raw fields stay available (decoders put them in meta.source.comments). Shared so the three -- formats read and write tags the same way. Never throws. -- -- local tags, raw = VorbisComment.tags({ "TITLE=x", "TRACKNUMBER=3" }) -- { title = "x", track = "3" }, raw list -- local fields = VorbisComment.fields(tags) -- sorted { "TITLE=x", "TRACKNUMBER=3" } local VorbisComment = {} export type Field = { key: string, value: string } -- key as written (upper-cased), value local TO_TAG: { [string]: string } = { tracknumber = "track", discnumber = "disc", description = "comment" } local TO_FIELD: { [string]: string } = { track = "TRACKNUMBER", disc = "DISCNUMBER" } -- tagKey(fieldName) -> the Types.Tags key function VorbisComment.tagKey(name: string): string local k = string.lower(name) return TO_TAG[k] or k end -- fieldName(tagKey) -> the Vorbis comment field name function VorbisComment.fieldName(key: string): string return TO_FIELD[key] or string.upper(key) end -- tags(list of "KEY=value") -> Types.Tags, { Field } (entries without "=" are skipped) function VorbisComment.tags(comments: { string }): ({ [string]: string }, { Field }) local tags: { [string]: string } = {} local raw: { Field } = {} for _, s in comments do local eq = string.find(s, "=", 1, true) if eq then local name, v = string.sub(s, 1, eq - 1), string.sub(s, eq + 1) table.insert(raw, { key = string.upper(name), value = v }) local k = VorbisComment.tagKey(name) local old = tags[k] tags[k] = if old then old .. "; " .. v else v end end return tags, raw end -- fields(tags) -> "KEY=value" strings in key order (string keys and values only) function VorbisComment.fields(tags: { [any]: any }): { string } local keys = {} for k, v in tags do if type(k) == "string" and type(v) == "string" then table.insert(keys, k) end end table.sort(keys) local out: { string } = table.create(#keys) :: any for i, k in keys do out[i] = VorbisComment.fieldName(k) .. "=" .. tags[k] end return out end return VorbisComment