[英]Controling audio balance (L/R) in Javascript
I want to control the audio balance when I use this code 使用此代码时我想控制音频平衡
var oscillator = audioCtx.createOscillator();
oscillator.type = 'square';
oscillator.frequency.value = frequency; // value in hertz
oscillator.connect(audioCtx.destination);
oscillator.start();
setTimeout(function(){oscillator.stop();}, duration);
and also when I play a local mp3 file. 以及当我播放本地mp3文件时。 How can I do this?
我怎样才能做到这一点?
Add a StereoPannerNode
to the chain using createStereoPanner
: 使用
createStereoPanner
将StereoPannerNode
添加到链中:
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();
Now you can control the L/R using: 现在您可以使用以下方法控制左/右:
panner.pan.value = someValue; // value in the range -1 (L) to 1 (R)
Functional example: 功能示例:
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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.