-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebcam_theremin.html
139 lines (119 loc) · 3.59 KB
/
webcam_theremin.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>ml5.js Handpose p5 Webcam Theremin</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.7.0/p5.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.10.2/addons/p5.sound.min.js"></script>
<script src="https://unpkg.com/[email protected]/dist/ml5.js"></script>
<style>
body, html {
margin: 0;
padding: 0;
width: 100%;
height: 100%;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
#title {
margin-bottom: 20px;
font-size: 25px;
color: #333;
text-align: center;
font-family: 'Helvetica', sans-serif;
font-weight: bold;
}
#H2 {
margin-bottom: 20px;
font-size: 18px;
color: #333;
text-align: center;
font-family: 'Helvetica', sans-serif;
}
#reminder {
margin-bottom: 20px;
font-size: 14px;
color: #666;
text-align: center;
font-family: 'Helvetica', sans-serif;
}
#sketch-container {
display: flex;
align-items: center;
justify-content: center;
}
</style>
</head>
<body>
<div id="title">Webcam Hand Theremin Instrument</div>
<div id="H2">by Fay Cai (<a href="https://www.faycai.com" target="_blank">www.faycai.com</a>)</div>
<div id="reminder">Note: Please raise one of your hands to play the instrument</div>
<div id="sketch-container"></div>
<script>
let handpose;
let video;
let hands = [];
let notes = [67, 66, 64, 62, 60, 59, 57, 55];
let letters = ["G", "A", "B", "C", "D", "E", "F#", "G"];
let osc;
let amp;
function preload() {
handpose = ml5.handpose();
}
function setup() {
let canvas = createCanvas(640, 480);
canvas.parent('sketch-container');
osc = new p5.Oscillator();
video = createCapture(VIDEO);
video.size(width, height);
video.hide();
handpose.detectStart(video, gotHands);
osc = new p5.Oscillator();
osc.start();
osc.amp(amp, 0.1);
}
function gotHands(results) {
hands = results;
}
function draw() {
translate(width, 0);
scale(-1, 1);
image(video, 0, 0, width, height);
for (let i = 0; i < hands.length; i++) {
let hand = hands[i];
for (let j = 0; j < hand.keypoints.length; j++) {
let keypoint = hand.keypoints[j];
fill(255, 0, 0);
noStroke();
circle(keypoint.x, keypoint.y, 10);
let n = floor(map(keypoint.x, 0, width, 0, 8));
osc.freq(midiToFreq(notes[n]));
let w = width / 8;
for (let i = 0; i < 8; i++) {
if (keypoint.x > i * w && keypoint.x < i * w + w) {
fill(100, 20);
} else {
noFill();
}
strokeWeight(1);
stroke(random(150, 200));
rect(i * w, 0, w, height);
fill(255);
strokeWeight(1);
textSize(30);
push();
translate(width, 0);
scale(-1, 1);
text(letters[i], i * w + 30, 80);
pop();
}
}
}
}
</script>
</body>
</html>