-
Notifications
You must be signed in to change notification settings - Fork 0
/
leap-drums-swing.html
709 lines (499 loc) · 15.4 KB
/
leap-drums-swing.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
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
<!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 src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.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">
<style>
body
{
background: #EEE;
}
#rules
{
border: 5px solid #615521;
border-radius: 15px;
background: #E6E8CC;
position:absolute;
z-index: -1;
margin-right:20px;
margin-top:20px;
padding:10px;
}
#cont
{
width:100%;
}
#mycanvas
{
margin-top: 10px;
margin-left: auto;
margin-right: auto;
display: block;
height:400px;
width:600px;
border:3px #555 solid
}
#clarific
{
border: 3px solid #5195F5;
border-radius: 25px;
background: #DAE5F5;
position:absolute;
margin-right:20px;
margin-top:450px;
padding:10px;
}
#signature
{
float:right;
}
#signature p
{
color:#A6A6A6;
font-size: small;
}
</style>
<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;
this.TriggerA = 4;
this.TriggerB = 5;
}
var guitar_on = false;
var guitar_out = true;
var oldX = 0;
var oldY = 0;
var oldZ = 0;
var oldExtra = 0;
var oldTriggerA = false;
var oldTriggerB = false;
var TriggerAtail = false;
var oldXpos = 0;
var oldYpos = 0;
var oldZpos = 0;
var settings = new Settings()
var leapvars =
{
leapX: 50,
leapY: 50,
leapZ:1,
salv: false,
TriggerA: false,
TriggerB: 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);
f5.add(settings, 'TriggerA').step(1);
f5.add(settings, 'TriggerB').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, 250, -100],
[200, 350, 100]
);
controller.addStep(new Leap.UI.Cursor())
controller.addStep(region.listener({nearThreshold:50}))
controller.loop(function(frame, done) {
//console.log(frame.fingers.length + "fingers");
if(frame.fingers.length > 3)
{
console.log("STOP!!!");
leapvars.TriggerA = true;
leapvars.TriggerB = false;
}
else if(frame.fingers.length > 1 && oldTriggerA == false && TriggerAtail == false)
{
console.log("two fingers");
leapvars.TriggerA = false;
leapvars.TriggerB = true;
}
else if (frame.cursorPosition) {
leap_enabled = true;
leapvars.TriggerA = false;
leapvars.TriggerB = false;
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;
leapvars.TriggerA = false;
leapvars.TriggerB = false;
if(settings.default_vals)
{
leapvars.leapX = ((settings.Xval_when_out-settings.minX)/settings.maxX-settings.minX)*150;
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");
});
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);
};
//new midi note, not control
function newMIDI(port, value)
{
var noteNumber = port;
var velocity = value;
midiMessage = midiAccess.createMIDIMessage(midiBridge.NOTE_ON, 0, noteNumber, velocity, sequencePosition);
output.sendMIDIMessage(midiMessage);
//console.log(port + " sending value " + value);
};
function MIDILoop()
{
//send for X
if (leapvars.leapX<150)
{
velocity=0;
}
else
{
velocity = Math.floor(((leapvars.leapX-150)/150*(settings.maxX-settings.minX)) + settings.minX); //0-300 to 0-127
}
if (velocity != oldX)
{
// if (leapvars.leapZ < 0)
// {
// noteNumber = settings.Xcont;
// var othernum = 5;
// }
// else
// {
// noteNumber = 5;
// var othernum = settings.Xcont;
// //console.log("sending to alternative 5");
// }
noteNumber = settings.Xcont;
var othernum = 5;
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;
midiMessage = midiAccess.createMIDIMessage(midiBridge.CONTROL_CHANGE, 0, othernum, 0, sequencePosition);
output.sendMIDIMessage(midiMessage);
}
//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
//console.log(leapvars.leapZ);
if (leapvars.leapZ<0)
{
var movement=Math.floor(getDist(leapvars.leapX, leapvars.leapY, leapvars.leapZ, oldXpos, oldYpos, oldZpos));
//velocity = Math.floor(movement/150*(settings.maxZ-settings.minZ)) + settings.minZ; //0-300 to 0-127;
velocity = Math.floor(Math.pow(movement,2)/150/150*(settings.maxZ-settings.minZ)) + settings.minZ; //0-300 to 0-127;
}
else
{
//velocity = Math.floor(((leapvars.leapZ-150)/150*(settings.maxZ-settings.minX)) + settings.minX); //0-300 to 0-127;
velocity = Math.floor(((leapvars.leapZ+1)/2*(settings.maxZ-settings.minZ)) + settings.minZ); //0-2 to 0-127
if (velocity< 10)
{velocity = 0;}
}
//console.log(velocity);
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
{
newMIDI(settings.Extra, settings.Extra_when_in);
//console.log("extra says guitar in");
oldExtra = true;
}
}
//send for TriggerA
if (leapvars.TriggerA != oldTriggerA)
{
if (leapvars.TriggerA == true)
{
sendMIDI(settings.TriggerA, 127);
//console.log("TriggerA on");
}
else
{
sendMIDI(settings.TriggerA, 0);
//console.log("TriggerA off");
}
}
//send for TriggerB
if (leapvars.TriggerB != oldTriggerB)
{
if (leapvars.TriggerB == true)
{
sendMIDI(settings.TriggerB, 127);
//console.log("TriggerB on");
}
else
{
sendMIDI(settings.TriggerB, 0);
//console.log("TriggerB off");
}
}
// if(leapvars.TriggerA == false && oldTriggerA == true && TriggerAtail == false)
// {
// TriggerAtail = true;
// var tail = setTimeout(function(){TriggerAtail = false; console.log("tail ended")},300 );
// }
oldXpos = leapvars.leapX;
oldYpos = leapvars.leapY;
oldZpos = leapvars.leapZ;
oldTriggerA = leapvars.TriggerA;
oldTriggerB = leapvars.TriggerB;
}
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/>";
});
});
});
function getDist(x, y, z, oldx, oldy, oldz)
{
var dx = Math.abs(x - oldx);
var dy = Math.abs(y - oldy);
var dz = Math.abs(z - oldz);
//return Math.sqrt(Math.pow(dx,2)+Math.pow(dy,2));
var plaindist = Math.sqrt(Math.pow(dx,2)+Math.pow(dy,2));
return Math.sqrt(Math.pow(plaindist,2)+Math.pow(dz,2));
}
//---------------------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('Sitck out of sensor range', 10, 130);
}
if (imgZ>1)
{
var bval = "FF";
}
else
{
var bval = "00";
}
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(bval,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 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>
<div id="signature">
<p>App by: <a href="http://nearnshaw.github.com" target="blank">Nicolas Earnshaw</a></p>
</div>
</div>
<div id="cont" class="span6">
<canvas id="mycanvas" ></canvas>
</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>
</body>
</html>