@@ -10,40 +10,49 @@ export default class Oscillator {
1010 this . _detune = props . detune
1111 this . _octave = props . octave
1212 this . _note = props . _note
13- this . _lfoPitch = null
14- this . _persistLFO = false
15- this . _lfoAmount = null
1613 this . _glide = Number ( props . glide ) / 100
1714
15+ // LFOs attached to this oscillator.
16+ this . pitchLFOs = [ ]
17+ this . amountLFOs = [ ]
18+
1819 this . output = props . output
1920 this . audioContext = props . audioContext
2021
2122 this . gainNode = props . audioContext . createGain ( )
2223 this . gainNode . connect ( this . output )
2324 }
2425
25- // Will connect immediately, but later on if the waveform is loaded
26- // it will connect again below.
27- // Only one LFO can be set on page load, however multiple LFOs can be
28- // set after page load. TODO : allow multiple on page load.
29- // 'persist' parameter fixes an issue in setting LFO from filter to volume -
30- // Very loud pops happen when a note is on and setting lfo to volume.
3126 connectPitchToLFO ( lfo , persist = false ) {
32- this . _lfoPitch = lfo
33- this . _persistLFO = persist
3427 if ( this . wavSource ) {
35- this . _lfoPitch . connect ( this . wavSource . detune , 10 , persist )
28+ // Connect now if wave is loaded.
29+ lfo . connect ( this . wavSource . detune , 10 , persist )
3630 }
31+ this . pitchLFOs . push ( lfo )
32+ }
33+
34+ disconnectPitchFromLFO ( lfo ) {
35+ lfo . disconnect ( )
36+ this . pitchLFOs = this . pitchLFOs . filter ( ( _lfo ) => {
37+ return _lfo . id !== lfo . id
38+ } )
3739 }
3840
39- connectAmountToLFO ( lfo , persist = false ) {
40- this . _lfoAmount = lfo
41- this . _persistLFO = persist
41+ connectAmountToLFO ( lfo ) {
42+ this . amountLFOs . push ( lfo )
4243 if ( this . wavSource ) {
43- this . _lfoAmount . connect ( this . gainNode . gain , 0.01 , persist )
44+ // Connect now if wave is loaded.
45+ lfo . connect ( this . gainNode . gain , 0.01 )
4446 }
4547 }
4648
49+ disconnectAmountFromLFO ( lfo ) {
50+ lfo . disconnect ( )
51+ this . amountLFOs = this . amountLFOs . filter ( ( _lfo ) => {
52+ return _lfo . id !== lfo . id
53+ } )
54+ }
55+
4756 // Limits gain between zero and 1.
4857 set amount ( val ) {
4958 this . gainNode . gain . value = limit ( 0 , 1 , val / 100 )
@@ -128,12 +137,14 @@ export default class Oscillator {
128137 this . wavSource . _started = true
129138 this . wavSource . connect ( this . gainNode )
130139
131- if ( this . _lfoPitch ) {
132- this . _lfoPitch . connect ( this . wavSource . detune , 10 , this . _persistLFO )
133- }
134- if ( this . _lfoAmount ) {
135- this . _lfoAmount . connect ( this . gainNode . gain , 0.01 )
136- }
140+ // Reconnect lfos to the new wavSource instance (if any were connected).
141+ this . pitchLFOs . forEach ( ( lfo ) => {
142+ lfo . connect ( this . wavSource . detune , 10 )
143+ } )
144+ this . amountLFOs . forEach ( ( lfo ) => {
145+ lfo . connect ( this . gainNode . gain , 0.01 , 10 )
146+ } )
147+
137148 this . updatePitch ( )
138149 }
139150
0 commit comments