-
Notifications
You must be signed in to change notification settings - Fork 0
/
music.js
326 lines (279 loc) · 7.5 KB
/
music.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
// TODO: Make it poly
let synth = new Tone.Synth().toDestination(),
html = "",
octave = 4;
let notesOne = [{
"note": 'C',
"key": 'A',
"ascii": '65'
}, {
"note": 'D',
"key": 'S',
"ascii": '83'
}, {
"note": 'E',
"key": 'D',
"ascii": '68'
}, {
"note": 'F',
"key": 'F',
"ascii": '70'
}, {
"note": 'G',
"key": 'G',
"ascii": '71'
}, {
"note": 'A',
"key": 'H',
"ascii": '72'
}, {
"note": 'B',
"key": 'J',
"ascii": '74'
}];
let sharpsOne = [{
"note": 'C#',
"key": 'W',
"ascii": '87'
}, {
"note": 'D#',
"key": 'E',
"ascii": '69'
}, {
"note": 'F#',
"key": 'T',
"ascii": '84'
}, {
"note": 'G#',
"key": 'Y',
"ascii": '89'
}, {
"note": 'A#',
"key": 'U',
"ascii": '85'
}];
let notesTwo = [{
"note": 'C',
"key": 'K',
"ascii": '75'
}, {
"note": 'D',
"key": 'L',
"ascii": '76'
}, {
"note": 'E',
"key": ';',
"ascii": '186'
}, {
"note": 'F',
"key": "'",
"ascii": '222'
}];
let sharpsTwo = [{
"note": 'C#',
"key": 'O',
"ascii": '79'
}, {
"note": 'D#',
"key": 'P',
"ascii": '80'
}];
synth.oscillator.type = "sine";
buildKeyboard(octave);
// On mouse down and up - octave and button color change
document.getElementById("octave-up").onmousedown = () => {
document.querySelector('#octave-up').style.background = "#6DBEE4";
octaveUp();
}
document.getElementById("octave-up").onmouseup = () => {
document.querySelector('#octave-up').style.background = "#F84D00";
octaveUp();
}
document.getElementById("octave-down").onmousedown = () => {
document.querySelector('#octave-down').style.background = "#6DBEE4";
octaveDown();
}
document.getElementById("octave-down").onmouseup = () => {
document.querySelector('#octave-down').style.background = "#F84D00";
octaveDown();
}
// ASCII Octave Up and Down
document.onkeydown = (e) => {
e = e || window.event;
console.log(e.keyCode);
if (e.keyCode == 49) {
document.querySelector('#octave-down').style.background = "#6DBEE4";
octaveDown();
}
if (e.keyCode == 50) {
document.querySelector('#octave-up').style.background = "#6DBEE4";
octaveUp();
}
}
// Change button color back onkeyup
document.onkeyup = (e) => {
e = e || window.event;
console.log(e.keyCode);
if (e.keyCode == 49) {
document.querySelector('#octave-down').style.background = "#F84D00";
}
if (e.keyCode == 50) {
document.querySelector('#octave-up').style.background = "#F84D00";
}
}
var waveButtons = document.getElementsByClassName('change-wave');
for (var i = 0; i < waveButtons.length; i++) {
let selected = 'wave-selected';
waveButtons[i].addEventListener('click', function() {
synth.oscillator.type = this.value;
// Remove selected class from all buttons, then add it to the currently selected button
for (let sibling of this.parentNode.children) {
sibling.classList.remove(selected);
}
this.classList.add(selected);
});
}
function octaveUp() {
octave += 1;
buildKeyboard(octave);
}
function octaveDown() {
octave -= 1;
buildKeyboard(octave);
}
function buildKeyboard(octave) {
html = "";
addKeys(notesOne.length, octave, notesOne, sharpsOne);
addKeys(notesTwo.length, octave + 1, notesTwo, sharpsTwo);
document.querySelector('#keys-container').innerHTML = html;
}
function addKeys(x, octave, notes, sharps){
// For every note we're iterating over:
for (var i = 0; i < x; i++) {
var displaySharp = true;
var note = notes[i].note;
var key = notes[i].key;
var ascii = notes[i].ascii;
// If second F, don't display sharp
if (x == 4) {
if (note == 'F') {
displaySharp = false;
}
}
// E# and B# don't exist, so don't display:
if (note == 'E' || note == 'B') {
displaySharp = false;
}
// HTML render for all white and black keys:
html += `<div class="whitenote"
onmousedown="noteDown(this, false)"
onmouseup="noteUp(this, false)"
onmouseleave="noteUp(this, false)"
data-ascii="${ascii}"
data-note="${note + (octave)}">`;
let noteSharp = note + '#';
// If displaying the sharp note, configure to have the correct ASCII key value.
if (displaySharp) {
html += `<div class="blacknote"
onmousedown="noteDown(this, true)"
onmouseup="noteUp(this, true)"
onmouseleave="noteUp(this, true)"
data-ascii="${ascii}"
data-note="${noteSharp + (octave)}">
<div class="ascii-key-display">`;
// i.e. if C# = C#, get the correct 'key' value and render it in HTML
for (var j = 0; j < sharps.length; j++) {
if (sharps[j].note == noteSharp) {
html += `${sharps[j].key}`;
}
}
// Close the divs
html += `</div></div>`;
}
// White note inner HTML
html += `<div class="ascii-key-display">${key}</div></div>`;
}
}
function noteUp(e, isSharp) {
e.style.background = isSharp ? 'black' : 'white';
}
function noteDown(e, isSharp) {
console.log(e)
var ascii = e.dataset.ascii
var note = e.dataset.note;
e.style.background = isSharp ? '#c29417' : '#fff6de';
synth.triggerAttackRelease(note, "16n");
event.stopPropagation();
}
// TODO: Refactor this with noteUp and noteDown
// Add keydown function / ascii playability
document.addEventListener('keydown', keyDown);
document.addEventListener('keyup', keyUp);
function keyDown(event) {
keyCode = event.keyCode;
// NotesOne
for (var i = 0; i < notesOne.length; i++) {
var note = notesOne[i].note
var ascii = notesOne[i].ascii
if (ascii == keyCode) {
note = note + (octave);
synth.triggerAttackRelease(note, "16n");
document.querySelector(`.whitenote[data-ascii="${ascii}"]`).style.background = '#fff6de';
}
}
// NotesTwo
for (var i = 0; i < notesTwo.length; i++) {
var note = notesTwo[i].note
var ascii = notesTwo[i].ascii
if (ascii == keyCode) {
note = note + (octave + 1);
synth.triggerAttackRelease(note, "16n");
document.querySelector(`.whitenote[data-ascii="${ascii}"]`).style.background = '#fff6de';
}
}
// SharpsOne
for (var i = 0; i < sharpsOne.length; i++) {
var note = sharpsOne[i].note
var ascii = sharpsOne[i].ascii
if (ascii == keyCode) {
note = note + (octave);
console.log(note);
synth.triggerAttackRelease(note, "16n");
document.querySelector(`.blacknote[data-note="${note}"]`).style.background = '#c29417';
}
}
// SharpsTwo
for (var i = 0; i < sharpsTwo.length; i++) {
var note = sharpsTwo[i].note
var ascii = sharpsTwo[i].ascii
if (ascii == keyCode) {
note = note + (octave + 1);
synth.triggerAttackRelease(note, "16n");
document.querySelector(`.blacknote[data-note="${note}"]`).style.background = '#c29417';
}
}
}
function keyUp() {
// NotesOne
for (var i = 0; i < notesOne.length; i++) {
var ascii = notesOne[i].ascii
document.querySelector(`.whitenote[data-ascii="${ascii}"]`).style.background = 'white';
}
// NotesTwo
for (var i = 0; i < notesTwo.length; i++) {
var ascii = notesTwo[i].ascii
document.querySelector(`.whitenote[data-ascii="${ascii}"]`).style.background = 'white';
}
// SharpsOne
for (var i = 0; i < sharpsOne.length; i++) {
var note = sharpsOne[i].note
note = note + octave
document.querySelector(`.blacknote[data-note="${note}"]`).style.background = 'black';
}
// SharpsTwo
for (var i = 0; i < sharpsTwo.length; i++) {
var note = sharpsTwo[i].note
note = note + (octave + 1)
document.querySelector(`.blacknote[data-note="${note}"]`).style.background = 'black';
}
}