使用将 a 添加StereoPannerNode
到链中createStereoPanner
:
var oscillator = audioCtx.createOscillator();
var panner = audioCtx.createStereoPanner();
oscillator.type = 'square';
oscillator.frequency.value = frequency; // value in hertz
oscillator.connect(panner); // connect audio source to panner
panner.connect(audioCtx.destination); // connect panner to output
oscillator.start();
现在您可以使用以下方法控制 L/R:
panner.pan.value = someValue; // value in the range -1 (L) to 1 (R)
功能示例:
var audioCtx = new (AudioContext || webkitAudioContext)();
var oscillator = audioCtx.createOscillator();
var panner = audioCtx.createStereoPanner();
oscillator.type = 'square';
oscillator.frequency.value = 440; // value in hertz
oscillator.connect(panner);
panner.connect(audioCtx.destination);
oscillator.start();
document.querySelector("input").oninput = function() {
panner.pan.value = +this.value;
};
setInterval(function() {
oscillator.frequency.value = Math.round(Math.random() * 10) * 400 + 200;
}, 100);
<label>L <input type=range min=-1 max=1 value=0 step=0.01> R</label>