codecs/audio: comments to specs only
This commit is contained in:
1 parent
0bd7b87d2b
commit
6dcce481bb
69 files changed
+523
-1604
No files matched your search
@@ -1,25 +1,6 @@
|
||||
--!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.
|
||||
-- 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 = {}
|
||||
@@ -28,17 +9,16 @@ 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
|
||||
pcm: () -> { number } | buffer,
|
||||
reset: () -> (),
|
||||
skip: number?, -- default 0
|
||||
stop: number?, -- default start(count + 1)
|
||||
preroll: number?, -- default 0
|
||||
skip: number?,
|
||||
stop: number?,
|
||||
preroll: number?,
|
||||
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
|
||||
scale: number?,
|
||||
ints: boolean?,
|
||||
}
|
||||
|
||||
-- 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
|
||||
@@ -48,7 +28,7 @@ function Stream.packets(dec: any, spec: Spec): (any, Err.Err?)
|
||||
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 decoded = 0
|
||||
local pendPos, pendN = 0, 0
|
||||
local dropUntil = skip
|
||||
local pos = 0
|
||||
@@ -144,8 +124,7 @@ function Stream.packets(dec: any, spec: Spec): (any, Err.Err?)
|
||||
|
||||
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 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
|
||||
@@ -171,7 +150,6 @@ function Stream.packets(dec: any, spec: Spec): (any, Err.Err?)
|
||||
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
|
||||
|
||||
Reference in new issue
Block a user