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 2
/
index.js
304 lines (255 loc) · 7.65 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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
var View = require('rincewind')
var become = require('become')
var jsonQuery = require('json-query')
var EventEmitter = require('events').EventEmitter
var behave = require('./behaviors')
var createInstance = require('./lib/create_instance')
var connectLocalInstance = require('./lib/connect_local_instance')
var connect = require('./lib/connect')
var Syncer = require('./lib/syncer')
var loadSample = require('./lib/load-sample')
var postFile = require('./lib/post-file')
var IAC = require('inheritable-audio-context')
/// the main view
var render = View(__dirname + '/remotelist.html')
module.exports = function(parentAudioContext, element){
var audioContext = IAC(parentAudioContext, true)
var self = new EventEmitter()
self.output = audioContext.createGain()
var releaseLocalInstance = null
audioContext.loadSample = loadSample
function broadcastLocalInstance(to){
context.connection.write({
nickname: context.data.nickname,
to: to
})
if (context.localInstance && context.connection){
context.localInstance.getDescriptors().forEach(function(descriptor){
context.connection.write({
updateSlot: descriptor,
to: to
})
})
context.localInstance.loop.getDescriptors().forEach(function(descriptor){
context.connection.write({
updateLoop: descriptor,
to: to
})
})
}
}
function remoteDisconnect(id){
var shouldRefresh = false
var remote = context.remoteLookup[id]
if (remote){
context.remoteLookup[id] = null
if (remote.instance){
remote.instance.disconnect()
remote.instance.destroy()
}
var index = context.data.remotes.indexOf(remote)
if (~index){
context.data.remotes.splice(index, 1)
shouldRefresh = true
}
}
return shouldRefresh
}
function handleSampleRequest(src){
if (audioContext.getSampleBlob){
var url = context.data.sampleRoot + context.data.clientId + '/' + src
audioContext.getSampleBlob(src, function(err, blob){
if (blob){
postFile(url, blob)
console.log('UPLOADING SAMPLE', src)
}
})
}
}
function updateRemote(id, message){
// add new clients
var shouldRefresh = false
var shouldBroadcastLocal = false
var remote = context.remoteLookup[message.from]
if (!remote){
remote = context.remoteLookup[message.from] = {id: message.from, isPlayer: false}
context.data.remotes.push(remote)
shouldBroadcastLocal = true
}
// update nickname
if (message.nickname && message.nickname !== remote.nickname){
remote.nickname = message.nickname
shouldRefresh = true
}
// update playback
if (message.updateSlot || message.updateLoop){
if (!remote.instance){
var sampleRoot = context.data.sampleRoot + remote.id + '/'
var scopedAudioContext = getScopedContext(audioContext, sampleRoot)
remote.instance = createInstance(scopedAudioContext, context.data.syncOffset)
remote.instance.connect(self.output)
remote.isPlayer = true
shouldRefresh = true
}
if (message.updateSlot){
remote.instance.update(message.updateSlot)
}
if (message.updateLoop){
remote.instance.loop.update(message.updateLoop)
}
}
if (shouldBroadcastLocal){
// this is a new connection, send our local instance to them
broadcastLocalInstance(message.from)
}
return shouldRefresh
}
self.setLocalInstance = function(instance){
context.localInstance = instance
if (context.connection){
if (releaseLocalInstance){
releaseLocalInstance()
}
releaseLocalInstance = connectLocalInstance(context.connection, context.localInstance)
broadcastLocalInstance()
}
}
self.setNickname = function(nickname){
context.data.nickname = nickname
if (context.connection){
context.connection.write({
nickname: context.data.nickname
})
}
self.emit('nickname', nickname)
refresh()
}
self.connect = function(server, nickname){
self.emit('connnecting', server)
context.data.state = 'connecting'
if (context.connection){
self.disconnect()
}
var server = server.replace(/^.+\:\/\//, '')
context.connection = connect('ws://' + server)
context.connection.on('error', function(err){
self.disconnect()
})
context.data.server = server
context.data.sampleRoot = 'http://' + server + '/files/'
// send our local messages to server
if (nickname){
context.data.nickname = nickname
}
// sync with other users
context.syncer = Syncer(audioContext.scheduler, context.connection)
context.syncer.sync()
// resync after the server noise has died down
setTimeout(function(){
context.syncer&&context.syncer.sync()
}, 3000)
if (context.localInstance){
releaseLocalInstance = connectLocalInstance(context.connection, context.localInstance)
}
broadcastLocalInstance()
refresh()
context.connection.on('data', function(message){
var shouldRefresh = false
if (message.from == 'server'){
if (message.clientId){
context.data.clientId = message.clientId
if (!context.data.nickname){
context.data.nickname = 'remote' + message.clientId
}
self.emit('connected', server, message.clientId)
context.data.state = 'connected'
shouldRefresh = true
}
if (message.clientDisconnect){
if (remoteDisconnect(message.clientDisconnect)){
shouldRefresh = true
}
}
if (message.requestFile){
handleSampleRequest(message.requestFile)
}
} else if (typeof message.from == 'number') { // message from remote
if (updateRemote(message.from, message)){
shouldRefresh = true
}
}
if (shouldRefresh){
refresh()
}
})
}
self.disconnect = function(){
if (context.connection){
context.connection.close()
context.data.remotes.forEach(function(remote){
if (remote.instance){
remote.instance.destroy()
}
})
if (releaseLocalInstance){
releaseLocalInstance()
releaseLocalInstance = null
}
context.data.remotes = []
context.remoteLookup = {}
context.connection = null
context.data.clientId = null
context.syncer = null
context.data.state = 'disconnected'
self.emit('disconnect')
refresh()
return true
} else {
return false
}
}
var updateBehaviors = behave(element)
var context = {
audioContext: audioContext,
connection: null,
get: getValue,
syncer: null,
refresh: refresh,
self: self,
remoteLookup: {},
globals: require('./providers'),
data: {
state: 'disconnected',
syncOffset: 0,
clientId: null,
remotes: []
},
source: null
}
element.context = context
function getValue(query, source){
if (source != null){
var context = Object.create(this)
context.parentContext = this
context.source = source
return jsonQuery(query, context).value
} else {
return jsonQuery(query, this).value
}
}
function refresh(){
var newContent = render(context)
become(element, newContent, {inner: true, onChange: updateBehaviors, tolerance: 0})
}
refresh()
return self
}
function obtain(obj){
return JSON.parse(JSON.stringify(obj))
}
function getScopedContext(parent, rootUrl){
var context = Object.create(parent)
context.sampleCache = {}
context.sampleRoot = rootUrl
return context
}