PannerNode: refDistance 属性

基线 广泛可用

此功能已十分成熟,可在多种设备和浏览器版本中使用。从 2021 年 4 月.

报告反馈

refDistance 属性是 PannerNode 接口的一个双精度值,表示随着音频源远离监听者而降低音量时的参考距离,即音量降低开始生效的距离。此值由所有距离模型使用。

refDistance 属性的默认值为 1

一个非负数。如果将值设置为小于 0,则会抛出 RangeError

异常

RangeError

示例

如果属性被赋予了超出接受范围的值,则会抛出此错误。

此示例演示了不同的 refDistance 值如何影响声音在远离监听者时音量衰减的方式。与 rolloffFactor 不同,更改此值也会延迟音量衰减,直到声音移动到参考点以外。
const context = new AudioContext();
// all our test tones will last this many seconds
const NOTE_LENGTH = 6;
// 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 refDistance
// 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 = (refDistance, startTime) => {
  const osc = new OscillatorNode(context);

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

  // 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 immediately and fairly quickly
scheduleTestTone(1, context.currentTime);
// this tone should decay slower and later than the previous one
scheduleTestTone(4, context.currentTime + NOTE_LENGTH);
// this tone should decay only slightly, and only start decaying fairly late
scheduleTestTone(7, context.currentTime + NOTE_LENGTH * 2);

js

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 refDistances affecting the resulting volume decay.

规范

运行此代码后,生成的波形应类似于以下内容
Web 音频 API
# 规范

浏览器兼容性

dom-pannernode-refdistance

另请参阅