This repository has been archived by the owner on Apr 27, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.js
221 lines (171 loc) · 5.24 KB
/
index.js
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
var ActiveList = require('./lib/active_list')
var SlotReferences = require('./lib/slot_references')
var slotTypes = {
default: require('audio-slot'),
modulator: require('audio-slot/modulator')
}
var createMeddler = require('audio-meddle')
var applyProviders = require('./lib/apply_providers')
var applyEmitter = require('./lib/apply_emitter')
var IAC = require('inheritable-audio-context')
////////////////////////////////////////////////
module.exports = function(parentAudioContext){
var audioContext = IAC(parentAudioContext, true)
var soundbank = audioContext.createGain()
soundbank._context = audioContext
var output = audioContext.createGain()
var meddler = createMeddler(audioContext)
audioContext.inputs = {
'meddler': meddler
}
meddler.connect(output)
output.connect(soundbank)
// turn the AudioNode into an EventEmitter
applyEmitter(soundbank)
var activeGroups = ActiveList()
var slots = soundbank._slots = {}
var descriptors = {}
var resolvedDescriptors = {}
var slotReferences = SlotReferences()
soundbank.update = function(descriptor){
descriptors[descriptor.id] = descriptor
refreshSlot(descriptor.id)
slotReferences.lookup(descriptor.id).forEach(refreshSlot)
soundbank.emit('change', descriptors[descriptor.id])
}
soundbank.remove = function(id){
var descriptor = descriptors[id] = {id: id, _deleted: true}
refreshSlot(descriptor.id)
slotReferences.lookup(descriptor.id).forEach(refreshSlot)
;delete descriptors[descriptor.id]
soundbank.emit('change', descriptor)
}
soundbank.getDescriptor = function(id){
return descriptors[id] || {id: String(id)}
}
soundbank.getDescriptors = function(){
return Object.keys(descriptors).map(function(id){
return descriptors[id]
})
}
soundbank.triggerOn = function(id, at){
at = at || audioContext.currentTime
soundbank.choke(id, at)
var slot = slots[id]
if (slot){
if (slot.chokeGroup){
activeGroups.set(slot.chokeGroup, slot, at)
}
slot.triggerOn(at)
var descriptor = resolvedDescriptors[id]
if (descriptor.inputMode === 'meddler'){
meddler.start(descriptor.id, at)
}
}
}
soundbank.triggerOff = function(id, at){
at = at || audioContext.currentTime
var slot = slots[id]
if (slot){
slot.triggerOff(at)
meddler.stop(id, at)
}
}
soundbank.choke = function(id, at){
at = at || audioContext.currentTime
var slot = slots[id]
if (slot){
slot.choke(at)
}
// choke group
var groupSlot = slot && slot.chokeGroup && activeGroups.get(slot.chokeGroup, at)
if (groupSlot){
activeGroups.remove(slot.chokeGroup, at)
groupSlot.choke(at)
}
}
function refreshSlot(id){
var oldDescriptor = resolvedDescriptors[id] || {}
var descriptor = descriptors[id]
var slot = slots[id]
var ctor = slotTypes[descriptor.node] || slotTypes['default']
if (descriptor._deleted){
ctor = null
}
var context = IAC(audioContext)
context.params = descriptor
context.descriptors = descriptors
context.slotReferences = []
descriptor = applyProviders(context, descriptor)
slotReferences.update(id, context.slotReferences)
// update existing slot
if (slot){
if (!ctor || !(slot instanceof ctor)){
if (audioContext.inputs[id]){
audioContext.inputs[id].disconnect()
}
slot.update({})
slot.disconnect()
slot = slots[id] = null
meddler.remove(id)
} else {
if (oldDescriptor.output != descriptor.output || oldDescriptor.inputMode != descriptor.inputMode){
updateOutput(slot, descriptor)
}
slot.update(descriptor)
}
}
// create new slot
if (!slot && ctor) {
slot = slots[id] = ctor(audioContext, descriptor)
if (audioContext.inputs[id] && slot.input){
audioContext.inputs[id].connect(slot.input)
}
updateOutput(slot, descriptor)
meddler.add(id, slot)
}
// emit with providers
resolvedDescriptors[id] = descriptor
soundbank.emit('refresh', descriptor)
}
function getSlotDescriptor(id){
return descriptors[id]
}
function updateOutput(slot, descriptor){
slot.disconnect()
if (descriptor.inputMode === 'meddler'){
// do nothing, output will be patched on trigger
} else if (!('output' in descriptor) || descriptor.output === true || descriptor.output == ''){
slot.connect(output)
} else if (Array.isArray(descriptor.output)){
descriptor.output.forEach(function(output){
slot.connect(getInput(output))
})
} else if (typeof descriptor.output === 'string'){
slot.connect(getInput(descriptor.output))
}
}
function getInput(id){
if (!audioContext.inputs[id]){
audioContext.inputs[id] = audioContext.createGain()
var slot = slots[id]
if (slot && slot.input){
audioContext.inputs[id].connect(slot.input)
}
}
return audioContext.inputs[id]
}
return soundbank
}
function merge(){
var result = {}
for (var i=0;i<arguments.length;i++){
var obj = arguments[i]
if (obj){
Object.keys(obj).forEach(function(key){
result[key] = obj[key]
})
}
}
return result
}