PannerNode: rolloffFactor 屬性

Baseline 已廣泛支援

此特性已相當成熟,可在許多裝置和瀏覽器版本上使用。自 ⁨2015 年 7 月⁩以來,各瀏覽器均已提供此特性。

PannerNode 介面的 rolloffFactor 屬性是一個雙精度浮點數值,它描述了當聲源遠離監聽者時音量降低的速度。此值由所有距離模型使用。rolloffFactor 屬性的預設值為 1

一個數字,其範圍取決於聲源的 distanceModel,如下所示(不允許負值)

"linear"

範圍是 0 到 1。

"inverse"

範圍是 0 到 Infinity

"exponential"

範圍是 0 到 Infinity

異常

RangeError

如果屬性的值超出了接受的範圍,則會丟擲此錯誤。

示例

此示例演示了不同的 rolloffFactor 值如何影響測試音的音量隨監聽者距離增加而衰減的方式。

js
const context = new AudioContext();
// all our test tones will last this many seconds
const NOTE_LENGTH = 4;
// this is how far we'll move the sound
const Z_DISTANCE = 20;

// this function creates a graph for the test tone with a given rolloffFactor
// and schedules it to move away from the listener along the Z (depth-wise) axis
// at the given start time, resulting in a decrease in volume (decay)
const scheduleTestTone = (rolloffFactor, startTime) => {
  const osc = new OscillatorNode(context);

  const panner = new PannerNode(context);
  panner.rolloffFactor = rolloffFactor;

  // set the initial Z position, then schedule the ramp
  panner.positionZ.setValueAtTime(0, startTime);
  panner.positionZ.linearRampToValueAtTime(Z_DISTANCE, startTime + NOTE_LENGTH);

  osc.connect(panner).connect(context.destination);

  osc.start(startTime);
  osc.stop(startTime + NOTE_LENGTH);
};

// this tone should decay fairly quickly
scheduleTestTone(1, context.currentTime);
// this tone should decay slower than the previous one
scheduleTestTone(0.5, context.currentTime + NOTE_LENGTH);
// this tone should decay only slightly
scheduleTestTone(0.1, context.currentTime + NOTE_LENGTH * 2);

執行此程式碼後,生成的波形應該看起來像這樣

A waveform visualization of three oscillator tones produced in Web Audio. Each oscillator moves away from the listener at the same speed, but with different rolloffFactors affecting the resulting volume decay.

規範

規範
Web Audio API
# dom-pannernode-rollofffactor

瀏覽器相容性

另見