ws-audio: Vorbis encoder (audio/vorbis/Encode: long blocks, floor 1 fit to the envelope, square-polar coupling, residue 2 with 6 classes + cascades, fixed Laplacian-Huffman codebooks, exact granules, Ogg via containers/Ogg Mux) wired into audio/vorbis encode; fixtures: ogginfo clean, oggdec exact lengths + SNR floors, ffmpeg; bench line
This commit is contained in:
1 parent
929b5d6d53
commit
cb05744367
7 files changed
+1068
-7
No files matched your search
@@ -5,8 +5,10 @@
|
||||
-- page's, granule-exact seeking (bisection, then decoding forward to a page boundary to recover the position).
|
||||
-- Supports: decode / open of any channel count (1..255), rates, block sizes 64..8192, floor 1, residues 0 / 1 / 2,
|
||||
-- coupling, sparse / ordered codebooks, lookup types 1 / 2; comment-header tags (lower-cased keys); a packet-level
|
||||
-- decoder (Vorbis.packetDecoder) for Vorbis in Matroska / WebM / MP4. Not supported: floor 0 (Err.UNSUPPORTED, as in
|
||||
-- stb_vorbis; no encoder has produced it since 2000), chained streams (the first logical Vorbis stream is decoded).
|
||||
-- decoder (Vorbis.packetDecoder) for Vorbis in Matroska / WebM / MP4. Encode (audio/vorbis/Encode, new code): a basic
|
||||
-- Ogg Vorbis encoder from an Audio or a streamed Decoder (long blocks, floor 1, coupled residue 2, a fixed codebook
|
||||
-- set, quality -1 .. 10). Not supported: floor 0 (Err.UNSUPPORTED, as in stb_vorbis; no encoder has produced it since
|
||||
-- 2000), chained streams (the first logical Vorbis stream is decoded), short blocks in the encoder.
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ requires
|
||||
local Err = require("@formats/core/Err")
|
||||
@@ -15,6 +17,8 @@ local Types = require("@formats/Types")
|
||||
local Ogg = require("@formats/containers/Ogg")
|
||||
local Setup = require("@formats/audio/vorbis/Setup")
|
||||
local Packet = require("@formats/audio/vorbis/Packet")
|
||||
local Encode = require("@formats/audio/vorbis/Encode")
|
||||
local Pcm = require("@formats/codecs/Pcm")
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ contract
|
||||
local Vorbis = {
|
||||
@@ -22,13 +26,18 @@ local Vorbis = {
|
||||
category = "audio",
|
||||
extensions = { "ogg", "oga" },
|
||||
mime = { "audio/ogg", "audio/vorbis" },
|
||||
deps = { "core/Err", "core/Bytes", "Types", "containers/Ogg", "codecs/Mdct" },
|
||||
caps = { decode = true, encode = false, streaming = true, seek = true, lossy = true, channels = 255 },
|
||||
deps = { "core/Err", "core/Bytes", "Types", "containers/Ogg", "codecs/Mdct", "codecs/Pcm" },
|
||||
caps = { decode = true, encode = true, streaming = true, seek = true, lossy = true, channels = 255 },
|
||||
}
|
||||
|
||||
export type OpenOptions = {
|
||||
verify: boolean?, -- check the Ogg page CRCs (default true)
|
||||
}
|
||||
export type EncodeOptions = {
|
||||
quality: number?, -- -1 .. 10 (default 3): the floor sits 2 * quality - 1 dB under the band energies (8 dB
|
||||
-- less at the top of the spectrum), which sets the residue's quantization step
|
||||
serial: number?, -- the Ogg stream serial number (default 0x464C5541)
|
||||
}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ constants
|
||||
local TAG_KEYS = { tracknumber = "track", description = "comment" }
|
||||
@@ -109,6 +118,35 @@ function Vorbis.decode(r: Bytes.Reader, opts: OpenOptions?): (Types.Audio?, Err.
|
||||
return a, nil
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ encode
|
||||
-- encode(audioOrDecoder, opts?) -> an Ogg Vorbis file | nil, Err
|
||||
function Vorbis.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 q = o.quality or 3
|
||||
if type(q) ~= "number" or q ~= q or q < -1 or q > 10 then
|
||||
return nil, Err.new(Err.ARG, "quality must be -1 .. 10")
|
||||
end
|
||||
if src.channels > 255 then
|
||||
return nil, Err.new(Err.ARG, "at most 255 channels")
|
||||
end
|
||||
if src.sampleRate ~= math.floor(src.sampleRate) or src.sampleRate < 1 or src.sampleRate >= 2 ^ 32 then
|
||||
return nil, Err.new(Err.ARG, "the sample rate must be a positive integer")
|
||||
end
|
||||
local serial = o.serial or 0x464C5541
|
||||
if type(serial) ~= "number" or serial ~= math.floor(serial) or serial < 0 or serial >= 2 ^ 32 then
|
||||
return nil, Err.new(Err.ARG, "serial must be a 32-bit unsigned integer")
|
||||
end
|
||||
local out, msg = Encode.encode(src :: any, { quality = q, vendor = "formats-luau Vorbis encoder", serial = serial })
|
||||
if not out then
|
||||
return nil, Err.new(Err.CORRUPT, tostring(msg))
|
||||
end
|
||||
return out, nil
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ open
|
||||
local Stream = {}
|
||||
Stream.__index = Stream
|
||||
|
||||
Reference in new issue
Block a user