Skip to content

Commit 7cffb3b

Browse files
committed
Fixed LFO mappings.
1 parent 88f7191 commit 7cffb3b

File tree

3 files changed

+55
-39
lines changed

3 files changed

+55
-39
lines changed

src/audio/LFO.js

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@ for affecting gain values, mulitiplier should be quite small, around .001.
77
let lfo = new LFO(options)
88
let multiplier = 0.001
99
lfo.connect(node.audioParam, multiplier)
10-
11-
When the LFO is connected to the filter, clicking can be heard if the LFO
12-
gain is greater than the current filter frequency. I don't know how to
13-
adjust the LFO gain automatically in accordance with the filter.
1410
*/
1511

1612
export default class LFO {
@@ -43,12 +39,9 @@ export default class LFO {
4339
this.updateShape(shape)
4440
}
4541

46-
connect (destination, multiplier, persist = false) {
42+
connect (destination, multiplier) {
4743
this.lfoGain.gain.value = this._value * multiplier
4844
this.multiplier = multiplier
49-
if (!persist) {
50-
this.init(this.lfo.frequency.value, this._shape, this.lfoGain.gain.value)
51-
}
5245
this.lfoGain.connect(destination)
5346
}
5447

src/audio/Oscillator.js

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -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

src/audio/Synth.js

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -160,17 +160,28 @@ class Synth extends React.Component {
160160
// Route LFOs to targets.
161161
routeLFO (lfo, destination) {
162162
lfo.disconnect()
163+
// Disconnect the LFO from any oscillators ( if it was set to any ).
164+
this.oscillators.map((osc) => {
165+
osc.disconnectPitchFromLFO(lfo)
166+
osc.disconnectAmountFromLFO(lfo)
167+
})
168+
163169
let id = destination.moduleId
164170

171+
// LFO to Amp.
165172
if (id === 'amp') {
166173
lfo.connect(this.oscillatorsBus.gain, 0.04)
167174
}
175+
176+
// LFO to Filter.
168177
if (id === 'filter') {
169178
// Have to do this for each filter instance.
170179
this.biquadFilter.filters.forEach((filter) => {
171-
lfo.connect(filter.detune, 50, true)
180+
lfo.connect(filter.detune, 50)
172181
})
173182
}
183+
184+
// LFO to oscillator detune and amount.
174185
if (id === 'oscAll') {
175186
this.oscillators.map((osc) => {
176187
osc.connectPitchToLFO(lfo, true)
@@ -185,18 +196,19 @@ class Synth extends React.Component {
185196
}
186197
}
187198

188-
if (id === 'chorus') {
189-
if (destination.property === 'time') {
190-
lfo.connect(this.chorus.lfoInputTime, 0.001)
191-
} else if (destination.property === 'amount') {
192-
lfo.connect(this.chorus.lfoInputAmount, 0.01)
199+
// LFO to Effects.
200+
if (id === 'effects') {
201+
if (destination.property === 'chorusTime') {
202+
lfo.connect(this.chorus.lfoInputTime, 0.01)
203+
} else if (destination.property === 'chorusAmount') {
204+
lfo.connect(this.chorus.lfoInputAmount, 0.05)
193205
}
194206
}
195207

196-
// LFO to LFO mappings. Sort of works.
208+
// LFO to LFO.
197209
if (id && id[0] === 'l') {
198-
let targetLFO = this.LFOs.find((osc) => osc.id === id)
199-
lfo.connect(targetLFO.lfoInputFrequency, 1, true)
210+
let targetLFO = this.LFOs.find((l) => l.id === id)
211+
lfo.connect(targetLFO.lfoInputFrequency, 1)
200212
}
201213
}
202214

0 commit comments

Comments
 (0)