forked from hoch/spiral-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrummer.html
62 lines (52 loc) · 1.91 KB
/
drummer.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
<!doctype html>
<html>
<head>
<title>Drummer | Spiral Boilerplate</title>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, minimum-scale=1.0, initial-scale=1, user-scalable=yes">
<link rel="shortcut icon" href="assets/favicon.ico">
<script src="bower_components/webcomponentsjs/webcomponents-lite.min.js"></script>
<link rel="import" href="assets/spiral-bp-header.html">
</head>
<body unresolved>
<h1>Drummer</h1>
<spiral-knob id="k-speed" label="Speed" value="1.0" min-value="0.01" max-value="2.0"></spiral-knob>
<script>
window.addEventListener('WebComponentsReady', function () {
var kSpeed = document.querySelector('#k-speed');
var context = new AudioContext();
var files = [
{ name: 'snare', url: 'sounds/sd-001.mp3' },
{ name: 'stage', url: 'sounds/960-BriteStage.mp3' }
];
var myBuffers;
var reverb = context.createConvolver();
reverb.to(context.DAC);
// TODO: Use AudioParam to shape the envelope.
// TODO: Change the playback speed of the sample with a knob.
// TODO: Use the keyboard to trigger the sample.
function setBuffers(buffers) {
myBuffers = buffers;
reverb.buffer = buffers.get('stage');
}
function playSnare() {
var source = context.createBufferSource();
source.buffer = myBuffers.get('snare');
source.playbackRate.value = kSpeed.getValue();
source.to(reverb);
source.start();
}
function listenKeybaord() {
window.addEventListener('keydown', function (event) {
if (event.keyCode === 13)
playSnare();
});
}
context.loadAudioFiles(files, function() {})
.then(setBuffers, null)
.then(listenKeybaord, null);
});
</script>
</body>
</html>