-
Notifications
You must be signed in to change notification settings - Fork 14
/
audio.js
35 lines (31 loc) · 913 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
/* global jsfxr */
var audioCtx, audioDest, audio, play; // eslint-disable-line
var AudioContext = window.AudioContext || window.webkitAudioContext;
if (AudioContext) {
audioCtx = new AudioContext();
audioDest = audioCtx.createDynamicsCompressor();
var gain = audioCtx.createGain();
gain.gain.value = 0.1;
audioDest.connect(gain);
gain.connect(audioCtx.destination);
audio = function (conf) { // eslint-disable-line no-unused-vars
var o = [];
jsfxr(conf, audioCtx, function (buf) {
o.push(buf);
});
return o;
};
play = function (o) { // eslint-disable-line no-unused-vars
if (!o[0]) return;
var source = audioCtx.createBufferSource();
source.buffer = o[0];
source.start(0);
source.connect(audioDest);
setTimeout(function () {
source.disconnect(audioDest);
}, o[0].duration * 1000 + 300);
};
}
else {
audio = play = function(){};
}