123456789101112131415161718192021222324252627282930313233343536373839404142(* 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 Signal.mli *)typet=floatarraytypestereo={left:t;right:t}(* the same array twice: nothing here writes into a sound once made *)letboth(s:t):stereo={left=s;right=s}letmono(s:stereo):t=Array.map2(funlr->(l+.r)/.2.)s.lefts.rightletrate=44100letnyquist=float_of_intrate/.2.letsamples(seconds:float):int=int_of_float(seconds*.float_of_intrate)letperiod_in_samples(frequency:float):float=float_of_intrate/.frequencyletalias(frequency:float):float=(* the spectrum repeats every [rate], and mirrors around each
* multiple of it *)letf=Float.rem(Float.absfrequency)(float_of_intrate)iniff<=nyquistthenfelsefloat_of_intrate-.fletof_function(seconds:float)(f:float->float):t=Array.init(samplesseconds)(funi->f(float_of_inti/.float_of_intrate))(* claude: compared as ints; it was
*
* max (-32768) (min 32767 (int_of_float (Float.round (x *. 32767.))))
*
* Stdlib's min and max are polymorphic, the runtime's generic compare
* twice per sample written (notes_opti_ocaml.md) *)letto_int16(x:float):int=letv=int_of_float(Float.round(x*.32767.))inifv<-32768then-32768elseifv>32767then32767elsev