@@ -25,18 +25,13 @@ export default class LFO {
2525 this . _frequency = 0
2626
2727 this . audioContext = props . audioContext
28-
29- this . init ( props . rate , props . shape , props . amount )
30- }
31-
32- init ( rate , shape , amount ) {
3328 this . lfo = this . audioContext . createOscillator ( )
3429 this . lfoGain = this . audioContext . createGain ( )
3530 this . lfo . start ( )
3631 this . lfo . connect ( this . lfoGain )
37- this . lfo . frequency . value = rate
38- this . lfoGain . gain . value = amount
39- this . updateShape ( shape )
32+ this . lfo . frequency . value = props . rate
33+ this . lfoGain . gain . value = props . amount
34+ this . updateShape ( props . shape )
4035 }
4136
4237 connect ( destination , multiplier ) {
@@ -77,6 +72,7 @@ export default class LFO {
7772
7873 updateAmount ( ) {
7974 this . lfoGain . gain . value = this . _value * this . multiplier
75+ console . log ( 'amount' , this . lfoGain . gain . value )
8076 }
8177
8278 set shape ( val ) {
@@ -101,24 +97,57 @@ export default class LFO {
10197 }
10298
10399 if ( shape === 'random' ) {
104- let waveData = [ 0 ]
105- for ( let i = 0 ; i < 48 ; i ++ ) {
106- if ( Math . random ( ) < 0.5 ) {
107- waveData . push ( 0 )
108- } else {
109- waveData . push ( 1 )
110- }
111- }
112- let real = new Float32Array ( waveData )
113- let imag = new Float32Array ( waveData . length )
114- waveData = this . audioContext . createPeriodicWave ( real , imag )
100+ let waveData = this . generateRandomShape ( )
115101 this . lfo . setPeriodicWave ( waveData )
116102 this . freqMultiplier = 0.0325
117103 this . updateRate ( this . _frequency )
104+ } else if ( shape === 'square' ) {
105+ let waveData = this . generateSmoothSquareShape ( )
106+ this . lfo . setPeriodicWave ( waveData )
107+ this . updateRate ( this . _frequency )
118108 } else {
119109 this . lfo . type = shape
120110 this . freqMultiplier = 1
121111 this . updateRate ( this . _frequency )
122112 }
123113 }
114+
115+ generateRandomShape ( ) {
116+ let waveData = [ 0 ]
117+ for ( let i = 0 ; i < 48 ; i ++ ) {
118+ if ( Math . random ( ) < 0.5 ) {
119+ waveData . push ( 0 )
120+ } else {
121+ waveData . push ( 1 )
122+ }
123+ }
124+ let real = new Float32Array ( waveData )
125+ let imag = new Float32Array ( waveData . length )
126+ waveData = this . audioContext . createPeriodicWave ( real , imag )
127+ return waveData
128+ }
129+
130+ generateSmoothSquareShape ( ) {
131+ let waveData = [ 0 , 1 , .8 , .5 ]
132+ // for (let i = 0; i < 20; i++) {
133+ // let val = (Math.sin(i) * 2)
134+ // if (val > 1) val = 1
135+ // if (val < 0) val = 0
136+ // waveData[i] = val
137+ // }
138+ console . log ( 'smooth sq' , waveData )
139+ let real = new Float32Array ( waveData )
140+ let imag = new Float32Array ( waveData . length )
141+ waveData = this . audioContext . createPeriodicWave ( real , imag )
142+ return waveData
143+ }
124144}
145+
146+ // might be better than default triangle ? :
147+ // let waveData = [0, 1]
148+
149+ // console.log('smooth sq', waveData)
150+ // let real = new Float32Array(waveData)
151+ // let imag = new Float32Array(waveData.length)
152+ // waveData = this.audioContext.createPeriodicWave(real, imag)
153+ // return waveData
0 commit comments