-
Notifications
You must be signed in to change notification settings - Fork 0
/
drumbass.html
345 lines (292 loc) · 9.87 KB
/
drumbass.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
<!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>
const volume_master = [];
let volumeg = 100;
function BufferLoader(context, urlList, callback) {
this.context = context;
this.urlList = urlList;
this.onload = callback;
this.bufferList = new Array();
this.loadCount = 0;
}
BufferLoader.prototype.loadBuffer = function(url, index) {
// Load buffer asynchronously
var request = new XMLHttpRequest();
request.open("GET", url, true);
request.responseType = "arraybuffer";
var loader = this;
request.onload = function() {
// Asynchronously decode the audio file data in request.response
loader.context.decodeAudioData(
request.response,
function(buffer) {
if (!buffer) {
alert('error decoding file data: ' + url);
return;
}
loader.bufferList[index] = buffer;
if (++loader.loadCount == loader.urlList.length)
loader.onload(loader.bufferList);
},
function(error) {
console.error('decodeAudioData error', error);
}
);
}
request.onerror = function() {
alert('BufferLoader: XHR error');
}
request.send();
}
BufferLoader.prototype.load = function() {
for (var i = 0; i < this.urlList.length; ++i)
this.loadBuffer(this.urlList[i], i);
}
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
var playit = false;
var context;
var bufferLoader;
const bufferLists = [];
function init_sound() {
// Fix up prefixing
window.AudioContext = window.AudioContext || window.webkitAudioContext;
context = new AudioContext();
bufferLoader = new BufferLoader(
context,
[
'drumbass.wav',
'drumbass.wav',
],
finishedLoading
);
bufferLoader.load();
}
function finishedLoading(bufferList) {
// Create two sources and play them both together.
var source1 = context.createBufferSource();
var source2 = context.createBufferSource();
// source1.buffer = bufferList[0];
source2.buffer = bufferList[1];
bufferLists.push(bufferList[0]);
bufferLists.push(bufferList[1]);
// source1.connect(context.destination);
source2.connect(context.destination);
// source1.start();
source2.start();
}
var RhythmSample = {
};
var volume_index = 0;
// Gain node needs to be mutated by volume control.
RhythmSample.gainNode = null;
RhythmSample.play = function() {
function playSound(buffer, time) {
if (!context.createGain)
context.createGain = context.createGainNode;
this.gainNode = context.createGain();
var source = context.createBufferSource();
source.buffer = buffer;
// Connect source to a gain node
source.connect(this.gainNode);
// Connect gain node to destination
this.gainNode.connect(context.destination);
// Start playback in a loop
// source.loop = true;
if (!source.start)
source.start = source.noteOn;
// this.gainNode.gain.value = volumeg;
// volumeg = volumeg / 2;
source.start(time);
this.source = source;
// volume_master.push(this.gainNode);
// volume_index = volume_index + 1;
}
var kick = bufferLists[0];
var snare = bufferLists[1];
// var hihat = BUFFERS.hihat;
// We'll start playing the rhythm 100 milliseconds from "now"
var startTime = context.currentTime;
var tempo = 80; // BPM (beats per minute)
var eighthNoteTime = (60 / tempo) / 2;
// Play 2 bars of the following:
for (var bar = 0; bar < 1; bar++) {
var time = startTime + bar * 8 * eighthNoteTime;
// Play the bass (kick) drum on beats 1, 5
playSound(kick, 0);
// playSound(kick, time + 4 * eighthNoteTime);
// Play the snare drum on beats 3, 7
// playSound(snare, time + 2 * eighthNoteTime);
// playSound(snare, time + 6 * eighthNoteTime);
// Play the hi-hat every eighthh note.
for (var i = 0; i < 8; ++i) {
// playSound(hihat, time + i * eighthNoteTime);
}
}
};
RhythmSample.changeVolume = function(element) {
var volume = element.value;
var fraction = parseInt(element.value) / parseInt(element.max);
// Let's use an x*x curve (x-squared) since simple linear (x) does not
// sound as good.
volumeg = fraction * fraction;
};
init_sound();
function stop(){
context = new AudioContext();
const buffer = context.createBuffer(1, 1, 100000);
var source = context.createBufferSource();
source.buffer = buffer;
source.connect(context.destination);
source.start();
}
var playin = false;
function handleMotionEvent(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;
}
if(event.acceleration.x > 4 && playin == false){
RhythmSample.play();
playin = true;
}
if(event.acceleration.x < -2 && playin == true){
playin = false;
}
// if(playit == true){
// RhythmSample.changeVolume(py/1000);
// }
dot = document.getElementsByClassName("indicatorDot")[0]
dot.setAttribute('style', "left:" + (px) + "%;" +
"top:" + (py) + "%;");
}
function getMotion(){
playit = true;
// feature detect
if (typeof DeviceMotionEvent.requestPermission === 'function') {
DeviceMotionEvent.requestPermission()
.then(permissionState => {
if (permissionState === 'granted') {
alert("Accelerometer IOS");
window.addEventListener("devicemotion", handleMotionEvent, true);
}
})
.catch(console.error);
} else {
// handle regular non iOS 13+ devices
alert("Accelerometer no IOS");
window.addEventListener("devicemotion", handleMotionEvent, true);
}
}
function getAccel(){
DeviceMotionEvent.requestPermission().then(response => {
if (response == 'granted') {
alert("Accelerometer activated");
// 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;
// px = Math.max(0, Math.min(98, event.acceleration.x);
// py = Math.max(0, Math.min(98, event.acceleration.y);
// dot = document.getElementsByClassName("indicatorDot")[0]
// dot.setAttribute('style', "left:" + (px) + "%;" +
// "top:" + (py) + "%;");
});
} else {
alert("Accelerometer not activated");
}
});
}
// var timin = 1;
// function myTimer() {
// RhythmSample.changeVolume(1/timin);
// timin = timin + 1;
// }
// var myVar = setInterval(myTimer, 200);
</script>
</head>
<body style="background-color:lightblue;">
<button onclick="onDownload()">Download</button>
<button onclick="RhythmSample.play()">▶ play</button>
<button onclick="init_sound()">STOP!!!!</button>
<button id="accelPermsButton" onclick="getMotion()">Get Accelerometer Permissions</button>
<div id="main">
<div class="indicatorDot" style="left:30%; top:30%;"></div>
</div>
<div id="container"></div>
vers 0.0.59
<input type="range" min="0" max="100" value="100" oninput="RhythmSample.changeVolume(this);">
</body>
</html>