-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbufferSource.js
36 lines (29 loc) · 885 Bytes
/
bufferSource.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
let context = new AudioContext();
// use an XMLHttpRequest (xhr) for getting sound files.
let url = "/TeamSleep.wav";
let xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
// An audio file data is binary, so the 'responseType' of the request is set to
// 'arraybuffer'. An ArrayBuffer is a container for binary data.
xhr.responseType = 'arraybuffer';
// Decode Asynchronously
// Once the (undecoded) audio file data has been received, it can be decoded
// using the AudioContext decodeAudioData() method using JS Promises:
xhr.onload = function() {
context.decodeAudioData(xhr.response)
.then(function(buffer) {
myBuffer = buffer;
});
}
xhr.send();
function play(event) {
source = context.createBufferSource();
source.buffer = event;
source.connect(context.destination);
source.start();
}
function stop() {
if (source) {
source.stop();
}
}