-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
576 lines (443 loc) · 12.1 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
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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
/**
* @module piano-keyboard
*/
import extend from 'xtend/mutable';
import key from 'piano-key';
import domify from 'domify';
import on from 'emmy/on';
import emit from 'emmy/emit';
import delegate from 'emmy/delegate';
import off from 'emmy/off';
import getUid from 'get-uid';
import isString from 'mutype/is-string';
import isArray from 'mutype/is-array';
import isNumber from 'mutype/is-number';
import selection from 'mucss/selection';
import css from 'mucss/css';
import isBetween from 'mumath/is-between';
import {findTouch} from 'get-client-xy';
import slice from 'sliced';
import {Duplex} from 'stream';
var doc = document;
/**
* Takes element and options
*
* @constructor
*/
class Keyboard extends Duplex {
constructor (options) {
super({
objectMode: true
});
var self = this;
extend(self, options);
//create element
if (!self.element) {
self.element = document.createElement('div');
}
selection.disable(self.element);
self.element.classList.add('piano-keyboard');
//to track events
self.id = getUid();
//transform range to numbers
self.range = self.range.map(function (value) {
return isString(value) ? key.getNumber(value) : value
});
//create number of keys according to the range
self.createKeys(self.range);
//set of active notes
self.activeNotes = new Set();
//set of pressed midi-keys, to manage shift mode
self._pressedKeys = new Set();
self.enable();
}
/**
* Read is called whenever the consumer wants to read more.
* To form a stack of data to read, call self.push(chunk)
*/
_read () {
//FIXME: it is considered to be always active right now, so if there is a case when it needs to be stopped - implement it
}
/**
* Write is called whenever there is a note to display
*/
_write ([code, note, value], encoding, callback) {
var self = this;
//handle noteoffs
if (code === 128 || (code === 144 && value === 0)) {
self._pressedKeys.delete(note);
self.noteOff(note);
}
//handle noteons
else if (code === 144) {
self._pressedKeys.add(note);
self.noteOn(note, value);
}
callback();
}
/**
* Create keyboard based of number of keys passed
*/
createKeys (range) {
var self = this;
var number = Math.abs(self.range[1] - self.range[0]);
var startWith = self.range[0];
var srcKeyEl = domify('<div class="piano-keyboard-key" data-key></div>');
var prevWhiteKeyEl;
for (var i = 0; i < number; i++) {
var keyEl = srcKeyEl.cloneNode();
var keyNumber = startWith + i;
var keyNote = key.getNote(keyNumber);
keyEl.setAttribute('data-key', keyNumber);
keyEl.setAttribute('title', key.getName(keyNumber));
keyEl.classList.add('piano-keyboard-key-' + keyNote.toLowerCase().replace('#', '-sharp'));
if (key.isBlack(keyNumber)) {
keyEl.setAttribute('data-key-black', true);
keyEl.classList.add('piano-keyboard-key-black');
//put blacks into white keys - the only way to implement flex sizing
if (prevWhiteKeyEl) {
prevWhiteKeyEl.appendChild(keyEl);
}
}
else {
keyEl.classList.add('piano-keyboard-key-white');
self.element.appendChild(keyEl);
prevWhiteKeyEl = keyEl;
}
}
return self;
}
/** Bind events */
enable () {
var self = this;
//active touches
var activeTouches = [];
//last mouse coords
var lastClientXY = [0,0];
//get start note number
var startWith = self.range[0];
self.element.removeAttribute('disabled');
//whether to block noteOn/noteOff
self.isBlocked = false;
self.update();
//keep rectangles updated
on(window, 'resize.' + self.id, function () {
self.update();
});
//notes pressed simultaneously
self.shiftGroupNotes = null;
on(window, 'keydown.' + self.id, function (e) {
//shift === 16
if (e.which === 16 && !self.isShiftPressed) {
self.shiftGroupNotes = new Set(self.activeNotes);
}
});
on(window, 'keyup.' + self.id, function (e) {
if (e.which === 16) {
self.shiftGroupNotes = null;
//update notes so to only the pressed ones remain
updateNotes(activeTouches, null, self.pressedKeys);
}
});
//enable note on mouse/touch
var isMouseDown = false;
on(self.element, 'mousedown.' + self.id + ' touchstart.' + self.id, function (e) {
e.preventDefault();
self.isBlocked = false;
isMouseDown = true;
updateNotes(e.touches || [0], e);
//don’t bind more than once
if (self.isActive) {
return;
}
self.isActive = true;
on(doc, 'mousemove.' + self.id, function (e) {
if (!isMouseDown) return;
e.preventDefault();
updateNotes([0], e);
});
on(doc, 'touchmove.' + self.id, function (e) {
e.preventDefault();
updateNotes(e.touches, e);
});
on(doc, 'mouseup.' + self.id + ' mouseleave.' + self.id, function (e) {
e.preventDefault();
updateNotes([]);
self.isBlocked = false;
isMouseDown = false;
});
on(doc, 'touchend.' + self.id, function (e) {
e.preventDefault();
// updateNotes(e.changedTouches, e);
updateNotes(e.touches, e);
});
});
//up keys on blur
on(window, 'blur.' + self.id, function (e) {
self.isBlocked = false;
self.shiftGroupNotes = null;
updateNotes([], e);
});
//walk the list of touches, for each touch activate the key
//pass empty touches list to just off all notes
function updateNotes (touches, e, ignoreNotes) {
var rects = self.rectangles;
var notes = self.notes;
var noteEls = self.noteElements;
touches = slice(touches);
//save last touches as active
activeTouches = touches;
//FIXME: Safari doesn't know what's that
// var notesOff = new Set(self.activeNotes);
var notesOff = new Set();
self.activeNotes.forEach(function (note) {
notesOff.add(note);
});
var notesOn = new Set();
var blackTouches = new Set();
//for all notes - find ones to turn on and ones to turn off
for (var i = 0; i < notes.length; i++) {
touches.forEach(function (touchId) {
if (touchId.identifier !== undefined) {
touchId = touchId.identifier;
};
var touch = findTouch(touches, touchId);
var clientXY;
if (touch) {
clientXY = [touch.clientX, touch.clientY]
}
else if (e) {
clientXY = [e.clientX, e.clientY]
//update last coords
lastClientXY = clientXY;
}
else {
clientXY = lastClientXY;
}
if (isBetween(clientXY[0], rects[i].left, rects[i].right) && isBetween(clientXY[1], rects[i].top, rects[i].bottom)) {
if (blackTouches.has(touchId)) {
return;
}
//add to on notes & reserve touch to avoid double-note pressing
if (!notesOn.has(notes[i])) {
notesOn.add(notes[i]);
if (noteEls[i].hasAttribute('data-key-black')) {
blackTouches.add(touchId);
}
}
//remove from planned notes to off
notesOff.delete(notes[i]);
}
});
}
//unbind/bind notes
if (ignoreNotes) {
ignoreNotes.forEach(function (note) {
notesOff.delete(note)
})
}
notesOff.forEach(self.noteOff, self);
notesOn.forEach(self.noteOn, self);
//check whether there are notes left
if (!self.activeNotes.size) {
self.isActive = false;
off(doc, '.' + self.id);
}
}
//enable a11y
if (self.a11y) {
//add tabindexes
self.noteElements.forEach(function (noteEl) {
noteEl.setAttribute('tabindex', 1);
});
//handle key/tab navigating
delegate(self.element, 'keydown', '[data-key]', function (e) {
var keyEl = e.delegateTarget;
var note = self.parseNote(keyEl);
if (note === undefined) {
return;
}
//space instantly presses the note
if (e.which === 32) {
self.noteOn(note);
}
//arrows moves key left&bottom/right&top
if (e.which === 39 || e.which === 38) {
var nextEl = keyEl.querySelector('[data-key]') || (keyEl.parentNode.matches('[data-key]') && keyEl.parentNode.nextSibling) || keyEl.nextSibling;
if (!nextEl) {
return;
}
nextEl.focus();
}
if (e.which === 37 || e.which === 40) {
var prevEl = (keyEl.parentNode.matches('[data-key]') && keyEl.parentNode) || (keyEl.previousSibling && keyEl.previousSibling.querySelector('[data-key]')) || keyEl.previousSibling;
if (!prevEl) {
return;
}
prevEl.focus();
}
});
delegate(self.element, 'keyup', '[data-key]', function (e) {
self.isBlocked = false;
var keyEl = e.delegateTarget;
var note = self.parseNote(keyEl);
if (note === undefined) {
return;
}
//space instantly stops note
if (e.which === 32) {
self.noteOff(note);
}
});
}
return self;
}
/** Update view */
update () {
var self = this;
//specially sorted array of keys (blacks first)
self.noteElements = slice(self.element.querySelectorAll('[data-key]')).sort(function (a, b) {
if (a.hasAttribute('data-key-black')) return -1;
return 1;
});
self.rectangles = self.noteElements.map(function (el) {
return el.getBoundingClientRect();
});
self.notes = self.noteElements.map(self.parseNote);
return self;
}
/** Parse note number from any arg */
parseNote (note) {
if (isString(note)) {
return key.getNumber(note);
}
else if (isNumber(note)) {
return note;
}
else if (note instanceof HTMLElement) {
//ignore not keys
if (!note.hasAttribute('data-key')) {
return;
}
return parseInt(note.getAttribute('data-key'));
}
}
/** Set active note */
noteOn (note, value) {
var self = this;
if (self.isBlocked) return self;
if (value === undefined) {
value = 127;
}
if (isArray(note)) {
slice(note).forEach(function (note, i) {
self.noteOn(note, value[i] !== undefined ? value[i] : value );
});
return self;
}
note = self.parseNote(note);
if (note === undefined) {
return self;
}
var keyEl = self.element.querySelector('[data-key="' + note + '"]');
if (!keyEl) {
// throw Error(key.getName(note) + ' does not exist');
return;
}
//save shift group, if any
if (self.shiftGroupNotes) {
//but if shiftGroup cointains the note - force trigger rather than on
if (self.shiftGroupNotes.has(note)) {
self.shiftGroupNotes.delete(note);
self.noteOff(note);
self.isBlocked = true;
//focus on disabled note
if (self.a11y) {
keyEl.focus();
}
return self;
}
self.shiftGroupNotes.add(note);
self.isBlocked = true;
}
//don’t trigger twice
if (self.activeNotes.has(note)) {
return self;
}
//save active key
self.activeNotes.add(note);
//send on note
emit(self, 'noteOn', {
target: keyEl,
which: note,
value: value
});
//send to stream
self.push([144, note, value]);
//reflect classes
keyEl.classList.add('piano-keyboard-key-active');
keyEl.setAttribute('data-key-active', true);
if (self.a11y) {
keyEl.focus();
}
return self;
}
/** Disable note or all notes */
noteOff (note) {
var self = this, keyEl;
if (self.isBlocked) return self;
//disable all active notes
if (note === undefined) {
self.activeNotes.forEach(self.noteOff, self);
return self;
}
if (isArray(note)) {
slice(note).forEach(function (note, i) {
self.noteOff(note);
});
return self;
}
note = self.parseNote(note);
if (note === undefined) {
return self;
}
var keyEl = self.element.querySelector('[data-key="' + note + '"]');
if (!keyEl) {
// throw Error(key.getName(note) + ' does not exist');
return self;
}
//ignore inactive key
if (!self.activeNotes.has(note)) {
return self;
}
//ignore key if it is in the shift group
if (self.shiftGroupNotes && self.shiftGroupNotes.has(note)) {
return self;
}
//forget key
self.activeNotes.delete(note);
//send off note
emit(self, 'noteOff', {
target: keyEl,
which: note,
value: 0
});
keyEl.classList.remove('piano-keyboard-key-active');
keyEl.removeAttribute('data-key-active');
return self;
}
/** Carefully clean up */
disable () {
var self = this;
self.element.setAttribute('disabled', true);
off(self.element, '.' + self.id);
off(window, '.' + self.id);
return self;
}
}
var proto = Keyboard.prototype;
/** Default notes range to display */
proto.range = ['C3', 'C4'];
/** Enable focusability, accessibility */
proto.a11y = false;
export default Keyboard;