-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
1242 lines (1080 loc) · 40.4 KB
/
app.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
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
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// app.js - Refined Gesture Detection without Calibration Controls
// =========================
// DOM Element Selections
// =========================
const videoElement = document.getElementById("video");
const canvasElement = document.getElementById("canvas");
const canvasCtx = canvasElement.getContext("2d");
const leftHandStatus = document.getElementById("left-hand-status");
const rightHandStatus = document.getElementById("right-hand-status");
const leftHandState = document.getElementById("left-hand-state");
const rightHandState = document.getElementById("right-hand-state");
const modulationStatus = document.getElementById("modulation-status");
const modulationValueDisplay = document.getElementById("modulation-value");
const fpsCounter = document.getElementById("fps-counter"); // FPS Counter Element
// DOM elements for distance controls
const minDistanceSlider = document.getElementById("min-distance");
const maxDistanceSlider = document.getElementById("max-distance");
const minDistanceNumber = document.getElementById("min-distance-number");
const maxDistanceNumber = document.getElementById("max-distance-number");
const overlapThresholdSlider = document.getElementById("overlap-threshold");
const overlapThresholdNumber = document.getElementById(
"overlap-threshold-number"
);
// MIDI Status Element
const midiStatus = document.getElementById("midi-status");
// MIDI Output Port Dropdown and Disconnect Button
const midiPortsDropdown = document.getElementById("midi-ports");
const disconnectMIDIBtn = document.getElementById("disconnect-midi");
// MIDI Access Overlay Elements
const midiOverlay = document.getElementById("midi-overlay");
const allowMIDIBtn = document.getElementById("allow-midi");
// Checkbox Element for Reversing Modulation Mapping
const reverseModulationCheckbox = document.getElementById("reverse-modulation");
// Gesture Progress Bar Element
const gestureProgressBar = document.getElementById("gesture-progress-bar");
// Camera Selection Dropdown
const cameraPortsDropdown = document.getElementById("camera-ports");
// =========================
// Hand Selection Elements
// =========================
// Radio buttons for hand selection
const gestureHandRadioButtons = document.getElementsByName("gesture-hand");
// Variable to store the selected hand ('left' or 'right')
let selectedHand = "left"; // Default as per HTML's checked value
// Add event listeners to update selectedHand when radio buttons change
gestureHandRadioButtons.forEach((radio) => {
radio.addEventListener("change", (event) => {
if (event.target.checked) {
selectedHand = event.target.value;
console.log(`Selected hand for gesture controls: ${selectedHand}`);
// Optionally, reset modulation wheel when hand selection changes
modulationWheelOn = false;
modulationStatus.textContent = `Modulation Wheel: OFF`;
modulationStatus.classList.remove("on");
modulationStatus.classList.add("off");
modulationValueDisplay.textContent = `Modulation Value: 0`;
}
});
});
// =========================
// MediaPipe Hands Initialization
// =========================
const hands = new Hands({
locateFile: (file) => {
return `https://cdn.jsdelivr.net/npm/@mediapipe/hands/${file}`;
},
});
hands.setOptions({
maxNumHands: 2, // Track both hands
modelComplexity: 1, // Increased complexity for better accuracy
minDetectionConfidence: 0.7, // Increased for more reliable detection
minTrackingConfidence: 0.7,
});
hands.onResults(onResults);
// =========================
// Gesture Control Variables
// =========================
let modulationWheelOn = false; // State of modulation wheel
let lastGestureTime = 0; // Timestamp of last gesture to enforce cooldown
const gestureCooldown = 1000; // 1 second cooldown in milliseconds
// Configurable Hand Distance Variables
let minDistance = parseFloat(minDistanceSlider.value); // Initialize from slider
let maxDistance = parseFloat(maxDistanceSlider.value); // Initialize from slider
// Overlap Threshold Variable
let overlapThreshold = parseFloat(overlapThresholdSlider.value); // Initialize from slider
// MIDI Access and Output
let midiAccess = null;
let midiOutput = null;
// Implement a smoothing mechanism
let modulationValuesQueue = [];
const smoothingFactor = 5; // Number of frames to average
// Gesture Strictness Levels (1-10)
let openHandStrictness = 5; // Default value
let pointingUpStrictness = 5; // Default value
// Gesture State Tracking
let isPointingUpActive = false;
// Gesture Confirmation Counters
const gestureConfirmationThreshold = 5; // Number of consecutive frames gesture must be held
let pointingUpCounter = 0;
// FPS Calculation Variables
let frameCount = 0;
let fps = 0;
let lastTime = performance.now(); // Ensure this is declared only once
// Current MediaStream
let currentStream = null;
// Flag to prevent multiple loops
let processing = false;
// =========================
// MIDI Throttling Variables
// =========================
let lastMIDITime = 0;
const midiThrottleInterval = 10; // in milliseconds (~100 FPS)
// Flag for debounce
let lastToggleTime = 0;
const toggleDebounceTime = 1000; // 1 second
// =========================
// Canvas Initialization
// =========================
function initializeCanvas() {
const scale = window.devicePixelRatio || 1;
canvasElement.width = 960 * scale; // Internal canvas width
canvasElement.height = 540 * scale; // Internal canvas height
canvasElement.style.width = "960px"; // CSS width
canvasElement.style.height = "540px"; // CSS height
canvasCtx.scale(scale, scale);
console.log("Canvas initialized with scaling:", scale);
}
// Listen for video metadata loaded to set canvas size
videoElement.addEventListener("loadedmetadata", () => {
console.log("Video metadata loaded.");
initializeCanvas();
// Verify actual video settings
const videoTrack = currentStream?.getVideoTracks()[0];
if (videoTrack) {
const settings = videoTrack.getSettings();
console.log(
"Actual Video Dimensions:",
settings.width,
"x",
settings.height
);
console.log("Actual Frame Rate:", settings.frameRate);
}
});
// =========================
// Video Processing
// =========================
async function processVideoFrame() {
if (processing) return; // Prevent multiple instances
processing = true;
const sendFrame = async () => {
if (videoElement.readyState >= 2) {
// HAVE_CURRENT_DATA
try {
await hands.send({ image: videoElement });
} catch (error) {
console.error("Error sending frame to MediaPipe Hands:", error);
}
}
requestAnimationFrame(sendFrame);
};
sendFrame();
}
// =========================
// Camera Handling
// =========================
async function populateCameraPorts() {
try {
const devices = await navigator.mediaDevices.enumerateDevices();
const videoDevices = devices.filter(
(device) => device.kind === "videoinput"
);
// Clear existing options
cameraPortsDropdown.innerHTML =
'<option value="">-- Select Camera --</option>';
videoDevices.forEach((device, index) => {
const option = document.createElement("option");
option.value = device.deviceId;
option.textContent = device.label || `Camera ${index + 1}`;
cameraPortsDropdown.appendChild(option);
});
// Attempt to select the internal camera by default
const internalKeywords = ["internal", "built-in", "default", "integrated"];
const internalDevice = videoDevices.find((device) =>
internalKeywords.some((keyword) =>
device.label.toLowerCase().includes(keyword)
)
);
if (internalDevice) {
cameraPortsDropdown.value = internalDevice.deviceId;
console.log(`Auto-selecting internal camera: ${internalDevice.label}`);
startCameraStream(internalDevice.deviceId);
} else if (videoDevices.length > 0) {
// If no internal camera found, select the first available
cameraPortsDropdown.value = videoDevices[0].deviceId;
console.log(
`Auto-selecting first available camera: ${videoDevices[0].label}`
);
startCameraStream(videoDevices[0].deviceId);
} else {
console.warn("No video input devices found.");
cameraPortsDropdown.innerHTML =
'<option value="">-- No Cameras Available --</option>';
}
} catch (error) {
console.error("Error enumerating devices:", error);
cameraPortsDropdown.innerHTML =
'<option value="">-- Error Loading Cameras --</option>';
}
}
// Function to start the camera stream with the selected device ID
async function startCameraStream(deviceId) {
try {
// Stop existing stream if any
if (currentStream) {
currentStream.getTracks().forEach((track) => track.stop());
console.log("Stopped previous camera stream.");
}
// Get the new stream
const constraints = {
video: {
deviceId: { exact: deviceId },
width: { ideal: 960 },
height: { ideal: 540 },
frameRate: { ideal: 60, max: 60 },
},
audio: false,
};
const stream = await navigator.mediaDevices.getUserMedia(constraints);
videoElement.srcObject = stream;
currentStream = stream;
console.log(`Started camera stream with device ID: ${deviceId}`);
// Ensure video starts playing
await videoElement.play();
// Start processing frames
processVideoFrame();
} catch (error) {
console.error(
`Failed to start camera stream with device ID: ${deviceId}`,
error
);
alert(`Failed to start camera stream: ${error.message}`);
}
}
// Event Listener for Camera Ports Dropdown Change
cameraPortsDropdown.addEventListener("change", (event) => {
const selectedDeviceId = event.target.value;
if (selectedDeviceId === "") {
console.log("No camera selected.");
if (currentStream) {
currentStream.getTracks().forEach((track) => track.stop());
currentStream = null;
videoElement.srcObject = null;
}
return;
}
console.log(`Camera selected: Device ID = ${selectedDeviceId}`);
startCameraStream(selectedDeviceId);
});
// Initialize Camera Ports on Page Load
window.addEventListener("load", () => {
if (navigator.mediaDevices && navigator.mediaDevices.enumerateDevices) {
populateCameraPorts();
// Listen for device changes to update camera list dynamically
navigator.mediaDevices.addEventListener("devicechange", () => {
console.log("Device change detected. Updating camera list.");
populateCameraPorts();
});
} else {
console.warn("Media Devices API not supported.");
cameraPortsDropdown.innerHTML =
'<option value="">-- Media Devices API Not Supported --</option>';
}
// MIDI Access Initialization
if (navigator.requestMIDIAccess) {
navigator
.requestMIDIAccess()
.then(onMIDISuccess, onMIDIFailure)
.catch(() => {
// If automatic request fails, show the overlay
midiOverlay.style.display = "flex";
});
} else {
alert("Web MIDI API is not supported in this browser.");
}
// Synchronize Control Groups
synchronizeControls();
});
// =========================
// MIDI Handling
// =========================
// Function to handle MIDI Access Success
function onMIDISuccess(access) {
midiAccess = access;
console.log("MIDI Access Obtained:", midiAccess);
populateMIDIPorts();
updateMIDIStatus();
// Listen for MIDI connection changes
midiAccess.onstatechange = onstatechange;
}
// Function to handle MIDI Access Failure
function onMIDIFailure() {
alert("Could not access your MIDI devices.");
}
// Function to handle MIDI State Changes
function onstatechange(event) {
console.log(`MIDI port ${event.port.name} ${event.port.state}`);
populateMIDIPorts();
updateMIDIStatus();
}
// Function to list and populate MIDI Output Ports in Dropdown
function populateMIDIPorts() {
if (!midiAccess) return;
const outputs = Array.from(midiAccess.outputs.values());
console.log("Available MIDI Outputs:", outputs);
// Store currently selected port ID
const currentPortId = midiPortsDropdown.value;
// Clear existing options except the first placeholder
midiPortsDropdown.innerHTML =
'<option value="">-- Select MIDI Port --</option>';
outputs.forEach((output) => {
const option = document.createElement("option");
option.value = output.id;
option.textContent = output.name;
midiPortsDropdown.appendChild(option);
});
// Check if current port is still available
if (currentPortId && midiAccess.outputs.get(currentPortId)) {
midiPortsDropdown.value = currentPortId;
midiOutput = midiAccess.outputs.get(currentPortId);
console.log("Re-selected MIDI Output:", midiOutput.name);
} else {
midiOutput = null;
midiPortsDropdown.value = "";
console.log("MIDI Output Disconnected or Changed.");
}
// Update MIDI Status Indicator
if (outputs.length === 0) {
midiStatus.textContent = "MIDI Status: No Outputs Available";
midiStatus.classList.remove("connected", "on");
midiStatus.classList.add("disconnected", "off");
disconnectMIDIBtn.disabled = true;
} else if (!midiOutput) {
midiStatus.textContent = "MIDI Status: Disconnected";
midiStatus.classList.remove("connected", "on");
midiStatus.classList.add("disconnected", "off");
disconnectMIDIBtn.disabled = true;
} else {
midiStatus.textContent = "MIDI Status: Connected";
midiStatus.classList.remove("disconnected", "off");
midiStatus.classList.add("connected", "on");
disconnectMIDIBtn.disabled = false;
}
}
// Function to update MIDI Status Indicator
function updateMIDIStatus() {
if (midiOutput) {
midiStatus.textContent = "MIDI Status: Connected";
midiStatus.classList.remove("disconnected", "off");
midiStatus.classList.add("connected", "on");
disconnectMIDIBtn.disabled = false;
} else {
midiStatus.textContent = "MIDI Status: Disconnected";
midiStatus.classList.remove("connected", "on");
midiStatus.classList.add("disconnected", "off");
disconnectMIDIBtn.disabled = true;
}
}
// Event Listener for Allow MIDI Access Button in Overlay
allowMIDIBtn.addEventListener("click", () => {
navigator
.requestMIDIAccess()
.then(onMIDISuccess, onMIDIFailure)
.then(() => {
// Hide the overlay upon successful MIDI access
midiOverlay.style.display = "none";
})
.catch(() => {
alert("Failed to obtain MIDI access.");
});
});
// Event Listener for MIDI Ports Dropdown Change
midiPortsDropdown.addEventListener("change", (event) => {
const selectedPortId = event.target.value;
if (selectedPortId === "") {
midiOutput = null;
updateMIDIStatus();
console.log("No MIDI Output Selected.");
return;
}
midiOutput = midiAccess.outputs.get(selectedPortId);
if (midiOutput) {
console.log("Selected MIDI Output:", midiOutput.name);
updateMIDIStatus();
} else {
console.log("Selected MIDI Output not found.");
midiStatus.textContent = "MIDI Status: Disconnected";
midiStatus.classList.remove("connected", "on");
midiStatus.classList.add("disconnected", "off");
}
});
// Event Listener for Disconnect MIDI Button
disconnectMIDIBtn.addEventListener("click", () => {
if (midiOutput) {
midiOutput = null;
midiPortsDropdown.value = "";
updateMIDIStatus();
console.log("MIDI Output Disconnected.");
} else {
console.log("No MIDI Output to Disconnect.");
}
});
// =========================
// MIDI Message Sending
// =========================
/**
* Sends a MIDI message with throttling and validation.
* @param {number} command - MIDI command byte.
* @param {number} controller - MIDI controller number.
* @param {number} value - MIDI controller value.
*/
function sendMIDIMessage(command, controller, value) {
const currentTime = performance.now();
if (currentTime - lastMIDITime >= midiThrottleInterval) {
// Clamp the MIDI values to valid ranges
const clampedController = clamp(controller, 0, 127);
const clampedValue = clamp(value, 0, 127);
try {
if (midiOutput) {
// Ensure value is a valid number
if (isNaN(clampedValue)) {
console.warn(`Attempted to send invalid MIDI value: ${clampedValue}`);
return;
}
midiOutput.send([command, clampedController, clampedValue]);
lastMIDITime = currentTime;
// Removed excessive logging for performance
} else {
console.warn(
"Attempted to send MIDI message, but no MIDI output is connected."
);
}
} catch (error) {
console.error("Failed to send MIDI message:", error);
}
}
}
/**
* Clamps a value between a minimum and maximum.
* @param {number} value - The value to clamp.
* @param {number} min - The minimum allowable value.
* @param {number} max - The maximum allowable value.
* @returns {number} - The clamped value.
*/
function clamp(value, min, max) {
if (typeof value !== "number" || isNaN(value)) {
return min;
}
return Math.max(min, Math.min(max, value));
}
// =========================
// Gesture Detection
// =========================
/**
* Calculates the angle between three points.
* @param {Object} p1 - First point with x and y properties.
* @param {Object} p2 - Second point with x and y properties (vertex).
* @param {Object} p3 - Third point with x and y properties.
* @returns {number} - The angle in degrees.
*/
function calculateAngle(p1, p2, p3) {
const radians =
Math.atan2(p3.y - p2.y, p3.x - p2.x) - Math.atan2(p1.y - p2.y, p1.x - p2.x);
let angle = Math.abs(radians * (180.0 / Math.PI));
if (angle > 180.0) {
angle = 360 - angle;
}
return angle;
}
/**
* Detects if the hand is making a pointing up gesture.
* @param {Array} landmarks - Hand landmarks.
* @param {number} strictness - Strictness level (1-10) for gesture sensitivity.
* @returns {boolean} - Whether the pointing gesture is detected.
*/
function isPointingUp(landmarks, strictness) {
if (!landmarks || landmarks.length < 21) {
console.warn("Invalid hand landmarks detected.");
return false;
}
// Check if the index finger is extended
const indexExtended = isFingerExtended(
landmarks,
5, // INDEX_FINGER_MCP
6, // INDEX_FINGER_PIP
7, // INDEX_FINGER_DIP
8, // INDEX_FINGER_TIP
strictness
);
// Check that all other fingers (middle, ring, pinky) are tightly curled
const middleCurled = isFingerTightlyCurled(
landmarks,
9,
10,
11,
12, // MIDDLE_FINGER_MCP, PIP, DIP, TIP
strictness
);
const ringCurled = isFingerTightlyCurled(
landmarks,
13,
14,
15,
16, // RING_FINGER_MCP, PIP, DIP, TIP
strictness
);
const pinkyCurled = isFingerTightlyCurled(
landmarks,
17,
18,
19,
20, // PINKY_FINGER_MCP, PIP, DIP, TIP
strictness
);
// Ensure the index finger tip is above the wrist (natural pointing condition)
const wrist = landmarks[0];
const indexTip = landmarks[8];
const indexAboveWrist = indexTip.y < wrist.y;
// Check thumb condition - Ensure the thumb is curled (overlapping/curling)
const thumbCurled = isThumbCurled(landmarks, strictness);
// Pointing up gesture requires:
// - Index finger extended
// - Middle, ring, and pinky fingers tightly curled
// - Thumb curled (overlapping/curling)
// - Index finger tip above wrist
const isPointingUpGesture =
indexExtended &&
thumbCurled &&
middleCurled &&
ringCurled &&
pinkyCurled &&
indexAboveWrist;
console.log(
`Gesture Detection: Index Extended=${indexExtended}, Thumb Curled=${thumbCurled}, Middle Tightly Curled=${middleCurled}, Ring Tightly Curled=${ringCurled}, Pinky Tightly Curled=${pinkyCurled}, Index Above Wrist=${indexAboveWrist} => Gesture=${isPointingUpGesture}`
);
return isPointingUpGesture;
}
/**
* Determines if a specific finger is extended based on landmark positions and strictness.
* @param {Array} handLandmarks - Array of hand landmarks.
* @param {number} mcp - MCP joint index of the finger.
* @param {number} pip - PIP joint index of the finger.
* @param {number} dip - DIP joint index of the finger.
* @param {number} tip - TIP joint index of the finger.
* @param {number} strictness - Strictness level (1-10).
* @returns {boolean} - True if the finger is extended, else false.
*/
function isFingerExtended(handLandmarks, mcp, pip, dip, tip, strictness) {
// In image coordinates, y increases downward
// Finger is extended if tip.y < pip.y
// Strictness adjusts the required y difference
const yDifference = handLandmarks[pip].y - handLandmarks[tip].y;
// Define a threshold based on strictness (higher strictness requires larger difference)
const baseThreshold = 0.015; // Increased base threshold for stricter extension
const delta = 0.01; // Adjusted delta for smoother scaling
const threshold = baseThreshold + delta * (strictness - 1);
// Finger is extended if the y difference exceeds the threshold
const isExtended = yDifference > threshold;
// Log the finger extension status for debugging
console.log(
`Finger [MCP:${mcp} PIP:${pip} DIP:${dip} TIP:${tip}] yDifference: ${yDifference.toFixed(
3
)} | Threshold: ${threshold.toFixed(3)} | Extended: ${isExtended}`
);
return isExtended;
}
/**
* Determines if a specific finger is tightly curled based on landmark positions and strictness.
* Ensures that the fingertip is approximately ⅔ down to the palm base.
* @param {Array} handLandmarks - Array of hand landmarks.
* @param {number} mcp - MCP joint index of the finger.
* @param {number} pip - PIP joint index of the finger.
* @param {number} dip - DIP joint index of the finger.
* @param {number} tip - TIP joint index of the finger.
* @param {number} strictness - Strictness level (1-10).
* @returns {boolean} - True if the finger is tightly curled with fingertip near desired position, else false.
*/
function isFingerTightlyCurled(handLandmarks, mcp, pip, dip, tip, strictness) {
// In image coordinates, y increases downward
// Finger is tightly curled if tip.y is approximately ⅔ down to the palm
// AND the fingertip is at the desired distance from the wrist
// Calculate the expected y-coordinate for ⅔ down to the palm
const wrist = handLandmarks[0];
const mcpLandmark = handLandmarks[mcp];
const expectedTipY = wrist.y + (mcpLandmark.y - wrist.y) * (2 / 3);
// Allow a margin based on strictness
const margin = 0.02 * (11 - strictness); // Less margin as strictness increases
const actualTipY = handLandmarks[tip].y;
const isApproachingTarget =
actualTipY > expectedTipY - margin && actualTipY < expectedTipY + margin;
// Additionally, ensure fingertip is not too close to the wrist
const fingertip = handLandmarks[tip];
const distanceToWrist = Math.sqrt(
Math.pow(fingertip.x - wrist.x, 2) + Math.pow(fingertip.y - wrist.y, 2)
);
// Define a distance threshold to prevent fingertips from being too close
const distanceThreshold = 0.15; // Adjust as needed
const isAtProperDistance = distanceToWrist > distanceThreshold;
// Log the finger curled status for debugging
console.log(
`Finger [MCP:${mcp} PIP:${pip} DIP:${dip} TIP:${tip}] Actual Tip Y: ${actualTipY.toFixed(
3
)} | Expected Tip Y: ${expectedTipY.toFixed(
3
)} | Within Margin: ${isApproachingTarget}`
);
console.log(
`Finger [TIP:${tip}] Distance to Wrist: ${distanceToWrist.toFixed(
3
)} | Threshold: ${distanceThreshold.toFixed(
3
)} | At Proper Distance: ${isAtProperDistance}`
);
// Finger is considered tightly curled if it's approaching the target position and at the proper distance
return isApproachingTarget && isAtProperDistance;
}
/**
* Determines if the thumb is curled based on its position relative to the palm center.
* The thumb should be in the middle of the palm when curled.
* @param {Array} handLandmarks - Array of hand landmarks.
* @param {number} strictness - Strictness level (1-10).
* @returns {boolean} - True if thumb is curled in the middle of the palm, else false.
*/
function isThumbCurled(handLandmarks, strictness) {
// Retrieve necessary landmarks
const thumbTIP = handLandmarks[4]; // Thumb TIP
const palmCenter = {
x: (handLandmarks[0].x + handLandmarks[9].x) / 2, // Average of wrist and MCP of middle finger
y: (handLandmarks[0].y + handLandmarks[9].y) / 2,
};
// Calculate Euclidean distance between thumb TIP and palm center
const distanceTipToCenter = Math.sqrt(
Math.pow(thumbTIP.x - palmCenter.x, 2) +
Math.pow(thumbTIP.y - palmCenter.y, 2)
);
// Define a distance threshold based on strictness
// Higher strictness requires the thumb TIP to be closer to the palm center
const baseThreshold = 0.15; // Increased base distance threshold for less strictness
const delta = 0.005; // Increment per strictness level
const threshold = baseThreshold - delta * (strictness - 1);
const finalThreshold = Math.max(threshold, 0.1); // Prevent threshold from being too low
console.log(
`Thumb TIP to Palm Center Distance: ${distanceTipToCenter.toFixed(
3
)} | Threshold: ${finalThreshold.toFixed(3)} | Curled: ${
distanceTipToCenter < finalThreshold
}`
);
// Thumb is considered curled if the distance is below the threshold
return distanceTipToCenter < finalThreshold;
}
/**
* Checks if the hand is wide open by verifying all fingers are extended.
* @param {Array} handLandmarks - Array of hand landmarks.
* @returns {boolean} - True if all fingers are extended, else false.
*/
function isHandWideOpen(handLandmarks) {
let allFingersExtended = true;
// Check all fingers including thumb
for (let finger of [
{ mcp: 1, pip: 2, dip: 3, tip: 4 }, // Thumb
{ mcp: 5, pip: 6, dip: 7, tip: 8 }, // Index
{ mcp: 9, pip: 10, dip: 11, tip: 12 }, // Middle
{ mcp: 13, pip: 14, dip: 15, tip: 16 }, // Ring
{ mcp: 17, pip: 18, dip: 19, tip: 20 }, // Pinky
]) {
if (
!isFingerExtended(
handLandmarks,
finger.mcp,
finger.pip,
finger.dip,
finger.tip,
openHandStrictness
)
) {
allFingersExtended = false;
break;
}
}
return allFingersExtended;
}
// =========================
// Modulation Wheel Control
// =========================
/**
* Toggles the modulation wheel on or off.
*/
function toggleModulationWheel() {
const currentTime = Date.now();
if (currentTime - lastToggleTime < toggleDebounceTime) {
// Prevent toggling if within debounce period
console.log("Toggle action is debounced.");
return;
}
modulationWheelOn = !modulationWheelOn;
console.log(`Modulation Wheel Toggled: ${modulationWheelOn ? "On" : "Off"}`);
// Update Modulation Status Text and Classes
modulationStatus.textContent = `Modulation Wheel: ${
modulationWheelOn ? "ON" : "OFF"
}`;
// Toggle 'on' and 'off' classes to align with CSS
modulationStatus.classList.toggle("on", modulationWheelOn);
modulationStatus.classList.toggle("off", !modulationWheelOn);
if (!modulationWheelOn) {
// Freeze the current modulation value
const lastValue =
modulationValuesQueue.length > 0
? modulationValuesQueue[modulationValuesQueue.length - 1]
: 0;
modulationValueDisplay.textContent = `Modulation Value: ${lastValue}`;
console.log("Modulation Wheel turned OFF. Value frozen.");
}
// Update the last toggle time
lastToggleTime = currentTime;
}
// =========================
// Gesture Detection and Handling
// =========================
/**
* Handles the results from MediaPipe Hands.
* @param {Object} results - The results object from MediaPipe Hands.
*/
function onResults(results) {
try {
// Increment frame count
frameCount++;
// Current time
const currentTime = performance.now();
// Calculate elapsed time in seconds
const elapsedTime = (currentTime - lastTime) / 1000;
// Update FPS every second
if (elapsedTime >= 1) {
fps = frameCount / elapsedTime;
fpsCounter.textContent = `FPS: ${fps.toFixed(1)}`;
// Reset counters
frameCount = 0;
lastTime = currentTime;
}
// Reset transformations and clear the canvas
canvasCtx.resetTransform();
canvasCtx.clearRect(0, 0, canvasElement.width, canvasElement.height);
// Draw the video frame covering the entire canvas
canvasCtx.drawImage(
results.image,
0,
0,
canvasElement.width,
canvasElement.height
);
// Reset hand detection statuses
leftHandDetected = false;
rightHandDetected = false;
leftHandOpen = false;
rightHandOpen = false;
if (results.multiHandLandmarks && results.multiHandedness) {
results.multiHandedness.forEach((hand, index) => {
const landmarks = results.multiHandLandmarks[index];
const handedness = hand.label; // "Left" or "Right"
// **Invert the handedness labels to account for CSS mirroring**
const isRightHand = handedness === "Left"; // Inverted due to mirroring
const isLeftHand = handedness === "Right"; // Inverted due to mirroring
// Log detected hands
console.log(
`Detected Hand: ${
isLeftHand ? "Left" : isRightHand ? "Right" : "Unknown"
}`
);
if (isRightHand) {
rightHandDetected = true;
rightHandStatus.textContent = "Right Hand: Detected";
}
if (isLeftHand) {
leftHandDetected = true;
leftHandStatus.textContent = "Left Hand: Detected";
}
// Draw hand landmarks with enhanced visual quality
drawConnectors(canvasCtx, landmarks, HAND_CONNECTIONS, {
color: isRightHand ? "#00FF00" : "#FF0000",
lineWidth: 3,
});
drawLandmarks(canvasCtx, landmarks, {
color: isRightHand ? "#00FF00" : "#FF0000",
lineWidth: 2,
radius: 3,
});
// **Add Visual Indicators on Hand Landmarks**
// [Existing code for drawing landmarks and annotations]
// Update Hand State (Open/Closed)
const handOpen = isHandWideOpen(landmarks);
if (isRightHand) {
rightHandState.textContent = `Right Hand State: ${
handOpen ? "Open" : "Closed"
}`;
rightHandState.classList.toggle("active", handOpen);
rightHandOpen = handOpen;
}
if (isLeftHand) {
leftHandState.textContent = `Left Hand State: ${
handOpen ? "Open" : "Closed"
}`;
leftHandState.classList.toggle("active", handOpen);
leftHandOpen = handOpen;
}
// **Process Gestures Only for Selected Hand**
let isSelectedHand = false;
if (selectedHand === "left" && isLeftHand) {
isSelectedHand = true;
} else if (selectedHand === "right" && isRightHand) {
isSelectedHand = true;
}
if (isSelectedHand) {
// Initialize or retrieve gesture state for the selected hand
if (typeof gestureState === "undefined") {
gestureState = {
isPointingUpActive: false,
pointingUpCounter: 0,
};
}
// **Pointing Up Gesture Detection**
if (isPointingUp(landmarks, pointingUpStrictness)) {
gestureState.pointingUpCounter++;
// Update the progress bar
const progressPercentage = Math.min(
(gestureState.pointingUpCounter / gestureConfirmationThreshold) *
100,
100
);
gestureProgressBar.style.width = `${progressPercentage}%`;
if (
gestureState.pointingUpCounter >= gestureConfirmationThreshold &&
!gestureState.isPointingUpActive
) {
toggleModulationWheel(); // Activate or deactivate modulation wheel
gestureProgressBar.style.width = `0%`; // Reset progress bar
gestureState.pointingUpCounter = 0; // Reset counter
gestureState.isPointingUpActive = true; // Prevent immediate re-toggling
}
} else {
gestureState.pointingUpCounter = 0; // Reset counter if gesture not detected
gestureState.isPointingUpActive = false; // Reset gesture active state
gestureProgressBar.style.width = `0%`; // Reset progress bar
}
// **Handle MIDI Controls Only for Selected Hand**
if (modulationWheelOn && handOpen && midiOutput) {
const modulationValue = calculateModulationValue(landmarks);
// Update Modulation Value Display
modulationValueDisplay.textContent = `Modulation Value: ${modulationValue}`;
// Send MIDI Control Change for Modulation Wheel (CC1) on Channel 1
sendMIDIMessage(0xb0, 1, modulationValue); // 0xB0 = CC1 on channel 1
}
}
// No action needed when modulationWheelOn is false and hand is open