12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061(* Claude Code
*
* Copyright (C) 2026 Yoann Padioleau
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public License
* (LGPL) as published by the Free Software Foundation; either version
* 2 of the License, or (at your option) any later version.
*)(* See Svf.mli *)typemodel=Chamberlin|Zero_delaytypemode=Low_pass|Band_pass|High_pass|Notchletmodes=[Low_pass;Band_pass;High_pass;Notch](* the two integrators' memories: for Chamberlin's, the low-pass and
* band-pass outputs; for the zero-delay one, the trapezoids' states *)typet={mutables1:float;mutables2:float}letcreate():t={s1=0.;s2=0.}letclamp(cutoff:float):float=Float.min20000.(Float.max10.cutoff)letpick(mode:mode)~low~band~high:float=matchmodewithLow_pass->low|Band_pass->band|High_pass->high|Notch->low+.high(* two integrators in a loop, each fed the last one's output of this
* sample or the last: low += f band, high = x - low - band / q, band +=
* f high *)letchamberlin(t:t)(mode:mode)~(cutoff:float)~(q:float)(x:float):float=letf=2.*.sin(Float.pi*.clampcutoff/.float_of_intSignal.rate)inletlow=t.s2+.(f*.t.s1)inlethigh=x-.low-.(t.s1/.q)inletband=t.s1+.(f*.high)int.s1<-band;t.s2<-low;pickmode~low~band~high(* the same loop solved: the high-pass first, from the two states, then
* the two trapezoidal integrators *)letzero_delay(t:t)(mode:mode)~(cutoff:float)~(q:float)(x:float):float=letg=tan(Float.pi*.clampcutoff/.float_of_intSignal.rate)inletr2=1./.qinlethigh=(x-.((r2+.g)*.t.s1)-.t.s2)/.(1.+.(r2*.g)+.(g*.g))inletv1=g*.highinletband=v1+.t.s1int.s1<-band+.v1;letv2=g*.bandinletlow=v2+.t.s2int.s2<-low+.v2;pickmode~low~band~highletprocess(t:t)(model:model)(mode:mode)~(cutoff:Signal.t)~(q:float)(samples:Signal.t):unit=fori=0toArray.lengthsamples-1doletcutoff=cutoff.(i)insamples.(i)<-(matchmodelwith|Chamberlin->chamberlintmode~cutoff~qsamples.(i)|Zero_delay->zero_delaytmode~cutoff~qsamples.(i))done