diff --git a/src/Rosetta/codecs/audio/Tracker/Mixer.luau b/src/Rosetta/codecs/audio/Tracker/Mixer.luau index cf120e0..84e115b 100644 --- a/src/Rosetta/codecs/audio/Tracker/Mixer.luau +++ b/src/Rosetta/codecs/audio/Tracker/Mixer.luau @@ -3,7 +3,7 @@ --!optimize 2 -- codecs/audio/Tracker/Mixer: tracker voice mixing (loops, IT sustain loops, IT two-pole resonant filter, click ramps). -- No third-party code. -local floor, ceil, min = math.floor, math.ceil, math.min +local floor, ceil, min, max = math.floor, math.ceil, math.min, math.max local readf32, writef32 = buffer.readf32, buffer.writef32 local Mixer = {} @@ -88,8 +88,8 @@ local function mixVoice(v: any, out: buffer?, off: number, n: number, linear: bo local fa, fb0, fb1 = v.fa, v.fb0, v.fb1 local y1l, y2l, y1r, y2r = v.y1l, v.y2l, v.y1r, v.y2r local o = (off + i) * 8 - -- the common voice (mono, interpolated, unfiltered) runs the general loop only while its volume ramps - local fast = nch == 1 and linear and not fOn + -- the common voice (mono, interpolated) runs the general loop only while its volume ramps + local fast = nch == 1 and linear local mg = if fast then (if rl > 0 then min(rl, m) else 0) else m for _ = 1, mg do local idx = floor(p) @@ -152,26 +152,59 @@ local function mixVoice(v: any, out: buffer?, off: number, n: number, linear: bo o += 8 p += step end - if mg < m then -- the general loop, specialised to the fast voice after its ramp - local k0 = mg + 1 - if step >= 0.001 and p >= 0 and s >= 0 and e <= len then - -- while p < e - 1, idx = floor(p) is in range and idx + 1 < e needs no wrap; two steps of margin - -- for the rounding of the running p - local safe = min(m, mg + floor((e - 1 - p) / step) - 2) - for _ = k0, safe do - local idx = floor(p) - local a = readf32(data, idx * 4) - local x = a + (readf32(data, idx * 4 + 4) - a) * (p - idx) - writef32(out, o, readf32(out, o) + x * gl) - writef32(out, o + 4, readf32(out, o + 4) + x * gr) - o += 8 - p += step - end - if safe >= k0 then - k0 = safe + 1 - end + -- while p < e - 1 (forward), idx = floor(p) is in range and idx + 1 < e needs no wrap: those steps skip the + -- checks; two steps of margin for the rounding of the running p + local k0 = mg + 1 + local safe = mg + if mg < m and step >= 0.001 and p >= 0 and s >= 0 and e <= len then + safe = min(m, mg + floor((e - 1 - p) / step) - 2) + end + if mg < m and fOn then -- the general loop, specialised to a filtered mono voice after its ramp + for _ = k0, safe do + local idx = floor(p) + local a = readf32(data, idx * 4) + local x = a + (readf32(data, idx * 4 + 4) - a) * (p - idx) + local y = x * fa + y1l * fb0 + y2l * fb1 + y2l, y1l = y1l, y + writef32(out, o, readf32(out, o) + y * gl) + writef32(out, o + 4, readf32(out, o + 4) + y * gr) + o += 8 + p += step end - for _ = k0, m do + for _ = max(k0, safe + 1), m do + local idx = floor(p) + if idx > hi then + idx = hi + elseif idx < 0 then + idx = 0 + end + local j = idx + 1 + if j >= e then + j = wrapJ + end + if j > hi then + j = hi + end + local a = readf32(data, idx * 4) + local x = a + (readf32(data, j * 4) - a) * (p - idx) + local y = x * fa + y1l * fb0 + y2l * fb1 + y2l, y1l = y1l, y + writef32(out, o, readf32(out, o) + y * gl) + writef32(out, o + 4, readf32(out, o + 4) + y * gr) + o += 8 + p += step + end + elseif mg < m then -- the same, unfiltered + for _ = k0, safe do + local idx = floor(p) + local a = readf32(data, idx * 4) + local x = a + (readf32(data, idx * 4 + 4) - a) * (p - idx) + writef32(out, o, readf32(out, o) + x * gl) + writef32(out, o + 4, readf32(out, o + 4) + x * gr) + o += 8 + p += step + end + for _ = max(k0, safe + 1), m do local idx = floor(p) if idx > hi then idx = hi