Bass 2 per-track audio filter | C++ reconstruction

The original side was reconstructed by tracing the 1998 DLL's x87 stack.
The replacement side expresses the patched x87 routine in C++17. Field names
were assigned for readability. Offsets are relative to a single track unless
marked as a Machine or MasterInfo field. The code models finite audio and
valid Buzz controls. The DLL can retain extra x87 precision between stores.

The two routines below were compiled separately and compared with their
respective DLLs on the same inputs. Across 160 original and 170 patched
blocks, every output float matched. The tests covered five sample rates,
moving and static cutoffs, resonance, and nonzero initial filter states.

--- Bass 2/filter.cpp
+++ Bass 2 TPT/filter.cpp
@@ -1,49 +1,62 @@
 // Reconstructed fields used by this routine (32-bit Buzz layout).
 #include <algorithm>
 #include <cmath>
 
 struct MasterInfo {
     int tempo, ticksPerBeat, sampleRate;          // sampleRate at +0x08
 };
 
 struct Machine {
     unsigned char header[0x10];
     MasterInfo* master;                          // +0x10
 };
 
 struct Track {
     double baseCutoff, cutoff, damping, decay;   // +0x00, +0x08, +0x10, +0x18
     double state20, state28, lastHigh;           // +0x20, +0x28, +0x30
     unsigned char remainingFields[0xa0 - 0x38];
     Machine* owner;                              // +0xa0
 
     void filter(float* samples, int count);
 };
 
 void Track::filter(float* samples, int count) {
+    if (count <= 0) return;
+
+    const double sampleRate = owner->master->sampleRate;
+    const double topHz = std::min(18000.0, 0.45 * sampleRate);
+    const double radiansPerHz = 3.14159265358979323846 / sampleRate;
+    const double d = std::clamp(damping, 0.0, 1.0);
+    const double k = 0.125 + (1.4142135623730950488 - 0.125) * d * d;
+
     double f = cutoff;
-    double ratio = decay;
-    double low = state20;
-    double band = state28;
-    double high;
+    double s1 = state20;
+    double s2 = state28;
+    double previous = -1.0;
+    double a1 = 0.0, a2 = 0.0, a3 = 0.0;
 
-    // The original caller supplies a positive block length.
-    do {
-        const double input = *samples;
-        low += f * band;
-        high = (input - low) - damping * band;
-        band += f * high;
-        *samples++ = static_cast<float>(low);
+    for (int i = 0; i < count; ++i) {
+        const double c = std::clamp(f, baseCutoff, 1.0);
+        if (c != previous) {
+            const double hz = 25.0 + (topHz - 25.0) * c * c * c;
+            const double g = std::tan(hz * radiansPerHz);  // x87 FPTAN in the DLL
+            a1 = 1.0 / (1.0 + g * (g + k));
+            a2 = g * a1;
+            a3 = g * a2;
+            previous = c;
+        }
 
-        f *= ratio;
-        if (f < baseCutoff) {
-            f = baseCutoff;
-            ratio = 1.0;
-        }
-    } while (--count);
+        const double v = samples[i] - s2;
+        const double band = a1 * s1 + a2 * v;
+        const double low = s2 + a2 * s1 + a3 * v;
+        s1 = band + (band - s1);
+        s2 = low + (low - s2);
+        samples[i] = static_cast<float>(low);
+
+        f = f > baseCutoff ? std::max(baseCutoff, f * decay) : baseCutoff;
+    }
 
     cutoff = f;
-    state20 = low;
-    state28 = band;
-    lastHigh = high;
+    state20 = std::abs(s1) < 1e-24 ? 0.0 : s1;
+    state28 = std::abs(s2) < 1e-24 ? 0.0 : s2;
 }
