Movatterモバイル変換


[0]ホーム

URL:


  1. 開発者向けのウェブ技術
  2. Web API
  3. AudioProcessingEvent

このページはコミュニティーの尽力で英語から翻訳されました。MDN Web Docsコミュニティーについてもっと知り、仲間になるにはこちらから。

View in EnglishAlways switch to English

AudioProcessingEvent

非推奨;: この機能は非推奨になりました。まだ対応しているブラウザーがあるかもしれませんが、すでに関連するウェブ標準から削除されているか、削除の手続き中であるか、互換性のためだけに残されている可能性があります。使用を避け、できれば既存のコードは更新してください。このページの下部にある互換性一覧表を見て判断してください。この機能は突然動作しなくなる可能性があることに注意してください。

AudioProcessingEventウェブオーディオ API のインターフェイスで、ScriptProcessorNode 入力バッファーが処理可能な状態になったときに発生するイベントを表します。

このインターフェイスを持つaudioprocess イベントは、音声処理が必要なときにScriptProcessorNode で発生します。音声処理中、入力バッファーが読み込まれ、処理されて出力音声データが生成され、出力バッファーに書き込まれます。

警告:この機能は非推奨となったので、AudioWorklet で置き換えてください。

Event AudioProcessingEvent

コンストラクター

AudioProcessingEvent()非推奨;

新しいAudioProcessingEvent オブジェクトを生成します。

インスタンスプロパティ

親プロパティであるEvent から継承されたプロパティも実装しています

playbackTime読取専用非推奨;

double 型で、音声が再生される時間を表します。AudioContext.currentTime の時刻で定義されます。

inputBuffer読取専用非推奨;

処理する入力音声データを含むバッファーであるAudioBuffer を指定します。チャンネル数はファクトリーメソッドAudioContext.createScriptProcessor() の引数numberOfInputChannels として定義されています。なお、返されたAudioBuffer は、イベントハンドラーのスコープ内のみで有効であることに注意してください。

outputBuffer読取専用非推奨;

出力する音声データを書き込むバッファーであるAudioBuffer を指定します。チャンネル数はファクトリーメソッドAudioContext.createScriptProcessor() の引数numberOutputChannels として定義されています。なお、返されたAudioBuffer は、イベントハンドラーのスコープ内のみで有効であることに注意してください。

次の例は、ScriptProcessorNode を使用して、AudioContext.decodeAudioData() で読み込んだ音声をAudioDestinationNode で処理し、入力トラック(バッファー)の各音声サンプルにホワイトノイズを追加し、AudioDestinationNode を通して再生する方法を示しています。 各チャンネルと各サンプルフレームに対して、scriptNode.onaudioprocess 関数は関連するaudioProcessingEvent を受け取り、それを用いて入力バッファー内の各チャンネルと各チャンネル内の各サンプルをループし、少量のホワイトノイズを追加してから、その結果をそれぞれのケースで出力サンプルに設定します。

メモ:完全な動作する例は、GitHub のscript-processor-node リポジトリーを参照してください(ソースコードにもアクセスできます)。

js
const myScript = document.querySelector("script");const myPre = document.querySelector("pre");const playButton = document.querySelector("button");// Create AudioContext and buffer sourceconst audioCtx = new AudioContext();const source = audioCtx.createBufferSource();// Create a ScriptProcessorNode with a bufferSize of 4096 and a single input and output channelconst scriptNode = audioCtx.createScriptProcessor(4096, 1, 1);console.log(scriptNode.bufferSize);// load in an audio track via XHR and decodeAudioDatafunction getData() {  request = new XMLHttpRequest();  request.open("GET", "viper.ogg", true);  request.responseType = "arraybuffer";  request.onload = () => {    const audioData = request.response;    audioCtx.decodeAudioData(      audioData,      (buffer) => {        myBuffer = buffer;        source.buffer = myBuffer;      },      (e) => console.error(`Error with decoding audio data: ${e.err}`),    );  };  request.send();}// Give the node a function to process audio eventsscriptNode.onaudioprocess = (audioProcessingEvent) => {  // The input buffer is the song we loaded earlier  const inputBuffer = audioProcessingEvent.inputBuffer;  // The output buffer contains the samples that will be modified and played  const outputBuffer = audioProcessingEvent.outputBuffer;  // Loop through the output channels (in this case there is only one)  for (let channel = 0; channel < outputBuffer.numberOfChannels; channel++) {    const inputData = inputBuffer.getChannelData(channel);    const outputData = outputBuffer.getChannelData(channel);    // Loop through the 4096 samples    for (let 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 the play buttonplayButton.onclick = () => {  source.connect(scriptNode);  scriptNode.connect(audioCtx.destination);  source.start();};// When the buffer source stops playing, disconnect everythingsource.onended = () => {  source.disconnect(scriptNode);  scriptNode.disconnect(audioCtx.destination);};

ブラウザーの互換性

関連情報

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp