-
Notifications
You must be signed in to change notification settings - Fork 7
/
audio.js
38 lines (29 loc) · 888 Bytes
/
audio.js
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
function Audio() {
var audioCtx = new (window.AudioContext || window.webkitAudioContext)();
var audioBuffer;
var loadSounds = function() {
var request = new XMLHttpRequest();
request.open('GET', '/sounds/domino_collision.mp3', true);
request.responseType = 'arraybuffer';
request.onload = function() {
var audioData = request.response;
audioCtx.decodeAudioData(audioData, function(buffer) {
audioBuffer = buffer;
},
function(e){"Error with decoding audio data" + e.err});
};
request.send();
}
var makeSource = function() {
var source = audioCtx.createBufferSource();
source.buffer = audioBuffer;
source.connect(audioCtx.destination);
return source;
};
this.playCollisionSound = function() {
var source = makeSource();
source.start();
};
loadSounds();
}
module.exports = new Audio();