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
|
||||
|
||||
Reference in new issue
Block a user