/
keys.js
117 lines (108 loc) · 2.93 KB
/
keys.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
if (Meteor.isClient) {
Meteor.startup(function() {
steps = /Firefox[\/\s](\d+\.\d+)/.test(navigator.userAgent) ? _keys : keys;
Meteor.subscribe('scales');
Meteor.subscribe('keys');
});
function message(x, y, z){
JZZ().openMidiOut().send([x, y, z])
};
function exe(some, x, y, z){
some(x, y, z);
};
Session.setDefault('current_scale', 0);
Session.setDefault('playing', []);
Session.setDefault('Keys', []);
keyAllowed = {};
document.onkeydown = function(e){
if (keyAllowed [e.which] === false) return;
keyAllowed [e.which] = false;
var e = window.event || e;
if (e.keyCode == 38){
Object.keys(steps[Session.get('current_scale')]).map(function(value, index){
steps[Session.get('current_scale')][value] ++;
_Keys = [];
});
highlight();
exe(message, 0xb0, 120, 0);
}
else if (e.keyCode == 40) {
Object.keys(steps[Session.get('current_scale')]).map(function(value, index){
steps[Session.get('current_scale')][value] --;
_Keys = [];
})
highlight();
exe(message, 0xb0, 120, 0);
}
else Session.set('Keys', []);
var key = steps[Session.get('current_scale')][e.keyCode];
if(key){
notes = Session.get('playing');
notes.push(key);
Session.set('playing', notes);
exe(message, 0x90, key, 111);
}
};
document.onkeyup = function(e){
keyAllowed [e.which] = true;
var e = window.event || e;
var key = steps[Session.get('current_scale')][e.keyCode];
if(key){
notes.splice(notes.indexOf(key),1);
Session.set('playing', notes);
exe(message, 0x80, key, 0);
}
};
var highlight = function() {
$.each(steps[Session.get('current_scale')], function(k, v) {
_Keys.push(v);
});
Session.set('Keys', _Keys);
};
Template.hello.helpers({
scales: function () {
return Scales.find({});
},
playing: function () {
return Session.get('playing');
},
color: function () {
var color = 'border-left: 3px solid #'+(Math.random() * 0xFFFFFF << 0).toString(16);
return color;
}
});
Template.hello.events({
'change select': function (e) {
var current_scale = parseInt($("select").val()) - 1;
Session.set("current_scale", current_scale);
_Keys = [];
highlight();
$('select').blur();
}
});
Template.piano.helpers({
ivory: function () {
return Keys.find({notes: 'white'});
},
ebony: function () {
return Keys.find({notes: {$ne: 'white'}});
},
break: function () {
return this.id === '|';
},
isSelected: function () {
var notes = Session.get('playing');
if( notes.indexOf(this.id) > -1){
return true;
}
return false;
},
isCol: function () {
var notes = Session.get('Keys');
if( notes.indexOf(this.id) > -1){
return true;
}
return false;
}
});
}