<html>
<head>
<title>WebAudioAPI Example</title>
<script>
function generateAudio(e)
{
var left = e.outputBuffer.getChannelData(0);
var right = e.outputBuffer.getChannelData(1);
var numSamples = right.length;
for (var i = 0; i < numSamples; i++)
{
var val = 0.1 * Math.sin(currentPhase);
left[i] = val;
right[i] = val;
currentPhase += phaseIncrement;
}
}
// Create the audio context
var context = new AudioContext();
// Create a source node with 1024, the number of samples to generate
// on each call. 0, our node has NO INPUTS as far as we just want to generate sound
// 2 for stereo...
var node = context.createScriptProcessor(1024, 0, 2);
// Specify the audio generation function
node.onaudioprocess = generateAudio;
// Set up the per-sample phase increment based on the desired
// sine tone frequency and the sample rate of the audio context.
var currentPhase = 0.0;
var phaseIncrement = 2 * Math.PI * 440 / context.sampleRate;
// Connect the node to a destination, i.e. the audio output.
node.connect(context.destination);
</script>
</head>
<body>
<h1>WebAudio API Example</h1>
<p>440 Hz sine tone.<br>
</p>
Try to change parameters from the code, the frequency (440, try other values), etc.
Link to draft Sound API specification : https://dvcs.w3.org/hg/audio/raw-file/tip/webaudio/specification.html
</body>
</html>
Output
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. |