-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path02_hello_audio.js
34 lines (32 loc) · 1.04 KB
/
02_hello_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
var canvas = document.getElementById("toy-canvas");
var startAudioContextButton = document.getElementById("start-audio-context-button");
var audioTags = [
...document.querySelectorAll('audio')
];
var canvasContext = canvas.getContext("2d");
var audioContext;
try {
audioContext = new AudioContext();
};
catch(e) {
alert('Web Audio API is not supported in this browser');
};
var gainNode = new GainNode(audioContext);
gainNode.connect(audioContext.destination);
var audioBuffers = audioTags.map(function(audioTag, index){
var audioBuffer = audioContext.createMediaElementSource(audioTag);
var bufferGainNode = new GainNode(audioContext);
bufferGainNode.connect(gainNode);
audioBuffer.connect(bufferGainNode);
return audioBuffer;
});
function init() {
// check if context is in suspended state (autoplay policy)
if (audioContext.state === 'suspended') {
audioContext.resume();
}
audioTags.forEach(function(audioTag){
audioTag.play();
})
}
startAudioContextButton.addEventListener('click', init, false);