ws-audio2: music/xm decodes ModPlug ADPCM samples (were silence), through codecs/audio/Adpcm.modplug
Fixture: tools/fixtures/_tracker.py write_xm gains an optional per-sample "adpcm" payload (two lines, shared with the tracker generators, backward compatible); a new xm case renders the same as openmpt123.
This commit is contained in:
1 parent
2717292366
commit
9466c5f471
7 files changed
+96
-13
No files matched your search
@@ -956,7 +956,7 @@ local list: { Entry } = {
|
||||
path = "music/xm",
|
||||
extensions = { "xm" },
|
||||
mime = { "audio/xm", "audio/x-xm" },
|
||||
deps = { "core/Err", "core/Bytes", "Types", "codecs/audio/Tracker" },
|
||||
deps = { "core/Err", "core/Bytes", "Types", "codecs/audio/Tracker", "codecs/audio/Adpcm" },
|
||||
caps = { decode = true, encode = true, render = true, channels = { 1, 64 } },
|
||||
load = function()
|
||||
return require("@formats/music/xm")
|
||||
|
||||
@@ -19,6 +19,7 @@
|
||||
-- Adpcm.adxCoefs(cutoff, rate) -> c1, c2
|
||||
-- Adpcm.psx(src, p, pstride, nframes, st, so, dst, d, ds) -- Sony PS-ADPCM (VAG): 16-byte frames, 28 samples
|
||||
-- Adpcm.dsp(src, p, count, coefs, co, st, so, dst, d, ds) -- Nintendo DSP: 8-byte frames, 14 samples
|
||||
-- Adpcm.modplug(src, p, count, dst, d) -- ModPlug ADPCM (XM / MOD samples)
|
||||
-- encoders: imaEncode, imaWavEncode, msEncode, adxEncode, psxEncode, dspEncode (see each)
|
||||
--
|
||||
-- Every function reads only the bytes its arguments describe: the caller checks the ranges (byte counts per call are
|
||||
@@ -685,4 +686,22 @@ function Adpcm.psxEncode(x: buffer, xi: number, xs: number, count: number, st: b
|
||||
writei32(st, so + 4, h2)
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ ModPlug
|
||||
-- modplug: ModPlug / OpenMPT ADPCM samples (XM / MOD sample data marked 0xAD): a table of 16 signed byte deltas at
|
||||
-- p, then count nibbles (low first) at p + 16, each adding its table delta to a running signed byte (wrapping).
|
||||
-- Writes count f32 (value / 128) to dst from float index d. Reads p .. p + 16 + ceil(count / 2) - 1.
|
||||
function Adpcm.modplug(src: buffer, p: number, count: number, dst: buffer, d: number)
|
||||
local tab = table.create(16, 0)
|
||||
for k = 0, 15 do
|
||||
tab[k + 1] = buffer.readi8(src, p + k)
|
||||
end
|
||||
local v = 0
|
||||
for i = 0, count - 1 do
|
||||
local b = readu8(src, p + 16 + rshift(i, 1))
|
||||
local nib = if band(i, 1) == 0 then band(b, 15) else rshift(b, 4)
|
||||
v = (v + tab[nib + 1] + 128) % 256 - 128
|
||||
writef32(dst, (d + i) * 4, v / 128)
|
||||
end
|
||||
end
|
||||
|
||||
return Adpcm
|
||||
@@ -5,13 +5,15 @@
|
||||
-- delta samples, multi-sample instruments with volume / panning envelopes, fadeout and auto-vibrato, linear and Amiga
|
||||
-- frequency tables, every FT2 effect and volume-column command. Writes XM from "xm" modules (byte-identical for files
|
||||
-- in FT2's layout) and from "mod" modules (one instrument per sample, Amiga frequencies).
|
||||
-- Not supported: ModPlug ADPCM samples (read as silence), OpenMPT's stereo-sample / extra-effect extensions on write.
|
||||
-- ModPlug ADPCM samples decode (codecs/audio/Adpcm; written back as plain delta PCM). Not supported: OpenMPT's
|
||||
-- stereo-sample / extra-effect extensions on write.
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ requires
|
||||
local Err = require("@formats/core/Err")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Types = require("@formats/Types")
|
||||
local Tracker = require("@formats/codecs/audio/Tracker")
|
||||
local Adpcm = require("@formats/codecs/audio/Adpcm")
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ contract
|
||||
local Xm = {
|
||||
@@ -19,7 +21,7 @@ local Xm = {
|
||||
category = "music",
|
||||
extensions = { "xm" },
|
||||
mime = { "audio/xm", "audio/x-xm" },
|
||||
deps = { "core/Err", "core/Bytes", "Types", "codecs/audio/Tracker" },
|
||||
deps = { "core/Err", "core/Bytes", "Types", "codecs/audio/Tracker", "codecs/audio/Adpcm" },
|
||||
caps = { decode = true, encode = true, render = true, channels = { 1, 64 } },
|
||||
}
|
||||
|
||||
@@ -240,12 +242,19 @@ function Xm.decode(r: Bytes.Reader, opts: DecodeOptions?): (any, Err.Err?)
|
||||
s.relativeNote = h.rel
|
||||
s.pan = h.pan
|
||||
local frames = h.len // bps
|
||||
if h.res == 0xAD then
|
||||
-- ModPlug ADPCM: 16-byte table + 4 bits per sample (not decoded: silence of the right length)
|
||||
local bytes = 16 + (h.len + 1) // 2
|
||||
r:skip(math.min(bytes, r:remaining()))
|
||||
frames = h.len
|
||||
if h.res == 0xAD and bits == 8 then
|
||||
-- ModPlug ADPCM (codecs/audio/Adpcm): a 16-byte delta table, then 4 bits per sample; a cut file keeps
|
||||
-- the samples whose nibbles are present
|
||||
frames = math.max(0, math.min(h.len, (r:remaining() - 16) * 2))
|
||||
total += frames * 4
|
||||
if total > maxBytes then
|
||||
return nil, Err.new(Err.LIMIT, "sample data over the maxBytes limit")
|
||||
end
|
||||
s.data = buffer.create(frames * 4)
|
||||
if frames > 0 then
|
||||
Adpcm.modplug(r.buf, r.pos, frames, s.data, 0)
|
||||
end
|
||||
r:skip(math.min(16 + (h.len + 1) // 2, r:remaining()))
|
||||
else
|
||||
local avail = r:remaining() // bps
|
||||
local got = math.min(frames, avail)
|
||||
|
||||
@@ -72,6 +72,22 @@ do
|
||||
t:check(z1 == 0 and z2 == 0, "adx coefs of rate 0")
|
||||
end
|
||||
|
||||
-- ModPlug ADPCM: deltas from the table, wrapping as a signed byte
|
||||
do
|
||||
local src = buffer.create(18)
|
||||
for k = 0, 15 do
|
||||
buffer.writei8(src, k, if k == 1 then 100 elseif k == 2 then -3 else 0)
|
||||
end
|
||||
buffer.writeu8(src, 16, 0x11) -- +100, +100 (wraps to -56)
|
||||
buffer.writeu8(src, 17, 0x02) -- -3, +0
|
||||
local out = buffer.create(16)
|
||||
Adpcm.modplug(src, 0, 4, out, 0)
|
||||
local want = { 100, -56, -59, -59 }
|
||||
for i, v in want do
|
||||
t:eq(buffer.readf32(out, (i - 1) * 4) * 128, v, "modplug sample " .. i)
|
||||
end
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ encoders
|
||||
local function snr(x: buffer, y: buffer, count: number): number
|
||||
local sig, err = 0, 0
|
||||
@@ -216,6 +232,9 @@ t:fuzz("kernels", function(b: buffer)
|
||||
if n >= 1 then
|
||||
Adpcm.ima(b, 0, 0, 1, math.min(n * 2, 1000), true, st, 32, out, 0, 1)
|
||||
end
|
||||
if n >= 17 then
|
||||
Adpcm.modplug(b, 0, math.min((n - 16) * 2, 1000), out, 0)
|
||||
end
|
||||
Adpcm.imaWav(b, 0, n, 2, 1017, buffer.create(1017 * 2 * 4), 0)
|
||||
Adpcm.msBlock(b, 0, n, 2, 1012, buffer.create(1012 * 2 * 4), 0, coefs, 8, buffer.create(40))
|
||||
local dec = Decoder.interleaved({ sampleRate = 8000, channels = 2, tags = {} }, { kind = "adx", buf = b, offset = 0, size = n, interleave = 18, c1 = 7334, c2 = -3283 })
|
||||
|
||||
+8
-2
@@ -1,5 +1,6 @@
|
||||
-- music/xm tests: generated FastTracker 2 modules (tools/fixtures/xm.py) parsed, rendered through codecs/audio/Tracker and
|
||||
-- compared with openmpt123's render (loudness envelope, band spectrum, stereo balance, song length), the XM writer's
|
||||
-- compared with openmpt123's render (loudness envelope, band spectrum, stereo balance, song length; a ModPlug ADPCM
|
||||
-- sample), the XM writer's
|
||||
-- round trip, the contract, and the fuzz no-throw pass.
|
||||
local T = require("@tests/T")
|
||||
local S = require("@tests/music/_similar")
|
||||
@@ -44,7 +45,12 @@ for _, c in Cases do
|
||||
t:check(out, c.name .. ": encode " .. tostring(werr))
|
||||
local m2 = Xm.decode(Bytes.reader(out :: buffer))
|
||||
t:check(m2, c.name .. ": re-decode")
|
||||
t:eq(buffer.tostring(out :: buffer), c.data, c.name .. ": round trip bytes")
|
||||
if c.name == "adpcm" then
|
||||
-- ModPlug ADPCM is written back as delta PCM: the same samples, not the same bytes
|
||||
t:eq(buffer.tostring(m2.samples[1].data), buffer.tostring(m.samples[1].data), c.name .. ": samples kept")
|
||||
else
|
||||
t:eq(buffer.tostring(out :: buffer), c.data, c.name .. ": round trip bytes")
|
||||
end
|
||||
end
|
||||
|
||||
-- seek: the position after a seek and the output continuing
|
||||
|
||||
@@ -242,9 +242,10 @@ def write_xm(title, instruments, orders, patterns, channels, speed=6, tempo=125,
|
||||
ls, ll, lt = s.get("loop") or (0, 0, 0)
|
||||
t = lt | (16 if bpsm == 2 else 0)
|
||||
out += struct.pack("<IIIBbBBbB", len(s["data"]), ls * bpsm, ll * bpsm, s.get("volume", 64), s.get("finetune", 0),
|
||||
t, s.get("pan", 128), s.get("relnote", 0), 0) + name(s.get("name", ""), 22)
|
||||
t, s.get("pan", 128), s.get("relnote", 0), 0xAD if s.get("adpcm") else 0) + name(s.get("name", ""), 22)
|
||||
for s in smps:
|
||||
out += delta16(s["data"]) if s.get("bits", 8) == 16 else delta8(s["data"])
|
||||
# "adpcm": ModPlug ADPCM bytes (16-byte delta table + nibbles) stored instead of the delta samples
|
||||
out += s["adpcm"] if s.get("adpcm") else delta16(s["data"]) if s.get("bits", 8) == 16 else delta8(s["data"])
|
||||
return bytes(out)
|
||||
|
||||
|
||||
|
||||
+30
-1
@@ -159,9 +159,38 @@ def song_effects(linear=True):
|
||||
return T.write_xm("effects", instruments(), [0, 1], [a, b], 6, speed=5, tempo=130, linear=linear, restart=1)
|
||||
|
||||
|
||||
TABLE = [0, 1, 2, 4, 8, 16, 32, 64, -1, -2, -4, -8, -16, -32, -64, -128]
|
||||
|
||||
|
||||
def modplug_adpcm(raw):
|
||||
"""ModPlug ADPCM of 8-bit sample bytes: the delta table, then one nibble per sample (low first), each nibble the
|
||||
table delta that lands nearest (the running value wraps as a signed byte)"""
|
||||
cur, nibs = 0, []
|
||||
for b in raw:
|
||||
want = b - 256 if b >= 128 else b
|
||||
best = min(range(16), key=lambda k: abs(((cur + TABLE[k] + 128) % 256 - 128) - want))
|
||||
cur = (cur + TABLE[best] + 128) % 256 - 128
|
||||
nibs.append(best)
|
||||
if len(nibs) % 2:
|
||||
nibs.append(0)
|
||||
return bytes(t & 255 for t in TABLE) + bytes(nibs[i] | nibs[i + 1] << 4 for i in range(0, len(nibs), 2))
|
||||
|
||||
|
||||
def song_adpcm():
|
||||
"""a ModPlug ADPCM sample (OpenMPT's compressed 8-bit samples) played at a few pitches"""
|
||||
raw = T.s8(T.synth("saw", 400, 100, 0.6))
|
||||
smp = {"name": "adpcm", "data": raw, "adpcm": modplug_adpcm(raw), "volume": 64, "pan": 128, "loop": (0, 400, 1)}
|
||||
p = empty(2)
|
||||
for i, note in enumerate((49, 53, 56, 61)):
|
||||
put(p, i * 8, 0, note, 1)
|
||||
put(p, i * 8 + 4, 1, note - 12, 1)
|
||||
return T.write_xm("adpcm", [{"name": "adpcm", "samples": [smp]}], [0], [p], 2, speed=4, tempo=125)
|
||||
|
||||
|
||||
def generate(ctx):
|
||||
cases = []
|
||||
for nm, data in (("instruments", song_instruments()), ("effects", song_effects()), ("amiga", song_effects(False))):
|
||||
for nm, data in (("instruments", song_instruments()), ("effects", song_effects()), ("amiga", song_effects(False)),
|
||||
("adpcm", song_adpcm())):
|
||||
ref = T.reference(ctx, data, "xm")
|
||||
cases.append({"name": nm, "data": data, "ref": ref, "duration": ref["duration"]})
|
||||
ctx.emit("xm_cases", cases)
|
||||
Reference in new issue
Block a user