-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLFO.ts
151 lines (145 loc) · 3.17 KB
/
LFO.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import { getContext, initValue, Super, BOOLEAN, FLOAT } from './common'
const LFO = function (properties) {
if (!properties) {
properties = this.getDefaults()
}
const userContext = (this.userContext = getContext(properties.context))
//Instantiate AudioNode
this.input = userContext.createGain()
this.output = userContext.createScriptProcessor(256, 1, 1)
this.activateNode = userContext.destination
//Set Properties
this.frequency = initValue(
properties.frequency,
this.defaults.frequency.value
)
this.offset = initValue(properties.offset, this.defaults.offset.value)
this.oscillation = initValue(
properties.oscillation,
this.defaults.oscillation.value
)
this.phase = initValue(properties.phase, this.defaults.phase.value)
this.target = properties.target || {}
this.output.onaudioprocess = this.callback(
properties.callback || function () {}
)
this.bypass = properties.bypass || this.defaults.bypass.value
}
LFO.prototype = Object.create(Super, {
name: {
value: 'LFO',
},
bufferSize: {
value: 256,
},
sampleRate: {
value: 44100,
},
defaults: {
value: {
frequency: {
value: 1,
min: 0,
max: 20,
automatable: false,
type: FLOAT,
},
offset: {
value: 0.85,
min: 0,
max: 22049,
automatable: false,
type: FLOAT,
},
oscillation: {
value: 0.3,
min: -22050,
max: 22050,
automatable: false,
type: FLOAT,
},
phase: {
value: 0,
min: 0,
max: 2 * Math.PI,
automatable: false,
type: FLOAT,
},
bypass: {
value: false,
automatable: false,
type: BOOLEAN,
},
},
},
frequency: {
get: function () {
return this._frequency
},
set: function (value) {
this._frequency = value
this._phaseInc =
(2 * Math.PI * this._frequency * this.bufferSize) / this.sampleRate
},
},
offset: {
get: function () {
return this._offset
},
set: function (value) {
this._offset = value
},
},
oscillation: {
get: function () {
return this._oscillation
},
set: function (value) {
this._oscillation = value
},
},
phase: {
get: function () {
return this._phase
},
set: function (value) {
this._phase = value
},
},
target: {
get: function () {
return this._target
},
set: function (value) {
this._target = value
},
},
activate: {
value: function (doActivate) {
if (doActivate) {
this.output.connect(this.userContext.destination)
if (this.activateCallback) {
this.activateCallback(doActivate)
}
} else {
this.output.disconnect()
}
},
},
callback: {
value: function (callback) {
var that = this
return function () {
that._phase += that._phaseInc
if (that._phase > 2 * Math.PI) {
that._phase = 0
}
callback(
that._target,
that._offset + that._oscillation * Math.sin(that._phase)
)
}
},
},
})
export default LFO