import type { IReset , Tuple } from "@thi.ng/api" ; import type { IRandom } from "@thi.ng/random" ; import { SYSTEM } from "@thi.ng/random/system" ; import { AGen } from "./agen.js" ; type PNoiseCoeffs = Tuple < number , 5 > ; const AMP = < PNoiseCoeffs > [ 3.8024 , 2.9694 , 2.597 , 3.087 , 3.4006 ] ; const PROB = < PNoiseCoeffs > [ 0.00198 , 0.0128 , 0.049 , 0.17 , 0.682 ] ; remarks param gain param rnd param amp param prob export const pinkNoise = ( gain ? , rnd ? , amp ? , prob ? ) => new PinkNoise ( gain , rnd , amp , prob ) ; export class PinkNoise extends AGen < number > implements IReset { protected _bins : number [ ] ; protected _psum : number [ ] ; constructor ( protected _gain = 1 , protected _rnd = SYSTEM , protected _amp = AMP , prob = PROB ) { super ( 0 ) ; this . _gain /= _amp . reduce ( ( acc , x ) => acc + x , 0 ) ; this . _psum = prob . reduce ( ( acc , x , i ) => ( acc . push ( i > 0 ? acc [ i - 1 ] + x : x ) , acc ) , [ ] ) ; this . _bins = [ 0 , 0 , 0 , 0 , 0 ] ; } reset ( ) { this . _bins . fill ( 0 ) ; return this ; } next ( ) { const { _bins , _rnd , _amp , _psum } = this ; const bin = _rnd . float ( ) ; for ( let i = 0 ; i < 5 ; i ++ ) { if ( bin <= _psum [ i ] ) { _bins [ i ] = _rnd . norm ( _amp [ i ] ) ; break ; } } return ( this . _val = this . _gain * ( _bins [ 0 ] + _bins [ 1 ] + _bins [ 2 ] + _bins [ 3 ] + _bins [ 4 ] ) ) ; } }