Files
rosetta/src/Rosetta/audio/mpc/init.luau
T

97 lines
3.9 KiB
Luau

--!strict
-- audio/mpc: Musepack (.mpc / .mp+ / .mpp), stream versions 7 and 8, through codecs/audio/Musepack (a libmpcdec
-- port). Supports: SV7 (mppenc 1.x) and SV8 (mpcenc 1.30) streams, mono / stereo, every rate (32 / 37.8 / 44.1 /
-- 48 kHz), gapless length (the synthesis delay, SV8 leading silence and the last frame's length are honoured), seeking
-- (SV8: by audio packet, exact; SV7: by frame with 32 frames of pre-roll, as libmpcdec), APEv2 tags
-- (codecs/audio/ApeTag), a leading ID3v2 tag (skipped), ReplayGain values in meta (not applied). Not supported:
-- writing, SV4 - SV6 (UNSUPPORTED), chapters.
-- ------------------------------------------------------------------------------------------------ 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 Musepack = require("@rosetta/codecs/audio/Musepack")
-- ------------------------------------------------------------------------------------------------ contract
local Mpc = {
name = "mpc",
category = "audio",
extensions = { "mpc", "mp+", "mpp" },
mime = { "audio/x-musepack", "audio/musepack" },
deps = { "core/Err", "core/Bytes", "Types", "codecs/audio/Pcm", "codecs/audio/Stream", "codecs/audio/ApeTag", "codecs/audio/Musepack" },
caps = { decode = true, encode = false, codecs = { "musepack" }, versions = { 7, 8 }, streaming = true, lossy = true },
}
-- ------------------------------------------------------------------------------------------------ types
export type DecodeOptions = {
maxFrames: number?, -- Err.LIMIT past this many output sample frames (default 2^31)
}
-- ------------------------------------------------------------------------------------------------ constants
-- the size of an ID3v2 tag at p (0 if none)
local function id3v2(b: buffer, p: number, limit: number): number
if p + 10 > limit or buffer.readstring(b, p, 3) ~= "ID3" then
return 0
end
local n = 10
for i = 6, 9 do
n += bit32.band(buffer.readu8(b, p + i), 127) * 128 ^ (9 - i)
end
return n + (if bit32.band(buffer.readu8(b, p + 5), 0x10) ~= 0 then 10 else 0)
end
-- ------------------------------------------------------------------------------------------------ sniff
function Mpc.sniff(r: Bytes.Reader): number
local b, p, limit = r.buf, r.pos, r.limit
p += id3v2(b, p, limit)
local v = Musepack.magic(b, p, limit)
if v == 8 then
return 1
end
return if v == 7 then 0.9 else 0
end
-- ------------------------------------------------------------------------------------------------ decode
function Mpc.open(r: Bytes.Reader, opts: DecodeOptions?): (Types.Decoder?, Err.Err?)
local o: DecodeOptions = opts or {}
local b, base, limit = r.buf, r.base, r.limit
local start = base + id3v2(b, base, limit)
local tags, info = ApeTag.find(b, start, limit)
local stop = if info and info.start > start then info.start else limit
local s, err = Musepack.open(b, start, stop)
if not s then
return nil, err
end
if s.frames > (o.maxFrames or 2 ^ 31) then
return nil, Err.new(Err.LIMIT, "more than " .. (o.maxFrames or 2 ^ 31) .. " frames")
end
local duration = s.frames / s.sampleRate
local dec = {
sampleRate = s.sampleRate,
channels = s.channels,
frames = s.frames,
duration = duration,
bitrate = if duration > 0 then math.floor(s.bytes * 8 / duration + 0.5) else 0,
tags = tags or {},
codec = "musepack",
version = s.version,
profile = s.profile,
gain = s.gain,
}
return Stream.packets(dec, s :: any)
end
function Mpc.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Audio?, Err.Err?)
local dec, err = Mpc.open(r, opts)
if not dec then
return nil, err
end
local d: any = dec
return Pcm.drain(d, { format = "mpc", codec = "musepack", version = d.version, profile = d.profile, gain = d.gain, bitrate = d.bitrate })
end
return Mpc