-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
214 lines (214 loc) · 7.04 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
"use strict";
/**
* @function limitTo
* @description limit number to certain range.
* @param {number} [num] input number
* @param {number} [min] lower limit
* @param {number} [max] upper limit
* @returns {number} number
*/
var limitTo = function (num, min, max) { return Math.min(Math.max(num, min), max); };
/**
* Map between keys and notes.
*/
var keyToNoteMap = {
'a': { note: 'C', freq: 16.35 },
'w': { note: 'C#', freq: 17.32 },
's': { note: 'D', freq: 18.35 },
'e': { note: 'D#', freq: 19.45 },
'd': { note: 'E', freq: 20.60 },
'f': { note: 'F', freq: 21.83 },
't': { note: 'F#', freq: 23.12 },
'g': { note: 'G', freq: 24.50 },
'y': { note: 'G#', freq: 25.96 },
'h': { note: 'A', freq: 27.50 },
'u': { note: 'A#', freq: 29.14 },
'j': { note: 'B', freq: 30.87 },
'k': { note: 'C', freq: 32.70 },
'o': { note: 'C#', freq: 34.65 },
'l': { note: 'D', freq: 36.71 },
'p': { note: 'D#', freq: 38.89 }
};
/**
* Map between keys and commands.
*/
var keyToCommandsMap = {
'z': 'octave-down',
'x': 'octave-up',
'c': 'velocity-down',
'v': 'velocity-up',
'b': 'hold-mode'
};
/**
* @constructor
* @constructs VirtualKeyBoard
* @description function constructor
* @param {number} octave initial octave
* @param {number} velocity initial velocity
* @returns {VirtualKeyBoard} self
*/
var VirtualKeyBoard = /** @class */ (function () {
function VirtualKeyBoard(octave, velocity) {
if (octave === void 0) { octave = 1; }
if (velocity === void 0) { velocity = 100; }
this.holdMode = false;
this.heldKeys = {};
this.subscribers = [];
this.activeVoices = 0;
this.octave = octave;
this.velocity = velocity;
this.activate();
}
/**
* @method subscribe
* @description register callback for subscription
* @param {Function} callback callback method
* @returns {VirtualKeyBoard} this
*/
VirtualKeyBoard.prototype.subscribe = function (callback) {
if (!(this.subscribers.indexOf(callback) < 0)) {
return this;
}
this.subscribers.push(callback);
return this;
};
/**
* @method unsubscribe
* @description un-register callback from subscription
* @param {Function} callback callback method
* @returns {VirtualKeyBoard} this
*/
VirtualKeyBoard.prototype.unsubscribe = function (callback) {
var position = this.subscribers.indexOf(callback);
if (position < 0) {
return this;
}
this.subscribers.splice(position, 1);
return this;
};
/**
* @method emit
* @description calls callbacks of subscribers
* @param {INoteData} data data to publish
* @returns {INoteData} data
*/
VirtualKeyBoard.prototype.emit = function (data) {
this.subscribers.map(function (f) { return f(data); });
return data;
};
/**
* @method noteToFreq
* @description converts note at specific octave to frequency
* @param {number} freq frequency
* @returns {number} frequency
*/
VirtualKeyBoard.prototype.freqCorrection = function (freq) {
return freq * Math.pow(2, this.octave);
};
/**
* @method keyHandler
* @description handles key events
* @param {Event} event keyboard event
*/
VirtualKeyBoard.prototype.keyHandler = function (event) {
var key = event.key.toLowerCase();
var note = keyToNoteMap[key];
var command = keyToCommandsMap[key];
if (event.type === 'keydown') {
// check if note should be (re)triggered
var doTrigger = !this.heldKeys.hasOwnProperty(key) || this.holdMode;
// handle note key
if (note && doTrigger) {
var voice = this.activeVoices++;
this.heldKeys[key] = voice;
var data = {
note: note,
voice: voice,
freq: this.freqCorrection(note.freq),
state: 'on'
};
this.emit({
action: 'date',
data: data
});
}
// handle command key
if (command) {
var data = {};
switch (command) {
case 'octave-up':
this.octave = limitTo(this.octave + 1, -8, 8);
data = { command: command, state: this.octave };
break;
case 'octave-down':
this.octave = limitTo(this.octave - 1, -8, 8);
;
data = { command: command, state: this.octave };
break;
case 'velocity-up':
this.velocity = limitTo(this.velocity + 5, 0, 127);
data = { command: command, state: this.velocity };
break;
case 'velocity-down':
this.velocity = limitTo(this.velocity - 5, 0, 127);
data = { command: command, state: this.velocity };
break;
case 'hold-mode':
this.holdMode = !this.holdMode;
data = { command: command, state: this.holdMode };
break;
}
if (data) {
this.emit({
action: 'settings',
data: data
});
}
}
}
if (event.type === 'keyup') {
// check if key was held and hold-mode is off
var heldKey = this.heldKeys[key];
if (typeof heldKey !== 'undefined' && !this.holdMode) {
delete this.heldKeys[key];
var data = {
note: note,
voice: heldKey,
freq: this.freqCorrection(note.freq),
state: 'off'
};
this.emit({
action: 'gate',
data: data
});
}
// reset voice indexer
if (!Object.keys(this.heldKeys).length) {
this.activeVoices = 0;
}
}
};
/**
* @method deactivate
* @description disable keyboard
* @returns {VirtualKeyBoard} this
*/
VirtualKeyBoard.prototype.deactivate = function () {
window.removeEventListener('keydown', this.keyHandler.bind(this));
window.removeEventListener('keyup', this.keyHandler.bind(this));
return this;
};
/**
* @method activate
* @description enable keyboard
* @returns {VirtualKeyBoard} this
*/
VirtualKeyBoard.prototype.activate = function () {
window.addEventListener('keydown', this.keyHandler.bind(this));
window.addEventListener('keyup', this.keyHandler.bind(this));
return this;
};
return VirtualKeyBoard;
}());
;
module.exports = VirtualKeyBoard;