rename: src/Formats -> src/Rosetta, @formats -> @rosetta requires, build/luajit/rosetta + rosetta._compat, build/roblox/Rosetta, ROSETTA_* env/globals, writer vendor strings
This commit is contained in:
1 parent
2956576d7f
commit
22ddcd78fa
941 files changed
+3364
-3364
No files matched your search
@@ -0,0 +1,181 @@
|
||||
--!strict
|
||||
-- codecs/audio/Stream: the streaming Decoder (Types.Decoder: read / seek / position) over a packetised codec, the part
|
||||
-- every packet-based audio format repeated: decode packets in order into a pending float block, return frames in
|
||||
-- read()-sized pieces, drop the encoder priming at the start and the padding at the end (gapless), and seek by
|
||||
-- finding the packet holding the target (with codec pre-roll) and dropping up to it. AAC (ADTS), CAF and M4A use it;
|
||||
-- formats with their own seeking (Opus granule bisection, FLAC, MP3, Vorbis) keep their streams. New in rosetta
|
||||
-- (the loop the three formats carried).
|
||||
--
|
||||
-- local dec, err = Stream.packets(dec, { -- dec: the Decoder table with its fields (sampleRate, channels,
|
||||
-- count = n, -- frames, duration, bitrate, tags, ...); read / seek / position
|
||||
-- start = function(i) return ... end, -- are added. start(i): packet i's first frame (absolute, before
|
||||
-- decode = function(i) return n, err end,-- trimming); decode(i) -> frames | nil, Err, the samples in pcm()
|
||||
-- pcm = function() return floats end, -- (interleaved, 1-based; or a buffer of f32) until the next decode
|
||||
-- reset = function() end, -- forget codec state (before a seek)
|
||||
-- skip = priming, stop = lastFrame, -- output = absolute frames [skip, stop)
|
||||
-- preroll = 1, -- packets decoded before the one holding a seek target (AAC: 1)
|
||||
-- scale = 1 / 32768, -- optional: read() writes pcm() values * scale (default 1)
|
||||
-- ints = true, -- optional: pcm() holds integers; adds dec:readInt(n, out?), the
|
||||
-- }) -- same frames as read() as i32 (bit-exact checks)
|
||||
--
|
||||
-- A decode failure ends the stream and is kept in dec.error (Pcm.drain reports it); a failing first packet is
|
||||
-- returned by packets() as the open error. Never throws.
|
||||
local Err = require("@rosetta/core/Err")
|
||||
|
||||
local Stream = {}
|
||||
|
||||
export type Spec = {
|
||||
count: number,
|
||||
start: (i: number) -> number,
|
||||
decode: (i: number) -> (number?, Err.Err?),
|
||||
pcm: () -> { number } | buffer, -- a buffer: interleaved f32 from byte 0
|
||||
reset: () -> (),
|
||||
skip: number?, -- default 0
|
||||
stop: number?, -- default start(count + 1)
|
||||
preroll: number?, -- default 0
|
||||
cumulative: boolean?, -- frames after a packet = frames before + its output (default: start(i) + its output)
|
||||
scale: number?, -- read() writes pcm() values times this (default 1)
|
||||
ints: boolean?, -- pcm() values are integers: dec:readInt(n, out?) returns them as i32
|
||||
}
|
||||
|
||||
-- packets(dec, spec) -> dec (with read / seek / position) | nil, Err (the first packet failed)
|
||||
function Stream.packets(dec: any, spec: Spec): (any, Err.Err?)
|
||||
local sr, ch = dec.sampleRate, dec.channels
|
||||
local count = spec.count
|
||||
local skip = spec.skip or 0
|
||||
local stopAt = spec.stop or spec.start(count + 1)
|
||||
local frames = math.max(0, stopAt - skip)
|
||||
local preroll = spec.preroll or 0
|
||||
local decodeP, pcmOf, startOf = spec.decode, spec.pcm, spec.start
|
||||
local nextPacket = 1
|
||||
local decoded = 0 -- absolute frame after the packets decoded so far
|
||||
local pendPos, pendN = 0, 0
|
||||
local dropUntil = skip
|
||||
local pos = 0
|
||||
local failed: Err.Err? = nil
|
||||
dec.error = nil
|
||||
|
||||
local function fill(): boolean
|
||||
while true do
|
||||
if nextPacket > count or decoded >= stopAt then
|
||||
return false
|
||||
end
|
||||
local i = nextPacket
|
||||
nextPacket += 1
|
||||
local first = if spec.cumulative then decoded else startOf(i)
|
||||
local n, e = decodeP(i)
|
||||
if not n then
|
||||
failed = e or Err.new(Err.CORRUPT, "packet " .. i .. " failed")
|
||||
dec.error = failed
|
||||
return false
|
||||
end
|
||||
decoded = first + n
|
||||
local a = math.max(first, dropUntil)
|
||||
local b = math.min(decoded, stopAt)
|
||||
if b > a then
|
||||
pendPos, pendN = a - first, b - first
|
||||
return true
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local scale = spec.scale or 1
|
||||
local function pull(n: number, out: buffer?, asInt: boolean): (buffer, number)
|
||||
local want = math.max(0, math.floor(n))
|
||||
local need = want * ch * 4
|
||||
local ob = if out and buffer.len(out) >= need then out else buffer.create(need)
|
||||
local got = 0
|
||||
local writef32, writei32 = buffer.writef32, buffer.writei32
|
||||
while got < want do
|
||||
if pendPos >= pendN then
|
||||
if failed or not fill() then
|
||||
break
|
||||
end
|
||||
end
|
||||
local take = math.min(want - got, pendN - pendPos)
|
||||
local src = pcmOf()
|
||||
local q = got * ch * 4
|
||||
local s = pendPos * ch
|
||||
if type(src) == "buffer" then
|
||||
if scale == 1 and not asInt then
|
||||
buffer.copy(ob, q, src, s * 4, take * ch * 4)
|
||||
else
|
||||
for k = 0, take * ch - 1 do
|
||||
local v = buffer.readf32(src, (s + k) * 4)
|
||||
if asInt then
|
||||
writei32(ob, q, v)
|
||||
else
|
||||
writef32(ob, q, v * scale)
|
||||
end
|
||||
q += 4
|
||||
end
|
||||
end
|
||||
elseif asInt then
|
||||
for k = 1, take * ch do
|
||||
writei32(ob, q, src[s + k])
|
||||
q += 4
|
||||
end
|
||||
elseif scale == 1 then
|
||||
for k = 1, take * ch do
|
||||
writef32(ob, q, src[s + k])
|
||||
q += 4
|
||||
end
|
||||
else
|
||||
for k = 1, take * ch do
|
||||
writef32(ob, q, src[s + k] * scale)
|
||||
q += 4
|
||||
end
|
||||
end
|
||||
pendPos += take
|
||||
got += take
|
||||
end
|
||||
pos += got
|
||||
return ob, got
|
||||
end
|
||||
|
||||
function dec.read(_self: any, n: number, out: buffer?): (buffer, number)
|
||||
return pull(n, out, false)
|
||||
end
|
||||
if spec.ints then
|
||||
function dec.readInt(_self: any, n: number, out: buffer?): (buffer, number)
|
||||
return pull(n, out, true)
|
||||
end
|
||||
end
|
||||
|
||||
function dec.seek(_self: any, seconds: number)
|
||||
local sec = if type(seconds) == "number" and seconds == seconds then seconds else 0
|
||||
local target = math.clamp(math.floor(sec * sr + 0.5), 0, frames) + skip -- absolute frame
|
||||
-- the last packet starting at or before the target, minus the codec's pre-roll
|
||||
local lo, hi, found = 1, count, 1
|
||||
while lo <= hi do
|
||||
local mid = (lo + hi) // 2
|
||||
if startOf(mid) <= target then
|
||||
found = mid
|
||||
lo = mid + 1
|
||||
else
|
||||
hi = mid - 1
|
||||
end
|
||||
end
|
||||
found = math.max(1, found - preroll)
|
||||
spec.reset()
|
||||
nextPacket = found
|
||||
decoded = if count > 0 then startOf(found) else 0
|
||||
dropUntil = target
|
||||
pendPos, pendN = 0, 0
|
||||
failed = nil
|
||||
dec.error = nil
|
||||
pos = target - skip
|
||||
end
|
||||
|
||||
function dec.position(_self: any): number
|
||||
return pos / sr
|
||||
end
|
||||
|
||||
-- prime: surface a broken first packet as an open error
|
||||
if count > 0 and frames > 0 and not fill() and failed then
|
||||
return nil, failed
|
||||
end
|
||||
return dec, nil
|
||||
end
|
||||
|
||||
return Stream
|
||||
Reference in new issue
Block a user