ScriptProcessorNode

この記事は技術レビューを必要としています。

この記事はまだ日本語に翻訳されていません。MDN の翻訳はボランティアによって行われています。是非 MDN に登録し、私たちの力になって下さい。

« Web Audio API

This is an experimental technology
Because this technology's specification has not stabilized, check the compatibility table for the proper prefixes to use in various browsers. Also note that the syntax and behavior of an experimental technology is subject to change in future versions of browsers as the spec changes.

The ScriptProcessorNode stores the input in a buffer, send the audioprocess event. The EventHandler takes the input buffer and fill the output buffer which is sent to the output by the ScriptProcessorNode.The ScriptProcessorNode interface allows the generation, processing, or analyzing of audio using JavaScript. It is an AudioNode, that is an audio-processing module, that is linked to two buffers, one containing the current input, one containing the output. An event, implementing the AudioProcessingEvent interface, is sent to the object each time the input buffer contains new data, and the event handler terminates when it has filled the output buffer with data.

The size of the input and output buffer are defined at the creation time, when the AudioContext.createScriptProcessor() factory method is called. The buffer size must be a power of 2 between 256 and 16384, that is 256, 512, 1024, 2048, 4096, 8192 or 16384. Small numbers lower the latency, but large number may be necessary to avoid audio breakup and glitches. If the size of the buffers isnot defined, which is recommended, the browser will pick one that its heuristic deems appropriate.

  • Number of inputs 1
  • Number of outputs 1
  • Channel count mode "max"
  • Channel count 2 (not used in the default count mode)
  • Channel interpretation "speakers"

Properties

Inherits properties from its parent, AudioNode.

ScriptProcessNode.bufferSize Read only
Returns an integer representing both the input and output buffer size. It is a power of 2 value between 256 and 16384, included.

Event handlers

ScriptProcessorNode.onaudioprocess
Represents the EventHandler to be called.

Methods

No specific methods; inherits methods from its parent, AudioNode.

Examples

Example: Generating a sine wave

The following example shows basic usage of a ScriptProcessorNode to generate a sine wave.

var context = new AudioContext();

// Create a ScriptProcessorNode with a bufferSize of 4096 and a single output channel
var audioNode = context.createScriptProcessor(4096, 0, 1);

var volume = .5;

// The frequncy of the sine wave tone
var frequency = 440;

// Give the node a function to process audio events
audioNode.onaudioprocess = function(audioProcessingEvent) {

  // The output buffer contains the samples that will be modified and played
  var outputBuffer = audioProcessingEvent.outputBuffer;

  // Loop through the output channels (in this case there is only one)
  for (var channel = 0; channel < outputBuffer.numberOfChannels; channel++) {
    var outData = outputBuffer.getChannelData(channel);

    // Loop through the 4096 samples
    for (var sample = 0; sample < outputBuffer.length; sample++) {

      // The time at which the sample will play
      var sampleTime = context.currentTime + outputBuffer.duration * sample / outputBuffer.length;

      // Set the data in the output buffer for each sample
      outData[sample] = volume * Math.sin(sampleTime * frequency * Math.PI * 2);
    }
  }
}

// Connect the node to the context to start playing the sound
audioNode.connect(context.destination); // Connect to speakers

Specifications

Specification Status Comment
Web Audio API Working Draft  

Browser compatibility

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support (Yes)webkit Activated on Nightly only Not supported Not supported ?
Feature Android Firefox Mobile (Gecko) IE Phone Opera Mobile Safari Mobile
Basic support ? Activated on Nightly only Not supported Not supported ?

See also

  • Using Web Audio

添付ファイル

ファイル サイズ 日時 添付者:
Action of a ScriptProcessorNode
The ScriptProcessorNode stores the input in a buffer, send the audioprocess event. The EventHandler takes the input buffer and fill the output buffer which is sent to the output by the ScriptProcessorNode.
17069 バイト 2013-04-23 08:17:54 teoli

Document Tags and Contributors

Contributors to this page: Sheppy, mankyd, chadoh, cronco, rick_carlino, Prachi_Dighe, Thormme, teoli
最終更新者: chadoh,