-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
216 lines (201 loc) · 7.6 KB
/
index.html
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
<html>
<head>
<title>LeapMIDI Prototype</title>
<script src="lib/leap-1.1.1.js"></script>
<script src="lib/webmidi.min.js"></script>
<script>
function normalize(val, min, max) { return (val - min) / (max - min); }
function toCC(val) { return Math.floor(126 * val); }
function sendControlChange(cc, value) { }
function normalizeCC(value, isRoll = false) {
const min = -3;
const max = isRoll ? 3 : 2;
return Math.floor(normalize(value, min, max) * 127);
// return Math.abs( // FIXME: Not needed once below behaves correctly
// Math.floor(64 + 64 * (isRoll ? value / 3 : value))
// );
}
var paused = false;
window.onkeypress = function(e) {
if (e.charCode == 32) {
if (paused == false) {
paused = true;
} else {
paused = false;
}
}
};
// Setup WebMidi
const midiName = "Midi Through";
var midiOutput = null;
WebMidi.enable(function (err) {
if (err) {
console.log("WebMidi could not be enabled.", err);
} else {
console.log("WebMidi enabled!", WebMidi.inputs, WebMidi.outputs);
WebMidi.outputs.forEach((output) => {
if (output.name.includes(midiName)) {
midiOutput = output;
} else {
console.log(`${midiName} not in ${output.name}:`, output);
}
});
console.log("Init MidiThrough:", midiOutput);
}
});
const ccMap = { // Here goes the track-specific mapping
djf: 49,
crush: 53,
modIndex: 54,
gain: {
master: 77,
kick: 78,
snare: 79,
drum: 80,
bass: 81,
keys: 82,
}
};
var controller = new Leap.Controller();
controller.loop(function(frame) {
try {
latestFrame = frame;
if (paused) {
document.getElementById('pause').innerHTML = "<strong>PAUSED</strong>";
return;
}
var str = "";
var count=0;
for (var i in frame.handsMap) {
count++;
const cStr = `<br/>[${count}] `;
var hand = frame.handsMap[i];
str += "<p>" +
"<strong>Roll:</strong> " + hand.roll() +
"<br/><strong>Pitch:</strong> " + hand.pitch() +
"<br/><strong>Yaw:</strong> " + hand.yaw() +
"</p>";
const roll = normalizeCC(hand.roll(), true);
const pitch = normalizeCC(hand.pitch());
const yaw = normalizeCC(hand.yaw());
console.log(count, "RollCC:", roll,
"PitchCC:", pitch,
"YawCC:", yaw);
str += "<p>" +
cStr + "<strong>RollCC:</strong> " + roll +
cStr + "<strong>PitchCC:</strong> " + pitch +
cStr + "<strong>YawCC:</strong> " + yaw +
"</p>";
// MIDI Control: send interpreted movements as CC
// E.G. Use roll to control DJF
// midiOutput.sendControlChange(ccMap.djf, roll);
// Use finger distal position to control individual instruments
hand.fingers.forEach((finger) => {
if (finger.extended) {
console.log(finger.type);
console.log(finger.dipPosition);
const x = finger.dipPosition[0];
const y = finger.dipPosition[1];
const z = finger.dipPosition[2];
str += "<p>" +
`<strong>X:</strong> ${x}` +
`<strong>Y:</strong> ${y}` +
`<strong>Z:</strong> ${z}` +
"</p>";
const xCC = toCC(normalize(x, -300, 300));
const yCC = toCC(normalize(y, 0, 450));
const zCC = toCC(normalize(z, -300, 300));
str += "<p>" +
`<strong>XCC:</strong> ${xCC}` +
`<strong>YCC:</strong> ${yCC}` +
`<strong>ZCC:</strong> ${zCC}` +
"</p>";
switch (hand.type) {
case "left": {
switch (finger.type) {
case 0:
// Thumb: control DJF
midiOutput.sendControlChange(ccMap.djf, 0.25 + yCC / 2);
break;
case 1:
// Index: control rhythmics
midiOutput.sendControlChange(ccMap.gain.kick, yCC);
midiOutput.sendControlChange(ccMap.gain.snare, yCC);
midiOutput.sendControlChange(ccMap.gain.drum, yCC);
break;
case 2:
// Majeur: bassline
midiOutput.sendControlChange(ccMap.gain.bass, yCC);
break;
case 3:
// Annulaire: auriculaire
midiOutput.sendControlChange(ccMap.gain.keys, yCC);
break;
}
}
case "right": {
switch (finger.type) {
case 0:
// Thumb: control master
midiOutput.sendControlChange(ccMap.gain.master, yCC);
break;
case 1:
// Index: control rhythmics
midiOutput.sendControlChange(ccMap.gain.kick, yCC);
midiOutput.sendControlChange(ccMap.gain.snare, yCC);
midiOutput.sendControlChange(ccMap.gain.drum, yCC);
break;
case 2:
// Majeur: bassline
midiOutput.sendControlChange(ccMap.gain.bass, yCC);
break;
case 3:
// Annulaire: clavier
midiOutput.sendControlChange(ccMap.gain.keys, yCC);
break;
}
}
}
}
});
}
document.getElementById('out').innerHTML = str;
} catch (e) {
console.error(e);
}
});
controller.on('ready', function() {
console.log("ready");
});
controller.on('connect', function() {
console.log("connect");
});
controller.on('disconnect', function() {
console.log("disconnect");
});
controller.on('focus', function() {
console.log("focus");
});
controller.on('blur', function() {
console.log("blur");
});
controller.on('deviceConnected', function() {
console.log("deviceConnected");
});
controller.on('deviceDisconnected', function() {
console.log("deviceDisconnected");
});
</script>
</head>
<body>
<br />
<h1>LeapMIDI</h1>
<h2>Prototype: TidalCycles/SuperCollider MIDI Input</h2>
<p>This is a demonstration of LeapMIDI: A setup to control MIDI instruments
using a LEAPMotion tool.</p>
<p> Here I'm controlling a TidalCycles track via Midi CC messages,
changing both individual instruments and global effects at the move of my fingers.
<div id="pause"></div>
<div id="out"></div>
</body>
</html>