AudioWorkletGlobalScope: sampleRate-Eigenschaft

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.

Die schreibgeschützte sampleRate-Eigenschaft der AudioWorkletGlobalScope-Schnittstelle gibt eine Fließkommazahl zurück, die die Abtastrate des zugehörigen BaseAudioContext darstellt, zu dem der Worklet gehört.

Wert

Eine Fließkommazahl, die die zugehörige Abtastrate darstellt.

Beispiele

Der AudioWorkletProcessor hat Zugriff auf die spezifischen Eigenschaften des AudioWorkletGlobalScope:

js
// AudioWorkletProcessor defined in : test-processor.js
class TestProcessor extends AudioWorkletProcessor {
  constructor() {
    super();

    // Logs the current sample-frame and time at the moment of instantiation.
    // They are accessible from the AudioWorkletGlobalScope.
    console.log(currentFrame);
    console.log(currentTime);
  }

  // The process method is required - output silence,
  // which the outputs are already filled with.
  process(inputs, outputs, parameters) {
    return true;
  }
}

// Logs the sample rate, that is not going to change ever,
// because it's a read-only property of a BaseAudioContext
// and is set only during its instantiation.
console.log(sampleRate);

// You can declare any variables and use them in your processors
// for example it may be an ArrayBuffer with a wavetable.
const usefulVariable = 42;
console.log(usefulVariable);

registerProcessor("test-processor", TestProcessor);

Das Hauptskript lädt den Prozessor, erstellt eine Instanz von AudioWorkletNode, übergibt den Namen des Prozessors an diesen und verbindet den Knoten mit einem Audiografen. Wir sollten die Ausgabe der console.log()-Aufrufe in der Konsole sehen:

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

Spezifikationen

Specification
Web Audio API
# dom-audioworkletglobalscope-samplerate

Browser-Kompatibilität

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

Siehe auch