AudioWorkletGlobalScope: sampleRate プロパティ

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since April 2021.

AudioWorkletGlobalScope インターフェイスの読み取り専用プロパティ sampleRate は、ワークレットが属する BaseAudioContext のサンプルレートを表す float 値を返します。

関連付けられたサンプルレートを表す浮動小数点数です。

この AudioWorkletProcessorAudioWorkletGlobalScope の特定のプロパティにアクセスできます。

js
// test-processor.js で定義された AudioWorkletProcessor
class TestProcessor extends AudioWorkletProcessor {
  constructor() {
    super();

    // 生成時のサンプルフレームと時刻を記録する。
    // これらの値には AudioWorkletGlobalScope からアクセスできる。
    console.log(currentFrame);
    console.log(currentTime);
  }

  // process メソッドは必須である。
  // (最初から入っている) 無音を出力する。
  process(inputs, outputs, parameters) {
    return true;
  }
}

// サンプルレートを記録する。
// これは BaseAudioContext の読み取り専用プロパティであり、
// 生成時にのみ設定されるので、変化しない。
console.log(sampleRate);

// 任意の変数を宣言し、処理器で利用できる。
// たとえば、波形テーブルが入った ArrayBuffer を宣言できる。
const usefulVariable = 42;
console.log(usefulVariable);

registerProcessor("test-processor", TestProcessor);

メインスクリプトでは処理器をロードし、処理器の名前を渡して AudioWorkletNode のインスタンスを生成し、そのノードを音声グラフに接続します。console.log() の呼び出しによる出力がコンソールに出るはずです。

js
const audioContext = new AudioContext();
await audioContext.audioWorklet.addModule("test-processor.js");
const testNode = new AudioWorkletNode(audioContext, "test-processor");
testNode.connect(audioContext.destination);

仕様書

Specification
Web Audio API
# dom-audioworkletglobalscope-samplerate

ブラウザーの互換性

Report problems with this compatibility data on GitHub
desktopmobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
sampleRate

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support

関連情報