import type { Fn } from "@thi.ng/api" ; import { gibbs } from "./anti-alias.js" ; import type { StatelessOscillator } from "./api.js" ; import { sin } from "./osc-sin.js" ; /**
 * Higher order function to produce an additive version of given
 *  StatelessOscillator . Returns new oscillator function.
 *
 *  remarks param osc param freqFn param ampFn param n export const additive = ( osc , freqFn , ampFn , n ) : StatelessOscillator => { const fcache = [ ] ; const acache = [ ] ; for ( let i = 1 ; i <= n ; i ++ ) { fcache . push ( freqFn ( i ) ) ; acache . push ( ampFn ( i ) ) ; } return ( phase , freq , amp = 1 , dc = 0 ) => { let y = 0 ; for ( let i = 0 ; i < n ; i ++ ) { y += osc ( phase , freq * fcache [ i ] , acache [ i ] ) ; } return dc + amp * y ; } ; } ; /**
 * Interactive graph of this oscillator:
 *  https param n export const squareAdditive = ( n = 8 ) => additive ( sin , ( i ) => 2 * ( i - 1 ) + 1 , ( i ) => ( 1 / ( 2 * ( i - 1 ) + 1 ) ) * gibbs ( n , i ) , n ) ; /**
 * Interactive graph of this oscillator:
 *  https param n export const sawAdditive = ( n = 8 ) => additive ( sin , ( i ) => i , ( i ) => ( 1 / i ) * gibbs ( n , i ) , n ) ;