-
Notifications
You must be signed in to change notification settings - Fork 0
/
leap-drums-palm-down.html
730 lines (497 loc) · 17.2 KB
/
leap-drums-palm-down.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
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
<!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="leap-guitar.css">
<link rel="stylesheet" href="bootstrap/css/bootstrap.min.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.sensitivity= 1;
this.easing = 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.Trigger = 4;
this.ModeB = 2;
}
var guitar_on = false;
var guitar_out = true;
var oldX = 0;
var oldY = 0;
var oldZ = 0;
var oldExtra = 0;
var oldTrigger = 0;
//var oldTriggerB = false;
//var TriggerAtail = false;
var movement = 0;
var oldMove = 0;
var settings = new Settings()
var c = 0;
var ctx = 0;
var leapvars =
{
leapX: 50,
leapY: 50,
leapZ:1,
salv: false,
Trigger: false,
ModeB: false
};
var oldPos = new Array();
oldPos.push(leapvars);
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);
f3.add(settings, 'ModeB').step(1);
f3.add(settings, 'sensitivity', 0.25, 8).step(0.25);
f3.add(settings, 'easing', 0, 25).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('Note on/off');
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);
f4.add(settings, 'Trigger').step(1);
f1.open();
f2.open();
f3.open();
c=document.getElementById("mycanvas");
ctx=c.getContext("2d");
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 > 2 && frame.hands.length>0)
{
//leap_enabled = true;
//console.log("two fingers");
leapvars.Trigger = true;
leapvars.ModeB = true;
guitar_on = true;
guitar_out = false;
var leapPosition = region.mapToXY(frame.hands[0].palmPosition, 300, 150);
//frame.fingers[0].tipPosition, 300, 150);
leapvars.leapX = leapPosition[0];
leapvars.leapY = leapPosition[1];
leapvars.leapZ = leapPosition[2];
//console.log ( "X: " + leapvars.leapX + " Y:" + leapvars.leapY + " z: " + leapvars.leapZ );
}
else if (frame.cursorPosition) {
//leap_enabled = true;
leapvars.Trigger = false;
leapvars.ModeB = 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.Trigger = false;
leapvars.ModeB = 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");
midiBridge.disconectAll();
});
var midiAccess,
outputs,
output,
midispeed = 33;
currentNote = 0,
sequenceLength = 100,
sequencePosition = 0,
selectOutput = document.getElementById("outputs"),
messages = document.getElementById("messages");
//btnStart = document.getElementById("start"),
//btnStop = document.getElementById("stop");
var screenfps = 30;
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
velocity = Math.floor(((leapvars.leapX)/250*(settings.maxX-settings.minX)) + settings.minX); //0-300
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
if (leapvars.ModeB == true)
{
movement = 0;
//used to divide total by number of segments
var segmentsum = 0;
for (var i = settings.easing; i > -1; i--)
{
//movement += getDist(leapvars.leapX, leapvars.leapY, leapvars.leapZ, oldPos[i].leapX, oldPos[i].leapY, oldPos[i].leapZ);
movement += getDown(leapvars.leapY, oldPos[i].leapY);
segmentsum += i+1;
}
movement = movement/(segmentsum)*settings.sensitivity; //if easing = 0 it´s /1 ...all good
velocity = Math.floor((movement/150)*(settings.maxZ-settings.minZ)+settings.minZ);
//movement=getDist(leapvars.leapX, leapvars.leapY, leapvars.leapZ, oldPos[settings.easing].leapX, oldPos[settings.easing].leapY, oldPos[settings.easing].leapZ);
//movement = movement/(settings.easing+1); //if easing = 0 it´s /1 ...all good
//console.log("movement: " + movement + " velocity " + velocity + " oldPos length " + oldPos.length + " oldPos[settings.easing].leapZ " + oldPos[settings.easing].leapZ);
//velocity = Math.floor(movement*settings.sensitivity);
}
else
{
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 > settings.maxZ)
{velocity = settings.maxZ}
else if (velocity < settings.minZ)
{ velocity = settings.minZ}
if (velocity != oldZ)
{
if (leapvars.ModeB == true)
{
noteNumber = settings.ModeB;
oldZ = velocity;
}
else
{
noteNumber = settings.Zcont;
oldZ = velocity;
}
midiMessage = midiAccess.createMIDIMessage(midiBridge.CONTROL_CHANGE, 0, noteNumber, velocity, sequencePosition);
output.sendMIDIMessage(midiMessage);
}
//send for Extra
if (guitar_out != oldExtra)
{
if (guitar_out == true)
{
//sendMIDI(settings.Extra, settings.Extra_when_out);
//console.log("extra says guitar out");
midiMessage = midiAccess.createMIDIMessage(midiBridge.NOTE_OFF, 1, 80, 0, sequencePosition);
output.sendMIDIMessage(midiMessage);
//midiBridge.sendMidiEvent(midiBridge.NOTE_ON,1,80,100);
oldExtra = false;
}
else
{
midiMessage = midiAccess.createMIDIMessage(midiBridge.NOTE_ON, 1, 80, 0, sequencePosition);
output.sendMIDIMessage(midiMessage);
//newMIDI(settings.Extra, settings.Extra_when_in);
//console.log("extra says guitar in");
oldExtra = true;
}
}
//send for Trigger
if (leapvars.Trigger != oldTrigger)
{
if (leapvars.Trigger == true)
{
sendMIDI(settings.Trigger, 127);
//console.log("Trigger on");
}
else
{
sendMIDI(settings.Trigger, 0);
//console.log("Trigger off");
}
}
// if(leapvars.TriggerA == false && oldTriggerA == true && TriggerAtail == false)
// {
// TriggerAtail = true;
// var tail = setTimeout(function(){TriggerAtail = false; console.log("tail ended")},300 );
// }
var cloneleapvars =
{
leapX: leapvars.leapX,
leapY: leapvars.leapY,
leapZ:leapvars.leapZ,
};
oldPos.unshift(cloneleapvars); //unshift like push but adds to the start of the array
if (oldPos.lenght > 500)
{
oldPos.splice(150,300);
}//keep the array from getting huge and affecting memmory
}
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));
}
function getDown(y, oldy)
{
return y - oldy;
}
//---------------------DRAW------------------------------
function drawScreen()
{
leap_drawScreen();
var imgX = leapvars.leapX;
var imgY = leapvars.leapY;
var imgZ = (leapvars.leapZ*-1) + 1;
if(guitar_out)
{
ctx.fillStyle='red';
ctx.font="16px Arial";
ctx.fillText('Stick out of sensor range', 50, c.height/2);
}
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()
{
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 drumstick´s head as if it were a hand.
<br>
X, Y and Z coordinates of your stick´s tip are sent in 3 separate MIDI modulation messages that you can then open in Reason, MAX, BFD, 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 for guitarists:</b>
<ul>
<a href="index.html"><li>Basic</li></a>
<a href="leap-guitar-move.html"><li>Movement tracking</li></a>
<a href="halfX.html"><li>Dual pitch bend</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="cont" class="span6">
<canvas id="mycanvas" ></canvas>
<div id ="extrainst" class= "span7">
<p> Show the sensor two or more extended fingers to send an additional MIDI parameter, ideal for switching an effect on or off.</p> <a href= "http://www.youtube.com/watch?v=4ZG9V0MNeh4">Watch a video demo!</a></p>
</div>
</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>