PannerNode: refDistance 屬性
PannerNode 介面的 refDistance 屬性是一個雙精度浮點數值,表示音訊源距離聽眾越遠音量衰減的參考距離——即音量開始衰減生效的距離。此值由所有距離模型使用。
refDistance 屬性的預設值為 1。
值
一個非負數。如果該值設定為小於 0,則會丟擲 RangeError。
異常
RangeError-
如果屬性的值超出了接受的範圍,則會丟擲此錯誤。
示例
此示例演示了 refDistance 的不同值如何影響聲音在遠離聽眾時的音量衰減。與 rolloffFactor 不同,更改此值還會延遲音量衰減,直到聲音經過參考點。
js
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);
執行此程式碼後,生成的波形應如下所示:

規範
| 規範 |
|---|
| Web Audio API # dom-pannernode-refdistance |
瀏覽器相容性
載入中…