WebAudio Experiments
Requires Chrome browser for support of proposed Web Audio API.
Simple Tone UNDER CONSTRUCTION
Use a JavaScriptProcessorNode to generate a tone using an oscillator.
Roll mouse over the paragraphs below to start and stop sound.
Roll mouse over for 800 Hz tone
Roll mouse over for 400 Hz tone
Roll mouse over for 200 Hz tone
Roll mouse over for 100 Hz tone
status messages
JavaScript code for the tone generator.
// Example showing how to produce a tone using Web Audio API.
var context;
var oscillator;
var amp;
// Change HTML in a DIV or other element for debugging
function writeMessageToID(id,message)
{
// Voodoo for browser compatibility.
d=document;
re = d.all ? d.all[id] : d.getElementById(id);
re.innerHTML=message;
}
// Create an AudioCOntext and a JavaScriptNode.
function initAudio()
{
if( window.webkitAudioContext )
{
context = new webkitAudioContext();
oscillator = context.createOscillator();
oscillator.frequency.value = 440;
amp = context.createGain();
amp.gain.value = 0;
// Connect ooscillator to amp and amp to the mixer of the context.
// This is like connecting cables between jacks on a modular synth.
oscillator.connect(amp);
amp.connect(context.destination);
oscillator.start(0);
writeMessageToID( "soundStatus", "<p>Audio initialized.</p>");
}
else
{
alert("Sorry. WebAudio API not supported. Try using the Google Chrome or Safari browser.");
}
}
// Set the frequency of the oscillator and start it running.
function startTone( frequency )
{
var now = context.currentTime;
oscillator.frequency.setValueAtTime(frequency, now);
// Ramp up the gain so we can hear the sound.
// We can ramp smoothly to the desired value.
// First we should cancel any previous scheduled events that might interfere.
amp.gain.cancelScheduledValues(now);
// Anchor beginning of ramp at current value.
amp.gain.setValueAtTime(amp.gain.value, now);
amp.gain.linearRampToValueAtTime(0.5, context.currentTime + 0.1);
writeMessageToID( "soundStatus", "<p>Play tone at frequency = " + frequency + "</p>");
}
function stopTone()
{
var now = context.currentTime;
amp.gain.cancelScheduledValues(now);
amp.gain.setValueAtTime(amp.gain.value, now);
amp.gain.linearRampToValueAtTime(0.0, context.currentTime + 1.0);
writeMessageToID( "soundStatus", "<p>Stop tone.</p>");
}
// init once the page has finished loading.
window.onload = initAudio;
