-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroom.html
More file actions
369 lines (314 loc) · 11.5 KB
/
room.html
File metadata and controls
369 lines (314 loc) · 11.5 KB
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="This is a mobile machine drum to play with friends in the same room. Admin can control intruments in the peers mobile devices.">
<meta name="keywords" content="mobile machine drum, mobile, machine, drum, machine drum, play, friend, friends">
<meta name="author" content="Free Geeks a.k.a. RRR">
<title>MobileDrums</title>
<link rel="stylesheet" href="css/reset.css" type="text/css" media="screen">
<link rel="stylesheet" href="css/styles.css" type="text/css" media="screen">
<link href='http://fonts.googleapis.com/css?family=Dosis' rel='stylesheet' type='text/css'>
<meta name="viewport" content="width=320; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;"/>
</head>
<body>
<header>
<h1>Mobile Machine Drum</h1>
<p>invite your drums:</p>
<em>url: <span>...</span></em>
<audio src="/snd/coin.wav" controls="controls"></audio>
</header>
<ul id="instruments">
<li class="instrument active a" data-color="#7c0900" data-name="hit-hat" data-sound="HH_2_closed_a"><span></span></li>
<li class="instrument active b" data-color="#c95624" data-name="kick" data-sound="Kick_Reg_1a"><span></span></li>
<li class="instrument active c" data-color="#ddaa1d" data-name="snare" data-sound="Snare_Reg_1a"><span></span></li>
<li class="instrument active d" data-color="#2d6600" data-name="tom" data-sound="Tom_Hi"><span></span></li>
<li class="instrument active e" data-color="#305ebd" data-name="ride" data-sound="Ride_Side"><span></span></li>
<li class="instrument active f" data-color="#730096" data-name="stick" data-sound="Stick"><span></span></li>
</ul>
<ul class="patterns">
<li class="pattern" data-pattern="pattern1"><a href="#">Drum'n'Bass</a></li>
<li class="pattern" data-pattern="pattern2"><a href="#">breakbeat</a></li>
<li class="pattern" data-pattern="pattern3"><a href="#">jungle</a></li>
</ul>
<script src="/socket.io/socket.io.js"></script>
<script>
/*
* Add JS here at the bottom of the page
* to avoid the whole document.ready since
* we are not using jquery
*/
// Single DOM element selection
window.$ = function(selector) {
return document.querySelector(selector);
};
// Multiple DOM element selection
window.$$ = function(selector) {
var items = {};
var results = Array.prototype.slice.call(document.querySelectorAll(selector));
var length = results.length;
for (var i = 0 ; i < length; i++) {
items[i] = results[i];
}
items.length = length;
items.splice = [].splice();
items.each = function(callback) {
for (var i = 0 ; i < length; i++) {
callback.call(items[i]);
}
};
items.on = function (event, fn) {
[]['forEach'].call(this, function (el) {
el.on(event, fn);
});
return this;
};
return items;
};
// Bind event using "on"
Element.prototype.on = Element.prototype.addEventListener;
Element.prototype.trigger = function (type, data) {
var event = document.createEvent('HTMLEvents');
event.initEvent(type, true, true);
event.data = data || {};
event.eventName = type;
event.target = this;
this.dispatchEvent(event);
return this;
};
var MachineDrums = function() {
var self = this;
// Config options
self.server = {
hostname: 'mobiledrums.freegeeks.nl',
port : 80
};
// self.server.hostname = 'mobiledrums.freegeeks.local';
// self.server.port = 3000;
self.animationTime = 150; // milliseconds
// Current sound
self.sound = null;
// Get each sound from the drums
self.sounds = {};
$$('.instrument').each(function() {
self.sounds[this.dataset.sound] = this;
// Paint the instruments with its own color
// and adds the text inside it
this.innerHTML = '<a href="javascript:;" style="background-color: ' + this.dataset.color + ';">' + this.dataset.name + '</a>';
});
// Define room name
self.room = location.pathname;
// Update placeholder
var port = (self.server.port !== 80) ? ':' + self.server.port : '';
$('em span').innerHTML = 'http://' + self.server.hostname + port + self.room;
// Handle the socket communication
var socketHandler = function() {
// Connect
self.socket = io.connect('http://' + self.server.hostname, self.server.port);
// Join the room
self.socket.on('connect', function() {
self.socket.emit('join', self.room);
});
self.socket.on('role', function(role) {
$('body').classList.remove('adm');
$('body').classList.remove('cli');
$('body').classList.add(role);
if (role == 'adm') {
self.roleAsAdmin();
} else {
self.roleAsInstrument();
}
});
// Listen to the HIT
self.socket.on('hit', function(sound) {
// Only play audio if selected sound
self.hit(sound, (self.sound === sound));
});
};
socketHandler();
return this;
};
MachineDrums.prototype.roleAsAdmin = function() {
var self = this;
// Set as admin and enable controls
$('p').innerHTML = 'invite your drums:';
// Listen to the STATS update
self.socket.on('instrument', function(data) {
// TODO this was copied from the painted can be reused
$$('.instrument').each(function () {
this.classList.add('active');
if (!data[this.dataset.sound]) {
// show button as disabled
this.classList.remove('active');
// created index in the object with ZERO
data[this.dataset.sound] = 0;
}
// update the contents of the button
this.innerHTML = '<a style="background-color: ' + this.dataset.color + ';">' + this.dataset.name + ' <b>' + data[this.dataset.sound] + '</b></a>';
});
});
// Once select the instrument
$$('.instrument').on('click', function () {
// Send the hit to server
self.socket.emit('hit', { room: self.room, sound: this.dataset.sound });
});
// Show Patterns
$('.patterns').style.display = '';
// On each pattern
$$('.pattern').on('click', function () {
this.classList.toggle('active');
// Always stop the pattern
self.socket.emit('stop');
if (!this.classList.contains('active')) {
return;
}
var patterns = {
'pattern1': {
tempo: 180,
pattern: [
[ 'Kick_Reg_1a', 'HH_2_closed_a' ],
null,
[ 'HH_2_closed_a' ],
null,
[ 'Snare_Reg_1a', 'HH_2_closed_a' ],
null,
[ 'HH_2_closed_a' ],
null,
[ 'HH_2_closed_a' ],
null,
[ 'Kick_Reg_1a' ],
null,
[ 'Snare_Reg_1a'],
[ 'Kick_Reg_1a' ],
null,
null
]
},
'pattern2': {
tempo: 150,
pattern: [
[ 'Tom_Hi', 'Ride_Side' ],
null,
[ 'Stick' ],
null,
[ 'Ride_Side', 'Stick' ],
[ 'Tom_Hi' ],
[ 'Kick_Reg_1a', 'Tom_Hi' ],
null,
[ 'Kick_Reg_1a' ],
null,
[ 'Snare_Reg_1a' ],
null,
[ 'HH_2_closed_a', 'Ride_Side' ],
[ 'Snare_Reg_1a' ],
null,
null
]
},
'pattern3': {
tempo: 180,
pattern: [
[ 'Kick_Reg_1a', 'HH_2_closed_a' ],
null,
[ 'HH_2_closed_a' ],
null,
[ 'Snare_Reg_1a', 'HH_2_closed_a' ],
null,
[ 'HH_2_closed_a' ],
null,
[ 'HH_2_closed_a' ],
null,
[ 'Kick_Reg_1a' ],
null,
[ 'Snare_Reg_1a'],
[ 'Kick_Reg_1a' ],
null,
null
]
}
};
var pattern = patterns[this.dataset.pattern];
self.socket.emit('start', { room: self.room, tempo: pattern.tempo, pattern: pattern.pattern });
});
function keyboard (e) {
var map = {
49: 'hit-hat',
50: 'kick',
51: 'snare',
52: 'tom',
53: 'ride',
54: 'stick'
};
if (map[e.keyCode]) {
$('.instrument[data-name="' + map[e.keyCode] + '"]').trigger('click');
}
}
window.addEventListener('keydown', keyboard, false);
};
MachineDrums.prototype.roleAsInstrument = function() {
var self = this;
// Set as instrument and disable controls
$('p').innerHTML = 'select your beat and share:';
// Once select the instrument
$$('.instrument a').each(function() {
var handleClick = function(event) {
event.preventDefault();
var node = this.parentNode;
var color = node.dataset.color;
var sound = node.dataset.sound;
// activate only one instrument
$$('.instrument').each(function () {
this.classList.remove('active');
});
this.parentNode.classList.add('active');
// Mark the selected sound
self.sound = sound;
// Load selected audio
self.audio = $('audio');
self.audio.controls = 'controls';
self.audio.src = 'http://' + self.server.hostname + ':' + self.server.port + '/snd/' + sound + '.wav';
self.audio.load();
self.audio.play();
self.audio.pause();
// Once audio finished playing
self.audio.on('ended', function () {
// Do something?
});
// Inform the server the instrument
self.socket.emit('instrument', { room: self.room, sound: sound });
};
this.on('click', handleClick);
this.on('touchstart', handleClick);
});
// Hide Patterns
$('.patterns').style.display = 'none';
};
MachineDrums.prototype.hit = function(sound, makeSound) {
var self = this;
// Get instrument element
var el = self.sounds[sound];
if (makeSound) {
self.audio.currentTime = 0;
self.audio.play();
}
el.classList.add('hit');
window.setTimeout(function() {
el.classList.remove('hit');
}, self.animationTime);
};
var instance = new MachineDrums();
</script>
<script>
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-41306693-1']);
_gaq.push(['_trackPageview']);
_gaq.push(['_setDomainName', '.mobiledrums.freegeeks.nl']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
</body>
</html>