ws-cleanup: codecs/Resample, codecs/RangeCoder; Opus / MP3 / MP2 encoders resample through it
- codecs/Resample (new): Kaiser-windowed sinc resampler, polyphase (exact phases when to / gcd <= 4096, else 4096
interpolated phases), quality low / medium / high (8 / 16 / 32 zero crossings), streaming (new / process / flush),
whole buffer (Resample.buffer) and a codecs/Pcm source wrapper (Resample.source). tests/codecs/resample: SNR
87 dB (44.1 -> 48 kHz, medium) .. 108 dB (high), alias rejection, streamed = whole, identity.
- audio/opus/Encode: input through codecs/Pcm (Pcm.source + collect) and codecs/Resample instead of its private
resampler and Decoder loop; tags through codecs/VorbisComment.
- audio/mp3 encode: rates the layer cannot hold are resampled to the next rate up (opts.sampleRate picks one,
opts.resampleQuality); was Err.ARG "no resampling".
- codecs/RangeCoder/{Decoder,Encoder}: the RFC 6716 range coder moved out of audio/opus (same code, no speed change).
- audio/opus/Mdct stays private: routing CELT through codecs/Mdct measured 2-3x slower CELT decoding and different
output bits (documented in its header).
- (this commit also carries the Opus side of the tags / channel order consistency: codecs/VorbisComment for OpusTags,
meta.source.comments, mapping family 1 decoded to WAVE channel order with Pcm.reorder / Pcm.WAVE_FROM_VORBIS.)
This commit is contained in:
1 parent
4f2ceec735
commit
7169f2cc2c
17 files changed
+777
-150
No files matched your search
@@ -6,7 +6,8 @@
|
||||
-- past garbage, mono / stereo switches (upmixed), ID3v2.2 / 2.3 / 2.4 and ID3v1 / 1.1 tags (APEv2 skipped).
|
||||
-- Float output (minimp3's MINIMP3_FLOAT_OUTPUT scalar path). Encode (ws-audio, audio/mp3/Encode3 + Encode2): layer III
|
||||
-- (shine-style CBR, MPEG-1 / 2 / 2.5) and layer II (MPEG-1 / 2), mono / stereo / joint stereo, an ID3v2.4 tag from
|
||||
-- the tags. Not supported: MPEG-2 multichannel extension, CRC checks, VBR / Xing output, encoder resampling.
|
||||
-- the tags; input at rates the layer cannot hold is resampled (codecs/Resample). Not supported: MPEG-2 multichannel
|
||||
-- extension, CRC checks, VBR / Xing output.
|
||||
--
|
||||
-- local dec = Mp3.open(Bytes.reader(buf)) -- the streaming Types.Decoder (+ layer, version, vbr, delay,
|
||||
-- local pcm, n = dec:read(4096) -- padding, mpegFrames, id3, :seekFrame(i), :readAll())
|
||||
@@ -23,6 +24,7 @@ local Stream = require("@formats/audio/mp3/Stream")
|
||||
local Encode3 = require("@formats/audio/mp3/Encode3")
|
||||
local Encode2 = require("@formats/audio/mp3/Encode2")
|
||||
local Pcm = require("@formats/codecs/Pcm")
|
||||
local Resample = require("@formats/codecs/Resample")
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ contract
|
||||
local Mp3 = {
|
||||
@@ -30,7 +32,7 @@ local Mp3 = {
|
||||
category = "audio",
|
||||
extensions = { "mp3", "mp2", "mp1", "mpga" },
|
||||
mime = { "audio/mpeg", "audio/mp3", "audio/mpa" },
|
||||
deps = { "core/Err", "core/Bytes", "Types", "codecs/Pcm" },
|
||||
deps = { "core/Err", "core/Bytes", "Types", "codecs/Pcm", "codecs/Resample" },
|
||||
caps = { decode = true, encode = true, streaming = true, seek = true, layers = { 1, 2, 3 }, channels = 2 },
|
||||
}
|
||||
|
||||
@@ -45,6 +47,9 @@ export type EncodeOptions = {
|
||||
mode: ("stereo" | "joint" | "mono")?, -- default "joint" (mid / side) for stereo layer III, "stereo" for layer II,
|
||||
-- "mono" for mono input; layer II has no joint stereo (intensity) and writes "joint" as "stereo"
|
||||
id3: boolean?, -- write an ID3v2.4 tag from the tags (default true when there are tags)
|
||||
sampleRate: number?, -- output rate (one the layer holds); default the input rate if possible, else the nearest
|
||||
-- higher one (input at other rates is resampled through codecs/Resample)
|
||||
resampleQuality: ("low" | "medium" | "high")?, -- codecs/Resample quality (default "medium")
|
||||
}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ constants
|
||||
@@ -101,7 +106,7 @@ local ID3_FRAMES = {
|
||||
{ "title", "TIT2" },
|
||||
{ "artist", "TPE1" },
|
||||
{ "album", "TALB" },
|
||||
{ "albumArtist", "TPE2" },
|
||||
{ "albumartist", "TPE2" },
|
||||
{ "year", "TDRC" },
|
||||
{ "date", "TDRC" },
|
||||
{ "track", "TRCK" },
|
||||
@@ -170,10 +175,30 @@ function Mp3.encode(value: any, opts: EncodeOptions?): (buffer?, Err.Err?)
|
||||
return nil, Err.new(Err.ARG, "layer must be 2 or 3")
|
||||
end
|
||||
local Enc = if layer == 3 then Encode3 else Encode2
|
||||
local info = Enc.RATES[src.sampleRate]
|
||||
if not info then
|
||||
return nil, Err.new(Err.ARG, string.format("layer %d cannot hold %s Hz (no resampling)", layer, tostring(src.sampleRate)))
|
||||
-- the output rate: opts.sampleRate, else the input's when the layer has it, else the nearest rate above it (else
|
||||
-- the highest); other input rates go through codecs/Resample
|
||||
local rate = o.sampleRate or src.sampleRate
|
||||
if o.sampleRate and not Enc.RATES[o.sampleRate] then
|
||||
return nil, Err.new(Err.ARG, string.format("layer %d cannot hold %s Hz", layer, tostring(o.sampleRate)))
|
||||
end
|
||||
if not Enc.RATES[rate] then
|
||||
local best, top = nil, 0
|
||||
for r in Enc.RATES do
|
||||
top = math.max(top, r)
|
||||
if r >= rate and (best == nil or r < best) then
|
||||
best = r
|
||||
end
|
||||
end
|
||||
rate = best or top
|
||||
end
|
||||
if rate ~= src.sampleRate then
|
||||
local rs, rerr = Resample.source(src, rate, { quality = o.resampleQuality })
|
||||
if not rs then
|
||||
return nil, Err.new(Err.ARG, tostring(rerr))
|
||||
end
|
||||
src = rs
|
||||
end
|
||||
local info = Enc.RATES[rate]
|
||||
local mpeg1 = info[1] == 3
|
||||
local mode = o.mode or (if src.channels == 1 then "mono" elseif layer == 3 then "joint" else "stereo")
|
||||
if mode ~= "stereo" and mode ~= "joint" and mode ~= "mono" then
|
||||
@@ -187,7 +212,7 @@ function Mp3.encode(value: any, opts: EncodeOptions?): (buffer?, Err.Err?)
|
||||
end
|
||||
local bitrate = o.bitrate or (if mpeg1 then (if layer == 3 then 128 else 192) else (if layer == 3 then 64 else 96))
|
||||
if not table.find(Enc.BITRATES[if mpeg1 then 1 else 2], bitrate) then
|
||||
return nil, Err.new(Err.ARG, string.format("%s kbps is not a layer %d bit rate at %d Hz", tostring(bitrate), layer, src.sampleRate))
|
||||
return nil, Err.new(Err.ARG, string.format("%s kbps is not a layer %d bit rate at %d Hz", tostring(bitrate), layer, rate))
|
||||
end
|
||||
if layer == 2 and mpeg1 then
|
||||
-- the standard's allowed bit rate / mode combinations
|
||||
@@ -206,7 +231,7 @@ function Mp3.encode(value: any, opts: EncodeOptions?): (buffer?, Err.Err?)
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ open
|
||||
-- the string-valued tag fields (title, artist, album, albumArtist, year, track, disc, genre, composer, comment)
|
||||
-- the string-valued tag fields (title, artist, album, albumartist, date, track, disc, genre, composer, comment)
|
||||
local function stringTags(t: any): { [string]: string }
|
||||
local out: { [string]: string } = {}
|
||||
for k, v in t do
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
--
|
||||
-- Arrays are Luau tables of numbers. All normalised-band data (X, Y, the folding memory `norm` and the scratch
|
||||
-- copies) lives in one table W, and the "pointers" of the C code are 0-based offsets into it.
|
||||
local RD = require("@formats/audio/opus/RangeDecoder")
|
||||
local RD = require("@formats/codecs/RangeCoder/Decoder")
|
||||
local Mdct = require("@formats/audio/opus/Mdct")
|
||||
local T = require("@formats/audio/opus/CeltTables")
|
||||
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
-- (celt_encoder.c, quant_bands.c, laplace.c, rate.c, bands.c, vq.c, cwrs.c; BSD: licenses/libopus-BSD.txt) that
|
||||
-- mirror audio/opus/Celt's decoder step for step; the analysis is simplified. The MDCT is codecs/Mdct.
|
||||
-- Never throws.
|
||||
local RE = require("@formats/audio/opus/RangeEncoder")
|
||||
local RE = require("@formats/codecs/RangeCoder/Encoder")
|
||||
local T = require("@formats/audio/opus/CeltTables")
|
||||
local Mdct = require("@formats/codecs/Mdct")
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
-- audio/opus/Decoder: The Opus decoder proper (libopus src/opus_decoder.c and opus_multistream_decoder.c): per-frame mode switching
|
||||
-- between SILK, hybrid and CELT, the redundant CELT frames of mode transitions, cross-fades, PLC fallbacks, the
|
||||
-- multi-frame packet loop and multistream (channel mapping) decoding. Output is 48 kHz float in Luau arrays.
|
||||
local RD = require("@formats/audio/opus/RangeDecoder")
|
||||
local RD = require("@formats/codecs/RangeCoder/Decoder")
|
||||
local Packet = require("@formats/audio/opus/Packet")
|
||||
local Celt = require("@formats/audio/opus/Celt")
|
||||
local Silk = require("@formats/audio/opus/Silk")
|
||||
|
||||
@@ -2,10 +2,14 @@
|
||||
-- audio/opus/Encode: Ogg Opus writing (RFC 7845) around 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 (a Kaiser-windowed sinc; quality enough for a codec input). New in formats-luau.
|
||||
-- other rates is resampled to 48 kHz (codecs/Resample); the input is a Types.Audio or a Decoder (codecs/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/Pcm")
|
||||
local Resample = require("@formats/codecs/Resample")
|
||||
local VorbisComment = require("@formats/codecs/VorbisComment")
|
||||
local CeltEncoder = require("@formats/audio/opus/CeltEncoder")
|
||||
|
||||
local Encode = {}
|
||||
@@ -17,69 +21,20 @@ export type Options = {
|
||||
serial: number?, -- Ogg stream serial (default 0x4f707573)
|
||||
}
|
||||
|
||||
local floor, sin, pi, sqrt = math.floor, math.sin, math.pi, math.sqrt
|
||||
|
||||
local function besselI0(x: number): number
|
||||
local sum, term = 1, 1
|
||||
for k = 1, 60 do
|
||||
term *= (x / (2 * k)) ^ 2
|
||||
sum += term
|
||||
if term < sum * 1e-15 then
|
||||
break
|
||||
end
|
||||
end
|
||||
return sum
|
||||
end
|
||||
|
||||
-- resample(src (interleaved f32 buffer), frames, channels, from, to) -> { number } interleaved, frames out
|
||||
local function resample(src: buffer, frames: number, channels: number, from: number, to: number): ({ number }, number)
|
||||
local outFrames = floor(frames * to / from + 0.5)
|
||||
local out = table.create(outFrames * channels, 0)
|
||||
local ratio = to / from
|
||||
local cutoff = 0.97 * math.min(1, ratio) -- of the input Nyquist
|
||||
local half = 16 -- zero crossings each side (at the cutoff)
|
||||
local width = half / cutoff
|
||||
local beta = 8
|
||||
local i0b = besselI0(beta)
|
||||
local readf32 = buffer.readf32
|
||||
for j = 0, outFrames - 1 do
|
||||
local t = j / ratio -- the input position
|
||||
local first = math.max(0, floor(t - width) + 1)
|
||||
local last = math.min(frames - 1, floor(t + width))
|
||||
for c = 0, channels - 1 do
|
||||
local acc, wsum = 0, 0
|
||||
for i = first, last do
|
||||
local d = i - t
|
||||
local x = d * cutoff
|
||||
local s = if x == 0 then 1 else sin(pi * x) / (pi * x)
|
||||
local r = d / width
|
||||
local w = besselI0(beta * sqrt(math.max(0, 1 - r * r))) / i0b
|
||||
local h = s * w
|
||||
acc += readf32(src, (i * channels + c) * 4) * h
|
||||
wsum += h
|
||||
end
|
||||
out[j * channels + c + 1] = if wsum ~= 0 then acc / wsum else 0
|
||||
end
|
||||
end
|
||||
return out, outFrames
|
||||
end
|
||||
|
||||
local TAG_NAMES: { [string]: string } = {
|
||||
title = "TITLE", artist = "ARTIST", album = "ALBUM", albumartist = "ALBUMARTIST", date = "DATE", genre = "GENRE",
|
||||
track = "TRACKNUMBER", tracktotal = "TRACKTOTAL", disc = "DISCNUMBER", comment = "COMMENT", composer = "COMPOSER",
|
||||
}
|
||||
local floor = math.floor
|
||||
|
||||
-- encode(audio | decoder, opts) -> buffer | nil, Err
|
||||
function Encode.encode(value: any, opts: Options?): (buffer?, Err.Err?)
|
||||
local o = opts or {}
|
||||
if type(value) ~= "table" or type(value.sampleRate) ~= "number" or type(value.channels) ~= "number" then
|
||||
return nil, Err.new(Err.ARG, "expected an Audio or a Decoder")
|
||||
local src, serr = Pcm.source(value)
|
||||
if not src then
|
||||
return nil, serr
|
||||
end
|
||||
local ch = value.channels
|
||||
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 = value.sampleRate
|
||||
local rate = src.sampleRate
|
||||
if rate < 1000 or rate > 768000 then
|
||||
return nil, Err.new(Err.ARG, "bad sample rate " .. tostring(rate))
|
||||
end
|
||||
@@ -87,40 +42,24 @@ function Encode.encode(value: any, opts: Options?): (buffer?, Err.Err?)
|
||||
if type(bitrate) ~= "number" or bitrate < 6000 or bitrate > 510000 then
|
||||
return nil, Err.new(Err.ARG, "bitrate 6000 .. 510000")
|
||||
end
|
||||
-- the whole input as an interleaved f32 buffer
|
||||
local samples: buffer, frames: number
|
||||
if value.kind == "audio" and type(value.samples) == "buffer" then
|
||||
samples, frames = value.samples, value.frames
|
||||
if buffer.len(samples) < frames * ch * 4 then
|
||||
return nil, Err.new(Err.ARG, "audio.samples is shorter than frames * channels")
|
||||
-- the whole input at 48 kHz (codecs/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
|
||||
elseif type(value.read) == "function" then
|
||||
local w = Bytes.writer(65536)
|
||||
frames = 0
|
||||
local chunk: buffer? = nil
|
||||
while true do
|
||||
local b, n = value:read(16384, chunk)
|
||||
chunk = b
|
||||
if n <= 0 then
|
||||
break
|
||||
end
|
||||
w:bytes(b, 0, n * ch * 4)
|
||||
frames += n
|
||||
end
|
||||
samples = w:finish()
|
||||
else
|
||||
return nil, Err.new(Err.ARG, "expected an Audio or a Decoder")
|
||||
src = rs
|
||||
end
|
||||
-- 48 kHz floats
|
||||
local pcm: { number }, n48: number
|
||||
if rate == 48000 then
|
||||
n48 = frames
|
||||
pcm = table.create(frames * ch, 0)
|
||||
for i = 1, frames * ch do
|
||||
pcm[i] = buffer.readf32(samples, (i - 1) * 4)
|
||||
end
|
||||
else
|
||||
pcm, n48 = resample(samples, frames, ch, rate, 48000)
|
||||
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
|
||||
@@ -144,22 +83,15 @@ function Encode.encode(value: any, opts: Options?): (buffer?, Err.Err?)
|
||||
head:u8(0)
|
||||
s:packet(head:finish(), 0, { flush = true })
|
||||
-- OpusTags
|
||||
local tags = o.tags or value.tags or {}
|
||||
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 keys = {}
|
||||
for k, v in tags do
|
||||
if type(k) == "string" and type(v) == "string" then
|
||||
table.insert(keys, k)
|
||||
end
|
||||
end
|
||||
table.sort(keys)
|
||||
tw:u32le(#keys)
|
||||
for _, k in keys do
|
||||
local field = (TAG_NAMES[k] or string.upper(k)) .. "=" .. tags[k]
|
||||
local fields = VorbisComment.fields(tags)
|
||||
tw:u32le(#fields)
|
||||
for _, field in fields do
|
||||
tw:u32le(#field)
|
||||
tw:string(field)
|
||||
end
|
||||
|
||||
@@ -4,6 +4,10 @@
|
||||
-- audio/opus/Mdct: CELT inverse MDCT (libopus celt/mdct.c clt_mdct_backward) over a mixed-radix complex FFT (a port of the
|
||||
-- kiss_fft stages used by libopus: radix 5, 3, 4 and 2). The 48 kHz mode uses N = 1920 >> shift for
|
||||
-- shift = 0..3 (FFT sizes 480, 240, 120, 60). Arrays are Luau tables of numbers, 1-based.
|
||||
-- Why not codecs/Mdct (measured by the cleanup pass): the shared transform computes the full N-sample IMDCT through a
|
||||
-- DCT-IV, of which CELT keeps the middle N / 2, so routing the decoder through it made CELT decoding 2-3x slower
|
||||
-- (luau: 25 -> 54-76 ms for 3 s of 32 kbps stereo) and changed the output bits (same SNR against opusdec). This
|
||||
-- fused libopus port (pre-rotation, a kiss_fft with fixed factors, post-rotation, TDAC) stays private.
|
||||
local Mdct = {}
|
||||
|
||||
local sin, cos, pi = math.sin, math.cos, math.pi
|
||||
|
||||
@@ -14,7 +14,7 @@
|
||||
--
|
||||
-- Fixed-point emulation: all values are Luau numbers holding C int32 / int16 values. Products of two int32 values
|
||||
-- (64-bit in C) are split so that every intermediate stays below 2^53 and therefore exact.
|
||||
local RD = require("@formats/audio/opus/RangeDecoder")
|
||||
local RD = require("@formats/codecs/RangeCoder/Decoder")
|
||||
local T = require("@formats/audio/opus/SilkTables")
|
||||
|
||||
local icdf = RD.icdf
|
||||
|
||||
@@ -24,7 +24,9 @@ local Types = require("@formats/Types")
|
||||
local Ogg = require("@formats/containers/Ogg")
|
||||
local Decoder = require("@formats/audio/opus/Decoder")
|
||||
local Packet = require("@formats/audio/opus/Packet")
|
||||
local RangeDecoder = require("@formats/audio/opus/RangeDecoder")
|
||||
local VorbisComment = require("@formats/codecs/VorbisComment")
|
||||
local Pcm = require("@formats/codecs/Pcm")
|
||||
local RangeDecoder = require("@formats/codecs/RangeCoder/Decoder")
|
||||
local Celt = require("@formats/audio/opus/Celt")
|
||||
local Silk = require("@formats/audio/opus/Silk")
|
||||
local Encode = require("@formats/audio/opus/Encode")
|
||||
@@ -35,7 +37,7 @@ local Opus = {
|
||||
category = "audio",
|
||||
extensions = { "opus" },
|
||||
mime = { "audio/opus", "audio/ogg" },
|
||||
deps = { "core/Err", "core/Bytes", "Types", "containers/Ogg", "codecs/Mdct" },
|
||||
deps = { "core/Err", "core/Bytes", "Types", "containers/Ogg", "codecs/Mdct", "codecs/RangeCoder/Decoder", "codecs/RangeCoder/Encoder", "codecs/Pcm", "codecs/Resample", "codecs/VorbisComment" },
|
||||
caps = { decode = true, encode = true, streaming = true, seek = true, lossy = true, channels = 255, rates = { 48000 }, encodeModes = { "celt" } },
|
||||
}
|
||||
|
||||
@@ -124,46 +126,40 @@ local function parseHead(b: buffer, off: number, size: number): (Head?, string?)
|
||||
return head, nil
|
||||
end
|
||||
|
||||
-- OpusTags (RFC 7845 section 5.2): vendor string and a table of lower-cased field names to values (repeated
|
||||
-- fields joined with "; ").
|
||||
local function parseTags(b: buffer, off: number, size: number): (string, { [string]: string })
|
||||
local tags = {}
|
||||
-- OpusTags (RFC 7845 section 5.2) -> vendor, tags (codecs/VorbisComment: lower-case keys, repeated fields joined
|
||||
-- with "; "), the raw fields
|
||||
local function parseTags(b: buffer, off: number, size: number): (string, { [string]: string }, { VorbisComment.Field })
|
||||
if size < 16 or buffer.readstring(b, off, 8) ~= "OpusTags" then
|
||||
return "", tags
|
||||
return "", {}, {}
|
||||
end
|
||||
local p = off + 8
|
||||
local e = off + size
|
||||
local vlen = buffer.readu32(b, p)
|
||||
p += 4
|
||||
if p + vlen > e then
|
||||
return "", tags
|
||||
return "", {}, {}
|
||||
end
|
||||
local vendor = buffer.readstring(b, p, vlen)
|
||||
p += vlen
|
||||
if p + 4 > e then
|
||||
return vendor, tags
|
||||
end
|
||||
local n = buffer.readu32(b, p)
|
||||
p += 4
|
||||
for _ = 1, n do
|
||||
if p + 4 > e then
|
||||
break
|
||||
end
|
||||
local l = buffer.readu32(b, p)
|
||||
local list = {}
|
||||
if p + 4 <= e then
|
||||
local n = buffer.readu32(b, p)
|
||||
p += 4
|
||||
if p + l > e then
|
||||
break
|
||||
end
|
||||
local s = buffer.readstring(b, p, l)
|
||||
p += l
|
||||
local eq = string.find(s, "=", 1, true)
|
||||
if eq then
|
||||
local k = string.lower(string.sub(s, 1, eq - 1))
|
||||
local v = string.sub(s, eq + 1)
|
||||
tags[k] = if tags[k] then tags[k] .. "; " .. v else v
|
||||
for _ = 1, n do
|
||||
if p + 4 > e then
|
||||
break
|
||||
end
|
||||
local l = buffer.readu32(b, p)
|
||||
p += 4
|
||||
if p + l > e then
|
||||
break
|
||||
end
|
||||
table.insert(list, buffer.readstring(b, p, l))
|
||||
p += l
|
||||
end
|
||||
end
|
||||
return vendor, tags
|
||||
local tags, raw = VorbisComment.tags(list)
|
||||
return vendor, tags, raw
|
||||
end
|
||||
|
||||
Opus.Decoder = Decoder
|
||||
@@ -213,7 +209,7 @@ function Opus.decode(r: any, opts: OpenOptions?): (Types.Audio?, Err.Err?)
|
||||
a.frames = total
|
||||
end
|
||||
a.tags = dec.tags
|
||||
a.meta.source = { format = "opus", channels = dec.sourceChannels, preSkip = dec.preSkip, vendor = dec.vendor, mappingFamily = dec.head.mappingFamily, inputSampleRate = dec.head.inputSampleRate }
|
||||
a.meta.source = { format = "opus", channels = dec.sourceChannels, preSkip = dec.preSkip, vendor = dec.vendor, comments = dec.comments, mappingFamily = dec.head.mappingFamily, inputSampleRate = dec.head.inputSampleRate }
|
||||
return a, nil
|
||||
end
|
||||
|
||||
@@ -288,6 +284,9 @@ function Stream.read(self: any, maxFrames: number, into: buffer?): (buffer, numb
|
||||
Decoder.writeDownmix(self.pend, self.pendPos * self.sourceChannels, n, self.sourceChannels, self.downmix, gain, out, got * 8)
|
||||
else
|
||||
Decoder.writeF32(self.pend, self.pendPos * ch, n * ch, gain, out, got * ch * 4)
|
||||
if self.order then
|
||||
Pcm.reorder(out, got * ch * 4, n, ch, self.order)
|
||||
end
|
||||
end
|
||||
self.pendPos += n
|
||||
got += n
|
||||
@@ -356,7 +355,7 @@ function Opus.open(r: any, opts: OpenOptions?): (any, Err.Err?)
|
||||
if tp == nil then
|
||||
return nil, Err.new(Err.TRUNCATED, "missing OpusTags")
|
||||
end
|
||||
local vendor, tags = parseTags(tp.data, tp.offset, tp.size)
|
||||
local vendor, tags, rawTags = parseTags(tp.data, tp.offset, tp.size)
|
||||
-- granule of the first audio sample: first page granule minus the packets that end on it
|
||||
local startGranule = 0
|
||||
local sum = 0
|
||||
@@ -391,6 +390,9 @@ function Opus.open(r: any, opts: OpenOptions?): (any, Err.Err?)
|
||||
head = head,
|
||||
vendor = vendor,
|
||||
tags = tags,
|
||||
comments = rawTags,
|
||||
-- mapping family 1 is in Vorbis channel order; the output is WAVE order (codecs/Pcm)
|
||||
order = if not downmix and head.mappingFamily == 1 and head.channels > 2 then Pcm.WAVE_FROM_VORBIS[head.channels] else nil,
|
||||
sampleRate = SAMPLE_RATE,
|
||||
channels = if downmix then 2 else head.channels,
|
||||
sourceChannels = head.channels,
|
||||
|
||||
Reference in new issue
Block a user