Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE HTML>
<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

Dismiss x
public
Bin info
MichelBuffapro
0viewers