128 lines
4.8 KiB
Luau
128 lines
4.8 KiB
Luau
--!strict
|
|
--!optimize 2
|
|
-- audio/ape: Monkey's Audio (.ape) via codecs/audio/Ape and ApeTag, from the Monkey's Audio SDK 13.28 (BSD 3-clause,
|
|
-- licenses/monkeys-audio-LICENSE.txt). Supports: versions 3.95+, every level, 8-32-bit integer and 32-bit float, 1-32
|
|
-- channels, an ID3v2 prefix, seeking by the seek table. Not supported: files before 3.95, encoding.
|
|
|
|
-- ------------------------------------------------------------------------------------------------ requires
|
|
local Err = require("@rosetta/core/Err")
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local Types = require("@rosetta/Types")
|
|
local Pcm = require("@rosetta/codecs/audio/Pcm")
|
|
local Stream = require("@rosetta/codecs/audio/Stream")
|
|
local ApeTag = require("@rosetta/codecs/audio/ApeTag")
|
|
local ApeCodec = require("@rosetta/codecs/audio/Ape")
|
|
|
|
-- ------------------------------------------------------------------------------------------------ contract
|
|
local Ape = {
|
|
name = "ape",
|
|
category = "audio",
|
|
extensions = { "ape" },
|
|
mime = { "audio/ape", "audio/x-ape", "audio/x-monkeys-audio" },
|
|
deps = { "core/Err", "core/Bytes", "Types", "codecs/audio/Pcm", "codecs/audio/Stream", "codecs/audio/ApeTag", "codecs/audio/Ape" },
|
|
caps = { decode = true, encode = false, streaming = true, seek = true, lossless = true, float = true, depths = { 8, 16, 24, 32 } },
|
|
}
|
|
|
|
-- ------------------------------------------------------------------------------------------------ types
|
|
export type OpenOptions = {
|
|
maxFrames: number?, -- default 2^28
|
|
}
|
|
|
|
-- ------------------------------------------------------------------------------------------------ constants
|
|
local LEVELS = { [1000] = "fast", [2000] = "normal", [3000] = "high", [4000] = "extra high", [5000] = "insane" }
|
|
|
|
-- ------------------------------------------------------------------------------------------------ sniff
|
|
function Ape.sniff(r: Bytes.Reader): number
|
|
local head = r:peek(4)
|
|
if head == "MAC " or head == "MACF" then -- MACF: float samples
|
|
return 1
|
|
end
|
|
if string.sub(head, 1, 3) == "ID3" then
|
|
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
|
|
local id = r:peek(4, 10 + sz + (if footer then 10 else 0))
|
|
return if id == "MAC " or id == "MACF" then 1 else 0
|
|
end
|
|
return 0
|
|
end
|
|
|
|
-- ------------------------------------------------------------------------------------------------ decode
|
|
function Ape.decode(r: Bytes.Reader, opts: OpenOptions?): (Types.Audio?, Err.Err?)
|
|
local dec, err = Ape.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, d.source, limit)
|
|
end
|
|
|
|
-- ------------------------------------------------------------------------------------------------ open
|
|
function Ape.open(r: Bytes.Reader, opts: OpenOptions?): (Types.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 buf, base, limit = r.buf, r.base, r.limit
|
|
local tags, tagInfo = ApeTag.find(buf, base, limit)
|
|
local stop = if tagInfo then tagInfo.start else limit
|
|
local info, err = ApeCodec.info(buf, base, stop)
|
|
if not info then
|
|
return nil, err
|
|
end
|
|
if info.channels * info.blocksPerFrame > 2 ^ 24 then
|
|
return nil, Err.new(Err.LIMIT, "frames of " .. info.blocksPerFrame .. " blocks")
|
|
end
|
|
local codec, cerr = ApeCodec.decoder(info)
|
|
if not codec then
|
|
return nil, cerr
|
|
end
|
|
local frames = info.totalBlocks
|
|
local bpf = info.blocksPerFrame
|
|
local dec: any = {
|
|
sampleRate = info.sampleRate,
|
|
channels = info.channels,
|
|
frames = frames,
|
|
duration = frames / info.sampleRate,
|
|
bitrate = if frames > 0 then math.floor((stop - base) * 8 * info.sampleRate / frames) else 0,
|
|
tags = tags or {},
|
|
bitsPerSample = info.bitsPerSample,
|
|
float = info.float,
|
|
level = LEVELS[info.level] or tostring(info.level),
|
|
version = info.version,
|
|
md5 = info.md5,
|
|
source = { format = "ape", bitDepth = info.bitsPerSample, float = info.float, level = info.level, version = info.version, programVersion = info.programVersion, tags = tagInfo and tagInfo.items },
|
|
}
|
|
local count = info.totalFrames
|
|
return Stream.packets(dec, {
|
|
count = count,
|
|
start = function(i: number): number
|
|
return if i > count then frames else (i - 1) * bpf
|
|
end,
|
|
decode = function(i: number): (number?, Err.Err?)
|
|
return codec:frame(buf, i)
|
|
end,
|
|
pcm = function(): { number }
|
|
return codec.pcm
|
|
end,
|
|
reset = function() end,
|
|
stop = frames,
|
|
scale = if info.float then 1 else 1 / 2 ^ (info.bitsPerSample - 1),
|
|
ints = not info.float,
|
|
})
|
|
end
|
|
|
|
return Ape
|