-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
187 lines (173 loc) · 5.6 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
<html lang="en">
<head>
<title>Motion Instrument</title>
<meta charset="UTF-8" />
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1 " />
<!-- Font Awesome -->
<link
rel="stylesheet"
href="https://use.fontawesome.com/releases/v5.11.2/css/all.css"
/>
<!-- Google Fonts-->
<link rel="preconnect" href="https://fonts.googleapis.com" />
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
<link
href="https://fonts.googleapis.com/css2?family=Audiowide&family=Water+Brush&display=swap"
rel="stylesheet"
/>
<!-- Custom styles -->
<link rel="stylesheet" href="css/style.css" />
<link rel="stylesheet" href="./app.css" />
</head>
<body>
<div class="video-wrapper">
<video playsinline autoplay muted loop>
<source
class="h-100"
src="https://mdbootstrap.com/img/video/Lines.mp4"
type="video/mp4"
/>
</video>
<div class="header">
<h1>Airstrument</h1>
<p>Revolutionize the way you make music</p>
<button class="mute" data-muted="false">
<span></span>
<span></span>
<span></span>
<span></span>
<span>Mute</span>
</button>
<button class="violin">
<span></span>
<span></span>
<span></span>
<span></span>
<span>Electronic</span>
</button>
<button class="drums">
<span></span>
<span></span>
<span></span>
<span></span>
<span>Drum</span>
</button>
</div>
</div>
<!-- <audio id="mp32" crossorigin="anonymous" src="drum.mp3" autoplay></audio> -->
<!-- <audio id="mp31" crossorigin="anonymous" src="cymbal.mp3" autoplay></audio> -->
<!-- <button class="mute" data-muted="false">Mute</button>
<button class="violin">Violin</button> -->
<script>
const ws = new WebSocket("ws://localhost:8080");
ws.addEventListener("open", () => {
console.log("web client: handshake! We are connected");
});
//---init for violin
let isAppInit = false;
var sinea = null;
var sineb = null;
var sinec = null;
var volume = null;
var delay;
var delayFeedback;
var audioCtx;
var x_val = 0;
var y_val = 0;
var inst = 0;
var count = 0;
const instruments = ["sine", "square", "sawtooth", "triangle"];
const notes = [261.63, 293.66, 329.63, 349.23, 392, 440, 493.88, 523.25, 554.27, 587.33, 622.25, 659.25, 698.46, 739.99, 783.99, 830.61, 880, 932];
window.addEventListener("click", init);
const ii = document.querySelector(".violin");
const jj = document.querySelector(".drums");
instrument_type = 0;
ii.onclick = function () {
volume.gain.value = 0.2;
instrument_type = 0;
};
jj.onclick = function () {
volume.gain.value = 0;
instrument_type = 1;
};
function init() {
if (isAppInit) {
return;
}
audioCtx = new (window.AudioContext || window.webkitAudioContext)();
volume = audioCtx.createGain();
sinea = audioCtx.createOscillator();
sinea.frequency.value = 440;
sinea.type = "sawtooth";
sinea.start();
sinea.connect(volume);
volume.connect(audioCtx.destination);
volume.gain.value = 0.2;
console.log("got here!");
isAppInit = true;
}
const mute = document.querySelector(".mute");
mute.onclick = function () {
if (mute.getAttribute("data-muted") === "false") {
volume.disconnect(audioCtx.destination);
mute.setAttribute("data-muted", "true");
} else {
volume.connect(audioCtx.destination);
mute.setAttribute("data-muted", "false");
}
};
test = new Audio("drum.mp3");
ws.addEventListener("message", function (e) {
const msg = e.data;
count += 1;
//console.log(e.data);
const nums_str = msg.split("\t");
x_val = parseFloat(nums_str[0]);
y_val = parseFloat(nums_str[1]);
angle_y = y_val;
z_accel = parseFloat(nums_str[4]);
if (instrument_type == 0) {
sinea.frequency.value = notes[Math.floor(x_val / 20) % 18];
volume.gain.value = y_val / 360;
if (z_accel > 2 && count % 10 == 0) {
inst += 1;
sinea.type = instruments[inst % 4];
console.log(instruments[inst % 4]);
}
} else {
console.log(angle_y);
if (angle_y > 140) {
// TO-DO: check value based on band design
console.log("b");
activated = 1;
} else if (angle_y < 40) {
console.log("c");
activated = 2;
} else {
//autoplay policy
console.log("d");
// if (audioContext.state === 'suspended') {
// audioContext.resume();
// }
if (activated == 1) {
console.log("hear the drum!");
test.play();
//drumElement.play();
activated = 0;
} else if (activated == 2) {
console.log("hear the kick");
console.log(`[From Main]: hello`);
test1 = new Audio("cymbal.mp3");
test1.play();
//snareElement.play()
activated = 0;
} else {
console.log("activated is 0");
}
}
}
});
</script>
</body>
</html>