-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
executable file
·441 lines (434 loc) · 17.6 KB
/
index.html
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title></title>
<link rel="stylesheet" type="text/css" href="css/mpc.css"/>
<link rel='stylesheet' type="text/css" href="http://www.steamdev.com/snippet/css/jquery.snippet.css"/>
<script type="text/javascript" src="http://code.createjs.com/soundjs-0.3.0.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.23/jquery-ui.min.js"></script>
<script src="http://www.steamdev.com/snippet/js/jquery.snippet.js"></script>
<script type="text/javascript" src="js/libs/preloadjs-0.2.0.min.js"></script>
<script type="text/javascript" src="js/libs/SoundJS.js"></script>
<script type="text/javascript" src="js/libs/HTMLAudioPlugin.js"></script>
<script type="text/javascript" src="js/libs/swfobject.js"></script>
<script type="text/javascript" src="js/libs/FlashPlugin.js"></script>
<script src="js/libs/class.js" type="text/javascript"></script>
<script src="js/libs/waveform-chrome.js" type="text/javascript"></script>
<script type="text/javascript" src="js/mpc.js"></script>
</head>
<body>
<div id="wrapper">
<div class="mpc-wrapper">
<div id="displayField">
<div id="titleDisplay">
<div id="soundTitle"></div>
<div id="volume"></div>
<div id="padNum"></div>
</div>
<div id="waveformDisplay">
<canvas id="waveform" width="400" height="50"></canvas>
<div id="soundPos"></div>
<div id="soundLength"></div>
</div>
<div id="modBtnDisplay">
<div class="displayOverflow" id="modDisplay1"></div>
<div class="displayOverflow" id="modDisplay2"></div>
<div class="displayOverflow" id="modDisplay3"></div>
</div>
</div>
<div id="buttonDrawArea">
<div id="subBtnDrawArea">
<div id="softBtns">
<a>
<div class="btns modBtn" data-button="soft1"></div>
</a>
<a>
<div class="btns modBtn" data-button="soft2"></div>
</a>
<a>
<div class="btns modBtn" data-button="soft3"></div>
</a>
</div>
<div id="modBtns">
<a id="leftMod">
<div class="btns modBtn" data-button="edit"></div>
</a>
<a id="rightMod">
<div class="btns modBtn" data-button="play"></div>
</a>
</div>
<div id="logo"></div>
<div id="padMonster">PAD<span class="lightText">monster</span></div>
</div>
<div id="soundBtnDrawArea" class="loading">
<div class="btnRow" id="row1"><a>
<div class="btns soundBtn" data-button="1">
<div></div>
</div>
</a><a>
<div class="btns soundBtn" data-button="2">
<div></div>
</div>
</a><a>
<div class="btns soundBtn" data-button="3">
<div></div>
</div>
</a><a>
<div class="btns soundBtn" data-button="4">
<div></div>
</div>
</a><span style="width:100%;display:inline-block;"></span></div>
<div class="btnRow" id="row2"><a>
<div class="btns soundBtn" data-button="5">
<div></div>
</div>
</a><a>
<div class="btns soundBtn" data-button="6">
<div></div>
</div>
</a><a>
<div class="btns soundBtn" data-button="7">
<div></div>
</div>
</a><a>
<div class="btns soundBtn" data-button="8">
<div></div>
</div>
</a><span style="width:100%;display:inline-block;"></span></div>
<div class="btnRow" id="row3"><a>
<div class="btns soundBtn" data-button="9">
<div></div>
</div>
</a><a>
<div class="btns soundBtn" data-button="10">
<div></div>
</div>
</a><a>
<div class="btns soundBtn" data-button="11">
<div></div>
</div>
</a><a>
<div class="btns soundBtn" data-button="12">
<div></div>
</div>
</a><span style="width:100%;display:inline-block;"></span></div>
<div class="btnRow" id="row4"><a>
<div class="btns soundBtn" data-button="13">
<div></div>
</div>
</a><a>
<div class="btns soundBtn" data-button="14">
<div></div>
</div>
</a><a>
<div class="btns soundBtn" data-button="15">
<div></div>
</div>
</a><a>
<div class="btns soundBtn" data-button="16">
<div></div>
</div>
</a>
<span style="width:100%;display:inline-block;"></span></div>
</div>
</div>
<div id="loadingMsg">
<div id="loadingText">Loading...</div>
</div>
</div>
<div id="tutDescription">
<div class="tutHeader">
<h1>MPC Style Sampler Tutorial</h1>
<h2>Featuring SoundJS</h2>
<a href="javascript:sampler.switch()" id="tutLink">Click here to see the tutorial</a>
<div id="description">
<h2>Description</h2>
<p>This is a virtual MPC-style sampler, built as a demonstration for the SoundJS and waveform-chrome javascript libraries. The waveform-chrome library was modified heavily to fit our needs, primarily the preloading multiple sounds, using canvas to draw the waveforms and disabling the sound playing feature. This tutorial also demonstrates use of an inheritance library to enable OO-like programming.</p>
</div>
</div>
<div class="tutContent">
<ol>
<li>
<p>First, all HTML elements were created. This was rapidly prototyped out using <strike>Zen Coding</strike>
<a href="https://github.com/sergeche/zen-coding" target="_blank">Emmet</a>.</p>
</li>
<li><p>Elements were styled using CSS. There was extensive usage of box-shadow as well as border-radius.</p>
<ul>
<li>In order to achieve the three dimensional look of the pad buttons, several box shadows were used with different
colors and offsets.
</li>
<pre class="styles">
.btns{
box-shadow: 1px 0px 1px #8A8A8A,0px 1px 1px #ACACAC,2px 1px 1px #8A8A8A,
1px 2px 1px #ACACAC, 3px 2px 1px #8A8A8A,2px 3px 1px #ACACAC,4px 3px 1px #8A8A8A,
3px 4px 1px #ACACAC, 5px 4px 1px #8A8A8A, 4px 5px 1px #ACACAC,6px 5px 1px #8A8A8A;
}</pre>
<li>To mimic the realistic response of pad buttons, a radial gradient was added to make it appear as
though an LED was activated under the button when pressed.
</li>
<pre class="styles">
.soundBtn.playing{
background-image: radial-gradient(farthest-side circle at center center, #f5c10a 49%, rgba(189, 189, 189, 0.7) 162%);
}</pre>
<li>A webfont was used to recreate the 8-segment look of an LCD display. The div also has a semi-transparent
PNG background in order to make it look more realistic.
</li>
<pre class="styles">
@font-face {
font-family:"digital7";
src: url('Digital-7Mono.woff');
}</pre>
</ul>
</li>
<li>
We wanted to utilize an Object Oriented coding structure within javascript, so we decided to use John Resig's <a
href="http://ejohn.org/blog/simple-javascript-inheritance/" target="_blank">Simple Javascript Inheritance</a>. This allows
us to create classes that have an init function that is run on instantiation.</li>
<pre class="js">
mpcDisplay = Class.extend({
init: function(){
this.initClickHandlers(); // Initialize click handlers (mouse)
this.initKeyHandlers(); // Initialize key press handlers (keyboard)
}
});</pre>
<li>This function initializes the click event handlers for the sound pad buttons.</li>
<pre class="js">
initClickHandlers:function(){
var that = this;
// On mousedown of pads
$('.soundBtn').mousedown(function(e){
// Begin Play sequence using pad's data-button attribute to ID button
that.processInput($(this).attr('data-button'));
});
// On mouseup of pads
$('.soundBtn').mouseup(function(e){
// Remove soundBtnActive class that makes the button look depressed in
$(this).removeClass('soundBtnActive');
})
}</pre>
<li>And this function initializes the key event handlers to complement the click events. We used separate keydown
and keyup handlers in order to be able to make it appear that the button is pressed until released.
</li>
<pre class="js">
initKeyHandlers:function(){
var that = this;
$(document).keydown(function(e){
var keyPressed = that.keycodeToCell(e.keyCode);
if(keyPressed){
if(!that.soundBank[keyPressed-1].isDown){
that.soundBank[keyPressed-1].isDown = true;
that.soundBank[keyPressed-1].element.addClass('soundBtnActive');
that.processInput(keyPressed);
}
}
});
$(document).keyup(function(e){
var keyPressed = that.keycodeToCell(e.keyCode);
if(keyPressed){
that.soundBank[keyPressed-1].isDown = false;
that.soundBank[keyPressed-1].element.removeClass('soundBtnActive');
}
});
},</pre>
<li>keycodeToCell is a helper function that determines which pad is associated with the activated key.</li>
<pre class="js">
keycodeToCell: function(keyPressed){
var keyCellVals = {
49: 1,
50: 2,
51: 3,
52: 4,
81: 5,
87: 6,
69: 7,
82: 8,
65: 9,
83: 10,
68: 11,
70: 12,
90: 13,
88: 14,
67: 15,
86: 16
};
return keyCellVals[keyPressed] || false;
}</pre>
</li>
<li><p>Next, <a href="http://www.createjs.com/#!/SoundJS" target="_blank">SoundJS</a> was set up to handle the actual audio functionality</p>
<ul>
<li>
The audio files were loaded using a preloadJS manifest. In this case, each item has some additional parameters so
that we can access them for the Display area.
All that is required for each file is src and id.</li>
<pre class="js">
var soundLibrary = {}; // Initialize manifest object for PreloadJS
soundLibrary.assetsPath = 'sounds/'; // Set root for audio files relative to HTML file (not js)
// Assign manifest of audio files for PreloadJS. Also includes custom values: title & fileName for use
in my code
soundLibrary.manifest= [
{src:soundLibrary.assetsPath+"kick.mp3", id:1, title: 'Kick', fileName: 'kick.mp3'},
{src:soundLibrary.assetsPath+"4dsnare.mp3", id:2, title: '4D Snare', fileName: '4dsnare.mp3'},
{src:soundLibrary.assetsPath+"2001-Chronic.mp3", id:3, title: '2001 Chronic', fileName:
'2001-Chronic.mp3'},
{src:soundLibrary.assetsPath+"harp1.mp3", id:4, title: 'Harp 1', fileName: 'harp1.mp3'},
{src:soundLibrary.assetsPath+"harp2.mp3", id:5, title: 'Harp 2', fileName: 'harp2.mp3'},
{src:soundLibrary.assetsPath+"AfricanChipmunks.mp3", id:6, title: 'African Chipmunks', fileName:
'AfricanChipmunks.mp3'},
{src:soundLibrary.assetsPath+"keepthechange.mp3", id:7, title: 'Keep The Change', fileName:
'keepthechange.mp3'},
{src:soundLibrary.assetsPath+"pitchedsnare.mp3", id:8, title: 'Pitched Snare', fileName:
'pitchedsnare.mp3'},
{src:soundLibrary.assetsPath+"snaphats.mp3", id:9, title: 'Snap Hats', fileName: 'snaphats.mp3'},
{src:soundLibrary.assetsPath+"vinylblip.mp3", id:10, title: 'Vinyl Blip', fileName: 'vinylblip.mp3'},
{src:soundLibrary.assetsPath+"comedown.mp3", id:11, title: 'Come Down', fileName: 'comedown.mp3'},
{src:soundLibrary.assetsPath+"fourletter.mp3", id:12, title: 'Four Letter', fileName: 'fourletter.mp3'},
{src:soundLibrary.assetsPath+"mouthsofdelight.mp3", id:13, title: 'Mouths of Delight', fileName:
'mouthsofdelight.mp3'},
{src:soundLibrary.assetsPath+"musicbox1.mp3", id:14, title: 'Music Box 1', fileName: 'musicbox1.mp3'},
{src:soundLibrary.assetsPath+"musicbox2.mp3", id:15, title: 'Music Box 2', fileName: 'musicbox2.mp3'},
{src:soundLibrary.assetsPath+"woodenrandarp.mp3", id:16, title: 'Wooden Rand Arp', fileName:
'woodenrandarp.mp3'}
];</pre>
<li>
I added an initCreateJs function for the rest of the setup.</li>
<pre class="js">
initCreateJs: function(){
var that = this;
// Set location of FlashAudioPlugin.swf in relation to SoundJS.js
createjs.FlashPlugin.BASE_PATH = './';
// Determine if SoundJS has been initialized. If not, initialize it.
if (!soundjs.checkPlugin(true)) {
console.error('createjs error');
return;
}
// Initialize local preload js
this.preload = new createjs.PreloadJS();
//Install SoundJS as a plugin, then PreloadJS will initialize it automatically.
this.preload.installPlugin(soundjs);
// On completion of loading for each sound file
this.preload.onFileLoad = function(event) {
// Assign associated audio file to the pad library after loading completed
that.soundBank[event.id-1].audioFile = event.result;
// Add class to light button
that.soundBank[event.id-1].element.addClass('loaded');
};
this.preload.onComplete = function(event) {
that.preloadDone = true;
// Run loadingComplete function
that.doneLoading();
};
//Load the manifest and pass 'true' to start loading immediately. Otherwise, you can call load()
manually.
this.preload.loadManifest(soundLibrary.manifest, true);
},</pre>
<li>When loading of all buttons is complete, I flash them and then hide the loading message using jQuery</li>
<pre class="js">
doneLoading: function(){
if (this.waveforms.doneLoading && this.preloadDone) {
$('#soundBtnDrawArea').removeClass('loading', 1000, 'swing', function () {
$('#soundBtnDrawArea').addClass('loading', 1000, 'swing', function () {
$('.soundBtn').removeClass('loaded');
$('#soundBtnDrawArea').removeClass('loading');
$('#loadingMsg').fadeOut();
});
});
}
},</pre>
</ul>
</li>
<li>Init was modified to include the assignment of all values required by each trigger pad button, including the jQuery element and sound info.</li>
<pre class="js">
$.each($('.soundBtn'), function(i){
defaultSound = soundLibrary.manifest[i]; // Default sound for pad index i equals manifest index i
that.soundBank.push({
triggerPad: $(this).attr('data-button'), // Pad # is determined from attribute data-button of pad element
element: $(this), // jQuery instance of dom element
soundTitle: defaultSound.title, // Title of the sound
fileName: defaultSound.fileName, // Filename of the sound
soundId: defaultSound.id, // PreloadJs ID of sound
sLength: null, // Length of sound file (will be loaded in later)
isPlaying: false, // Play state of sound
isDown: false // Is pad currently being pressed (key held down or click held down)
});
});</pre>
<li>A function was created to handle playing the sounds and putting the info on the display. An interval was used to
update the progress. Any time text was updated, it was faded in for realism. Also, any sound being replayed too quickly
is sent back to that sound's initial position, rather than loading a new instance of the sound to play.
Finally, the associated soundbank is sent to the function.
</li>
<pre class="js">
playSound: function(target){
// Clear interval for progress percentage
// Clear interval for progress bar
window.clearInterval(this.currentInterval);
window.clearInterval(mpcDisplay.progressInterval);
var that = this;
// Bugfix to allow repitition of the same sound quicker
if(target.soundId == this.currentId)
// Set position to 0 rather than replayin sound
this.currentInstance.setPosition(0);
else{
// Play the sound: play (src, interrupt, delay, offset, loop, volume, pan)
var instance = createjs.SoundJS.play(target.soundId, soundjs.INTERRUPT_ANY, 0, 0, false, 1);
this.currentInstance = instance;
// If errors, don't continue
if (instance == null || instance.playState == soundjs.PLAY_FAILED) { return; }
// Fade out old title
$('#soundTitle').fadeOut('fast',function(){
// Fade in new title
$('#soundTitle').text(target.soundTitle).fadeIn('fast');
});
// If sound bank has not yet included the length, add it
if (!target.sLength)
target.sLength = instance.getDuration();
// Display the sounds length
$('#soundLength').text(convertMS(target.sLength));
}
// Set interval for percentage update
this.currentInterval = window.setInterval(function(){
$('#soundPos').text(((instance.getPosition()/target.sLength).toFixed(2)*100).toFixed(0) + '%');
},100);
target.isPlaying = true;
// Draw the waveform ( returns the interval for progress)
mpcDisplay.progressInterval = this.waveforms.drawSpecifiedWaveform(target.soundId,this.currentInstance.getDuration());
// After sound completed
instance.onComplete = function(instance) {
// Stop remove lit class
target.element.removeClass('playing');
target.isPlaying = false;
// If this is the last sound played
if(instance == that.currentInstance){
//Clear the progress interval
window.clearInterval(that.currentInterval);
// Set progress to 100
$('#soundPos').text('100%');
}
}
}</pre>
<li>
For an added touch of style, we modified Waveform-chrome to support display of the progress of a song within the canvas.
Each waveform is preloaded and displayed when necessary. See code for more details.
</li>
<pre class="js">
preloadWaveforms:function(){
var fileArray = [];
$.each(soundLibrary.manifest,function(){
fileArray.push(this.src);
});
this.waveforms = Waveform({
fileArray: fileArray,
canvas: $('#waveform'),
status: $('#status'), // Status div for
colorOne: '#DBD2E8',
colorTwo: '#7857A5',
loadingCallback: function(){}
})
}</pre>
</ol>
</div>
</div>
</div>
</body>
</html>