Files
rosetta/src/Rosetta/codecs/audio/Stream.luau
T

161 lines
4.0 KiB
Luau

--!strict
--!optimize 2
-- codecs/audio/Stream: the streaming Types.Decoder over a packetised codec (gapless trim, seeking with pre-roll).
-- New in rosetta.
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,
reset: () -> (),
skip: number?,
stop: number?,
preroll: number?,
cumulative: boolean?, -- frames after a packet = frames before + its output (default: start(i) + its output)
scale: number?,
ints: boolean?,
}
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
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
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
if count > 0 and frames > 0 and not fill() and failed then
return nil, failed
end
return dec, nil
end
return Stream