BaseAudioContext.createScriptProcessor()
非推奨: この機能は非推奨になりました。まだ対応しているブラウザーがあるかもしれませんが、すでに関連するウェブ標準から削除されているか、削除の手続き中であるか、互換性のためだけに残されている可能性があります。使用を避け、できれば既存のコードは更新してください。このページの下部にある互換性一覧表を見て判断してください。この機能は突然動作しなくなる可能性があることに注意してください。
createScriptProcessor()
は BaseAudioContext
インターフェイスのメソッドで、直接音声処理に用いられる ScriptProcessorNode
を生成します。
メモ: この機能は AudioWorklet と AudioWorkletNode
インターフェイスに置き換えられました。
構文
createScriptProcessor(bufferSize, numberOfInputChannels, numberOfOutputChannels)
引数
bufferSize
-
サンプルフレーム単位でのバッファーサイズ。指定する場合、 bufferSize は 256, 512, 1024, 2048, 4096, 8192, 16384 の値のいずれかでなければなりません。これが渡されなかった場合、あるいは値が 0 の場合、実装は与えられた環境に最適なバッファーサイズを選択し、それはノードの寿命を通じて 2 の一定乗となります。
この値は、
audioprocess
イベントが配信される頻度と、各呼び出しで処理される必要があるサンプルフレームの数を制御します。bufferSize
の値を小さくすると、遅延は小さく(良く)なります。音声が中断したりグリッチを避けるためには、より高い値が必要です。作者はこのバッファーサイズを指定せず、遅延と音質のバランスをとるために、実装が適切なバッファーサイズを選択できるようにすることをお勧めします。 numberOfInputChannels
-
整数で、このノードの入力のチャンネル数を指定します。既定値は 2 です。
numberOfOutputChannels
-
整数で、このノードの出力のチャンネル数を指定します。既定値は 2 です。
警告: Webkit は現在(バージョン 31)、このメソッドを呼び出すときに有効な bufferSize
を渡すことを要求しています。
メモ: numberOfInputChannelsと
numberOfOutputChannels` の両方が 0 にするのは無効です。
返値
例
次の例は、 AudioContext.decodeAudioData()
によって読み込んだトラックを、入力トラック(バッファー)のそれぞれの音声サンプルにホワイトノイズを加えて処理し AudioDestinationNode
によって再生する ScriptProcessorNode
の基本的な使用方法を示しています。各チャンネルと各サンプルフレームに対して、scriptNode.onaudioprocess
関数は関連する audioProcessingEvent
を受け取り、それを使って入力バッファの各チャンネルと各チャンネルの各サンプルを通してループし、少量のホワイトノイズを追加してからその結果を各ケースで出力サンプルとしてセットします。
メモ: 完全な動作する例については、 GitHub の script-processor-node リポジトリを参照してください(ソースコードも見てください)。
var myScript = document.querySelector('script');
var myPre = document.querySelector('pre');
var playButton = document.querySelector('button');
// Create AudioContext and buffer source
var audioCtx = new AudioContext();
source = audioCtx.createBufferSource();
// Create a ScriptProcessorNode with a bufferSize of 4096 and a single input and output channel
var scriptNode = audioCtx.createScriptProcessor(4096, 1, 1);
console.log(scriptNode.bufferSize);
// load in an audio track via XHR and decodeAudioData
function getData() {
request = new XMLHttpRequest();
request.open('GET', 'viper.ogg', true);
request.responseType = 'arraybuffer';
request.onload = function() {
var audioData = request.response;
audioCtx.decodeAudioData(audioData, function(buffer) {
myBuffer = buffer;
source.buffer = myBuffer;
},
function(e){"Error with decoding audio data" + e.err});
}
request.send();
}
// Give the node a function to process audio events
scriptNode.onaudioprocess = function(audioProcessingEvent) {
// The input buffer is the song we loaded earlier
var inputBuffer = audioProcessingEvent.inputBuffer;
// 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 inputData = inputBuffer.getChannelData(channel);
var outputData = outputBuffer.getChannelData(channel);
// Loop through the 4096 samples
for (var sample = 0; sample < inputBuffer.length; sample++) {
// make output equal to the same as the input
outputData[sample] = inputData[sample];
// add noise to each output sample
outputData[sample] += ((Math.random() * 2) - 1) * 0.2;
}
}
}
getData();
// wire up play button
playButton.onclick = function() {
source.connect(scriptNode);
scriptNode.connect(audioCtx.destination);
source.start();
}
// When the buffer source stops playing, disconnect everything
source.onended = function() {
source.disconnect(scriptNode);
scriptNode.disconnect(audioCtx.destination);
}
仕様書
2014 年 8 月 29 日のウェブオーディオ API 仕様書公開以降、この機能は非推奨となりました。標準化の目処が立たなくなりました。
これは AudioWorklet と AudioWorkletNode
インターフェイスに置き換えられました。
ブラウザーの互換性
BCD tables only load in the browser