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,161 @@
|
||||
--!strict
|
||||
-- audio/flac: FLAC (RFC 9639), native and in Ogg (the Ogg FLAC mapping). Absorbed from flac-luau 7083d0d (same author,
|
||||
-- no third-party code); the Ogg demuxer is now a direct require of containers/Ogg instead of an injected module.
|
||||
-- Supports: every subframe type (constant, verbatim, fixed, LPC up to order 32, wasted bits), Rice / Rice2 residuals
|
||||
-- with escapes, every stereo mode, 4-32 bits, 1-8 channels, any sample rate, fixed and variable block sizes, frame
|
||||
-- CRC-16 checks with resync past corrupt frames, sample-accurate seeking (seek table + bisection), an ID3v2 tag in
|
||||
-- front, STREAMINFO / SEEKTABLE / VORBIS_COMMENT / PICTURE metadata, the total length from the last frame when
|
||||
-- STREAMINFO has none. Encode (codecs/audio/Flac/Encode, ws-audio): native FLAC from an Audio or a streamed Decoder, levels
|
||||
-- 0-8 like libFLAC's presets, 4-24 bits, 1-8 channels, MD5, VORBIS_COMMENT, SEEKTABLE. Not supported: MD5
|
||||
-- verification on decode, chained Ogg streams, Ogg FLAC output.
|
||||
-- The codec itself is codecs/audio/Flac; this folder maps it onto the file (container, tags, seeking, writing).
|
||||
--
|
||||
-- local dec = Flac.open(Bytes.reader(buf)) -- the Types.Decoder (+ bitsPerSample, readInt, seekSample, ...)
|
||||
-- local f32, n = dec:read(4096) -- interleaved f32 in [-1, 1); n = 0 at the end
|
||||
-- local i32, n = dec:readInt(4096) -- bit-exact integer samples
|
||||
-- local audio = Flac.decode(Bytes.reader(buf)) -- Types.Audio
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ requires
|
||||
local Err = require("@rosetta/core/Err")
|
||||
local Bytes = require("@rosetta/core/Bytes")
|
||||
local Types = require("@rosetta/Types")
|
||||
local Ogg = require("@rosetta/containers/Ogg")
|
||||
local Stream = require("@rosetta/audio/flac/Stream")
|
||||
local Encode = require("@rosetta/codecs/audio/Flac/Encode")
|
||||
local Pcm = require("@rosetta/codecs/audio/Pcm")
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ contract
|
||||
local Flac = {
|
||||
name = "flac",
|
||||
category = "audio",
|
||||
extensions = { "flac", "oga" },
|
||||
mime = { "audio/flac", "audio/x-flac" },
|
||||
deps = { "core/Err", "core/Bytes", "Types", "containers/Ogg", "codecs/audio/Pcm", "codecs/audio/VorbisComment", "codecs/audio/Flac/Frame", "codecs/audio/Flac/Encode" },
|
||||
caps = { decode = true, encode = true, streaming = true, depths = { 4, 8, 12, 16, 20, 24, 32 }, ogg = true, seek = true },
|
||||
}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ types
|
||||
export type Decoder = Stream.Decoder
|
||||
export type OpenOptions = {
|
||||
crc16: boolean?, -- check every frame's CRC-16 (default true; a bad frame is skipped by resyncing)
|
||||
maxFrames: number?, -- decode(): refuse streams longer than this many sample frames (default 2^28)
|
||||
}
|
||||
export type EncodeOptions = {
|
||||
level: number?, -- 0..8 like libFLAC's -0 .. -8 (default 5)
|
||||
bits: number?, -- bits per sample 4..24 (default: the source's meta.source.bitDepth / bitsPerSample if 4..24, else 16)
|
||||
blockSize: number?, -- 16..65535 (default: the level's, 1152 or 4096)
|
||||
seekInterval: number?, -- seconds between SEEKTABLE points when the length is known (default 10; 0 = none)
|
||||
padding: number?, -- bytes of PADDING after the metadata (default 0)
|
||||
}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ sniff
|
||||
function Flac.sniff(r: Bytes.Reader): number
|
||||
local head = r:peek(4)
|
||||
if head == "fLaC" then
|
||||
return 1
|
||||
end
|
||||
if head == "OggS" then
|
||||
-- the first page's packet: 0x7F "FLAC" (the Ogg FLAC mapping)
|
||||
local nseg = r:peekU8(26)
|
||||
if nseg > 0 and r:peek(5, 27 + nseg) == "\127FLAC" then
|
||||
return 1
|
||||
end
|
||||
return 0
|
||||
end
|
||||
if string.sub(head, 1, 3) == "ID3" then
|
||||
-- an ID3v2 tag, then fLaC (mp3 files start with ID3 too)
|
||||
local sz = 0
|
||||
for i = 6, 9 do
|
||||
local v = r:peekU8(i)
|
||||
if v < 0 then
|
||||
return 0
|
||||
end
|
||||
sz = sz * 128 + bit32.band(v, 127)
|
||||
end
|
||||
local footer = bit32.band(math.max(r:peekU8(5), 0), 0x10) ~= 0
|
||||
return if r:peek(4, 10 + sz + (if footer then 10 else 0)) == "fLaC" then 1 else 0
|
||||
end
|
||||
return 0
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ decode
|
||||
-- decode(r, opts?) -> Types.Audio | nil, Err: the whole stream
|
||||
function Flac.decode(r: Bytes.Reader, opts: OpenOptions?): (Types.Audio?, Err.Err?)
|
||||
local dec, err = Flac.open(r, opts)
|
||||
if not dec then
|
||||
return nil, err
|
||||
end
|
||||
local d = dec :: any
|
||||
local limit = opts and opts.maxFrames or 2 ^ 28
|
||||
if d.frames > limit or d.frames * d.channels > 2 ^ 28 then
|
||||
return nil, Err.new(Err.LIMIT, string.format("%d frames is over the limit", d.frames))
|
||||
end
|
||||
return Pcm.drain(d, { format = "flac", bitDepth = d.bitsPerSample, container = d.container, vendor = d.vendor, md5 = d.md5, pictures = d.pictures, comments = d.comments }, limit)
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ encode
|
||||
-- encode(audioOrDecoder, opts?) -> FLAC file buffer | nil, Err
|
||||
function Flac.encode(value: any, opts: EncodeOptions?): (buffer?, Err.Err?)
|
||||
local src, serr = Pcm.source(value)
|
||||
if not src then
|
||||
return nil, serr
|
||||
end
|
||||
local o: EncodeOptions = opts or {}
|
||||
local level = o.level or 5
|
||||
if type(level) ~= "number" or level ~= math.floor(level) or level < 0 or level > 8 then
|
||||
return nil, Err.new(Err.ARG, "level must be 0..8")
|
||||
end
|
||||
local bits = o.bits
|
||||
if bits == nil then
|
||||
local v = value :: any
|
||||
local sb = (type(v.meta) == "table" and type(v.meta.source) == "table" and v.meta.source.bitDepth) or v.bitsPerSample
|
||||
bits = if type(sb) == "number" and sb >= 4 and sb <= 24 then sb else 16
|
||||
end
|
||||
if type(bits) ~= "number" or bits ~= math.floor(bits) or bits < 4 or bits > 24 then
|
||||
return nil, Err.new(Err.ARG, "bits must be 4..24")
|
||||
end
|
||||
local bs = o.blockSize
|
||||
if bs ~= nil and (type(bs) ~= "number" or bs ~= math.floor(bs) or bs < 16 or bs > 65535) then
|
||||
return nil, Err.new(Err.ARG, "blockSize must be 16..65535")
|
||||
end
|
||||
if src.channels > 8 then
|
||||
return nil, Err.new(Err.ARG, "FLAC holds at most 8 channels")
|
||||
end
|
||||
if src.sampleRate ~= math.floor(src.sampleRate) or src.sampleRate < 1 or src.sampleRate >= 2 ^ 20 then
|
||||
return nil, Err.new(Err.ARG, "the sample rate must be an integer below 2^20")
|
||||
end
|
||||
local out, msg = Encode.encode(src :: any, {
|
||||
level = level,
|
||||
bits = bits :: number,
|
||||
blockSize = bs,
|
||||
seekInterval = math.max(0, o.seekInterval or 10),
|
||||
padding = math.clamp(math.floor(o.padding or 0), 0, 0xFFFFFF),
|
||||
vendor = "rosetta",
|
||||
})
|
||||
if not out then
|
||||
return nil, Err.new(Err.CORRUPT, tostring(msg))
|
||||
end
|
||||
return out, nil
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ open
|
||||
-- open(r, opts?) -> Decoder | nil, Err
|
||||
function Flac.open(r: Bytes.Reader, opts: OpenOptions?): (Decoder?, Err.Err?)
|
||||
if type(r) == "buffer" then
|
||||
r = Bytes.reader(r :: any)
|
||||
end
|
||||
if type(r) ~= "table" or type(r.buf) ~= "buffer" then
|
||||
return nil, Err.new(Err.ARG, "open: expected a Reader")
|
||||
end
|
||||
local dec, msg, code = Stream.open(r.buf, r.base, r.limit - r.base, opts)
|
||||
if not dec then
|
||||
return nil, Err.new(code or Err.CORRUPT, tostring(msg))
|
||||
end
|
||||
return dec, nil
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ extras
|
||||
Flac.Stream = Stream
|
||||
Flac.Ogg = Ogg
|
||||
|
||||
return Flac
|
||||
Reference in new issue
Block a user