118 lines
4.1 KiB
Luau
118 lines
4.1 KiB
Luau
--!strict
|
|
-- audio/opus/Encode: Ogg Opus writing (RFC 7845) around codecs/audio/Opus/CeltEncoder: CELT-only packets (TOC config 31:
|
|
-- full band, 20 ms, code 0), OpusHead (mapping family 0, the pre-skip = the encoder's delay), OpusTags (vendor +
|
|
-- the tags as Vorbis comments), granule positions with end trimming, pages through containers/Ogg's muxer. Input at
|
|
-- other rates is resampled to 48 kHz (codecs/audio/Resample); the input is a Types.Audio or a Decoder (codecs/audio/Pcm). New in
|
|
-- formats-luau.
|
|
local Err = require("@formats/core/Err")
|
|
local Bytes = require("@formats/core/Bytes")
|
|
local Ogg = require("@formats/containers/Ogg")
|
|
local Pcm = require("@formats/codecs/audio/Pcm")
|
|
local Resample = require("@formats/codecs/audio/Resample")
|
|
local VorbisComment = require("@formats/codecs/audio/VorbisComment")
|
|
local CeltEncoder = require("@formats/codecs/audio/Opus/CeltEncoder")
|
|
|
|
local Encode = {}
|
|
|
|
export type Options = {
|
|
bitrate: number?, -- bits per second (default 64000 per channel; 6000 .. 510000, constant); values below 1000
|
|
-- are kbit/s (96 = 96000), as for every encoder's bitrate option
|
|
tags: { [string]: string }?, -- default: the audio's tags
|
|
vendor: string?, -- OpusTags vendor (default "formats-luau")
|
|
serial: number?, -- Ogg stream serial (default 0x4f707573)
|
|
}
|
|
|
|
local floor = math.floor
|
|
|
|
-- encode(audio | decoder, opts) -> buffer | nil, Err
|
|
function Encode.encode(value: any, opts: Options?): (buffer?, Err.Err?)
|
|
local o: Options = opts or {}
|
|
local src, serr = Pcm.source(value)
|
|
if not src then
|
|
return nil, serr
|
|
end
|
|
local ch = src.channels
|
|
if ch ~= 1 and ch ~= 2 then
|
|
return nil, Err.new(Err.UNSUPPORTED, "the CELT encoder writes mono or stereo (got " .. ch .. " channels)")
|
|
end
|
|
local rate = src.sampleRate
|
|
if rate < 1000 or rate > 768000 then
|
|
return nil, Err.new(Err.ARG, "bad sample rate " .. tostring(rate))
|
|
end
|
|
local bitrate = o.bitrate or 64000 * ch
|
|
if type(bitrate) == "number" and bitrate > 0 and bitrate < 1000 then
|
|
bitrate *= 1000 -- kbit/s
|
|
end
|
|
if type(bitrate) ~= "number" or bitrate < 6000 or bitrate > 510000 then
|
|
return nil, Err.new(Err.ARG, "bitrate 6000 .. 510000")
|
|
end
|
|
-- the whole input at 48 kHz (codecs/audio/Resample for other rates), then as floats
|
|
if rate ~= 48000 then
|
|
local rs, rerr = Resample.source(src, 48000)
|
|
if not rs then
|
|
return nil, Err.new(Err.ARG, tostring(rerr))
|
|
end
|
|
src = rs
|
|
end
|
|
local all, aerr = Pcm.collect(src :: Pcm.Source)
|
|
if not all then
|
|
return nil, aerr
|
|
end
|
|
local n48 = all.frames
|
|
local samples = all.samples
|
|
local pcm: { number } = table.create(n48 * ch, 0)
|
|
local readf32 = buffer.readf32
|
|
for i = 1, n48 * ch do
|
|
pcm[i] = readf32(samples, (i - 1) * 4)
|
|
end
|
|
local FRAME = CeltEncoder.FRAME
|
|
local preSkip = CeltEncoder.DELAY
|
|
local packets = (n48 + preSkip + FRAME - 1) // FRAME
|
|
-- zero padding to whole frames
|
|
for i = n48 * ch + 1, packets * FRAME * ch do
|
|
pcm[i] = 0
|
|
end
|
|
local bytes = math.clamp(floor(bitrate * FRAME / 48000 / 8), if ch == 2 then 40 else 20, 1275 - 1)
|
|
local st = CeltEncoder.new(ch)
|
|
local mux = Ogg.muxer({ pageSize = 4096 })
|
|
local s = mux:stream(o.serial or 0x4f707573)
|
|
-- OpusHead
|
|
local head = Bytes.writer(19)
|
|
head:string("OpusHead")
|
|
head:u8(1)
|
|
head:u8(ch)
|
|
head:u16le(preSkip)
|
|
head:u32le(floor(rate))
|
|
head:u16le(0)
|
|
head:u8(0)
|
|
s:packet(head:finish(), 0, { flush = true })
|
|
-- OpusTags
|
|
local tags = o.tags or src.tags or {}
|
|
local tw = Bytes.writer(256)
|
|
tw:string("OpusTags")
|
|
local vendor = o.vendor or "formats-luau"
|
|
tw:u32le(#vendor)
|
|
tw:string(vendor)
|
|
local fields = VorbisComment.fields(tags)
|
|
tw:u32le(#fields)
|
|
for _, field in fields do
|
|
tw:u32le(#field)
|
|
tw:string(field)
|
|
end
|
|
s:packet(tw:finish(), 0, { flush = true })
|
|
-- audio
|
|
local toc = 31 * 8 + (if ch == 2 then 4 else 0)
|
|
for p = 0, packets - 1 do
|
|
local frame = CeltEncoder.encodeFrame(st, pcm, p * FRAME * ch + 1, ch, bytes)
|
|
local pkt = buffer.create(bytes + 1)
|
|
buffer.writeu8(pkt, 0, toc)
|
|
buffer.copy(pkt, 1, frame, 0, bytes)
|
|
local last = p == packets - 1
|
|
local granule = if last then preSkip + n48 else (p + 1) * FRAME
|
|
s:packet(pkt, granule, { eos = last })
|
|
end
|
|
return mux:finish(), nil
|
|
end
|
|
|
|
return Encode
|