PannerNode:rolloffFactor 属性

基线 广泛可用

此功能非常成熟,并且可以在许多设备和浏览器版本上运行。它已在所有浏览器中可用,自 2021 年 4 月.

rolloffFactor 属性是 PannerNode 接口的一个双精度值,用于描述当声源远离监听者时音量降低的速度。此值由所有距离模型使用。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 音频 API
# dom-pannernode-rollofffactor

浏览器兼容性

BCD 表格仅在启用 JavaScript 的浏览器中加载。

另请参阅