BaseAudioContext: createDynamicsCompressor()-Methode

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 createDynamicsCompressor()-Methode der BaseAudioContext-Schnittstelle wird verwendet, um einen DynamicsCompressorNode zu erstellen, der zur Anwendung von Kompression auf ein Audiosignal genutzt werden kann.

Die Kompression verringert die Lautstärke der lautesten Teile des Signals und erhöht die Lautstärke der leisesten Teile. Insgesamt kann ein lauteres, reichhaltigeres und volleres Klangbild erzielt werden. Dies ist besonders wichtig in Spielen und Musik-Anwendungen, bei denen eine große Anzahl einzelner Klänge gleichzeitig abgespielt wird und Sie den Gesamtpegel des Signals kontrollieren und helfen möchten, das Übersteuern (Verzerren) der Audioausgabe zu vermeiden.

Hinweis: Der DynamicsCompressorNode()-Konstruktor ist die empfohlene Methode, um einen DynamicsCompressorNode zu erstellen; siehe Erstellen eines AudioNode.

Syntax

js
createDynamicsCompressor()

Parameter

Keine.

Rückgabewert

Beispiele

Der folgende Code zeigt, wie createDynamicsCompressor() verwendet wird, um Kompression zu einem Audiotrack hinzuzufügen. Für ein vollständigeres Beispiel sehen Sie sich unser Grundlegendes Compressor-Beispiel (Quellcode anzeigen) an.

js
// Create a MediaElementAudioSourceNode
// Feed the HTMLMediaElement into it
const source = audioCtx.createMediaElementSource(myAudio);

// Create a compressor node
const compressor = audioCtx.createDynamicsCompressor();
compressor.threshold.setValueAtTime(-50, audioCtx.currentTime);
compressor.knee.setValueAtTime(40, audioCtx.currentTime);
compressor.ratio.setValueAtTime(12, audioCtx.currentTime);
compressor.attack.setValueAtTime(0, audioCtx.currentTime);
compressor.release.setValueAtTime(0.25, audioCtx.currentTime);

// connect the AudioBufferSourceNode to the destination
source.connect(audioCtx.destination);

button.onclick = () => {
  const active = button.getAttribute("data-active");
  if (active === "false") {
    button.setAttribute("data-active", "true");
    button.textContent = "Remove compression";

    source.disconnect(audioCtx.destination);
    source.connect(compressor);
    compressor.connect(audioCtx.destination);
  } else if (active === "true") {
    button.setAttribute("data-active", "false");
    button.textContent = "Add compression";

    source.disconnect(compressor);
    compressor.disconnect(audioCtx.destination);
    source.connect(audioCtx.destination);
  }
};

Spezifikationen

Specification
Web Audio API
# dom-baseaudiocontext-createdynamicscompressor

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
createDynamicsCompressor

Legend

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

Full support
Full support

Siehe auch