<html>
<head>
<meta charset="utf-8">
<title>WebAudio example of convolver node</title>
</head>
<body>
<audio src="http://mainline.i3s.unice.fr/mooc/guitarRiff1.mp3" id="convolverPlayer" controls loop crossorigin="anonymous"></audio>
<br>
<label for="convolverSlider">Reverb (Dry/Wet)</label>
<input type="range" min="0" max="1" step="0.1" value="0" id="convolverSlider" />
</body>
</html>
// This line is a trick to initialize the AudioContext
// that will work on all recent browsers
var ctx = window.AudioContext || window.webkitAudioContext;
var audioContext;
var convolverSlider, convolverNode, convolverGain, directGain;
var impulseURL = "https://mainline.i3s.unice.fr/mooc/Scala-Milan-Opera-Hall.wav";
var decodedImpulse;
window.onload = function() {
// get the AudioContext
audioContext = new ctx();
// the audio element
playerConvolver = document.querySelector('#convolverPlayer');
playerConvolver.onplay = (e) =>{audioContext.resume();}
playerConvolver.onplay = (e) => {audioContext.resume();}
convolverSlider = document.querySelector('#convolverSlider');
loadImpulse(impulseURL, function() {
// we get here only when the impulse has finished
// loading
buildAudioGraphConvolver();
});
// input listener on the gain slider
convolverSlider.oninput = function(evt){
// We set the gain at the output of the convolver (wet signal route) with the
// slider value, and we set the gain between the source and dest
// (dry signal route) to 1 - wet
convolverGain.gain.value = parseFloat(evt.target.value);
directGain.gain.value = 1 - convolverGain.gain.value;
};
};
function buildAudioGraphConvolver() {
// create source and gain node
var source = audioContext.createMediaElementSource(playerConvolver);
convolverNode = audioContext.createConvolver();
convolverNode.buffer = decodedImpulse;
convolverGain = audioContext.createGain();
convolverGain.gain.value = 0;
directGain = audioContext.createGain();
directGain.gain.value = 1;
// direct/dry route source -> directGain -> destination
source.connect(directGain);
directGain.connect(audioContext.destination);
// wet route with convolver: source -> convolver
// -> convolverGain -> destination
source.connect(convolverNode);
convolverNode.connect(convolverGain);
convolverGain.connect(audioContext.destination);
}
function loadImpulse(url, callback) {
ajaxRequest = new XMLHttpRequest();
ajaxRequest.open('GET', url, true);
ajaxRequest.responseType = 'arraybuffer';
ajaxRequest.onload = function() {
var impulseData = ajaxRequest.response;
audioContext.decodeAudioData(impulseData, function(buffer) {
decodedImpulse = buffer;
callback();
});
};
ajaxRequest.onerror = function(e) {
console.log("Error with decoding audio data" + e.err);
};
ajaxRequest.send();
}
Output
300px
You can jump to the latest bin by adding /latest
to your URL
Keyboard Shortcuts
Shortcut | Action |
---|---|
ctrl + [num] | Toggle nth panel |
ctrl + 0 | Close focused panel |
ctrl + enter | Re-render output. If console visible: run JS in console |
Ctrl + l | Clear the console |
ctrl + / | Toggle comment on selected lines |
ctrl + ] | Indents selected lines |
ctrl + [ | Unindents selected lines |
tab | Code complete & Emmet expand |
ctrl + shift + L | Beautify code in active panel |
ctrl + s | Save & lock current Bin from further changes |
ctrl + shift + s | Open the share options |
ctrl + y | Archive Bin |
Complete list of JS Bin shortcuts |
JS Bin URLs
URL | Action |
---|---|
/ | Show the full rendered output. This content will update in real time as it's updated from the /edit url. |
/edit | Edit the current bin |
/watch | Follow a Code Casting session |
/embed | Create an embeddable version of the bin |
/latest | Load the very latest bin (/latest goes in place of the revision) |
/[username]/last | View the last edited bin for this user |
/[username]/last/edit | Edit the last edited bin for this user |
/[username]/last/watch | Follow the Code Casting session for the latest bin for this user |
/quiet | Remove analytics and edit button from rendered output |
.js | Load only the JavaScript for a bin |
.css | Load only the CSS for a bin |
Except for username prefixed urls, the url may start with http://jsbin.com/abc and the url fragments can be added to the url to view it differently. |