Real time PCM output
HTML
<h1>Real time PCM output with Web Audio API</h1>
<p>You should hear noise.</p>
<p>You may need to press the "allow" button above</p>
<a href="http://papers.traustikristjansson.info/?p=486" target="_blank">Follow this link for a tutorial.</a>
CSS
h1 {
font-family: Helvetica, Arial;
font-weight: bold;
font-size: 18px;
}
body, p{
font-family: Helvetica, Arial;
}
JavaScript
// This is the PCM sample source. You modify this.
function myPCMSource() {
return 0; // Generate noise.
}
// The rest is boilerplate that you can copy-paste.
var audioContext;
try {
window.AudioContext = window.AudioContext || window.webkitAudioContext;
audioContext = new AudioContext();
} catch(e) {
alert('Web Audio API is not supported in this browser');
}
var bufferSize = 4096;
var myPCMProcessingNode = audioContext.createScriptProcessor(bufferSize, 1, 1);
myPCMProcessingNode.onaudioprocess = function(e) {
var output = e.outputBuffer.getChannelData(0);
for (var i = 0; i < bufferSize; i++) {
// Generate and copy over PCM samples.
output[i] = myPCMSource();
}
}
myPCMProcessingNode.connect(audioContext.destination);
myPCMProcessingNode.start(0);