-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
517 lines (346 loc) · 11.9 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
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
<!doctype html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<script type="text/javascript" src="leap.min.js"></script>
<script type="text/javascript" src="jquery/jquery.js"></script>
<script type="text/javascript" src="js/dat.Gui.min.js"></script>
<script type="text/javascript" src="MIDIBridge/lib/Midibridge.js"></script>
<script type="text/javascript" src="MIDIBridge/lib/Midiselector.js"></script>
<script type="text/javascript" src="js/setup-connections-direct.js"></script>
<link rel="stylesheet" href="bootstrap/css/bootstrap.min.css">
<link rel="stylesheet" href="leap-guitar.css">
<script type="text/javascript">
//-------------------------GUI stuff-----------------
var Settings = function()
{
this.maxX = 127;
this.minX = 0;
this.maxY = 127;
this.minY = 0;
this.maxZ = 127;
this.minZ = 0;
this.MIDIchan = 1;
this.Xcont = 0;
this.Ycont = 1;
this.Zcont = 2;
this.default_vals = false;
this.Xval_when_out = 0;
this.Yval_when_out = 0;
this.Zval_when_out = 0;
this.Extra = 3;
this.Extra_when_out = 0;
this.Extra_when_in = 127;
}
var guitar_on = false;
var guitar_out = true;
var oldX = 0;
var oldY = 0;
var oldZ = 0;
var oldExtra = 0;
var settings = new Settings()
var leapvars =
{
leapX: 50,
leapY: 50,
leapZ:100,
salv: false
};
window.onload = function()
{
var gui = new dat.GUI();
var f1 = gui.addFolder('X');
f1.add(settings, 'Xcont').step(1);
f1.add(settings, 'minX', 0, 126).step(1);
f1.add(settings, 'maxX', 1, 127).step(1);
var f2 = gui.addFolder('Y');
f2.add(settings, 'Ycont').step(1);
f2.add(settings, 'minY', 0, 126).step(1);
f2.add(settings, 'maxY', 1, 127).step(1);
var f3 = gui.addFolder('Z');
f3.add(settings, 'Zcont').step(1);
f3.add(settings, 'minZ', 0, 126).step(1);
f3.add(settings, 'maxZ', 1, 127).step(1);
var f4 = gui.addFolder('Defaults when out of sensor range');
f4.add(settings,'default_vals');
f4.add(settings, 'Xval_when_out',0,127).step(1);
f4.add(settings, 'Yval_when_out',0,127).step(1);
f4.add(settings, 'Zval_when_out',0,127).step(1);
var f5 = gui.addFolder('Extra on/off message');
f5.add(settings, 'Extra').step(1);
f5.add(settings, 'Extra_when_out',0,127).step(1);
f5.add(settings, 'Extra_when_in',0,127).step(1);
f1.open();
f2.open();
f3.open();
startLeap();
};
//-------------------------leap stuff-------------------
function startLeap()
{
var controller = new Leap.Controller({enableGestures: true});
var region = new Leap.UI.Region(
[-50, 200, -100],
[200, 300, 100]
);
controller.addStep(new Leap.UI.Cursor())
controller.addStep(region.listener({nearThreshold:50}))
controller.loop(function(frame, done) {
if (frame.cursorPosition) {
leap_enabled = true;
var leapPosition = region.mapToXY(
frame.cursorPosition, 300, 150);
leapvars.leapX = leapPosition[0];
leapvars.leapY = leapPosition[1];
leapvars.leapZ = leapPosition[2]*-1;
guitar_on = true;
guitar_out = false;
}
else
{
guitar_out = true;
if(settings.default_vals)
{
leapvars.leapX = ((settings.Xval_when_out-settings.minX)/settings.maxX-settings.minX)*300;
leapvars.leapY = ((settings.Yval_when_out-settings.minY)/settings.maxY-settings.minY)*150;
leapvars.leapZ = ((settings.Zval_when_out-settings.minZ)/settings.maxZ-settings.minZ)*2;
guitar_on = false;
}
}
done();
});
controller.connection.on('disconnect', function() {
leap_enabled = false;
});
};
//----------------------------MIDI stuff -----------
window.addEventListener('load', function() {
$("#start").click(function(){
if(!output){
messages.innerHTML = "<b>Please select a MIDI output first!</b>";
return;
}
messages.innerHTML = "";
sequencePosition = 0;
runningInterval = setInterval(MIDILoop, Math.round(midispeed));
document.getElementById("stop").classList.add("btn-primary");
document.getElementById("start").classList.remove("btn-primary");
});
$("#stop").click(function()
{
currentNote = sequenceLength;
clearInterval(runningInterval);
document.getElementById("start").classList.add("btn-primary");
document.getElementById("stop").classList.remove("btn-primary");
midiBridge.disconectAll();
});
var midiAccess,
outputs,
output,
midispeed = 100;
currentNote = 0,
sequenceLength = 100,
sequencePosition = 0,
selectOutput = document.getElementById("outputs"),
messages = document.getElementById("messages");
//btnStart = document.getElementById("start"),
//btnStop = document.getElementById("stop");
var screenfps = 60;
drawInterval = setInterval(drawScreen, Math.round(1000/screenfps));
function sendMIDI(port, value)
{
var noteNumber = port;
var velocity = value;
midiMessage = midiAccess.createMIDIMessage(midiBridge.CONTROL_CHANGE, 0, noteNumber, velocity, sequencePosition);
output.sendMIDIMessage(midiMessage);
//console.log(port + " sending value " + value);
};
function MIDILoop()
{
//send for X
velocity = Math.floor((leapvars.leapX/300*(settings.maxX-settings.minX)) + settings.minX); //0-300 to 0-127
if (velocity != oldX)
{
noteNumber = settings.Xcont;
if (velocity > settings.maxX)
{velocity = settings.maxX}
else if (velocity < settings.minX)
{ velocity = settings.minX}
midiMessage = midiAccess.createMIDIMessage(midiBridge.CONTROL_CHANGE, 0, noteNumber, velocity, sequencePosition);
output.sendMIDIMessage(midiMessage);
//console.log("X sending " + velocity + " to port " + settings.Xcont );
oldX = velocity;
}
//send for Y
velocity = Math.floor((leapvars.leapY/150*(settings.maxY-settings.minY)) + settings.minY); //0-150 to 0-127
if (velocity != oldY)
{
noteNumber = settings.Ycont;
if (velocity > settings.maxY)
{velocity = settings.maxY}
else if (velocity < settings.minY)
{ velocity = settings.minY}
midiMessage = midiAccess.createMIDIMessage(midiBridge.CONTROL_CHANGE, 0, noteNumber, velocity, sequencePosition);
output.sendMIDIMessage(midiMessage);
//console.log("Y sending " + velocity + " to port " + settings.Ycont );
oldY = velocity;
}
//send for Z
velocity = Math.floor(((leapvars.leapZ+1)/2*(settings.maxZ-settings.minZ)) + settings.minZ); //0-2 to 0-127
if (velocity != oldZ)
{
noteNumber = settings.Zcont;
if (velocity > settings.maxZ)
{velocity = settings.maxZ}
else if (velocity < settings.minZ)
{ velocity = settings.minZ}
midiMessage = midiAccess.createMIDIMessage(midiBridge.CONTROL_CHANGE, 0, noteNumber, velocity, sequencePosition);
output.sendMIDIMessage(midiMessage);
//console.log("Z sending " + velocity + " to port " + settings.Zcont );
oldZ = velocity;
}
//send for Extra
if (guitar_out != oldExtra)
{
if (guitar_out == true)
{
sendMIDI(settings.Extra, settings.Extra_when_out);
//console.log("extra says guitar out");
oldExtra = false;
}
else
{
sendMIDI(settings.Extra, settings.Extra_when_in);
//console.log("extra says guitar in");
oldExtra = true;
}
}
}
midiBridge.init(function(_midiAccess){
midiAccess = _midiAccess;
outputs = midiAccess.enumerateOutputs();
//create dropdown menu for MIDI outputs and add an event listener to the change event
midiBridge.createMIDIDeviceSelector(selectOutput,outputs,"ouput",function(deviceId){
if(output){
output.close();
}
output = midiAccess.getOutput(outputs[deviceId]);
messages.innerHTML = "connected to " + output.deviceName + "<br/>";
});
});
});
//---------------------DRAW------------------------------
function drawScreen()
{
leap_drawScreen();
var imgX = leapvars.leapX;
var imgY = leapvars.leapY;
var imgZ = (leapvars.leapZ*-1) + 1;
var c=document.getElementById("mycanvas");
var ctx=c.getContext("2d");
if(guitar_out)
{
ctx.fillStyle='red';
ctx.font="16px Arial";
ctx.fillText('Guitar out of sensor range', 50, c.height/2);
}
if (guitar_on)
{
var rval = Math.floor(Math.abs(leapvars.leapZ*leapvars.leapZ)*100);
if (rval > 99)
{
rval = 99
}
else if (rval < 10)
{
rval = "00"
}
var hsh = "#";
var rd = hsh.concat("FF",rval,rval);
ctx.fillStyle=rd;
//console.log(rd);
}
else
{
ctx.fillStyle='#00CCFF';
}
ctx.strokeStyle = "#000";
ctx.beginPath();
ctx.arc(imgX,imgY,12*(imgZ+0.1),0,2*Math.PI);
ctx.closePath();
ctx.fill();
//console.log("X:" + leapvars.leapX + "Y: " + leapvars.leapY);
//console.log("Z: " + imgZ + " X:" + imgX + " Y: " + imgY);
}
function leap_drawScreen()
{
var c=document.getElementById("mycanvas");
var ctx=c.getContext("2d");
ctx.clearRect(0,0,c.width,c.height);
ctx.fillStyle="#FF3399";
ctx.beginPath();
ctx.arc(400,200,5,0,2*Math.PI);
ctx.moveTo(0,200);
ctx.lineTo(800,200);
ctx.moveTo(400,0);
ctx.lineTo(400,400);
ctx.stroke();
ctx.closePath();
}
</script>
</head>
<body>
<div class="span3" id="cont1">
<div id="logobox" class="span3">
<img id="logo" src="logo.png">
</div>
<div id="rules" class="span3">
<h4> Press <b>Start</b> and this app will send a stream of MIDI messages to MIDI Port 1. </h4>
<p>
The <a href="http://www.leapmotion.com" target="_blank">Leap Motion Sensor</a> can detect your guitar's head as if it were a hand. <br>
X, Y and Z coordinates of your guitar are sent in 3 separate MIDI modulation messages that you can then open in Reason, MAX, GuitarRig, FL Studio or whatever you prefer and map to any variable you want.
<br>
<i>(port1, Xcont, Leap X Coordinate)</i>
</p>
<p>
<b>App Variations:</b>
<ul>
<a href="leap-guitar-move.html"><li>Movement tracking</li></a>
<a href="halfX.html"><li>Dual pitch bend</li></a>
<a href="leap-drums.html"><li>For drummers</li></a>
</ul>
</p>
<div id="signature">
<p>App by: <a href="http://nearnshaw.github.com" target="blank">Nicolas Earnshaw</a></p>
</div>
</div>
<div id="clarific" class="span3">
<div id="messages"><b>Select a MIDI output</b></div>
<br>
<!-- dropdown menu for selecting a MIDI output -->
<select id="outputs"></select>
<!-- button that starts the sequence -->
<a id="start" class="btn btn-primary btn-medium pull-left">
Start
</a>
<!-- button that stops the sequence -->
<a id="stop" class="btn btn-medium pull-left" style="margin-left:7px">
Stop
</a>
</div>
</div>
<div id="cont2" class="span8">
<canvas id="mycanvas" ></canvas>
</div>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-46638598-1']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
</body>
</html>