absorb: merge ws/absorb-mp3
This commit is contained in:
commit
d5372992e7
45 files changed
+3608
-2
No files matched your search
@@ -26,6 +26,18 @@ local list: { Entry } = {
|
||||
return require("@formats/image/png")
|
||||
end,
|
||||
},
|
||||
{
|
||||
name = "mp3",
|
||||
category = "audio",
|
||||
path = "audio/mp3",
|
||||
extensions = { "mp3", "mp2", "mp1", "mpga" },
|
||||
mime = { "audio/mpeg", "audio/mp3", "audio/mpa" },
|
||||
deps = { "core/Err", "core/Bytes", "Types" },
|
||||
caps = { decode = true, encode = false, streaming = true, seek = true, layers = { 1, 2, 3 }, channels = 2 },
|
||||
load = function()
|
||||
return require("@formats/audio/mp3")
|
||||
end,
|
||||
},
|
||||
{
|
||||
name = "wav",
|
||||
category = "audio",
|
||||
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,140 @@
|
||||
--!strict
|
||||
--!native
|
||||
--!optimize 2
|
||||
-- audio/mp3/Header: MPEG audio frame headers (minimp3's hdr_* macros and functions), frame sync (mp3d_find_frame /
|
||||
-- mp3d_match_frame). Every function reads 4 header bytes at a byte offset of a buffer; callers keep offset + 4
|
||||
-- inside the buffer. Never throws.
|
||||
local T = require("@formats/audio/mp3/Tables")
|
||||
|
||||
local band, bxor, rshift = bit32.band, bit32.bxor, bit32.rshift
|
||||
local ru8 = buffer.readu8
|
||||
|
||||
local HALFRATE = T.halfrate
|
||||
local HZ = { [0] = 44100, 48000, 32000 }
|
||||
|
||||
local H = {}
|
||||
|
||||
H.MAX_FREE_FORMAT_FRAME_SIZE = 2304
|
||||
H.MAX_FRAME_SYNC_MATCHES = 10
|
||||
|
||||
-- hdr_valid
|
||||
function H.valid(b: buffer, o: number): boolean
|
||||
if ru8(b, o) ~= 0xFF then
|
||||
return false
|
||||
end
|
||||
local h1, h2 = ru8(b, o + 1), ru8(b, o + 2)
|
||||
return (band(h1, 0xF0) == 0xF0 or band(h1, 0xFE) == 0xE2)
|
||||
and band(rshift(h1, 1), 3) ~= 0
|
||||
and rshift(h2, 4) ~= 15
|
||||
and band(rshift(h2, 2), 3) ~= 3
|
||||
end
|
||||
local valid = H.valid
|
||||
|
||||
-- hdr_compare: h2 is a valid header of the same version / layer / sample rate / free-format-ness as h1
|
||||
function H.compare(b: buffer, o1: number, o2: number): boolean
|
||||
if not valid(b, o2) then
|
||||
return false
|
||||
end
|
||||
local a1, a2 = ru8(b, o1 + 1), ru8(b, o1 + 2)
|
||||
local c1, c2 = ru8(b, o2 + 1), ru8(b, o2 + 2)
|
||||
return band(bxor(a1, c1), 0xFE) == 0
|
||||
and band(bxor(a2, c2), 0x0C) == 0
|
||||
and (band(a2, 0xF0) == 0) == (band(c2, 0xF0) == 0)
|
||||
end
|
||||
local compare = H.compare
|
||||
|
||||
-- from the header bytes h1 = byte 1, h2 = byte 2
|
||||
function H.bitrateKbps(h1: number, h2: number): number
|
||||
local mpeg1 = if band(h1, 8) ~= 0 then 1 else 0
|
||||
local layer = band(rshift(h1, 1), 3)
|
||||
return 2 * HALFRATE[mpeg1 * 45 + (layer - 1) * 15 + rshift(h2, 4)]
|
||||
end
|
||||
local bitrateKbps = H.bitrateKbps
|
||||
|
||||
function H.sampleRate(h1: number, h2: number): number
|
||||
local hz = HZ[band(rshift(h2, 2), 3)]
|
||||
if band(h1, 8) == 0 then
|
||||
hz //= 2
|
||||
end
|
||||
if band(h1, 0x10) == 0 then
|
||||
hz //= 2
|
||||
end
|
||||
return hz
|
||||
end
|
||||
local sampleRate = H.sampleRate
|
||||
|
||||
function H.frameSamples(h1: number): number
|
||||
if band(h1, 6) == 6 then
|
||||
return 384
|
||||
elseif band(h1, 14) == 2 then
|
||||
return 576
|
||||
end
|
||||
return 1152
|
||||
end
|
||||
local frameSamples = H.frameSamples
|
||||
|
||||
-- hdr_frame_bytes (without padding); free format = 0 -> freeSize
|
||||
function H.frameBytes(b: buffer, o: number, freeSize: number): number
|
||||
local h1, h2 = ru8(b, o + 1), ru8(b, o + 2)
|
||||
local hz = sampleRate(h1, h2)
|
||||
local fb = frameSamples(h1) * bitrateKbps(h1, h2) * 125 // hz
|
||||
if band(h1, 6) == 6 then
|
||||
fb = band(fb, 0xFFFFFFFC)
|
||||
end
|
||||
return if fb ~= 0 then fb else freeSize
|
||||
end
|
||||
local frameBytes = H.frameBytes
|
||||
|
||||
function H.padding(b: buffer, o: number): number
|
||||
if band(ru8(b, o + 2), 2) == 0 then
|
||||
return 0
|
||||
end
|
||||
return if band(ru8(b, o + 1), 6) == 6 then 4 else 1
|
||||
end
|
||||
local padding = H.padding
|
||||
|
||||
-- mp3d_match_frame: the frames chained after o (n bytes left) keep comparing equal
|
||||
local function matchFrame(b: buffer, o: number, n: number, fb: number): boolean
|
||||
local i = 0
|
||||
for nmatch = 0, H.MAX_FRAME_SYNC_MATCHES - 1 do
|
||||
i += frameBytes(b, o + i, fb) + padding(b, o + i)
|
||||
if i + 4 > n then
|
||||
return nmatch > 0
|
||||
end
|
||||
if not compare(b, o, o + i) then
|
||||
return false
|
||||
end
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
||||
-- mp3d_find_frame over b[base, base + n): returns (skip, frame bytes incl. padding or 0, free format size)
|
||||
function H.findFrame(b: buffer, base: number, n: number, ffb: number): (number, number, number)
|
||||
for i = 0, n - 5 do
|
||||
local o = base + i
|
||||
if valid(b, o) then
|
||||
local fb = frameBytes(b, o, ffb)
|
||||
local fap = fb + padding(b, o)
|
||||
local k = 4
|
||||
while fb == 0 and k < H.MAX_FREE_FORMAT_FRAME_SIZE and i + 2 * k < n - 4 do
|
||||
if compare(b, o, o + k) then
|
||||
local f = k - padding(b, o)
|
||||
local nextfb = f + padding(b, o + k)
|
||||
if not (i + k + nextfb + 4 > n or not compare(b, o, o + k + nextfb)) then
|
||||
fap = k
|
||||
fb = f
|
||||
ffb = f
|
||||
end
|
||||
end
|
||||
k += 1
|
||||
end
|
||||
if (fb ~= 0 and i + fap <= n and matchFrame(b, o, n - i, fb)) or (i == 0 and fap == n) then
|
||||
return i, fap, ffb
|
||||
end
|
||||
ffb = 0
|
||||
end
|
||||
end
|
||||
return n, 0, ffb
|
||||
end
|
||||
|
||||
return H
|
||||
@@ -0,0 +1,194 @@
|
||||
--!strict
|
||||
--!native
|
||||
--!optimize 2
|
||||
-- audio/mp3/Index: the frame index of a stream, built once on open. It follows minimp3_ex's mp3dec_load_buf frame by frame
|
||||
-- (same sync, resync and Xing / LAME handling), and simulates the layer III bit reservoir from the side info, so
|
||||
-- it knows in advance which frames give output and where each one's samples land. That makes the total length
|
||||
-- exact and seeking sample-accurate. Never throws.
|
||||
local Header = require("@formats/audio/mp3/Header")
|
||||
local Core = require("@formats/audio/mp3/Core")
|
||||
|
||||
local band, rshift = bit32.band, bit32.rshift
|
||||
local ru8 = buffer.readu8
|
||||
local min, max = math.min, math.max
|
||||
|
||||
local compare, frameBytes, padding, findFrame =
|
||||
Header.compare, Header.frameBytes, Header.padding, Header.findFrame
|
||||
|
||||
local Index = {}
|
||||
|
||||
Index.FLAG_RESET = 1 -- decoder state is zeroed before this frame (minimp3 re-synced here)
|
||||
Index.FLAG_OUTPUT = 2 -- this frame gives samples in a continuous decode
|
||||
|
||||
export type Index = {
|
||||
n: number, -- frames
|
||||
off: buffer, -- u32 byte offset per frame
|
||||
size: buffer, -- u16 byte size per frame (header and padding included)
|
||||
flags: buffer, -- u8 FLAG_*
|
||||
out: buffer, -- u32 first output sample (before the encoder delay is cut) per frame
|
||||
main: buffer, -- u16 layer III main data bytes per frame (0 for layers I / II)
|
||||
outTotal: number, -- samples (per channel) of a continuous decode, delay not cut
|
||||
bytes: number, -- audio bytes in indexed frames
|
||||
-- format of the first frame
|
||||
sampleRate: number,
|
||||
channels: number, -- the most channels of any frame
|
||||
layer: number,
|
||||
version: number, -- 1, 2 or 2.5
|
||||
spf: number, -- samples per frame
|
||||
-- Xing / Info / LAME
|
||||
vbrTag: number, -- mp3dec_check_vbrtag result of the first frame (0 = none)
|
||||
tagFrames: number,
|
||||
delay: number,
|
||||
padding: number,
|
||||
skipped: number, -- frames with another sample rate or layer, left out
|
||||
}
|
||||
|
||||
local function grow(b: buffer, n: number): buffer
|
||||
local nb = buffer.create(n)
|
||||
buffer.copy(nb, 0, b, 0, math.min(buffer.len(b), n))
|
||||
return nb
|
||||
end
|
||||
|
||||
function Index.build(b: buffer, start: number, stop: number): Index?
|
||||
local cap = 1024
|
||||
local OFF, SIZE, FLAGS, OUT, MAIN = buffer.create(cap * 4), buffer.create(cap * 2), buffer.create(cap), buffer.create(cap * 4), buffer.create(cap * 2)
|
||||
local n = 0
|
||||
local pos = start
|
||||
local prev = -1 -- offset of the last decoded header (dec->header), -1 = none
|
||||
local ffb = 0 -- free format bytes
|
||||
local reserv = 0
|
||||
local outTotal = 0
|
||||
local bytes = 0
|
||||
local first = true
|
||||
local skipped = 0
|
||||
local outCh = 0 -- the most channels of a frame that gives output
|
||||
local hz, nch, layer, spf, version = 0, 0, 0, 0, 0
|
||||
local vbr, tagFrames, delay, pad = 0, 0, 0, 0
|
||||
|
||||
while true do
|
||||
local remaining = stop - pos
|
||||
if remaining <= 0 then
|
||||
break
|
||||
end
|
||||
local fs = 0
|
||||
local reset = false
|
||||
if remaining > 4 and prev >= 0 and compare(b, prev, pos) then
|
||||
fs = frameBytes(b, pos, ffb) + padding(b, pos)
|
||||
if fs ~= remaining and (fs + 4 > remaining or not compare(b, pos, pos + fs)) then
|
||||
fs = 0
|
||||
end
|
||||
end
|
||||
if fs == 0 then
|
||||
reset = true
|
||||
reserv = 0
|
||||
local i
|
||||
i, fs, ffb = findFrame(b, pos, remaining, 0)
|
||||
if fs == 0 or i + fs > remaining then
|
||||
break
|
||||
end
|
||||
pos += i
|
||||
end
|
||||
if fs < 4 or fs > Core.MAX_FRAME then
|
||||
-- cannot happen with a well-formed header, but never index an impossible frame
|
||||
prev = -1
|
||||
pos += max(fs, 1)
|
||||
continue
|
||||
end
|
||||
local h1, h2, h3 = ru8(b, pos + 1), ru8(b, pos + 2), ru8(b, pos + 3)
|
||||
local fLayer = 4 - band(rshift(h1, 1), 3)
|
||||
local fHz = Header.sampleRate(h1, h2)
|
||||
local fCh = if band(h3, 0xC0) == 0xC0 then 1 else 2
|
||||
|
||||
if first then
|
||||
first = false
|
||||
hz, nch, layer, spf = fHz, fCh, fLayer, Header.frameSamples(h1)
|
||||
version = if band(h1, 8) ~= 0 then 1 elseif band(h1, 0x10) ~= 0 then 2 else 2.5
|
||||
if fLayer == 3 then
|
||||
local ret, frames, d, p = Core.vbrTag(b, pos, fs)
|
||||
if ret ~= 0 then
|
||||
vbr, tagFrames, delay, pad = ret, frames, d, p
|
||||
if ret < 0 then
|
||||
delay, pad = 0, 0
|
||||
end
|
||||
-- the tag frame is not audio; the next frame is found again from scratch
|
||||
prev = -1
|
||||
pos += fs
|
||||
continue
|
||||
end
|
||||
end
|
||||
elseif fHz ~= hz or fLayer ~= layer then
|
||||
skipped += 1
|
||||
prev = pos
|
||||
pos += fs
|
||||
continue
|
||||
end
|
||||
|
||||
local output = true
|
||||
local mainBytes = 0
|
||||
local nextPrev = pos
|
||||
if fLayer == 3 then
|
||||
local mdb, p23, mb = Core.probeL3(b, pos, fs)
|
||||
if mdb < 0 then
|
||||
output = false
|
||||
nextPrev = -1 -- mp3dec_init: the next frame re-syncs
|
||||
else
|
||||
mainBytes = mb
|
||||
local have = min(reserv, mdb)
|
||||
output = reserv >= mdb
|
||||
local remains = have + mb - (if output then (p23 + 7) // 8 else 0)
|
||||
reserv = max(0, min(remains, 511))
|
||||
end
|
||||
else
|
||||
output = Core.probeL12(b, pos, fs)
|
||||
if not output then
|
||||
nextPrev = -1
|
||||
end
|
||||
end
|
||||
|
||||
if output and fCh > outCh then
|
||||
outCh = fCh -- mono / stereo switches: the stream is stereo, mono frames are duplicated
|
||||
end
|
||||
if n == cap then
|
||||
cap *= 2
|
||||
OFF, SIZE, FLAGS, OUT, MAIN = grow(OFF, cap * 4), grow(SIZE, cap * 2), grow(FLAGS, cap), grow(OUT, cap * 4), grow(MAIN, cap * 2)
|
||||
end
|
||||
buffer.writeu32(OFF, n * 4, pos)
|
||||
buffer.writeu16(SIZE, n * 2, fs)
|
||||
buffer.writeu8(FLAGS, n, (if reset then Index.FLAG_RESET else 0) + (if output then Index.FLAG_OUTPUT else 0))
|
||||
buffer.writeu32(OUT, n * 4, outTotal)
|
||||
buffer.writeu16(MAIN, n * 2, min(mainBytes, 65535))
|
||||
n += 1
|
||||
bytes += fs
|
||||
if output then
|
||||
outTotal += spf
|
||||
end
|
||||
prev = nextPrev
|
||||
pos += fs
|
||||
end
|
||||
|
||||
if first then
|
||||
return nil
|
||||
end
|
||||
return {
|
||||
n = n,
|
||||
off = OFF,
|
||||
size = SIZE,
|
||||
flags = FLAGS,
|
||||
out = OUT,
|
||||
main = MAIN,
|
||||
outTotal = outTotal,
|
||||
bytes = bytes,
|
||||
sampleRate = hz,
|
||||
channels = if outCh > 0 then outCh else nch,
|
||||
layer = layer,
|
||||
version = version,
|
||||
spf = spf,
|
||||
vbrTag = vbr,
|
||||
tagFrames = tagFrames,
|
||||
delay = delay,
|
||||
padding = pad,
|
||||
skipped = skipped,
|
||||
}
|
||||
end
|
||||
|
||||
return Index
|
||||
@@ -0,0 +1,233 @@
|
||||
--!strict
|
||||
--!optimize 2
|
||||
-- audio/mp3/Stream: the streaming Decoder (Types.Decoder) over a frame index: gapless (LAME delay / padding), exact
|
||||
-- length, sample-accurate seeking. From mp3-luau's Mp3.open / Decoder (same author). Never throws.
|
||||
local Core = require("@formats/audio/mp3/Core")
|
||||
local Index = require("@formats/audio/mp3/Index")
|
||||
|
||||
local FLAG_RESET, FLAG_OUTPUT = Index.FLAG_RESET, Index.FLAG_OUTPUT
|
||||
local ru8, ru16, ru32 = buffer.readu8, buffer.readu16, buffer.readu32
|
||||
local band = bit32.band
|
||||
local min, max, floor = math.min, math.max, math.floor
|
||||
|
||||
local Stream = {}
|
||||
|
||||
local Decoder = {}
|
||||
Decoder.__index = Decoder
|
||||
|
||||
export type Decoder = typeof(setmetatable(
|
||||
{} :: {
|
||||
sampleRate: number,
|
||||
channels: number,
|
||||
frames: number,
|
||||
duration: number,
|
||||
bitrate: number,
|
||||
tags: { [string]: string },
|
||||
id3: any,
|
||||
layer: number,
|
||||
version: number,
|
||||
vbr: boolean,
|
||||
delay: number,
|
||||
padding: number,
|
||||
mpegFrames: number,
|
||||
_buf: buffer,
|
||||
_idx: any,
|
||||
_st: any,
|
||||
_pcm: buffer,
|
||||
_cur: number,
|
||||
_pcmPos: number,
|
||||
_pcmEnd: number,
|
||||
_minAbs: number,
|
||||
_endAbs: number,
|
||||
_pos: number,
|
||||
},
|
||||
Decoder
|
||||
))
|
||||
|
||||
-- new(b, idx, tags, id3) -> Decoder over the whole buffer b, with its frame index (Index.build)
|
||||
function Stream.new(b: buffer, idx: any, tags: { [string]: string }, id3: any): Decoder
|
||||
local spf, hz, nch = idx.spf, idx.sampleRate, idx.channels
|
||||
-- gapless: minimp3_ex cuts delay samples off the start and caps the length at the Xing / LAME count
|
||||
local delay = 0
|
||||
local total = idx.outTotal
|
||||
if idx.vbrTag > 0 then
|
||||
delay = idx.delay
|
||||
local detected = spf * idx.tagFrames
|
||||
if detected >= delay then
|
||||
detected -= delay
|
||||
end
|
||||
if idx.padding > 0 and detected >= idx.padding then
|
||||
detected -= idx.padding
|
||||
end
|
||||
total = max(0, total - delay)
|
||||
total = min(total, detected)
|
||||
end
|
||||
delay = min(delay, idx.outTotal)
|
||||
local self = setmetatable({
|
||||
sampleRate = hz,
|
||||
channels = nch,
|
||||
frames = total,
|
||||
duration = total / hz,
|
||||
bitrate = if idx.n > 0 then floor(idx.bytes * 8 * hz / (idx.n * spf) / 1000 + 0.5) else 0,
|
||||
tags = tags,
|
||||
id3 = id3,
|
||||
layer = idx.layer,
|
||||
version = idx.version,
|
||||
vbr = idx.vbrTag ~= 0,
|
||||
delay = delay,
|
||||
padding = if idx.vbrTag > 0 then max(0, idx.padding) else 0,
|
||||
mpegFrames = idx.n,
|
||||
_buf = b,
|
||||
_idx = idx,
|
||||
_st = Core.newState(),
|
||||
_pcm = buffer.create(1152 * 2 * 4),
|
||||
_cur = 0,
|
||||
_pcmPos = 0,
|
||||
_pcmEnd = 0,
|
||||
_minAbs = delay,
|
||||
_endAbs = delay + total,
|
||||
_pos = 0,
|
||||
}, Decoder)
|
||||
return self
|
||||
end
|
||||
|
||||
|
||||
-- decodes frames until one has samples inside [minAbs, endAbs); false at the end
|
||||
local function fill(self: Decoder): boolean
|
||||
local idx = self._idx
|
||||
local n = idx.n
|
||||
local spf = idx.spf
|
||||
local st = self._st
|
||||
while self._cur < n do
|
||||
local f = self._cur
|
||||
self._cur = f + 1
|
||||
local flags = ru8(idx.flags, f)
|
||||
if band(flags, FLAG_RESET) ~= 0 then
|
||||
Core.reset(st)
|
||||
end
|
||||
local a = ru32(idx.out, f * 4)
|
||||
if a >= self._endAbs then
|
||||
self._cur = n
|
||||
return false
|
||||
end
|
||||
local got = Core.decodeFrame(st, self._buf, ru32(idx.off, f * 4), ru16(idx.size, f * 2), self._pcm)
|
||||
if got > 0 and band(flags, FLAG_OUTPUT) ~= 0 then
|
||||
if Core.lastChannels < self.channels then
|
||||
Core.upmix(self._pcm, got)
|
||||
end
|
||||
local lo = max(a, self._minAbs)
|
||||
local hi = min(a + spf, self._endAbs)
|
||||
if hi > lo then
|
||||
self._pcmPos = lo - a
|
||||
self._pcmEnd = hi - a
|
||||
return true
|
||||
end
|
||||
end
|
||||
end
|
||||
return false
|
||||
end
|
||||
|
||||
-- Reads up to maxFrames sample frames: (buffer of interleaved f32, frames read); 0 frames = end of stream.
|
||||
-- With out (a buffer of at least maxFrames * channels * 4 bytes) the samples go there and out is returned; a
|
||||
-- smaller out is ignored.
|
||||
function Decoder.read(self: Decoder, maxFrames: number?, out: buffer?): (buffer, number)
|
||||
local ch = self.channels
|
||||
local want = tonumber(maxFrames) or 4096
|
||||
if want ~= want then
|
||||
want = 0
|
||||
end
|
||||
want = floor(max(0, min(want, self.frames - self._pos)))
|
||||
if out and buffer.len(out) < want * ch * 4 then
|
||||
out = nil -- too small: a new buffer (Types.Decoder: out is reused when big enough)
|
||||
end
|
||||
local dst = out or buffer.create(want * ch * 4)
|
||||
local got = 0
|
||||
local pcm = self._pcm
|
||||
while got < want do
|
||||
if self._pcmPos >= self._pcmEnd then
|
||||
if not fill(self) then
|
||||
break
|
||||
end
|
||||
end
|
||||
local k = min(want - got, self._pcmEnd - self._pcmPos)
|
||||
buffer.copy(dst, got * ch * 4, pcm, self._pcmPos * ch * 4, k * ch * 4)
|
||||
got += k
|
||||
self._pcmPos += k
|
||||
end
|
||||
self._pos += got
|
||||
if not out and got < want then
|
||||
local r = buffer.create(got * ch * 4)
|
||||
buffer.copy(r, 0, dst, 0, got * ch * 4)
|
||||
dst = r
|
||||
end
|
||||
return dst, got
|
||||
end
|
||||
|
||||
-- Seeks to a time in seconds (clamped to the stream). Sample-accurate: decoding restarts a few frames earlier
|
||||
-- (enough to refill the bit reservoir and the filter history) and the output before the target is dropped.
|
||||
function Decoder.seek(self: Decoder, seconds: number)
|
||||
local hz = self.sampleRate
|
||||
local target = floor((tonumber(seconds) or 0) * hz + 0.5)
|
||||
target = max(0, min(target, self.frames))
|
||||
self:seekFrame(target)
|
||||
end
|
||||
|
||||
-- Seeks to a sample frame (0 .. frames).
|
||||
function Decoder.seekFrame(self: Decoder, target: number)
|
||||
target = tonumber(target) or 0
|
||||
if target ~= target then
|
||||
target = 0
|
||||
end
|
||||
target = max(0, min(floor(target), self.frames))
|
||||
local idx = self._idx
|
||||
local abs = self.delay + target
|
||||
self._pos = target
|
||||
self._minAbs = abs
|
||||
self._pcmPos, self._pcmEnd = 0, 0
|
||||
Core.reset(self._st)
|
||||
local n = idx.n
|
||||
if abs >= self._endAbs or n == 0 then
|
||||
self._cur = n
|
||||
return
|
||||
end
|
||||
-- last frame starting at or before abs
|
||||
local lo, hi = 0, n - 1
|
||||
while lo < hi do
|
||||
local mid = (lo + hi + 1) // 2
|
||||
if ru32(idx.out, mid * 4) <= abs then
|
||||
lo = mid
|
||||
else
|
||||
hi = mid - 1
|
||||
end
|
||||
end
|
||||
local j = lo
|
||||
-- go back >= 2 frames (layer I needs 2 for the polyphase history), and for layer III until the main data
|
||||
-- before frame j - 1 covers the largest reservoir (511 bytes), stopping at a frame where the decoder resets
|
||||
local need = if idx.layer == 3 then 511 else 0
|
||||
local s, back, acc = j, 0, 0
|
||||
while s > 0 and band(ru8(idx.flags, s), FLAG_RESET) == 0 do
|
||||
if back >= 2 and acc >= need then
|
||||
break
|
||||
end
|
||||
s -= 1
|
||||
back += 1
|
||||
if back >= 2 then
|
||||
acc += ru16(idx.main, s * 2)
|
||||
end
|
||||
end
|
||||
self._cur = s
|
||||
end
|
||||
|
||||
-- Current position: (seconds, sample frame)
|
||||
function Decoder.position(self: Decoder): (number, number)
|
||||
return self._pos / self.sampleRate, self._pos
|
||||
end
|
||||
|
||||
-- Decodes everything from the current position to the end: (buffer of interleaved f32, frames)
|
||||
function Decoder.readAll(self: Decoder): (buffer, number)
|
||||
return self:read(max(0, self.frames - self._pos))
|
||||
end
|
||||
|
||||
Stream.Decoder = Decoder
|
||||
|
||||
return Stream
|
||||
@@ -0,0 +1,224 @@
|
||||
--!strict
|
||||
-- audio/mp3/Tables: constant tables of minimp3 (https://github.com/lieff/minimp3, CC0), generated by tools/gen_tables.py.
|
||||
-- Every list is flattened row-major and 0-based (t[0] is the first element), like the C arrays.
|
||||
local T = {}
|
||||
T.halfrate = { [0] = 0,
|
||||
4, 8, 12, 16, 20, 24, 28, 32, 40, 48, 56, 64, 72, 80, 0, 4, 8, 12, 16, 20, 24, 28, 32, 40,
|
||||
48, 56, 64, 72, 80, 0, 16, 24, 28, 32, 40, 48, 56, 64, 72, 80, 88, 96, 112, 128, 0, 16, 20, 24,
|
||||
28, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 0, 16, 24, 28, 32, 40, 48, 56, 64, 80, 96, 112, 128,
|
||||
160, 192, 0, 16, 32, 48, 64, 80, 96, 112, 128, 144, 160, 176, 192, 208, 224,
|
||||
}
|
||||
T.g_bitalloc_code_tab = { [0] = 0,
|
||||
17, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 0, 17, 18, 3, 19, 4, 5, 6, 7,
|
||||
8, 9, 10, 11, 12, 13, 16, 0, 17, 18, 3, 19, 4, 5, 16, 0, 17, 18, 16, 0, 17, 18, 19, 4,
|
||||
5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0, 17, 18, 3, 19, 4, 5, 6, 7, 8, 9, 10, 11,
|
||||
12, 13, 14, 0, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
|
||||
}
|
||||
T.g_scf_long = { [0] = 6,
|
||||
6, 6, 6, 6, 6, 8, 10, 12, 14, 16, 20, 24, 28, 32, 38, 46, 52, 60, 68, 58, 54, 0, 12, 12,
|
||||
12, 12, 12, 12, 16, 20, 24, 28, 32, 40, 48, 56, 64, 76, 90, 2, 2, 2, 2, 2, 0, 6, 6, 6,
|
||||
6, 6, 6, 8, 10, 12, 14, 16, 20, 24, 28, 32, 38, 46, 52, 60, 68, 58, 54, 0, 6, 6, 6, 6,
|
||||
6, 6, 8, 10, 12, 14, 16, 18, 22, 26, 32, 38, 46, 54, 62, 70, 76, 36, 0, 6, 6, 6, 6, 6,
|
||||
6, 8, 10, 12, 14, 16, 20, 24, 28, 32, 38, 46, 52, 60, 68, 58, 54, 0, 4, 4, 4, 4, 4, 4,
|
||||
6, 6, 8, 8, 10, 12, 16, 20, 24, 28, 34, 42, 50, 54, 76, 158, 0, 4, 4, 4, 4, 4, 4, 6,
|
||||
6, 6, 8, 10, 12, 16, 18, 22, 28, 34, 40, 46, 54, 54, 192, 0, 4, 4, 4, 4, 4, 4, 6, 6,
|
||||
8, 10, 12, 16, 20, 24, 30, 38, 46, 56, 68, 84, 102, 26, 0,
|
||||
}
|
||||
T.g_scf_short = { [0] = 4,
|
||||
4, 4, 4, 4, 4, 4, 4, 4, 6, 6, 6, 8, 8, 8, 10, 10, 10, 12, 12, 12, 14, 14, 14, 18,
|
||||
18, 18, 24, 24, 24, 30, 30, 30, 40, 40, 40, 18, 18, 18, 0, 8, 8, 8, 8, 8, 8, 8, 8, 8,
|
||||
12, 12, 12, 16, 16, 16, 20, 20, 20, 24, 24, 24, 28, 28, 28, 36, 36, 36, 2, 2, 2, 2, 2, 2,
|
||||
2, 2, 2, 26, 26, 26, 0, 4, 4, 4, 4, 4, 4, 4, 4, 4, 6, 6, 6, 6, 6, 6, 8, 8,
|
||||
8, 10, 10, 10, 14, 14, 14, 18, 18, 18, 26, 26, 26, 32, 32, 32, 42, 42, 42, 18, 18, 18, 0, 4,
|
||||
4, 4, 4, 4, 4, 4, 4, 4, 6, 6, 6, 8, 8, 8, 10, 10, 10, 12, 12, 12, 14, 14, 14, 18,
|
||||
18, 18, 24, 24, 24, 32, 32, 32, 44, 44, 44, 12, 12, 12, 0, 4, 4, 4, 4, 4, 4, 4, 4, 4,
|
||||
6, 6, 6, 8, 8, 8, 10, 10, 10, 12, 12, 12, 14, 14, 14, 18, 18, 18, 24, 24, 24, 30, 30, 30,
|
||||
40, 40, 40, 18, 18, 18, 0, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 6, 6, 6, 8, 8,
|
||||
8, 10, 10, 10, 12, 12, 12, 14, 14, 14, 18, 18, 18, 22, 22, 22, 30, 30, 30, 56, 56, 56, 0, 4,
|
||||
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 6, 6, 6, 6, 6, 6, 10, 10, 10, 12, 12, 12, 14,
|
||||
14, 14, 16, 16, 16, 20, 20, 20, 26, 26, 26, 66, 66, 66, 0, 4, 4, 4, 4, 4, 4, 4, 4, 4,
|
||||
4, 4, 4, 6, 6, 6, 8, 8, 8, 12, 12, 12, 16, 16, 16, 20, 20, 20, 26, 26, 26, 34, 34, 34,
|
||||
42, 42, 42, 12, 12, 12, 0,
|
||||
}
|
||||
T.g_scf_mixed = { [0] = 6,
|
||||
6, 6, 6, 6, 6, 6, 6, 6, 8, 8, 8, 10, 10, 10, 12, 12, 12, 14, 14, 14, 18, 18, 18, 24,
|
||||
24, 24, 30, 30, 30, 40, 40, 40, 18, 18, 18, 0, 0, 0, 0, 12, 12, 12, 4, 4, 4, 8, 8, 8,
|
||||
12, 12, 12, 16, 16, 16, 20, 20, 20, 24, 24, 24, 28, 28, 28, 36, 36, 36, 2, 2, 2, 2, 2, 2,
|
||||
2, 2, 2, 26, 26, 26, 0, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 8, 8, 8, 10, 10,
|
||||
10, 14, 14, 14, 18, 18, 18, 26, 26, 26, 32, 32, 32, 42, 42, 42, 18, 18, 18, 0, 0, 0, 0, 6,
|
||||
6, 6, 6, 6, 6, 6, 6, 6, 8, 8, 8, 10, 10, 10, 12, 12, 12, 14, 14, 14, 18, 18, 18, 24,
|
||||
24, 24, 32, 32, 32, 44, 44, 44, 12, 12, 12, 0, 0, 0, 0, 6, 6, 6, 6, 6, 6, 6, 6, 6,
|
||||
8, 8, 8, 10, 10, 10, 12, 12, 12, 14, 14, 14, 18, 18, 18, 24, 24, 24, 30, 30, 30, 40, 40, 40,
|
||||
18, 18, 18, 0, 0, 0, 0, 4, 4, 4, 4, 4, 4, 6, 6, 4, 4, 4, 6, 6, 6, 8, 8, 8,
|
||||
10, 10, 10, 12, 12, 12, 14, 14, 14, 18, 18, 18, 22, 22, 22, 30, 30, 30, 56, 56, 56, 0, 0, 4,
|
||||
4, 4, 4, 4, 4, 6, 6, 4, 4, 4, 6, 6, 6, 6, 6, 6, 10, 10, 10, 12, 12, 12, 14, 14,
|
||||
14, 16, 16, 16, 20, 20, 20, 26, 26, 26, 66, 66, 66, 0, 0, 4, 4, 4, 4, 4, 4, 6, 6, 4,
|
||||
4, 4, 6, 6, 6, 8, 8, 8, 12, 12, 12, 16, 16, 16, 20, 20, 20, 26, 26, 26, 34, 34, 34, 42,
|
||||
42, 42, 12, 12, 12, 0, 0,
|
||||
}
|
||||
T.g_expfrac = { [0] = 9.31322575e-10,
|
||||
7.83145814e-10, 6.58544508e-10, 5.53767716e-10,
|
||||
}
|
||||
T.g_scf_partitions = { [0] = 6,
|
||||
5, 5, 5, 6, 5, 5, 5, 6, 5, 7, 3, 11, 10, 0, 0, 7, 7, 7, 0, 6, 6, 6, 3, 8,
|
||||
8, 5, 0, 8, 9, 6, 12, 6, 9, 9, 9, 6, 9, 12, 6, 15, 18, 0, 0, 6, 15, 12, 0, 6,
|
||||
12, 9, 6, 6, 18, 9, 0, 9, 9, 6, 12, 9, 9, 9, 9, 9, 9, 12, 6, 18, 18, 0, 0, 12,
|
||||
12, 12, 0, 12, 9, 9, 6, 15, 12, 9, 0,
|
||||
}
|
||||
T.g_scfc_decode = { [0] = 0,
|
||||
1, 2, 3, 12, 5, 6, 7, 9, 10, 11, 13, 14, 15, 18, 19,
|
||||
}
|
||||
T.g_mod = { [0] = 5,
|
||||
5, 4, 4, 5, 5, 4, 1, 4, 3, 1, 1, 5, 6, 6, 1, 4, 4, 4, 1, 4, 3, 1, 1,
|
||||
}
|
||||
T.g_preamp = { [0] = 1,
|
||||
1, 1, 1, 2, 2, 3, 3, 3, 2,
|
||||
}
|
||||
T.g_pow43 = { [0] = 0,
|
||||
-1, -2.519842, -4.326749, -6.349604, -8.549880, -10.902724, -13.390518, -16.000000, -18.720754, -21.544347, -24.463781, -27.473142, -30.567351, -33.741992, -36.993181, 0, 1, 2.519842, 4.326749, 6.349604, 8.549880, 10.902724, 13.390518, 16.000000,
|
||||
18.720754, 21.544347, 24.463781, 27.473142, 30.567351, 33.741992, 36.993181, 40.317474, 43.711787, 47.173345, 50.699631, 54.288352, 57.937408, 61.644865, 65.408941, 69.227979, 73.100443, 77.024898, 81.000000, 85.024491, 89.097188, 93.216975, 97.382800, 101.593667,
|
||||
105.848633, 110.146801, 114.487321, 118.869381, 123.292209, 127.755065, 132.257246, 136.798076, 141.376907, 145.993119, 150.646117, 155.335327, 160.060199, 164.820202, 169.614826, 174.443577, 179.305980, 184.201575, 189.129918, 194.090580, 199.083145, 204.107210, 209.162385, 214.248292,
|
||||
219.364564, 224.510845, 229.686789, 234.892058, 240.126328, 245.389280, 250.680604, 256.000000, 261.347174, 266.721841, 272.123723, 277.552547, 283.008049, 288.489971, 293.998060, 299.532071, 305.091761, 310.676898, 316.287249, 321.922592, 327.582707, 333.267377, 338.976394, 344.709550,
|
||||
350.466646, 356.247482, 362.051866, 367.879608, 373.730522, 379.604427, 385.501143, 391.420496, 397.362314, 403.326427, 409.312672, 415.320884, 421.350905, 427.402579, 433.475750, 439.570269, 445.685987, 451.822757, 457.980436, 464.158883, 470.357960, 476.577530, 482.817459, 489.077615,
|
||||
495.357868, 501.658090, 507.978156, 514.317941, 520.677324, 527.056184, 533.454404, 539.871867, 546.308458, 552.764065, 559.238575, 565.731879, 572.243870, 578.774440, 585.323483, 591.890898, 598.476581, 605.080431, 611.702349, 618.342238, 625.000000, 631.675540, 638.368763, 645.079578,
|
||||
}
|
||||
T.tabs = { [0] = 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 785, 785, 785, 785, 784, 784, 784, 784, 513, 513, 513, 513, 513, 513, 513, 513, 256,
|
||||
256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, -255, 1313, 1298, 1282, 785, 785, 785, 785, 784,
|
||||
784, 784, 784, 769, 769, 769, 769, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 290,
|
||||
288, -255, 1313, 1298, 1282, 769, 769, 769, 769, 529, 529, 529, 529, 529, 529, 529, 529, 528, 528, 528, 528, 528, 528, 528,
|
||||
528, 512, 512, 512, 512, 512, 512, 512, 512, 290, 288, -253, -318, -351, -367, 785, 785, 785, 785, 784, 784, 784, 784, 769,
|
||||
769, 769, 769, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 819, 818, 547, 547, 275,
|
||||
275, 275, 275, 561, 560, 515, 546, 289, 274, 288, 258, -254, -287, 1329, 1299, 1314, 1312, 1057, 1057, 1042, 1042, 1026, 1026, 784,
|
||||
784, 784, 784, 529, 529, 529, 529, 529, 529, 529, 529, 769, 769, 769, 769, 768, 768, 768, 768, 563, 560, 306, 306, 291,
|
||||
259, -252, -413, -477, -542, 1298, -575, 1041, 1041, 784, 784, 784, 784, 769, 769, 769, 769, 256, 256, 256, 256, 256, 256, 256,
|
||||
256, 256, 256, 256, 256, 256, 256, 256, 256, -383, -399, 1107, 1092, 1106, 1061, 849, 849, 789, 789, 1104, 1091, 773, 773, 1076,
|
||||
1075, 341, 340, 325, 309, 834, 804, 577, 577, 532, 532, 516, 516, 832, 818, 803, 816, 561, 561, 531, 531, 515, 546, 289,
|
||||
289, 288, 258, -252, -429, -493, -559, 1057, 1057, 1042, 1042, 529, 529, 529, 529, 529, 529, 529, 529, 784, 784, 784, 784, 769,
|
||||
769, 769, 769, 512, 512, 512, 512, 512, 512, 512, 512, -382, 1077, -415, 1106, 1061, 1104, 849, 849, 789, 789, 1091, 1076, 1029,
|
||||
1075, 834, 834, 597, 581, 340, 340, 339, 324, 804, 833, 532, 532, 832, 772, 818, 803, 817, 787, 816, 771, 290, 290, 290,
|
||||
290, 288, 258, -253, -349, -414, -447, -463, 1329, 1299, -479, 1314, 1312, 1057, 1057, 1042, 1042, 1026, 1026, 785, 785, 785, 785, 784,
|
||||
784, 784, 784, 769, 769, 769, 769, 768, 768, 768, 768, -319, 851, 821, -335, 836, 850, 805, 849, 341, 340, 325, 336, 533,
|
||||
533, 579, 579, 564, 564, 773, 832, 578, 548, 563, 516, 321, 276, 306, 291, 304, 259, -251, -572, -733, -830, -863, -879, 1041,
|
||||
1041, 784, 784, 784, 784, 769, 769, 769, 769, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256,
|
||||
256, -511, -527, -543, 1396, 1351, 1381, 1366, 1395, 1335, 1380, -559, 1334, 1138, 1138, 1063, 1063, 1350, 1392, 1031, 1031, 1062, 1062, 1364,
|
||||
1363, 1120, 1120, 1333, 1348, 881, 881, 881, 881, 375, 374, 359, 373, 343, 358, 341, 325, 791, 791, 1123, 1122, -703, 1105, 1045,
|
||||
-719, 865, 865, 790, 790, 774, 774, 1104, 1029, 338, 293, 323, 308, -799, -815, 833, 788, 772, 818, 803, 816, 322, 292, 307,
|
||||
320, 561, 531, 515, 546, 289, 274, 288, 258, -251, -525, -605, -685, -765, -831, -846, 1298, 1057, 1057, 1312, 1282, 785, 785, 785,
|
||||
785, 784, 784, 784, 784, 769, 769, 769, 769, 512, 512, 512, 512, 512, 512, 512, 512, 1399, 1398, 1383, 1367, 1382, 1396, 1351,
|
||||
-511, 1381, 1366, 1139, 1139, 1079, 1079, 1124, 1124, 1364, 1349, 1363, 1333, 882, 882, 882, 882, 807, 807, 807, 807, 1094, 1094, 1136,
|
||||
1136, 373, 341, 535, 535, 881, 775, 867, 822, 774, -591, 324, 338, -671, 849, 550, 550, 866, 864, 609, 609, 293, 336, 534,
|
||||
534, 789, 835, 773, -751, 834, 804, 308, 307, 833, 788, 832, 772, 562, 562, 547, 547, 305, 275, 560, 515, 290, 290, -252,
|
||||
-397, -477, -557, -622, -653, -719, -735, -750, 1329, 1299, 1314, 1057, 1057, 1042, 1042, 1312, 1282, 1024, 1024, 785, 785, 785, 785, 784,
|
||||
784, 784, 784, 769, 769, 769, 769, -383, 1127, 1141, 1111, 1126, 1140, 1095, 1110, 869, 869, 883, 883, 1079, 1109, 882, 882, 375,
|
||||
374, 807, 868, 838, 881, 791, -463, 867, 822, 368, 263, 852, 837, 836, -543, 610, 610, 550, 550, 352, 336, 534, 534, 865,
|
||||
774, 851, 821, 850, 805, 593, 533, 579, 564, 773, 832, 578, 578, 548, 548, 577, 577, 307, 276, 306, 291, 516, 560, 259,
|
||||
259, -250, -2107, -2507, -2764, -2909, -2974, -3007, -3023, 1041, 1041, 1040, 1040, 769, 769, 769, 769, 256, 256, 256, 256, 256, 256, 256,
|
||||
256, 256, 256, 256, 256, 256, 256, 256, 256, -767, -1052, -1213, -1277, -1358, -1405, -1469, -1535, -1550, -1582, -1614, -1647, -1662, -1694, -1726,
|
||||
-1759, -1774, -1807, -1822, -1854, -1886, 1565, -1919, -1935, -1951, -1967, 1731, 1730, 1580, 1717, -1983, 1729, 1564, -1999, 1548, -2015, -2031, 1715, 1595,
|
||||
-2047, 1714, -2063, 1610, -2079, 1609, -2095, 1323, 1323, 1457, 1457, 1307, 1307, 1712, 1547, 1641, 1700, 1699, 1594, 1685, 1625, 1442, 1442, 1322,
|
||||
1322, -780, -973, -910, 1279, 1278, 1277, 1262, 1276, 1261, 1275, 1215, 1260, 1229, -959, 974, 974, 989, 989, -943, 735, 478, 478, 495,
|
||||
463, 506, 414, -1039, 1003, 958, 1017, 927, 942, 987, 957, 431, 476, 1272, 1167, 1228, -1183, 1256, -1199, 895, 895, 941, 941, 1242,
|
||||
1227, 1212, 1135, 1014, 1014, 490, 489, 503, 487, 910, 1013, 985, 925, 863, 894, 970, 955, 1012, 847, -1343, 831, 755, 755, 984,
|
||||
909, 428, 366, 754, 559, -1391, 752, 486, 457, 924, 997, 698, 698, 983, 893, 740, 740, 908, 877, 739, 739, 667, 667, 953,
|
||||
938, 497, 287, 271, 271, 683, 606, 590, 712, 726, 574, 302, 302, 738, 736, 481, 286, 526, 725, 605, 711, 636, 724, 696,
|
||||
651, 589, 681, 666, 710, 364, 467, 573, 695, 466, 466, 301, 465, 379, 379, 709, 604, 665, 679, 316, 316, 634, 633, 436,
|
||||
436, 464, 269, 424, 394, 452, 332, 438, 363, 347, 408, 393, 448, 331, 422, 362, 407, 392, 421, 346, 406, 391, 376, 375,
|
||||
359, 1441, 1306, -2367, 1290, -2383, 1337, -2399, -2415, 1426, 1321, -2431, 1411, 1336, -2447, -2463, -2479, 1169, 1169, 1049, 1049, 1424, 1289, 1412,
|
||||
1352, 1319, -2495, 1154, 1154, 1064, 1064, 1153, 1153, 416, 390, 360, 404, 403, 389, 344, 374, 373, 343, 358, 372, 327, 357, 342,
|
||||
311, 356, 326, 1395, 1394, 1137, 1137, 1047, 1047, 1365, 1392, 1287, 1379, 1334, 1364, 1349, 1378, 1318, 1363, 792, 792, 792, 792, 1152,
|
||||
1152, 1032, 1032, 1121, 1121, 1046, 1046, 1120, 1120, 1030, 1030, -2895, 1106, 1061, 1104, 849, 849, 789, 789, 1091, 1076, 1029, 1090, 1060,
|
||||
1075, 833, 833, 309, 324, 532, 532, 832, 772, 818, 803, 561, 561, 531, 560, 515, 546, 289, 274, 288, 258, -250, -1179, -1579,
|
||||
-1836, -1996, -2124, -2253, -2333, -2413, -2477, -2542, -2574, -2607, -2622, -2655, 1314, 1313, 1298, 1312, 1282, 785, 785, 785, 785, 1040, 1040, 1025,
|
||||
1025, 768, 768, 768, 768, -766, -798, -830, -862, -895, -911, -927, -943, -959, -975, -991, -1007, -1023, -1039, -1055, -1070, 1724, 1647, -1103,
|
||||
-1119, 1631, 1767, 1662, 1738, 1708, 1723, -1135, 1780, 1615, 1779, 1599, 1677, 1646, 1778, 1583, -1151, 1777, 1567, 1737, 1692, 1765, 1722, 1707,
|
||||
1630, 1751, 1661, 1764, 1614, 1736, 1676, 1763, 1750, 1645, 1598, 1721, 1691, 1762, 1706, 1582, 1761, 1566, -1167, 1749, 1629, 767, 766, 751,
|
||||
765, 494, 494, 735, 764, 719, 749, 734, 763, 447, 447, 748, 718, 477, 506, 431, 491, 446, 476, 461, 505, 415, 430, 475,
|
||||
445, 504, 399, 460, 489, 414, 503, 383, 474, 429, 459, 502, 502, 746, 752, 488, 398, 501, 473, 413, 472, 486, 271, 480,
|
||||
270, -1439, -1455, 1357, -1471, -1487, -1503, 1341, 1325, -1519, 1489, 1463, 1403, 1309, -1535, 1372, 1448, 1418, 1476, 1356, 1462, 1387, -1551, 1475,
|
||||
1340, 1447, 1402, 1386, -1567, 1068, 1068, 1474, 1461, 455, 380, 468, 440, 395, 425, 410, 454, 364, 467, 466, 464, 453, 269, 409,
|
||||
448, 268, 432, 1371, 1473, 1432, 1417, 1308, 1460, 1355, 1446, 1459, 1431, 1083, 1083, 1401, 1416, 1458, 1445, 1067, 1067, 1370, 1457, 1051,
|
||||
1051, 1291, 1430, 1385, 1444, 1354, 1415, 1400, 1443, 1082, 1082, 1173, 1113, 1186, 1066, 1185, 1050, -1967, 1158, 1128, 1172, 1097, 1171, 1081,
|
||||
-1983, 1157, 1112, 416, 266, 375, 400, 1170, 1142, 1127, 1065, 793, 793, 1169, 1033, 1156, 1096, 1141, 1111, 1155, 1080, 1126, 1140, 898,
|
||||
898, 808, 808, 897, 897, 792, 792, 1095, 1152, 1032, 1125, 1110, 1139, 1079, 1124, 882, 807, 838, 881, 853, 791, -2319, 867, 368,
|
||||
263, 822, 852, 837, 866, 806, 865, -2399, 851, 352, 262, 534, 534, 821, 836, 594, 594, 549, 549, 593, 593, 533, 533, 848,
|
||||
773, 579, 579, 564, 578, 548, 563, 276, 276, 577, 576, 306, 291, 516, 560, 305, 305, 275, 259, -251, -892, -2058, -2620, -2828,
|
||||
-2957, -3023, -3039, 1041, 1041, 1040, 1040, 769, 769, 769, 769, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256,
|
||||
256, 256, 256, -511, -527, -543, -559, 1530, -575, -591, 1528, 1527, 1407, 1526, 1391, 1023, 1023, 1023, 1023, 1525, 1375, 1268, 1268, 1103,
|
||||
1103, 1087, 1087, 1039, 1039, 1523, -604, 815, 815, 815, 815, 510, 495, 509, 479, 508, 463, 507, 447, 431, 505, 415, 399, -734,
|
||||
-782, 1262, -815, 1259, 1244, -831, 1258, 1228, -847, -863, 1196, -879, 1253, 987, 987, 748, -767, 493, 493, 462, 477, 414, 414, 686,
|
||||
669, 478, 446, 461, 445, 474, 429, 487, 458, 412, 471, 1266, 1264, 1009, 1009, 799, 799, -1019, -1276, -1452, -1581, -1677, -1757, -1821,
|
||||
-1886, -1933, -1997, 1257, 1257, 1483, 1468, 1512, 1422, 1497, 1406, 1467, 1496, 1421, 1510, 1134, 1134, 1225, 1225, 1466, 1451, 1374, 1405, 1252,
|
||||
1252, 1358, 1480, 1164, 1164, 1251, 1251, 1238, 1238, 1389, 1465, -1407, 1054, 1101, -1423, 1207, -1439, 830, 830, 1248, 1038, 1237, 1117, 1223,
|
||||
1148, 1236, 1208, 411, 426, 395, 410, 379, 269, 1193, 1222, 1132, 1235, 1221, 1116, 976, 976, 1192, 1162, 1177, 1220, 1131, 1191, 963,
|
||||
963, -1647, 961, 780, -1663, 558, 558, 994, 993, 437, 408, 393, 407, 829, 978, 813, 797, 947, -1743, 721, 721, 377, 392, 844,
|
||||
950, 828, 890, 706, 706, 812, 859, 796, 960, 948, 843, 934, 874, 571, 571, -1919, 690, 555, 689, 421, 346, 539, 539, 944,
|
||||
779, 918, 873, 932, 842, 903, 888, 570, 570, 931, 917, 674, 674, -2575, 1562, -2591, 1609, -2607, 1654, 1322, 1322, 1441, 1441, 1696,
|
||||
1546, 1683, 1593, 1669, 1624, 1426, 1426, 1321, 1321, 1639, 1680, 1425, 1425, 1305, 1305, 1545, 1668, 1608, 1623, 1667, 1592, 1638, 1666, 1320,
|
||||
1320, 1652, 1607, 1409, 1409, 1304, 1304, 1288, 1288, 1664, 1637, 1395, 1395, 1335, 1335, 1622, 1636, 1394, 1394, 1319, 1319, 1606, 1621, 1392,
|
||||
1392, 1137, 1137, 1137, 1137, 345, 390, 360, 375, 404, 373, 1047, -2751, -2767, -2783, 1062, 1121, 1046, -2799, 1077, -2815, 1106, 1061, 789,
|
||||
789, 1105, 1104, 263, 355, 310, 340, 325, 354, 352, 262, 339, 324, 1091, 1076, 1029, 1090, 1060, 1075, 833, 833, 788, 788, 1088,
|
||||
1028, 818, 818, 803, 803, 561, 561, 531, 531, 816, 771, 546, 546, 289, 274, 288, 258, -253, -317, -381, -446, -478, -509, 1279,
|
||||
1279, -811, -1179, -1451, -1756, -1900, -2028, -2189, -2253, -2333, -2414, -2445, -2511, -2526, 1313, 1298, -2559, 1041, 1041, 1040, 1040, 1025, 1025, 1024,
|
||||
1024, 1022, 1007, 1021, 991, 1020, 975, 1019, 959, 687, 687, 1018, 1017, 671, 671, 655, 655, 1016, 1015, 639, 639, 758, 758, 623,
|
||||
623, 757, 607, 756, 591, 755, 575, 754, 559, 543, 543, 1009, 783, -575, -621, -685, -749, 496, -590, 750, 749, 734, 748, 974,
|
||||
989, 1003, 958, 988, 973, 1002, 942, 987, 957, 972, 1001, 926, 986, 941, 971, 956, 1000, 910, 985, 925, 999, 894, 970, -1071,
|
||||
-1087, -1102, 1390, -1135, 1436, 1509, 1451, 1374, -1151, 1405, 1358, 1480, 1420, -1167, 1507, 1494, 1389, 1342, 1465, 1435, 1450, 1326, 1505, 1310,
|
||||
1493, 1373, 1479, 1404, 1492, 1464, 1419, 428, 443, 472, 397, 736, 526, 464, 464, 486, 457, 442, 471, 484, 482, 1357, 1449, 1434,
|
||||
1478, 1388, 1491, 1341, 1490, 1325, 1489, 1463, 1403, 1309, 1477, 1372, 1448, 1418, 1433, 1476, 1356, 1462, 1387, -1439, 1475, 1340, 1447, 1402,
|
||||
1474, 1324, 1461, 1371, 1473, 269, 448, 1432, 1417, 1308, 1460, -1711, 1459, -1727, 1441, 1099, 1099, 1446, 1386, 1431, 1401, -1743, 1289, 1083,
|
||||
1083, 1160, 1160, 1458, 1445, 1067, 1067, 1370, 1457, 1307, 1430, 1129, 1129, 1098, 1098, 268, 432, 267, 416, 266, 400, -1887, 1144, 1187,
|
||||
1082, 1173, 1113, 1186, 1066, 1050, 1158, 1128, 1143, 1172, 1097, 1171, 1081, 420, 391, 1157, 1112, 1170, 1142, 1127, 1065, 1169, 1049, 1156,
|
||||
1096, 1141, 1111, 1155, 1080, 1126, 1154, 1064, 1153, 1140, 1095, 1048, -2159, 1125, 1110, 1137, -2175, 823, 823, 1139, 1138, 807, 807, 384,
|
||||
264, 368, 263, 868, 838, 853, 791, 867, 822, 852, 837, 866, 806, 865, 790, -2319, 851, 821, 836, 352, 262, 850, 805, 849,
|
||||
-2399, 533, 533, 835, 820, 336, 261, 578, 548, 563, 577, 532, 532, 832, 772, 562, 562, 547, 547, 305, 275, 560, 515, 290,
|
||||
290, 288, 258,
|
||||
}
|
||||
T.tab32 = { [0] = 130,
|
||||
162, 193, 209, 44, 28, 76, 140, 9, 9, 9, 9, 9, 9, 9, 9, 190, 254, 222, 238, 126, 94, 157, 157, 109,
|
||||
61, 173, 205,
|
||||
}
|
||||
T.tab33 = { [0] = 252,
|
||||
236, 220, 204, 188, 172, 156, 140, 124, 108, 92, 76, 60, 44, 28, 12,
|
||||
}
|
||||
T.tabindex = { [0] = 0,
|
||||
32, 64, 98, 0, 132, 180, 218, 292, 364, 426, 538, 648, 746, 0, 1126, 1460, 1460, 1460, 1460, 1460, 1460, 1460, 1460, 1842,
|
||||
1842, 1842, 1842, 1842, 1842, 1842, 1842,
|
||||
}
|
||||
T.g_linbits = { [0] = 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 8, 10, 13, 4,
|
||||
5, 6, 7, 8, 9, 11, 13,
|
||||
}
|
||||
T.g_pan = { [0] = 0,
|
||||
1, 0.21132487, 0.78867513, 0.36602540, 0.63397460, 0.5, 0.5, 0.63397460, 0.36602540, 0.78867513, 0.21132487, 1, 0,
|
||||
}
|
||||
T.g_aa = { [0] = 0.85749293,
|
||||
0.88174200, 0.94962865, 0.98331459, 0.99551782, 0.99916056, 0.99989920, 0.99999316, 0.51449576, 0.47173197, 0.31337745, 0.18191320, 0.09457419, 0.04096558, 0.01419856, 0.00369997,
|
||||
}
|
||||
T.g_twid9 = { [0] = 0.73727734,
|
||||
0.79335334, 0.84339145, 0.88701083, 0.92387953, 0.95371695, 0.97629601, 0.99144486, 0.99904822, 0.67559021, 0.60876143, 0.53729961, 0.46174861, 0.38268343, 0.30070580, 0.21643961, 0.13052619, 0.04361938,
|
||||
}
|
||||
T.g_twid3 = { [0] = 0.79335334,
|
||||
0.92387953, 0.99144486, 0.60876143, 0.38268343, 0.13052619,
|
||||
}
|
||||
T.g_mdct_window = { [0] = 0.99904822,
|
||||
0.99144486, 0.97629601, 0.95371695, 0.92387953, 0.88701083, 0.84339145, 0.79335334, 0.73727734, 0.04361938, 0.13052619, 0.21643961, 0.30070580, 0.38268343, 0.46174861, 0.53729961, 0.60876143, 0.67559021, 1, 1, 1, 1, 1, 1, 0.99144486,
|
||||
0.92387953, 0.79335334, 0, 0, 0, 0, 0, 0, 0.13052619, 0.38268343, 0.60876143,
|
||||
}
|
||||
T.g_sec = { [0] = 10.19000816,
|
||||
0.50060302, 0.50241929, 3.40760851, 0.50547093, 0.52249861, 2.05778098, 0.51544732, 0.56694406, 1.48416460, 0.53104258, 0.64682180, 1.16943991, 0.55310392, 0.78815460, 0.97256821, 0.58293498, 1.06067765, 0.83934963, 0.62250412, 1.72244716, 0.74453628, 0.67480832, 5.10114861,
|
||||
}
|
||||
T.g_win = { [0] = -1,
|
||||
26, -31, 208, 218, 401, -519, 2063, 2000, 4788, -5517, 7134, 5959, 35640, -39336, 74992, -1, 24, -35, 202, 222, 347, -581, 2080, 1952,
|
||||
4425, -5879, 7640, 5288, 33791, -41176, 74856, -1, 21, -38, 196, 225, 294, -645, 2087, 1893, 4063, -6237, 8092, 4561, 31947, -43006, 74630, -1,
|
||||
19, -41, 190, 227, 244, -711, 2085, 1822, 3705, -6589, 8492, 3776, 30112, -44821, 74313, -1, 17, -45, 183, 228, 197, -779, 2075, 1739,
|
||||
3351, -6935, 8840, 2935, 28289, -46617, 73908, -1, 16, -49, 176, 228, 153, -848, 2057, 1644, 3004, -7271, 9139, 2037, 26482, -48390, 73415, -2,
|
||||
14, -53, 169, 227, 111, -919, 2032, 1535, 2663, -7597, 9389, 1082, 24694, -50137, 72835, -2, 13, -58, 161, 224, 72, -991, 2001, 1414,
|
||||
2330, -7910, 9592, 70, 22929, -51853, 72169, -2, 11, -63, 154, 221, 36, -1064, 1962, 1280, 2006, -8209, 9750, -998, 21189, -53534, 71420, -2,
|
||||
10, -68, 147, 215, 2, -1137, 1919, 1131, 1692, -8491, 9863, -2122, 19478, -55178, 70590, -3, 9, -73, 139, 208, -29, -1210, 1870, 970,
|
||||
1388, -8755, 9935, -3300, 17799, -56778, 69679, -3, 8, -79, 132, 200, -57, -1283, 1817, 794, 1095, -8998, 9966, -4533, 16155, -58333, 68692, -4,
|
||||
7, -85, 125, 189, -83, -1356, 1759, 605, 814, -9219, 9959, -5818, 14548, -59838, 67629, -4, 7, -91, 117, 177, -106, -1428, 1698, 402,
|
||||
545, -9416, 9916, -7154, 12980, -61289, 66494, -5, 6, -97, 111, 163, -127, -1498, 1634, 185, 288, -9585, 9838, -8540, 11455, -62684, 65290,
|
||||
}
|
||||
return T
|
||||
@@ -0,0 +1,315 @@
|
||||
--!strict
|
||||
--!optimize 2
|
||||
-- audio/mp3/Tags: ID3 tags: ID3v2.2 / 2.3 / 2.4 at the start (text frames, COMM, unsynchronisation, extended header) and ID3v1 /
|
||||
-- ID3v1.1 at the end; APEv2 tags are only skipped. Also finds the byte range that holds the audio, the same way
|
||||
-- minimp3_ex does (mp3dec_skip_id3). Text is returned as UTF-8. Never throws: malformed tags are cut short.
|
||||
local band, bor, lshift = bit32.band, bit32.bor, bit32.lshift
|
||||
local ru8 = buffer.readu8
|
||||
|
||||
local Tags = {}
|
||||
|
||||
-- ID3v1 genres 0..147 (the original 80 + the Winamp extensions)
|
||||
local GENRES = {
|
||||
[0] = "Blues", "Classic Rock", "Country", "Dance", "Disco", "Funk", "Grunge", "Hip-Hop", "Jazz", "Metal",
|
||||
"New Age", "Oldies", "Other", "Pop", "R&B", "Rap", "Reggae", "Rock", "Techno", "Industrial", "Alternative",
|
||||
"Ska", "Death Metal", "Pranks", "Soundtrack", "Euro-Techno", "Ambient", "Trip-Hop", "Vocal", "Jazz+Funk",
|
||||
"Fusion", "Trance", "Classical", "Instrumental", "Acid", "House", "Game", "Sound Clip", "Gospel", "Noise",
|
||||
"AlternRock", "Bass", "Soul", "Punk", "Space", "Meditative", "Instrumental Pop", "Instrumental Rock", "Ethnic",
|
||||
"Gothic", "Darkwave", "Techno-Industrial", "Electronic", "Pop-Folk", "Eurodance", "Dream", "Southern Rock",
|
||||
"Comedy", "Cult", "Gangsta", "Top 40", "Christian Rap", "Pop/Funk", "Jungle", "Native American", "Cabaret",
|
||||
"New Wave", "Psychadelic", "Rave", "Showtunes", "Trailer", "Lo-Fi", "Tribal", "Acid Punk", "Acid Jazz", "Polka",
|
||||
"Retro", "Musical", "Rock & Roll", "Hard Rock", "Folk", "Folk-Rock", "National Folk", "Swing", "Fast Fusion",
|
||||
"Bebob", "Latin", "Revival", "Celtic", "Bluegrass", "Avantgarde", "Gothic Rock", "Progressive Rock",
|
||||
"Psychedelic Rock", "Symphonic Rock", "Slow Rock", "Big Band", "Chorus", "Easy Listening", "Acoustic", "Humour",
|
||||
"Speech", "Chanson", "Opera", "Chamber Music", "Sonata", "Symphony", "Booty Bass", "Primus", "Porn Groove",
|
||||
"Satire", "Slow Jam", "Club", "Tango", "Samba", "Folklore", "Ballad", "Power Ballad", "Rhythmic Soul",
|
||||
"Freestyle", "Duet", "Punk Rock", "Drum Solo", "A capella", "Euro-House", "Dance Hall", "Goa", "Drum & Bass",
|
||||
"Club-House", "Hardcore", "Terror", "Indie", "BritPop", "Negerpunk", "Polsk Punk", "Beat",
|
||||
"Christian Gangsta Rap", "Heavy Metal", "Black Metal", "Crossover", "Contemporary Christian", "Christian Rock",
|
||||
"Merengue", "Salsa", "Thrash Metal", "Anime", "JPop", "Synthpop",
|
||||
}
|
||||
Tags.GENRES = GENRES
|
||||
|
||||
-- frame id -> field name (v2.3/2.4 ids and their v2.2 equivalents)
|
||||
local FIELDS = {
|
||||
TIT2 = "title", TT2 = "title",
|
||||
TPE1 = "artist", TP1 = "artist",
|
||||
TALB = "album", TAL = "album",
|
||||
TPE2 = "albumArtist", TP2 = "albumArtist",
|
||||
TYER = "year", TYE = "year", TDRC = "year",
|
||||
TRCK = "track", TRK = "track",
|
||||
TPOS = "disc", TPA = "disc",
|
||||
TCON = "genre", TCO = "genre",
|
||||
TCOM = "composer", TCM = "composer",
|
||||
COMM = "comment", COM = "comment",
|
||||
}
|
||||
|
||||
local function utf8char(cp: number): string
|
||||
if cp < 0 or cp > 0x10FFFF or (cp >= 0xD800 and cp <= 0xDFFF) then
|
||||
cp = 0xFFFD
|
||||
end
|
||||
return utf8.char(cp)
|
||||
end
|
||||
|
||||
-- bytes [i, j) of b, decoded from ID3 text encoding enc (0 latin-1, 1 UTF-16 + BOM, 2 UTF-16BE, 3 UTF-8)
|
||||
local function decodeText(b: buffer, i: number, j: number, enc: number): string
|
||||
local out = {}
|
||||
if enc == 1 or enc == 2 then
|
||||
local le = false
|
||||
if enc == 1 and j - i >= 2 then
|
||||
local b0, b1 = ru8(b, i), ru8(b, i + 1)
|
||||
if b0 == 0xFF and b1 == 0xFE then
|
||||
le = true
|
||||
i += 2
|
||||
elseif b0 == 0xFE and b1 == 0xFF then
|
||||
i += 2
|
||||
else
|
||||
le = true -- no BOM: assume little-endian (what most taggers write)
|
||||
end
|
||||
end
|
||||
local pending = 0
|
||||
while i + 1 < j do
|
||||
local u = if le then bor(ru8(b, i), lshift(ru8(b, i + 1), 8)) else bor(lshift(ru8(b, i), 8), ru8(b, i + 1))
|
||||
i += 2
|
||||
if u >= 0xD800 and u <= 0xDBFF then
|
||||
pending = u
|
||||
else
|
||||
if u >= 0xDC00 and u <= 0xDFFF and pending ~= 0 then
|
||||
u = 0x10000 + lshift(pending - 0xD800, 10) + (u - 0xDC00)
|
||||
end
|
||||
pending = 0
|
||||
table.insert(out, if u == 0 then "\0" else utf8char(u))
|
||||
end
|
||||
end
|
||||
elseif enc == 3 then
|
||||
local s = buffer.readstring(b, i, j - i)
|
||||
if utf8.len(s) then
|
||||
return s
|
||||
end
|
||||
enc = 0 -- invalid UTF-8: fall back to latin-1
|
||||
end
|
||||
if enc == 0 then
|
||||
for k = i, j - 1 do
|
||||
local c = ru8(b, k)
|
||||
table.insert(out, if c < 0x80 then string.char(c) else utf8char(c))
|
||||
end
|
||||
end
|
||||
return table.concat(out)
|
||||
end
|
||||
|
||||
-- trim trailing NULs / spaces, join multi-value NUL separators with " / "
|
||||
local function clean(s: string): string
|
||||
s = string.gsub(s, "[%z%s]+$", "")
|
||||
s = string.gsub(s, "%z+", " / ")
|
||||
return s
|
||||
end
|
||||
|
||||
-- "(17)", "(17)Rock", "17" -> "Rock"
|
||||
local function genreName(s: string): string
|
||||
local n = tonumber(string.match(s, "^%((%d+)%)$") or string.match(s, "^(%d+)$"))
|
||||
if n and GENRES[n] then
|
||||
return GENRES[n]
|
||||
end
|
||||
local rest = string.match(s, "^%(%d+%)(.+)$")
|
||||
return rest or s
|
||||
end
|
||||
|
||||
local function syncsafe(b: buffer, o: number): number
|
||||
return bor(lshift(band(ru8(b, o), 0x7F), 21), lshift(band(ru8(b, o + 1), 0x7F), 14), lshift(band(ru8(b, o + 2), 0x7F), 7), band(ru8(b, o + 3), 0x7F))
|
||||
end
|
||||
|
||||
local function be32(b: buffer, o: number): number
|
||||
return bor(lshift(ru8(b, o), 24), lshift(ru8(b, o + 1), 16), lshift(ru8(b, o + 2), 8), ru8(b, o + 3))
|
||||
end
|
||||
|
||||
-- undo unsynchronisation (FF 00 -> FF) of b[i, j) into a new buffer
|
||||
local function deunsync(b: buffer, i: number, j: number): buffer
|
||||
local out = buffer.create(math.max(0, j - i))
|
||||
local n = 0
|
||||
local k = i
|
||||
while k < j do
|
||||
local c = ru8(b, k)
|
||||
buffer.writeu8(out, n, c)
|
||||
n += 1
|
||||
k += if c == 0xFF and k + 1 < j and ru8(b, k + 1) == 0 then 2 else 1
|
||||
end
|
||||
local r = buffer.create(n)
|
||||
buffer.copy(r, 0, out, 0, n)
|
||||
return r
|
||||
end
|
||||
|
||||
-- minimp3's ID3v2 detection: total tag size (header + body + footer) or 0
|
||||
function Tags.id3v2Size(b: buffer, o: number, len: number): number
|
||||
if len - o < 10 or buffer.readstring(b, o, 3) ~= "ID3" then
|
||||
return 0
|
||||
end
|
||||
if band(ru8(b, o + 5), 15) ~= 0 then
|
||||
return 0
|
||||
end
|
||||
for k = 6, 9 do
|
||||
if band(ru8(b, o + k), 0x80) ~= 0 then
|
||||
return 0
|
||||
end
|
||||
end
|
||||
local size = syncsafe(b, o + 6) + 10
|
||||
if band(ru8(b, o + 5), 16) ~= 0 then
|
||||
size += 10
|
||||
end
|
||||
return size
|
||||
end
|
||||
|
||||
local function setField(tags: any, id: string, text: string)
|
||||
tags.raw[id] = text
|
||||
local f = FIELDS[id]
|
||||
if f and tags[f] == nil and text ~= "" then
|
||||
tags[f] = if f == "genre" then genreName(text) else text
|
||||
end
|
||||
end
|
||||
|
||||
-- parse an ID3v2 tag at b[o ..] (o .. o + size within the buffer) into tags
|
||||
local function parseV2(b: buffer, o: number, size: number, tags: any)
|
||||
local ver = ru8(b, o + 3)
|
||||
local flags = ru8(b, o + 5)
|
||||
if ver < 2 or ver > 4 then
|
||||
return
|
||||
end
|
||||
tags.id3v2 = "2." .. ver
|
||||
local bodyEnd = math.min(o + 10 + syncsafe(b, o + 6), buffer.len(b))
|
||||
local body = b
|
||||
local p, e = o + 10, bodyEnd
|
||||
if band(flags, 0x80) ~= 0 and ver < 4 then
|
||||
body = deunsync(b, p, e)
|
||||
p, e = 0, buffer.len(body)
|
||||
end
|
||||
if band(flags, 0x40) ~= 0 and ver >= 3 and p + 4 <= e then
|
||||
-- extended header: v2.3 size excludes its own 4 bytes, v2.4 includes them (syncsafe)
|
||||
p += if ver == 3 then be32(body, p) + 4 else syncsafe(body, p)
|
||||
end
|
||||
local hdr = if ver == 2 then 6 else 10
|
||||
while p + hdr <= e do
|
||||
local c0 = ru8(body, p)
|
||||
if c0 == 0 then
|
||||
break -- padding
|
||||
end
|
||||
local id, fsize, fflags
|
||||
if ver == 2 then
|
||||
id = buffer.readstring(body, p, 3)
|
||||
fsize = bor(lshift(ru8(body, p + 3), 16), lshift(ru8(body, p + 4), 8), ru8(body, p + 5))
|
||||
fflags = 0
|
||||
else
|
||||
id = buffer.readstring(body, p, 4)
|
||||
fsize = if ver == 4 then syncsafe(body, p + 4) else be32(body, p + 4)
|
||||
fflags = bor(lshift(ru8(body, p + 8), 8), ru8(body, p + 9))
|
||||
end
|
||||
if not string.match(id, "^[A-Z0-9]+$") or fsize <= 0 or p + hdr + fsize > e then
|
||||
break
|
||||
end
|
||||
local fs, fe = p + hdr, p + hdr + fsize
|
||||
local fb = body
|
||||
local skip = false
|
||||
if ver == 3 then
|
||||
skip = band(fflags, 0x00C0) ~= 0 -- compressed / encrypted
|
||||
elseif ver == 4 then
|
||||
skip = band(fflags, 0x000C) ~= 0
|
||||
if not skip and band(fflags, 0x0001) ~= 0 then
|
||||
fs += 4 -- data length indicator
|
||||
end
|
||||
if not skip and band(fflags, 0x0002) ~= 0 and fs < fe then
|
||||
fb = deunsync(body, fs, fe)
|
||||
fs, fe = 0, buffer.len(fb)
|
||||
end
|
||||
end
|
||||
if not skip and fs < fe then
|
||||
local first = string.sub(id, 1, 1)
|
||||
local enc = ru8(fb, fs)
|
||||
if first == "T" and id ~= "TXXX" and id ~= "TXX" then
|
||||
setField(tags, id, clean(decodeText(fb, fs + 1, fe, enc)))
|
||||
elseif (id == "COMM" or id == "COM") and fe - fs >= 4 then
|
||||
-- encoding, language[3], short description (NUL-terminated in the encoding), text
|
||||
local q = fs + 4
|
||||
local wide = enc == 1 or enc == 2
|
||||
while q < fe do
|
||||
if wide then
|
||||
if q + 1 < fe and ru8(fb, q) == 0 and ru8(fb, q + 1) == 0 then
|
||||
q += 2
|
||||
break
|
||||
end
|
||||
q += 2
|
||||
else
|
||||
if ru8(fb, q) == 0 then
|
||||
q += 1
|
||||
break
|
||||
end
|
||||
q += 1
|
||||
end
|
||||
end
|
||||
if q <= fe then
|
||||
setField(tags, id, clean(decodeText(fb, q, fe, enc)))
|
||||
end
|
||||
end
|
||||
end
|
||||
p += hdr + fsize
|
||||
end
|
||||
end
|
||||
|
||||
local function v1String(b: buffer, o: number, n: number): string
|
||||
return (string.gsub(decodeText(b, o, o + n, 0), "[%z%s]+$", ""))
|
||||
end
|
||||
|
||||
-- Parses tags and returns (tags, audio start, audio end) for the buffer b.
|
||||
-- tags = { title?, artist?, album?, albumArtist?, year?, track?, disc?, genre?, composer?, comment?,
|
||||
-- raw = { [frame id] = text }, id3v2 = "2.3"?, id3v1 = true? }
|
||||
function Tags.parse(b: buffer): (any, number, number)
|
||||
local len = buffer.len(b)
|
||||
local tags: any = { raw = {} }
|
||||
local start, stop = 0, len
|
||||
local v2 = Tags.id3v2Size(b, 0, len)
|
||||
if v2 > 0 then
|
||||
pcall(parseV2, b, 0, v2, tags)
|
||||
start = math.min(v2, len)
|
||||
end
|
||||
-- mp3dec_skip_id3v1 on what follows the ID3v2 tag
|
||||
local size = stop - start
|
||||
if size >= 128 and buffer.readstring(b, start + size - 128, 3) == "TAG" then
|
||||
local o = start + size - 128
|
||||
tags.id3v1 = true
|
||||
local v1 = {
|
||||
title = v1String(b, o + 3, 30),
|
||||
artist = v1String(b, o + 33, 30),
|
||||
album = v1String(b, o + 63, 30),
|
||||
year = v1String(b, o + 93, 4),
|
||||
}
|
||||
if ru8(b, o + 125) == 0 and ru8(b, o + 126) ~= 0 then
|
||||
v1.comment = v1String(b, o + 97, 28)
|
||||
v1.track = tostring(ru8(b, o + 126))
|
||||
else
|
||||
v1.comment = v1String(b, o + 97, 30)
|
||||
end
|
||||
local g = ru8(b, o + 127)
|
||||
if GENRES[g] then
|
||||
v1.genre = GENRES[g]
|
||||
end
|
||||
for k, v in v1 do
|
||||
if tags[k] == nil and v ~= "" then
|
||||
tags[k] = v
|
||||
end
|
||||
end
|
||||
size -= 128
|
||||
if size >= 227 and buffer.readstring(b, start + size - 227, 4) == "TAG+" then
|
||||
size -= 227
|
||||
end
|
||||
end
|
||||
if size > 32 and buffer.readstring(b, start + size - 32, 8) == "APETAGEX" then
|
||||
size -= 32
|
||||
local t = start + size + 12
|
||||
local tagSize = bor(ru8(b, t), lshift(ru8(b, t + 1), 8), lshift(ru8(b, t + 2), 16), lshift(ru8(b, t + 3), 24))
|
||||
if size >= tagSize then
|
||||
size -= tagSize
|
||||
end
|
||||
tags.ape = true
|
||||
end
|
||||
stop = start + size
|
||||
return tags, start, stop
|
||||
end
|
||||
|
||||
return Tags
|
||||
@@ -0,0 +1,130 @@
|
||||
--!strict
|
||||
-- audio/mp3: MPEG audio (ISO/IEC 11172-3, 13818-3; MPEG-2.5), layers I, II and III. Absorbed from mp3-luau c755bbf
|
||||
-- (same author; a port of minimp3 by lieff, CC0: licenses/minimp3-CC0.txt). Supports: decode of MPEG-1 / 2 / 2.5
|
||||
-- layers I / II / III, mono / stereo / joint stereo / dual channel, CBR / VBR / free format, gapless playback (Xing /
|
||||
-- Info + LAME delay and padding), exact length and sample-accurate seeking from a frame index built on open, resync
|
||||
-- past garbage, mono / stereo switches (upmixed), ID3v2.2 / 2.3 / 2.4 and ID3v1 / 1.1 tags (APEv2 skipped).
|
||||
-- Float output (minimp3's MINIMP3_FLOAT_OUTPUT scalar path). Not supported: encoding (see the mp3 encoder in the
|
||||
-- audio workstream), MPEG-2 multichannel extension, CRC checks.
|
||||
--
|
||||
-- local dec = Mp3.open(Bytes.reader(buf)) -- the streaming Types.Decoder (+ layer, version, vbr, delay,
|
||||
-- local pcm, n = dec:read(4096) -- padding, mpegFrames, id3, :seekFrame(i), :readAll())
|
||||
-- local audio = Mp3.decode(Bytes.reader(buf)) -- a Types.Audio (the whole file)
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ requires
|
||||
local Err = require("@formats/core/Err")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Types = require("@formats/Types")
|
||||
local Header = require("@formats/audio/mp3/Header")
|
||||
local Index = require("@formats/audio/mp3/Index")
|
||||
local Tags = require("@formats/audio/mp3/Tags")
|
||||
local Stream = require("@formats/audio/mp3/Stream")
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ contract
|
||||
local Mp3 = {
|
||||
name = "mp3",
|
||||
category = "audio",
|
||||
extensions = { "mp3", "mp2", "mp1", "mpga" },
|
||||
mime = { "audio/mpeg", "audio/mp3", "audio/mpa" },
|
||||
deps = { "core/Err", "core/Bytes", "Types" },
|
||||
caps = { decode = true, encode = false, streaming = true, seek = true, layers = { 1, 2, 3 }, channels = 2 },
|
||||
}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ types
|
||||
export type OpenOptions = {
|
||||
maxFrames: number?, -- refuse streams longer than this many sample frames per channel (default 2^31)
|
||||
}
|
||||
export type Decoder = Stream.Decoder
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ constants
|
||||
local MAX_FRAMES = 2 ^ 31
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ sniff
|
||||
-- an ID3v2 tag and / or a valid frame header at the start, more when the next frame chains on
|
||||
function Mp3.sniff(r: Bytes.Reader): number
|
||||
local b, p, lim = r.buf, r.pos, r.limit
|
||||
local id3 = false
|
||||
for _ = 1, 4 do -- (ffmpeg writes a tag even when the input had one: tags can follow each other)
|
||||
local n = if p + 3 <= lim and buffer.readstring(b, p, 3) == "ID3" then Tags.id3v2Size(b, p, lim) else 0
|
||||
if n <= 0 then
|
||||
break
|
||||
end
|
||||
id3 = true
|
||||
p += n
|
||||
end
|
||||
if p + 4 > lim or not Header.valid(b, p) then
|
||||
return if id3 then 0.5 else 0
|
||||
end
|
||||
local fb = Header.frameBytes(b, p, 0)
|
||||
if fb > 0 then
|
||||
fb += Header.padding(b, p)
|
||||
if p + fb + 4 <= lim and Header.compare(b, p, p + fb) then
|
||||
return 0.9
|
||||
end
|
||||
if p + fb == lim then
|
||||
return 0.6
|
||||
end
|
||||
end
|
||||
return if id3 then 0.7 else 0.25
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ decode
|
||||
-- decode(r, opts) -> Types.Audio (the whole stream, through open)
|
||||
function Mp3.decode(r: Bytes.Reader, opts: OpenOptions?): (Types.Audio?, Err.Err?)
|
||||
local dec, err = Mp3.open(r, opts)
|
||||
if not dec then
|
||||
return nil, err
|
||||
end
|
||||
local a = Types.Audio.new(dec.sampleRate, dec.channels, dec.frames)
|
||||
local _, n = dec:read(dec.frames, a.samples)
|
||||
if n < dec.frames then
|
||||
return nil, Err.new(Err.CORRUPT, string.format("decoded %d of %d frames", n, dec.frames))
|
||||
end
|
||||
a.tags = dec.tags
|
||||
a.meta.source = { format = "mp3", layer = dec.layer, version = dec.version, bitrate = dec.bitrate, vbr = dec.vbr }
|
||||
return a, nil
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ open
|
||||
-- the string-valued tag fields (title, artist, album, albumArtist, year, track, disc, genre, composer, comment)
|
||||
local function stringTags(t: any): { [string]: string }
|
||||
local out: { [string]: string } = {}
|
||||
for k, v in t do
|
||||
if type(k) == "string" and type(v) == "string" and k ~= "id3v2" then
|
||||
out[k] = v
|
||||
end
|
||||
end
|
||||
return out
|
||||
end
|
||||
|
||||
-- open(r, opts) -> the streaming Decoder (Types.Decoder) | nil, Err
|
||||
function Mp3.open(r: Bytes.Reader, opts: OpenOptions?): (Decoder?, Err.Err?)
|
||||
local b = r.buf
|
||||
if r.base ~= 0 or r.limit ~= buffer.len(b) then
|
||||
b = buffer.create(r.limit - r.base) -- the core indexes from offset 0
|
||||
buffer.copy(b, 0, r.buf, r.base, r.limit - r.base)
|
||||
end
|
||||
local okTags, tags, start, stop = pcall(Tags.parse, b)
|
||||
if not okTags then
|
||||
tags, start, stop = { raw = {} }, 0, buffer.len(b)
|
||||
end
|
||||
local idx = Index.build(b, start, stop)
|
||||
if not idx then
|
||||
return nil, Err.new(Err.CORRUPT, "no MPEG audio frames found")
|
||||
end
|
||||
local dec = Stream.new(b, idx, stringTags(tags), {
|
||||
raw = tags.raw,
|
||||
id3v2 = tags.id3v2,
|
||||
id3v1 = tags.id3v1 == true,
|
||||
ape = tags.ape == true,
|
||||
})
|
||||
if dec.frames > (opts and opts.maxFrames or MAX_FRAMES) then
|
||||
return nil, Err.new(Err.LIMIT, string.format("%d sample frames is over the limit", dec.frames))
|
||||
end
|
||||
return dec, nil
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ extras
|
||||
Mp3.Tags = Tags -- Tags.parse(buf) -> (tags, audio start, audio end)
|
||||
|
||||
return Mp3
|
||||
Reference in new issue
Block a user