AudioContext.createJavaScriptNode

  • Revision slug: Web/API/AudioContext.createJavaScriptNode
  • Revision title: AudioContext.createJavaScriptNode
  • Revision id: 505341
  • Created:
  • Creator: kevincennis
  • Is current revision? Yes
  • Comment

Revision Content

{{WebAudioRef("AudioContext")}}

The AudioContext.createJavaScriptNode() method creates a {{domxref("JavaScriptNode")}} which is used for directly manipulating audio data with JavaScript. The method was renamed to  AudioContext.createScriptProcessor. See also ScriptProcessorNode

Syntax

AudioContext.createJavaScriptNode(bufferSize, numInputChannels, numOutputChannels)

Parameters

bufferSize
The buffer size must be in units of sample frames, i.e., one of: 256, 512, 1024, 2048, 4096, 8192, or 16384. It controls the frequency of callbacks asking for a buffer refill. Smaller sizes allow for lower latency and higher for better overall quality.
numInputChannels
The number of input channels in the audio stream.
numOutputChannels
The number of output channels in the audio stream.

The following script illustrate the use of createJavaScriptNode:

SineWave = function(context) {
  var that = this;
  this.x = 0; // Initial sample number
  this.context = context;
  this.node = context.createJavaScriptNode(1024, 1, 1);
  this.node.onaudioprocess = function(e) { that.process(e) };
}

SineWave.prototype.process = function(e) {
  var data = e.outputBuffer.getChannelData(0);
  for (var i = 0; i < data.length; ++i) {
    data[i] = Math.sin(this.x++);
  }
}

SineWave.prototype.play = function() {
  this.node.connect(this.context.destination);
}

SineWave.prototype.pause = function() {
  this.node.disconnect();
}

See also

Revision Source

<p>{{WebAudioRef("AudioContext")}}</p>
<p>The <code>AudioContext.createJavaScriptNode()</code> method creates a {{domxref("JavaScriptNode")}} which is used for directly manipulating audio data with JavaScript. The method was renamed to &nbsp;<a href="/en-US/docs/Web/API/AudioContext.createScriptProcessor">AudioContext.createScriptProcessor</a>. See also <a href="/en-US/docs/Web/API/ScriptProcessorNode">ScriptProcessorNode</a></p>
<h2 id="Syntax">Syntax</h2>
<pre class="syntaxbox">
<code><em>AudioContext</em>.createJavaScriptNode(<em>bufferSize</em>, <em>numInputChannels</em>, <em>numOutputChannels</em>)</code>
</pre>
<h3 id="Parameters">Parameters</h3>
<dl>
 <dt>
  <code>bufferSize</code></dt>
 <dd>
  The buffer size must be in units of sample frames, i.e., one of: 256, 512, 1024, 2048, 4096, 8192, or 16384. It controls the frequency of callbacks asking for a buffer refill. Smaller sizes allow for lower latency and higher for better overall quality.</dd>
 <dt>
  <code>numInputChannels</code></dt>
 <dd>
  The number of input channels in the audio stream.</dd>
 <dt>
  <code>numOutputChannels</code></dt>
 <dd>
  The number of output channels in the audio stream.</dd>
</dl>
<p>The following script illustrate the use of createJavaScriptNode:</p>
<pre class="brush: js">
SineWave = function(context) {
  var that = this;
  this.x = 0; // Initial sample number
  this.context = context;
  this.node = context.createJavaScriptNode(1024, 1, 1);
  this.node.onaudioprocess = function(e) { that.process(e) };
}

SineWave.prototype.process = function(e) {
  var data = e.outputBuffer.getChannelData(0);
  for (var i = 0; i &lt; data.length; ++i) {
    data[i] = Math.sin(this.x++);
  }
}

SineWave.prototype.play = function() {
  this.node.connect(this.context.destination);
}

SineWave.prototype.pause = function() {
  this.node.disconnect();
}
</pre>
<h2 id="See_also" name="See_also">See also</h2>
<ul>
 <li><a href="http://0xfe.blogspot.com/2011/08/generating-tones-with-web-audio-api.html" title="http://0xfe.blogspot.com/2011/08/generating-tones-with-web-audio-api.html">Generating Tones with the Web Audio API</a></li>
 <li><a href="http://www.smartjava.org/content/exploring-html5-web-audio-visualizing-sound" title="http://www.smartjava.org/content/exploring-html5-web-audio-visualizing-sound">Exploring the HTML5 Web Audio: visualizing sound</a></li>
</ul>
Revert to this revision