-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPiano-Keyboard.js
More file actions
217 lines (143 loc) · 4.7 KB
/
Copy pathPiano-Keyboard.js
File metadata and controls
217 lines (143 loc) · 4.7 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
(function() {
var proto = Object.create(HTMLElement.prototype);
proto.createdCallback = function() {
this.pressedKeys = {};
this.keyClass = 'key';
this.keyBlackClass = 'key black';
this.keyboardLayout = 'ZSXDCVGBHNJMQ2W3ER5T6Y7U'.split('');
this.blackKeys = [ false, true, false, true, false, false, true, false, true, false, true, false ];
this.rebuildKeyboard();
};
proto.rebuildKeyboard = function() {
this.keys = [];
this.numOctaves = this.getAttribute('octaves');
initLayout(this);
};
function initLayout(kb) {
var blacksDiv;
var numBlacks = kb.blackKeys.length;
kb.innerHTML = '';
kb.classList.add('keyboard');
blacksDiv = document.createElement('div');
kb.appendChild(blacksDiv);
blacksDiv.className = 'blacks';
for(var i = 0; i < kb.numOctaves; i++) {
for(var j = 0; j < numBlacks; j++) {
var isBlack = kb.blackKeys[j],
keyDiv = document.createElement( 'div' ),
index = j + numBlacks * i,
label = kb.keyboardLayout[ index ];
keyDiv.className = isBlack ? kb.keyBlackClass : kb.keyClass;
keyDiv.innerHTML = label;
keyDiv.dataset.index = index;
keyDiv.addEventListener('mousedown', makeCallback(kb, onDivMouseDown), false);
keyDiv.addEventListener('mouseup', makeCallback(kb, onDivMouseUp), false);
kb.keys.push( keyDiv );
if(isBlack) {
blacksDiv.appendChild( keyDiv );
if(j >= 2 && !kb.blackKeys[j - 1] && !kb.blackKeys[j - 2] || (j === 1 && i > 0) ) {
keyDiv.classList.add('prevwhite');
}
} else {
kb.appendChild( keyDiv );
}
var numKeys = kb.keys.length;
kb.pressedKeys[numKeys] = false;
}
}
// Even if we set tabIndex=1 here, the browser is smart enough to
// let us cycle between keyboards when there's more than one on screen
// at the same time, by pressing TAB
kb.tabIndex = 1;
kb.addEventListener('keydown', makeCallback(kb, onKeyDown), false);
kb.addEventListener('keyup', makeCallback(kb, onKeyUp), false);
}
function makeCallback(kb, fn) {
var cb = function(e) {
fn(kb, e);
};
return cb;
}
function onDivMouseDown( keyboard, ev ) {
var key = ev.target;
dispatchNoteOn( keyboard, key.dataset.index );
}
function onDivMouseUp( keyboard, ev ) {
var key = ev.target;
dispatchNoteOff( keyboard, key.dataset.index );
}
function onKeyDown( keyboard, e ) {
var index = findKeyIndex( keyboard, e );
// TODO might want to check if it's checked already to prevent the multiple events being fired?
if( index === -1 || e.altKey || e.altGraphKey || e.ctrlKey || e.metaKey || e.shiftKey ) {
// no further processing
return;
}
dispatchNoteOn( keyboard, index );
}
function onKeyUp( keyboard, e ) {
var index = findKeyIndex( keyboard, e );
// Only fire key up if the key is in the defined layout
// TODO: maybe move this check to onKeyDown?
if( index !== -1 ) {
dispatchNoteOff( keyboard, index );
}
}
function isKeyPressed(keyboard, index) {
return keyboard.pressedKeys[index];
}
function setKeyPressed(keyboard, index, pressed) {
keyboard.pressedKeys[index] = pressed;
}
function findKeyIndex( keyboard, e ) {
var keyCode = e.keyCode || e.which,
keyChar = String.fromCharCode( keyCode ),
index = keyboard.keyboardLayout.indexOf( keyChar );
return index;
}
function makeEvent(type, detailData) {
return new CustomEvent(type, { detail: detailData });
}
function dispatchNoteOn( keyboard, index ) {
var key = keyboard.keys[index],
currentClass = key.className;
if(isKeyPressed(keyboard, index)) {
// Already pressed, are we mouseclicking and keyboarding
// at the same time?
console.log('already pressed', index);
return;
}
setKeyPressed(keyboard, index, true);
key.classList.add('active');
var evt = makeEvent('noteon', { index: index });
keyboard.dispatchEvent(evt);
}
function dispatchNoteOff( keyboard, index ) {
var key = keyboard.keys[index];
if(!isKeyPressed(keyboard, index)) {
// TODO ghost note offs!? maybe if we press down on one key but
// release the mouse in another key?
console.error('this key is not pressed', index);
return;
}
key.classList.remove('active');
setKeyPressed(keyboard, index, false);
var evt = makeEvent('noteoff', { index: index });
keyboard.dispatchEvent(evt);
}
//
var component = {};
component.prototype = proto;
component.register = function(name) {
document.registerElement(name, {
prototype: proto
});
};
if(typeof define === 'function' && define.amd) {
define(function() { return component; });
} else if(typeof module !== 'undefined' && module.exports) {
module.exports = component;
} else {
component.register('openmusic-piano-keyboard'); // automatic registration
}
}).call(this);