123456789101112131415161718192021222324252627282930313233343536373839404142434445(* 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 Noise.mli *)typemode=Long|Shortletstep(mode:mode)(r:int):int=lettap=matchmodewithLong->1|Short->6inletfeedback=(rland1)lxor((rlsrtap)land1)in(rlsr1)lor(feedbacklsl14)letperiod(mode:mode):int=letrecgonr=ifr=1&&n>0thennelsego(n+1)(stepmoder)ingo01letrender?(mode=Long)~(rate:float)(seconds:float):Signal.t=letr=ref1andclock=ref0.inArray.init(Signal.samplesseconds)(fun_->letout=if!rland1=1then1.else-1.in(* rate / 44,100 steps per sample: a step each time the clock
* passes a whole number *)clock:=!clock+.(rate/.float_of_intSignal.rate);while!clock>=1.dor:=stepmode!r;clock:=!clock-.1.done;out)(* mod 2^32 by computing in 32 bits, which wrap by themselves, natively
* and in a browser alike (where OCaml's ints are 32 bits anyway); the
* state comes out signed, hence [uniform]'s correction *)letlcg(x:int):int=Int32.to_int(Int32.add(Int32.mul1664525l(Int32.of_intx))1013904223l)letuniform(x:int):float=letx=float_of_intxinletx=ifx<0.thenx+.4294967296.elsexin(2.*.x/.4294967296.)-.1.