ws-audio2: codecs/audio/ApeTag: APEv1 / APEv2 tags -> Types.Tags (find / parse / tagKey), shared by WavPack, APE, TTA, Musepack
This commit is contained in:
1 parent
de4f14a343
commit
b7006dbbed
2 files changed
+189
No files matched your search
@@ -0,0 +1,110 @@
|
||||
--!strict
|
||||
-- codecs/audio/ApeTag: APEv1 / APEv2 tags (the tag block of Monkey's Audio, WavPack, Musepack, TTA, OptimFROG and
|
||||
-- some MP3s) -> Types.Tags. Written from the APEv2 specification (wiki.hydrogenaud.io "APEv2 specification"); no
|
||||
-- third-party code. A tag sits at the end of the file (before an optional ID3v1 tag) as items + a 32-byte footer
|
||||
-- "APETAGEX", with an optional 32-byte header in front (APEv2). Item keys become lower case tag keys (Year -> date,
|
||||
-- Track -> track, Album Artist -> albumartist, Disc -> disc, Comment -> comment); text values holding several
|
||||
-- values (NUL separated) are joined with "; "; binary items (cover art) and external links stay in info.items.
|
||||
-- Never throws.
|
||||
--
|
||||
-- local tags, info = ApeTag.find(buf, start, limit) -- the tag ending at limit (or before an ID3v1 tag there)
|
||||
-- -- tags: { title = ..., artist = ... } | nil (no tag / malformed)
|
||||
-- -- info: { start = first byte (the header when present), stop = past the footer, version = 1000 | 2000,
|
||||
-- -- items = { { key = "Title", value = "...", kind = "text" | "binary" | "link" } } }
|
||||
-- local tags, info = ApeTag.parse(buf, footer, start) -- the tag whose footer is at byte `footer` (>= start)
|
||||
local ApeTag = {}
|
||||
|
||||
export type Item = { key: string, value: string, kind: string }
|
||||
export type Info = { start: number, stop: number, version: number, items: { Item } }
|
||||
|
||||
local TO_TAG: { [string]: string } = {
|
||||
year = "date",
|
||||
["album artist"] = "albumartist",
|
||||
albumartist = "albumartist",
|
||||
tracknumber = "track",
|
||||
discnumber = "disc",
|
||||
description = "comment",
|
||||
}
|
||||
local KINDS = { [0] = "text", "binary", "link", "text" }
|
||||
|
||||
-- tagKey(itemKey) -> the Types.Tags key
|
||||
function ApeTag.tagKey(key: string): string
|
||||
local k = string.lower(key)
|
||||
return TO_TAG[k] or k
|
||||
end
|
||||
|
||||
-- parse(buf, footer, start?) -> tags, info | nil: the tag whose 32-byte footer starts at byte `footer`; the items
|
||||
-- must lie at or after `start` (default 0)
|
||||
function ApeTag.parse(buf: buffer, footer: number, start: number?): ({ [string]: string }?, Info?)
|
||||
local lo = start or 0
|
||||
local n = buffer.len(buf)
|
||||
if footer < lo or footer + 32 > n or buffer.readstring(buf, footer, 8) ~= "APETAGEX" then
|
||||
return nil, nil
|
||||
end
|
||||
local version = buffer.readu32(buf, footer + 8)
|
||||
local size = buffer.readu32(buf, footer + 12) -- items + footer
|
||||
local count = buffer.readu32(buf, footer + 16)
|
||||
local flags = buffer.readu32(buf, footer + 20)
|
||||
if size < 32 or footer + 32 - size < lo then
|
||||
return nil, nil
|
||||
end
|
||||
local p = footer + 32 - size
|
||||
local first = p
|
||||
if version >= 2000 and bit32.band(flags, 0x80000000) ~= 0 and p - 32 >= lo and buffer.readstring(buf, p - 32, 8) == "APETAGEX" then
|
||||
first = p - 32
|
||||
end
|
||||
local stop = footer
|
||||
local tags: { [string]: string } = {}
|
||||
local items: { Item } = {}
|
||||
for _ = 1, math.min(count, 65536) do
|
||||
if p + 9 > stop then
|
||||
break
|
||||
end
|
||||
local len = buffer.readu32(buf, p)
|
||||
local iflags = buffer.readu32(buf, p + 4)
|
||||
local k = p + 8
|
||||
while k < stop and buffer.readu8(buf, k) ~= 0 do
|
||||
k += 1
|
||||
end
|
||||
if k >= stop then
|
||||
break
|
||||
end
|
||||
local key = buffer.readstring(buf, p + 8, k - p - 8)
|
||||
local v0 = k + 1
|
||||
if len > stop - v0 then
|
||||
break
|
||||
end
|
||||
local value = buffer.readstring(buf, v0, len)
|
||||
local kind = if version >= 2000 then KINDS[bit32.band(bit32.rshift(iflags, 1), 3)] else "text"
|
||||
table.insert(items, { key = key, value = value, kind = kind })
|
||||
if kind == "text" and key ~= "" then
|
||||
local tk = ApeTag.tagKey(key)
|
||||
local s = (string.gsub(value, "%z+$", ""))
|
||||
s = (string.gsub(s, "%z", "; "))
|
||||
local old = tags[tk]
|
||||
tags[tk] = if old then old .. "; " .. s else s
|
||||
end
|
||||
p = v0 + len
|
||||
end
|
||||
return tags, { start = first, stop = footer + 32, version = version, items = items }
|
||||
end
|
||||
|
||||
-- find(buf, start, limit) -> tags, info | nil: the tag at the end of [start, limit), or just before a 128-byte
|
||||
-- ID3v1 tag ("TAG") there
|
||||
function ApeTag.find(buf: buffer, start: number, limit: number): ({ [string]: string }?, Info?)
|
||||
if limit > buffer.len(buf) then
|
||||
limit = buffer.len(buf)
|
||||
end
|
||||
if limit - start >= 32 then
|
||||
local tags, info = ApeTag.parse(buf, limit - 32, start)
|
||||
if tags then
|
||||
return tags, info
|
||||
end
|
||||
end
|
||||
if limit - start >= 160 and buffer.readstring(buf, limit - 128, 3) == "TAG" then
|
||||
return ApeTag.parse(buf, limit - 160, start)
|
||||
end
|
||||
return nil, nil
|
||||
end
|
||||
|
||||
return ApeTag
|
||||
Reference in new issue
Block a user