-
Notifications
You must be signed in to change notification settings - Fork 1
/
jquery.piano.js
360 lines (325 loc) · 14.6 KB
/
jquery.piano.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
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
/**
* JS piano plugin for jquery
* @method piano
* @version 1.0.0
* @author Panagiotis Vourtsis <vourtsis_pan@hotmail.com>
*/
(function($) {
/**
* Here is my definition of the piano plugin in JQuery
* @name piano
* @param {Object} config - the parameters of the configuration of the librray
* @class
*/
$.fn.piano = function( config ) {
var options = $.extend({
tone: 1
}, config );
var context = new window.AudioContext();
var self = this;
var octavis = ['C','C#','D','D#','E','F','F#','G','G#','A','A#','B'];
this.demoPlaying = false;
this.timeouts = [];
/**
* This will handle the click of the keys. It will calculate the key frequency and based on the tone will pass the sound for production.
* @function
* @name toneClick
* @param {Object} e - The event object from the element
*/
this.toneClick = function(e) {
var key = $(e.currentTarget).data("key");
var tone = $(e.currentTarget).data("tone");
var freq = this.frequencySelector(key);
var t = 0;
for(var i=0; i<tone; i++) {
t = t + (freq*2);
}
this.soundPlay(t);
};
/**
* It will produce the sounds based on the frequence that is being passed
* @function
* @name soundPlay
* @param {Integer} freq - The frequence to play
*/
this.soundPlay = function(freq) {
var now = context.currentTime;
var oscillator = context.createOscillator();
var gain = context.createGain();
oscillator.type = "sine";
oscillator.frequency.value = freq;
gain.gain.linearRampToValueAtTime(0, now);
gain.gain.linearRampToValueAtTime(.6, now + .1);
gain.gain.linearRampToValueAtTime(0, now + 1);
oscillator.connect(gain);
gain.connect(context.destination);
oscillator.start(0);
setTimeout(function() { oscillator.stop(); }, 1500);
};
/**
* My first demo for when you click the demo 1 option | Coldplay - scientist intro
* @function
* @name demos
* @param {Object} e - The event object from the element
*/
this.demos = function(e) {
if((typeof(e) !== 'undefined' && this.demoPlaying) ) {
$(e.currentTarget).removeClass("active");
this.demoPlaying = false;
for (var i = 0; i < this.timeouts.length; i++) {
clearTimeout(this.timeouts[i]);
}
this.timeouts = [];
} else {
if((typeof(e) !== 'undefined')){
if(!$(e.currentTarget).hasClass("active")) {
$(e.currentTarget).addClass("active");
}
}
this.demoPlaying = true;
var self = this;
var loopTimeout = 750;
for (var i = 0; i < 4; i++) {
self.timeouts.push(setTimeout(function () {
if(self.demoPlaying) {
$("body " + self.selector).find("div[data-key='D'][data-tone='1']").mousedown().addClass("active").delay(500).queue(function (next) {
$(this).removeClass("active");
next();
});
$("body " + self.selector).find("div[data-key='F'][data-tone='1']").mousedown().addClass("active").delay(500).queue(function (next) {
$(this).removeClass("active");
next();
});
$("body " + self.selector).find("div[data-key='A'][data-tone='1']").mousedown().addClass("active").delay(500).queue(function (next) {
$(this).removeClass("active");
next();
});
}
}, loopTimeout * i));
}
self.timeouts.push(setTimeout(function () {
for (var i = 0; i < 4; i++) {
setTimeout(function () {
if(self.demoPlaying) {
$("body " + self.selector).find("div[data-key='D'][data-tone='1']").mousedown().addClass("active").delay(500).queue(function (next) {
$(this).removeClass("active");
next();
});
$("body " + self.selector).find("div[data-key='F'][data-tone='1']").mousedown().addClass("active").delay(500).queue(function (next) {
$(this).removeClass("active");
next();
});
$("body " + self.selector).find("div[data-key='A#'][data-tone='1']").mousedown().addClass("active").delay(500).queue(function (next) {
$(this).removeClass("active");
next();
});
}
}, loopTimeout * i);
}
}, 3000));
self.timeouts.push(setTimeout(function () {
if(self.demoPlaying) {
$("body " + self.selector).find("div[data-key='C'][data-tone='2']").mousedown().addClass("active").delay(500).queue(function (next) {
$(this).removeClass("active");
next();
});
}
}, 5700));
self.timeouts.push(setTimeout(function () {
for (var i = 0; i < 4; i++) {
setTimeout(function () {
if(self.demoPlaying) {
$("body " + self.selector).find("div[data-key='C'][data-tone='1']").mousedown().addClass("active").delay(500).queue(function (next) {
$(this).removeClass("active");
next();
});
$("body " + self.selector).find("div[data-key='F'][data-tone='1']").mousedown().addClass("active").delay(500).queue(function (next) {
$(this).removeClass("active");
next();
});
$("body " + self.selector).find("div[data-key='A'][data-tone='1']").mousedown().addClass("active").delay(500).queue(function (next) {
$(this).removeClass("active");
next();
});
}
}, loopTimeout * i);
}
}, 6100));
self.timeouts.push(setTimeout(function () {
for (var i = 0; i < 4; i++) {
setTimeout(function () {
if(self.demoPlaying) {
$("body " + self.selector).find("div[data-key='C'][data-tone='1']").mousedown().addClass("active").delay(500).queue(function (next) {
$(this).removeClass("active");
next();
});
$("body " + self.selector).find("div[data-key='F'][data-tone='1']").mousedown().addClass("active").delay(500).queue(function (next) {
$(this).removeClass("active");
next();
});
$("body " + self.selector).find("div[data-key='G'][data-tone='1']").mousedown().addClass("active").delay(500).queue(function (next) {
$(this).removeClass("active");
next();
});
}
}, loopTimeout * i);
}
}, 9100));
self.timeouts.push(setTimeout(function () {
if(self.demoPlaying) {
self.demos();
}
}, 12100));
}
};
/**
* My second demo for when you click the demo 2 option | Great Big World - Say Something
* @function
* @name demos2
* @param {Object} e - The event object from the element
*/
this.demos2 = function(e) {
if((typeof(e) !== 'undefined' && this.demoPlaying) ) {
$(e.currentTarget).removeClass("active");
this.demoPlaying = false;
for (var i = 0; i < this.timeouts.length; i++) {
clearTimeout(this.timeouts[i]);
}
this.timeouts = [];
} else {
if((typeof(e) !== 'undefined')){
if(!$(e.currentTarget).hasClass("active")) {
$(e.currentTarget).addClass("active");
}
}
this.demoPlaying = true;
var self = this;
var loopTimeout = 1500;
for (var i = 0; i < 4; i++) {
self.timeouts.push(setTimeout(function () {
if(self.demoPlaying) {
$("body " + self.selector).find("div[data-key='B'][data-tone='1']").mousedown().addClass("active").delay(500).queue(function (next) {
$(this).removeClass("active");
next();
});
$("body " + self.selector).find("div[data-key='G#'][data-tone='2']").mousedown().addClass("active").delay(500).queue(function (next) {
$(this).removeClass("active");
next();
});
$("body " + self.selector).find("div[data-key='D'][data-tone='3']").mousedown().addClass("active").delay(400).queue(function (next) {
$(this).removeClass("active");
next();
});
self.timeouts.push(setTimeout(function () {
$("body " + self.selector).find("div[data-key='D'][data-tone='3']").mousedown().addClass("active").delay(400).queue(function (next) {
$(this).removeClass("active");
next();
});
}, 1000));
}
}, loopTimeout * i));
}
//self.timeouts.push(setTimeout(function () {
// if(self.demoPlaying) {
// //self.demos2();
// }
//}, 12100));
}
};
/**
* This function creates all the dom objects for the piano and bind events on them
* @function
* @name init
*/
this.init = function() {
var $container = $("<div>", {class: "keyboard"});
var $left_speaker = $("<div>", {class: "speaker left_speaker"});
var $right_speaker = $("<div>", {class: "speaker right_speaker"});
var $options = $("<div>", {class: "options"});
if(options.tone >= 2) {
var $item1 = $("<div>", {class: "item"});
$item1.click($.proxy(this.demos, this));
$item1.html("Demo 1");
$options.append($item1);
}
/**
* @todo make the second demo
*/
//if(options.tone >= 3) {
// var $item2 = $("<div>", {class: "item"});
// $item2.click($.proxy(this.demos2, this));
// $item2.html("Demo 2");
// $options.append($item2);
//}
for(var ii = 1; ii <= options.tone; ii++) {
for (var i = 0; i < 12; i++) {
if (octavis[i].indexOf("#") > 0) {
var $div = $("<div>", {class: "keys black-keys", "data-key": octavis[i], "data-tone": ii});
} else {
var $div = $("<div>", {class: "keys", "data-key": octavis[i], "data-tone": ii});
}
$div.mousedown($.proxy(this.toneClick, this));
$container.append($div);
}
}
$(this).append($left_speaker);
$(this).append($right_speaker);
$(this).append($options);
$(this).append($container);
};
/**
* This function determines and returns the frequency for a specific key
* @function
* @name frequencySelector
* @param {String} note - The note that was clicked
* @returns {Integer} return the integer of the frequency
*/
this.frequencySelector = function(note) {
var frequency = '';
switch(note) {
case 'C':
frequency = 130.81;
break;
case 'C#':
frequency = 138.59;
break;
case 'D':
frequency = 146.83;
break;
case 'D#':
frequency = 155.56;
break;
case 'E':
frequency = 164.81;
break;
case 'F':
frequency = 174.61;
break;
case 'F#':
frequency = 185;
break;
case 'G':
frequency = 196;
break;
case 'G#':
frequency = 207.65;
break;
case 'A':
frequency = 220;
break;
case 'A#':
frequency = 233.08;
break;
case 'B':
frequency = 246.94;
break;
}
return frequency;
};
/**
* Here i am initializing the plugin
*/
this.init();
return this;
}
}(jQuery));