-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathavgp.html
329 lines (296 loc) · 10.2 KB
/
avgp.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<script src="https://code.jquery.com/jquery-1.10.2.js">
</script>
<title>Accelerometer Demo</title>
<style>
.indicatorDot{
width: 30px;
height: 30px;
background-color: #ffab56;
border-radius: 50%;
position:fixed;
}
</style>
<style>
button {
color: white;
padding: 10px 20px;
font-size: large;
margin-bottom: 10px;
background: #96729E;
cursor: pointer;
border-radius: 4px;
border: 1px solid rgba(0, 0, 0, .15);
box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.25), 0 1px 2px rgba(0, 0, 0, 0.25);
}
button:hover, [role="button"]:hover {
filter: brightness(1.2);
color: white;
}
</style>
<script>
var obj = {gamma: 123, teta: "4"};
var px = 50; // Position x and y
var py = 50;
var vx = 0.0; // Velocity x and y
var vy = 0.0;
var position_sx = 0.0;
var record = [];
var playing_snare = false;
var updateRate = 1/60; // Sensor refresh rate
let faked = true;
if (!window.AudioContext && window.webkitAudioContext) {
window.AudioContext = window.webkitAudioContext;
faked = false;
}
const audioContext = new AudioContext();
let sample;
const SAMPLO = ['kickhardcore.wav','rainstick2.wav'];
let samplerain;
let samplesitar;
let sample2;
let audiobuffer2;
const sources = [];
const volumes = [];
function load(files) {
return new Promise((resolve, reject) => {
const buffers = new Map;
files.forEach(f => {
fetch(f)
.then(response => response.arrayBuffer())
.then(arrayBuffer => {
if (audioContext.decodeAudioData.length === 2) { // Safari
return new Promise(resolve => {
audioContext.decodeAudioData(arrayBuffer, buffer => {
resolve(buffer);
});
});
} else {
return audioContext.decodeAudioData(arrayBuffer);
}
})
.then(audioBuffer => {
buffers.set(f, audioBuffer);
if (buffers.size === files.length) {
resolve(buffers);
}
})
.catch(e => console.error('uff', e));
});
});
}
function play() {
if (!faked) {
faked = true;
const buffer = audioContext.createBuffer(1, 1, audioContext.sampleRate);
sample = audioContext.createBufferSource();
sample.buffer = buffer;
sample.connect(audioContext.destination);
sample.start();
}
load(SAMPLO).then(buffers => {
for (var i = 0; i < SAMPLO.length; i++) {
const volume = audioContext.createGain ? audioContext.createGain() : audioContext.createGainNode();
volume.gain.value = 1;
volume.connect(audioContext.destination);
volumes.push(volume);
const source = audioContext.createBufferSource();
source.buffer = buffers.get(SAMPLO[i]);
source.loop = true;
source.connect(volume);
source.start();
sources.push(source);
}
});
// fetch('kickhardcore.wav')
// .then(response => response.arrayBuffer())
// .then(arrayBuffer => {
// if (audioContext.decodeAudioData.length === 2) { // Safari
// return new Promise(resolve => {
// audioContext.decodeAudioData(arrayBuffer, buffer => {
// resolve(buffer);
// });
// });
// } else {
// return audioContext.decodeAudioData(arrayBuffer);
// }
// })
// .then(audioBuffer => {
// sample = audioContext.createBufferSource();
// sample.buffer = audioBuffer;
// sample.loop = true;
// sample.connect(audioContext.destination);
// sample.start();
// })
// .catch(e => console.error('uff', e));
// fetch('sitar.wav')
// .then(response => response.arrayBuffer())
// .then(arrayBuffer => {
// if (audioContext.decodeAudioData.length === 2) { // Safari
// return new Promise(resolve => {
// audioContext.decodeAudioData(arrayBuffer, buffer => {
// resolve(buffer);
// });
// });
// } else {
// return audioContext.decodeAudioData(arrayBuffer);
// }
// })
// .then(audioBuffer => {
// samplesitar = audioContext.createBufferSource();
// samplesitar.buffer = audioBuffer;
// samplesitar.loop = true;
// samplesitar.connect(audioContext.destination);
// // samplesitar.start();
// })
// .catch(e => console.error('uff', e));
// fetch('rainstick2.wav')
// .then(response => response.arrayBuffer())
// .then(arrayBuffer => {
// if (audioContext.decodeAudioData.length === 2) { // Safari
// return new Promise(resolve => {
// audioContext.decodeAudioData(arrayBuffer, buffer => {
// resolve(buffer);
// });
// });
// } else {
// return audioContext.decodeAudioData(arrayBuffer);
// }
// })
// .then(audioBuffer => {
// samplerain = audioContext.createBufferSource();
// samplerain.buffer = audioBuffer;
// samplerain.loop = true;
// samplerain.connect(audioContext.destination);
// // samplerain.start();
// })
// .catch(e => console.error('uff', e));
// fetch('snare.wav')
// .then(response => response.arrayBuffer())
// .then(arrayBuffer => {
// if (audioContext.decodeAudioData.length === 2) { // Safari
// return new Promise(resolve => {
// audioContext.decodeAudioData(arrayBuffer, buffer => {
// resolve(buffer);
// });
// });
// } else {
// return audioContext.decodeAudioData(arrayBuffer);
// }
// })
// .then(audioBuffer2 => {
// audiobuffer2 = audioBuffer2;
// // sample2 = audioContext.createBufferSource();
// // sample2.buffer = audiobuffer2;
// // sample2.loop = true;
// // sample2.connect(audioContext.destination);
// // sample2.start();
// })
// .catch(e => console.error('uff', e));
}
function stop() {
sample.stop();
// var data = "text/json;charset=utf-8," + encodeURIComponent(JSON.stringify(obj));
// $('<a href="data:' + data + '" download="data.json">download JSON</a>').appendTo('#container');
}
function getAccel(){
DeviceMotionEvent.requestPermission().then(response => {
if (response == 'granted') {
// Add a listener to get smartphone orientation
// in the alpha-beta-gamma axes (units in degrees)
// window.addEventListener('devicemotion', function(event) {
// // position.x = event.acceleration.x;
// // position.y = event.acceleration.y;
// // position.z = event.acceleration.z;
// });
window.addEventListener('devicemotion',(event) => {
// Expose each orientation angle in a more readable way
var position = {};
position.x = event.acceleration.x;
position.y = event.acceleration.y;
position.z = event.acceleration.z;
rotation_degrees = event.rotationRate.alpha;
frontToBack_degrees = event.rotationRate.beta;
leftToRight_degrees = event.rotationRate.gamma;
position.alpha = rotation_degrees;
position.beta = frontToBack_degrees;
position.gamma = leftToRight_degrees;
position_sx = position_sx + position.y;
record.push(position);
// Update velocity according to how tilted the phone is
// Since phones are narrower than they are long, double the increase to the x velocity
vx = vx + position.gamma * updateRate/20;
vy = vy + position.beta * updateRate/20;
// Update position and clip it to bounds
px = px + vx*.5;
if (px > 98 || px < 0){
px = Math.max(0, Math.min(98, px)) // Clip px between 0-98
vx = 0;
}
py = py + vy*.5;
if (py > 98 || py < 0){
py = Math.max(0, Math.min(98, py)) // Clip py between 0-98
vy = 0;
}
dot = document.getElementsByClassName("indicatorDot")[0]
dot.setAttribute('style', "left:" + (px) + "%;" +
"top:" + (py) + "%;");
// sample.detune.value = window.innerWidth/((px)+(window.innerWidth/2));
// sample.detune.value = position.x * 1000;
// sources[0].playbackRate.value = 0.98 + position.gamma/300;
volumes[0].gain.value = Math.min(position.x/9, 1);
// volumes[1].gain.value = 1 - Math.min(position.x/9, 1);
sources[1].playbackRate.value = 1 - Math.abs(position.alpha/200);
// volumes[1].gain.value = 0.5;
// samplerain.playbackRate.value = 1 + position.gamma/300;
// samplesitar.playbackRate.value = 0.6 + position.gamma/800;
// if(position.x > 7 && playing_snare == false){
// sample2 = audioContext.createBufferSource();
// sample2.buffer = audiobuffer2;
// // sample2.loop = true;
// sample2.connect(audioContext.destination);
// sample2.start();
// playing_snare = true;
// samplerain.playbackRate.value = 0;
// }else{
// sample2.stop();
// playing_snare = false;
// samplerain.playbackRate.value = 0.5;
// }
if(position_sx < 2){
volumes[1].gain.value = 0;
}else{
volumes[1].gain.value = 0.8;
}
});
}
});
}
function download(content, fileName, contentType) {
const a = document.createElement("a");
const file = new Blob([content], { type: contentType });
a.href = URL.createObjectURL(file);
a.download = fileName;
a.click();
}
function onDownload(){
download(JSON.stringify(record), "json-file-name.json", "text/plain");
// record.clear();
}
</script>
</head>
<body style="background-color:lightblue;">
<button onclick="onDownload()">Download</button>
<button onclick="play()">▶ play</button>
<button onclick="stop()">STOP!!!!</button>
<button id="accelPermsButton" onclick="getAccel()">Get Accelerometer Permissions</button>
<div id="main">
<div class="indicatorDot" style="left:30%; top:30%;"></div>
</div>
<div id="container"></div>
vers 0.0.13
</body>
</html>