Jeskola Bass 2 with a new resonant filter I gave Codex the 7.5 KB Jeskola Bass 2 DLL from April 1998 and asked it to modernize the filter. The DLL was the only input. Codex traced the 32-bit x86 audio code, found the filter routine shared by all eight tracks, and patched that routine to call a new filter. The finished DLL is 512 bytes larger. The replacement is a two-pole resonant low-pass using a topology-preserving transform (TPT). At 44.1 or 48 kHz, the cutoff runs from about 25 Hz to 18 kHz. The cutoff knob has a cubic response, giving you finer control in the bass range. The existing resonance, envelope modulation, decay, and Max Resonance controls all feed the new filter. The oscillator, volume envelope, slides, and track mixing still run the original machine code. The new machine is called Jeskola Bass 2 TPT, so you can put it alongside the original in Gear\Generators and try the same pattern on both. In the level-matched comparison MP3, the original plays first and the TPT version starts at 2.25 seconds. Codex ran the patched DLL in a 32-bit test host, checked its impulse response against an independent reference at six sample rates from 11.025 to 192 kHz, and rendered all eight tracks together. A test in Buzz itself is still pending. Assembly Both audio render paths already called the same per-track filter. Replacing five bytes at its entry redirected both calls to the new routine. -10001250 55 push ebp -10001251 8B EC mov ebp, esp -10001253 83 E4 F8 and esp, 0xfffffff8 +10001250 E9 2B 0B 00 00 jmp 0x10001d80 The jump overwrites the first five of the six original bytes shown. It skips the trailing F8 and the old filter body. The replacement occupies 596 bytes, including its constants. Reconstructed C++ The original filter advances low from the previous band, then uses that updated low to calculate high. The TPT filter computes the current band-pass and low-pass outputs from the input and two stored integrator values, s1 and s2. - low += f * band; - high = (input - low) - damping * band; - band += f * high; + const double v = input - s2; + const double band = a1 * s1 + a2 * v; + const double low = s2 + a2 * s1 + a3 * v; + s1 = band + (band - s1); + s2 = low + (low - s2); Both versions write low back to the audio buffer. The new coefficients are recalculated whenever the cutoff moves. With g = tan(π × cutoffHz / sampleRate) and k = 1/Q, they are a1 = 1 / (1 + g × (g + k)), a2 = g × a1, and a3 = g × a2. The assembly listing comes directly from the two DLLs. I assigned readable names in the C++ reconstruction and compiled each version to check it against its DLL. Every output sample matched across 160 original-filter test blocks and 170 patched-filter test blocks. Full assembly diff Bass 2 per-track audio filter | Intel x86 disassembly Each line pairs a preferred virtual address (image base 0x10000000) with the actual bytes in the named DLL. Semicolon comments are annotations. This diff follows the executed filter path in each DLL. The original bytes at file offset 0x650 / RVA 0x1250 begin with 55 8B EC 83 E4 F8. The patch overwrites the first five bytes with E9 2B 0B 00 00, a jump to RVA 0x1D80. The F8 at RVA 0x1255 is the last byte of the old AND instruction. The old body from that byte onward is still physically present and the jump skips it. Both call sites are shown at the bottom. ECX holds the track pointer and the callee removes the two stack arguments (sample buffer and count). The replacement occupies 596 bytes from RVA 0x1D80 through 0x1FD3, including its constant pool. The PE also contains header and machine name changes. --- Bass 2/filter.asm +++ Bass 2 TPT/filter.asm @@ -1,81 +1,225 @@ ; Filter entry and reachable implementation -10001250 55 push ebp -10001251 8b ec mov ebp,esp -10001253 83 e4 f8 and esp,0xfffffff8 -10001256 83 ec 24 sub esp,0x24 -10001259 8b 51 1c mov edx,DWORD PTR [ecx+0x1c] -1000125C 8b 41 18 mov eax,DWORD PTR [ecx+0x18] -1000125F dd 41 20 fld QWORD PTR [ecx+0x20] -10001262 dd 41 28 fld QWORD PTR [ecx+0x28] -10001265 dd 41 08 fld QWORD PTR [ecx+0x8] -10001268 89 54 24 08 mov DWORD PTR [esp+0x8],edx -1000126C 8b 51 14 mov edx,DWORD PTR [ecx+0x14] -1000126F 89 44 24 04 mov DWORD PTR [esp+0x4],eax -10001273 8b 41 10 mov eax,DWORD PTR [ecx+0x10] -10001276 89 54 24 20 mov DWORD PTR [esp+0x20],edx -1000127A 8b 51 04 mov edx,DWORD PTR [ecx+0x4] -1000127D 89 44 24 1c mov DWORD PTR [esp+0x1c],eax -10001281 8b 01 mov eax,DWORD PTR [ecx] -10001283 56 push esi -10001284 8b 75 0c mov esi,DWORD PTR [ebp+0xc] -10001287 89 54 24 14 mov DWORD PTR [esp+0x14],edx -1000128B 8b 55 08 mov edx,DWORD PTR [ebp+0x8] -1000128E 89 44 24 10 mov DWORD PTR [esp+0x10],eax - ; Sample loop. Advance the low-pass state using the previous band state. -10001292 d9 c0 fld st(0) -10001294 d8 ca fmul st,st(2) -10001296 d9 02 fld DWORD PTR [edx] -10001298 dd 44 24 20 fld QWORD PTR [esp+0x20] -1000129C d9 ca fxch st(2) -1000129E de c5 faddp st(5),st -100012A0 d9 c2 fld st(2) -100012A2 d9 ca fxch st(2) -100012A4 d8 cc fmul st,st(4) -100012A6 d9 ca fxch st(2) -100012A8 dc 4c 24 08 fmul QWORD PTR [esp+0x8] - ; Compute the next cutoff from the envelope ratio. -100012AC d9 c9 fxch st(1) -100012AE d8 e5 fsub st,st(5) -100012B0 d9 c9 fxch st(1) -100012B2 d9 cb fxch st(3) -100012B4 d9 ca fxch st(2) -100012B6 d9 c9 fxch st(1) -100012B8 d9 c9 fxch st(1) - ; Subtract damping * band from input - low to get high. -100012BA de e9 fsubp st(1),st -100012BC d9 c2 fld st(2) -100012BE dc 5c 24 10 fcomp QWORD PTR [esp+0x10] - ; Round the high-pass intermediate to double before updating band. -100012C2 dd 5c 24 18 fstp QWORD PTR [esp+0x18] -100012C6 dc 4c 24 18 fmul QWORD PTR [esp+0x18] -100012CA df e0 fnstsw ax -100012CC f6 c4 01 test ah,0x1 -100012CF de c2 faddp st(2),st -100012D1 74 16 je 0x100012e9 - ; Cutoff fell below its base. Hold it there for the rest of this block. -100012D3 dd d8 fstp st(0) -100012D5 dd 44 24 10 fld QWORD PTR [esp+0x10] -100012D9 c7 44 24 08 00 00 00 00 mov DWORD PTR [esp+0x8],0x0 -100012E1 c7 44 24 0c 00 00 f0 3f mov DWORD PTR [esp+0xc],0x3ff00000 -100012E9 d9 c2 fld st(2) +10001250 e9 2b 0b 00 00 jmp 0x10001d80 + +10001D80 55 push ebp +10001D81 e8 12 02 00 00 call 0x10001f98 +10001D86 81 c2 42 02 00 00 add edx,0x242 +10001D8C 89 e5 mov ebp,esp +10001D8E 56 push esi +10001D8F 53 push ebx +10001D90 83 e4 f8 and esp,0xfffffff8 +10001D93 83 ec 38 sub esp,0x38 +10001D96 8b 5d 0c mov ebx,DWORD PTR [ebp+0xc] +10001D99 85 db test ebx,ebx +10001D9B 0f 8e d7 01 00 00 jle 0x10001f78 + ; Read track->owner->master->sampleRate. +10001DA1 8b 81 a0 00 00 00 mov eax,DWORD PTR [ecx+0xa0] +10001DA7 8b 40 10 mov eax,DWORD PTR [eax+0x10] +10001DAA db 40 08 fild DWORD PTR [eax+0x8] +10001DAD dd 82 e0 ff ff ff fld QWORD PTR [edx-0x20] +10001DB3 d8 c9 fmul st,st(1) +10001DB5 d9 82 d8 ff ff ff fld DWORD PTR [edx-0x28] +10001DBB db f1 fcomi st,st(1) +10001DBD db d1 fcmovnbe st,st(1) +10001DBF dd d9 fstp st(1) +10001DC1 dd 5c 24 08 fstp QWORD PTR [esp+0x8] +10001DC5 dc ba e8 ff ff ff fdivr QWORD PTR [edx-0x18] +10001DCB dd 5c 24 18 fstp QWORD PTR [esp+0x18] +10001DCF dd 01 fld QWORD PTR [ecx] +10001DD1 dd 41 18 fld QWORD PTR [ecx+0x18] +10001DD4 dd 5c 24 10 fstp QWORD PTR [esp+0x10] + ; Clamp the legacy damping control and map it to k = 1 / Q. +10001DD8 dd 41 10 fld QWORD PTR [ecx+0x10] +10001DDB d9 ee fldz +10001DDD df f1 fcomip st,st(1) +10001DDF 0f 87 a3 01 00 00 ja 0x10001f88 +10001DE5 d9 e8 fld1 +10001DE7 d9 c9 fxch st(1) +10001DE9 db f1 fcomi st,st(1) +10001DEB db d1 fcmovnbe st,st(1) +10001DED dd d9 fstp st(1) +10001DEF dd 82 f0 ff ff ff fld QWORD PTR [edx-0x10] +10001DF5 d8 c9 fmul st,st(1) +10001DF7 de c9 fmulp st(1),st +10001DF9 d8 82 d4 ff ff ff fadd DWORD PTR [edx-0x2c] +10001DFF dd 1c 24 fstp QWORD PTR [esp] +10001E02 dd 41 08 fld QWORD PTR [ecx+0x8] +10001E05 dd 41 20 fld QWORD PTR [ecx+0x20] +10001E08 dd 41 28 fld QWORD PTR [ecx+0x28] +10001E0B d9 ee fldz +10001E0D 8b 45 08 mov eax,DWORD PTR [ebp+0x8] +10001E10 d9 54 24 30 fst DWORD PTR [esp+0x30] +10001E14 dd 54 24 20 fst QWORD PTR [esp+0x20] +10001E18 8d 1c 98 lea ebx,[eax+ebx*4] +10001E1B d9 44 24 30 fld DWORD PTR [esp+0x30] +10001E1F dd 5c 24 28 fstp QWORD PTR [esp+0x28] +10001E23 d9 e8 fld1 +10001E25 d9 e0 fchs +10001E27 dd 5c 24 30 fstp QWORD PTR [esp+0x30] +10001E2B d9 cc fxch st(4) +10001E2D eb 23 jmp 0x10001e52 +10001E2F 90 nop +10001E30 dc 4c 24 10 fmul QWORD PTR [esp+0x10] +10001E34 d9 cb fxch st(3) +10001E36 db f3 fcomi st,st(3) +10001E38 d9 cb fxch st(3) +10001E3A db d3 fcmovnbe st,st(3) +10001E3C 83 c0 04 add eax,0x4 +10001E3F 39 c3 cmp ebx,eax +10001E41 0f 84 e9 00 00 00 je 0x10001f30 +10001E47 d9 cb fxch st(3) +10001E49 eb 07 jmp 0x10001e52 +10001E4B 2e 8d 74 26 00 lea esi,cs:[esi+eiz*1+0x0] +10001E50 d9 cb fxch st(3) + ; Clamp the instantaneous cutoff and compare it with the cached value. +10001E52 db f3 fcomi st,st(3) +10001E54 d9 c3 fld st(3) +10001E56 db d1 fcmovnbe st,st(1) +10001E58 d9 e8 fld1 +10001E5A d9 c9 fxch st(1) +10001E5C db f1 fcomi st,st(1) +10001E5E db d1 fcmovnbe st,st(1) +10001E60 dd d9 fstp st(1) +10001E62 dd 44 24 30 fld QWORD PTR [esp+0x30] +10001E66 d9 c9 fxch st(1) +10001E68 db e9 fucomi st,st(1) +10001E6A dd d9 fstp st(1) +10001E6C 7a 0e jp 0x10001e7c + ; Reuse the coefficients if the cutoff is the same. +10001E6E 74 70 je 0x10001ee0 +10001E70 dd dd fstp st(5) +10001E72 d9 c9 fxch st(1) +10001E74 d9 ca fxch st(2) +10001E76 d9 cb fxch st(3) +10001E78 d9 cc fxch st(4) +10001E7A eb 14 jmp 0x10001e90 +10001E7C dd dd fstp st(5) +10001E7E d9 c9 fxch st(1) +10001E80 d9 ca fxch st(2) +10001E82 d9 cb fxch st(3) +10001E84 d9 cc fxch st(4) +10001E86 66 90 xchg ax,ax +10001E88 2e 8d b4 26 00 00 00 00 lea esi,cs:[esi+eiz*1+0x0] + ; Map normalized cutoff to Hz on a cubic curve. +10001E90 d9 82 dc ff ff ff fld DWORD PTR [edx-0x24] +10001E96 dd 44 24 08 fld QWORD PTR [esp+0x8] +10001E9A d8 e1 fsub st,st(1) +10001E9C d8 ca fmul st,st(2) +10001E9E d8 ca fmul st,st(2) +10001EA0 d8 ca fmul st,st(2) +10001EA2 de c1 faddp st(1),st +10001EA4 dc 4c 24 18 fmul QWORD PTR [esp+0x18] + ; Compute tan(pi * hz / sampleRate) for the TPT coefficients. +10001EA8 d9 f2 fptan +10001EAA dd d8 fstp st(0) +10001EAC dd 04 24 fld QWORD PTR [esp] +10001EAF d8 c1 fadd st,st(1) +10001EB1 d8 c9 fmul st,st(1) +10001EB3 d9 e8 fld1 +10001EB5 dc c1 fadd st(1),st +10001EB7 de f1 fdivrp st(1),st +10001EB9 dd 54 24 28 fst QWORD PTR [esp+0x28] +10001EBD d8 c9 fmul st,st(1) +10001EBF dc c9 fmul st(1),st +10001EC1 d9 c9 fxch st(1) +10001EC3 dd 5c 24 20 fstp QWORD PTR [esp+0x20] +10001EC7 d9 c9 fxch st(1) +10001EC9 dd 5c 24 30 fstp QWORD PTR [esp+0x30] +10001ECD d9 cc fxch st(4) +10001ECF d9 cb fxch st(3) +10001ED1 d9 ca fxch st(2) +10001ED3 d9 c9 fxch st(1) +10001ED5 eb 0b jmp 0x10001ee2 +10001ED7 90 nop +10001ED8 2e 8d b4 26 00 00 00 00 lea esi,cs:[esi+eiz*1+0x0] +10001EE0 dd d8 fstp st(0) + ; Apply the two-integrator low-pass update to one sample. +10001EE2 d9 c1 fld st(1) +10001EE4 d8 28 fsubr DWORD PTR [eax] +10001EE6 dd 44 24 28 fld QWORD PTR [esp+0x28] +10001EEA d8 cc fmul st,st(4) +10001EEC d9 c6 fld st(6) +10001EEE d8 ca fmul st,st(2) +10001EF0 de c1 faddp st(1),st +10001EF2 d9 c6 fld st(6) +10001EF4 d8 cd fmul st,st(5) +10001EF6 d8 c4 fadd st,st(4) +10001EF8 d9 ca fxch st(2) +10001EFA dc 4c 24 20 fmul QWORD PTR [esp+0x20] +10001EFE de c2 faddp st(2),st +10001F00 dc e4 fsubr st(4),st +10001F02 de c4 faddp st(4),st +10001F04 dc e2 fsubr st(2),st +10001F06 dc c2 fadd st(2),st ; Write the low-pass sample back to the buffer. -100012EB d9 1a fstp DWORD PTR [edx] -100012ED 83 c2 04 add edx,0x4 -100012F0 4e dec esi -100012F1 75 9f jne 0x10001292 - ; Persist cutoff, band, high and low. -100012F3 dd 59 08 fstp QWORD PTR [ecx+0x8] -100012F6 8b 44 24 18 mov eax,DWORD PTR [esp+0x18] -100012FA 8b 54 24 1c mov edx,DWORD PTR [esp+0x1c] -100012FE dd 59 28 fstp QWORD PTR [ecx+0x28] -10001301 89 41 30 mov DWORD PTR [ecx+0x30],eax -10001304 89 51 34 mov DWORD PTR [ecx+0x34],edx -10001307 dd 59 20 fstp QWORD PTR [ecx+0x20] -1000130A 5e pop esi -1000130B 8b e5 mov esp,ebp -1000130D 5d pop ebp -1000130E c2 08 00 ret 0x8 +10001F08 d9 18 fstp DWORD PTR [eax] +10001F0A d9 cb fxch st(3) +10001F0C db f3 fcomi st,st(3) +10001F0E 0f 87 1c ff ff ff ja 0x10001e30 +10001F14 dd d8 fstp st(0) +10001F16 d9 c2 fld st(2) +10001F18 83 c0 04 add eax,0x4 +10001F1B 39 c3 cmp ebx,eax +10001F1D 0f 85 2d ff ff ff jne 0x10001e50 +10001F23 dd dc fstp st(4) +10001F25 dd da fstp st(2) +10001F27 d9 ca fxch st(2) +10001F29 eb 0b jmp 0x10001f36 +10001F2B 2e 8d 74 26 00 lea esi,cs:[esi+eiz*1+0x0] +10001F30 dd dc fstp st(4) +10001F32 dd da fstp st(2) +10001F34 d9 ca fxch st(2) + ; Persist cutoff and clear tiny integrator states. +10001F36 dd 59 08 fstp QWORD PTR [ecx+0x8] +10001F39 d9 c1 fld st(1) +10001F3B d9 e1 fabs +10001F3D dd 82 f8 ff ff ff fld QWORD PTR [edx-0x8] +10001F43 dd 54 24 30 fst QWORD PTR [esp+0x30] +10001F47 df f1 fcomip st,st(1) +10001F49 dd d8 fstp st(0) +10001F4B 76 0b jbe 0x10001f58 +10001F4D dd d9 fstp st(1) +10001F4F d9 ee fldz +10001F51 eb 07 jmp 0x10001f5a +10001F53 2e 8d 74 26 00 lea esi,cs:[esi+eiz*1+0x0] +10001F58 d9 c9 fxch st(1) +10001F5A dd 59 20 fstp QWORD PTR [ecx+0x20] +10001F5D d9 c0 fld st(0) +10001F5F d9 e1 fabs +10001F61 dd 82 f8 ff ff ff fld QWORD PTR [edx-0x8] +10001F67 dd 54 24 30 fst QWORD PTR [esp+0x30] +10001F6B df f1 fcomip st,st(1) +10001F6D dd d8 fstp st(0) +10001F6F 76 04 jbe 0x10001f75 +10001F71 dd d8 fstp st(0) +10001F73 d9 ee fldz +10001F75 dd 59 28 fstp QWORD PTR [ecx+0x28] +10001F78 8d 65 f8 lea esp,[ebp-0x8] +10001F7B 5b pop ebx +10001F7C 5e pop esi +10001F7D 5d pop ebp +10001F7E c2 08 00 ret 0x8 +10001F81 8d b4 26 00 00 00 00 lea esi,[esi+eiz*1+0x0] +10001F88 dd d8 fstp st(0) +10001F8A d9 82 d4 ff ff ff fld DWORD PTR [edx-0x2c] +10001F90 dd 1c 24 fstp QWORD PTR [esp] +10001F93 e9 6a fe ff ff jmp 0x10001e02 + ; Position-independent constant access. Return the call site in EDX. +10001F98 8b 14 24 mov edx,DWORD PTR [esp] +10001F9B c3 ret + +; Embedded constants and position-independent addressing table +10001F9C 00 00 00 3e float 0.125 +10001FA0 00 a0 8c 46 float 18000.0 +10001FA4 00 00 c8 41 float 25.0 +10001FA8 cd cc cc cc cc cc dc 3f double 0.45 +10001FB0 18 2d 44 54 fb 21 09 40 double 3.141592653589793 +10001FB8 cd 3b 7f 66 9e a0 f4 3f double 1.2892135623730951 +10001FC0 a7 8e a8 99 c2 57 f3 3a double 1e-24 +10001FC8 00 00 00 00 dword 0 +10001FCC 00 00 00 00 dword 0 +10001FD0 00 00 00 00 dword 0 ; Calls from Work() for the first active track and subsequent tracks 10001AAA e8 a1 f7 ff ff call 0x10001250 10001ACD e8 7e f7 ff ff call 0x10001250 Full reconstructed C++ diff 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 #include 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(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(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; }