var ctx; //audio context
var buf; //audio buffer
var url = "https://dl.dropboxusercontent.com/u/30075450/Rays.wav";
//init the sound system
function init() {
console.log("in init");
try {
ctx = new AudioContext();
loadFile();
} catch(e) {
alert('you need webaudio support');
}
}
window.addEventListener('load',init,false);
//load and decode mp3 file
function loadFile() {
var req = new XMLHttpRequest();
req.open("GET",url,true);
req.responseType = "arraybuffer";
req.onload = function() {
//decode the loaded data
ctx.decodeAudioData(req.response, function(buffer) {
buf = buffer;
setupButtons();
});
};
req.send();
}
//play the loaded file
function play() {
//create a source node from the buffer
var src = ctx.createBufferSource();
src.buffer = buf;
//connect to the final output node (the speakers)
src.connect(ctx.destination);
//play immediately
src.noteOn(0);
}
function setupButtons() {
document.getElementById('play1').onclick = function() {
play();
}
document.getElementById('play2').onclick = function() {
var time = ctx.currentTime;
for(var i=0; i<4; i++) {
var src = ctx.createBufferSource();
src.buffer = buf;
//connect to the final output node (the speakers)
src.connect(ctx.destination);
//play immediately
src.noteOn(time+i/4);
}
}
}
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.