Skip to content

Commit af331df

Browse files
committed
Add custom LFO shapes.
1 parent 9a09ffa commit af331df

File tree

2 files changed

+54
-30
lines changed

2 files changed

+54
-30
lines changed

src/audio/LFO.js

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ let multiplier = 0.001
99
lfo.connect(node.audioParam, multiplier)
1010
*/
1111

12+
import Coefficients from '../data/Coefficients.js'
13+
1214
export default class LFO {
1315
constructor (props) {
1416
this.name = props.name
@@ -62,7 +64,7 @@ export default class LFO {
6264
}
6365

6466
updateRate (val) {
65-
this.lfo.frequency.value = (val / 10) * this.freqMultiplier
67+
this.lfo.frequency.value = (val * val / 500) * this.freqMultiplier * 3
6668
}
6769

6870
set amount (val) {
@@ -72,7 +74,6 @@ export default class LFO {
7274

7375
updateAmount () {
7476
this.lfoGain.gain.value = this._value * this.multiplier
75-
console.log('amount', this.lfoGain.gain.value)
7677
}
7778

7879
set shape (val) {
@@ -99,47 +100,42 @@ export default class LFO {
99100
if (shape === 'random') {
100101
let waveData = this.generateRandomShape()
101102
this.lfo.setPeriodicWave(waveData)
102-
this.freqMultiplier = 0.0325
103+
this.freqMultiplier = 0.01
103104
this.updateRate(this._frequency)
104105
} else if (shape === 'square') {
105-
let waveData = this.generateSmoothSquareShape()
106+
let waveData = this.generateSquareShape()
106107
this.lfo.setPeriodicWave(waveData)
108+
this.freqMultiplier = 1
109+
this.updateRate(this._frequency)
110+
} else if (shape === 'sawtooth') {
111+
let waveData = this.generateSawtoothShape()
112+
this.lfo.setPeriodicWave(waveData)
113+
this.freqMultiplier = 1
107114
this.updateRate(this._frequency)
108115
} else {
116+
// We use the default triangle shape only right now.
109117
this.lfo.type = shape
110118
this.freqMultiplier = 1
111119
this.updateRate(this._frequency)
112120
}
113121
}
114122

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
123+
generateSquareShape () {
124+
let real = new Float32Array(Coefficients.square.real)
125+
let imag = new Float32Array(Coefficients.square.imag)
126+
return this.audioContext.createPeriodicWave(real, imag)
128127
}
129128

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
129+
generateSawtoothShape () {
130+
let real = new Float32Array(Coefficients.sawtooth.real)
131+
let imag = new Float32Array(Coefficients.sawtooth.imag)
132+
return this.audioContext.createPeriodicWave(real, imag)
133+
}
134+
135+
generateRandomShape () {
136+
let real = new Float32Array(Coefficients.noise.real)
137+
let imag = new Float32Array(Coefficients.noise.imag)
138+
return this.audioContext.createPeriodicWave(real, imag)
143139
}
144140
}
145141

src/data/Coefficients.js

Lines changed: 28 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)