// The context is the base for the API.
var audioContext = new AudioContext();
// We can load an audio file by using an audio tag
var audiotoload = document.getElementById("audiotoplay");
var audioSource = audioContext.createMediaElementSource(audiotoload);
// To simply play it, we can connect it to the "destination" or default output of the context
audioSource.connect(audioContext.destination); // Connect to the default output
audiotoload.play();
var audioContext = new AudioContext();
var audiotoload = document.getElementById("audiotoplay");
var audioSource = audioContext.createMediaElementSource(audiotoload); // No longer playable by the normal audio tag
// We can construct a "Gain node" and connect our audio to it
var gainNode = audioContext.createGain();
audioSource.connect(gainNode);
// We can then connect it to our output to play it with gain control
gainNode.connect(audioContext.destination);
audiotoload.play();
// Here is a slider on the page which change the gain
var volumeControl = document.getElementById("volume");
volumeControl.addEventListener("change", function(event) {
console.log(event);
gainNode.gain.value = event.target.value;
audiotoload.play();
});
// The element on the page
// <input type="range" name="volume" id="volume" min="0" max="10" step=".1">
More
HTML5 Rocks: Web Audio Intro
Mozilla: Web Audio API
window.URL = window.URL || window.webkitURL || window.mozURL || window.msURL;
navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia;
if (navigator.getUserMedia) {
navigator.getUserMedia({audio: true}, function(stream) {
var audioContext = new AudioContext();
var audioSource = audioContext.createMediaStreamSource(stream);
}, function(error) {alert("Failure " + error.code);}
);
}
var analyser = (analyser || audioContext.createAnalyser());
audioSource.connect(analyser);
audiotoload.play();
var drawingCanvas = document.getElementById("drawingCanvas");
var drawingContext = drawingCanvas.getContext("2d");
drawingContext.fillStyle = "#FF0000";
var performAnalysis = function() {
var frequencies = new Uint8Array(analyser.frequencyBinCount);
analyser.getByteFrequencyData(frequencies);
for (var i = 0; i < frequencies.length; i++) {
//console.log(frequencies[i]);
drawingContext.fillRect(i*10,frequencies[i], 10, drawingCanvas.height);
}
requestAnimationFrame(performAnalysis);
};
performAnalysis();