如何使用createPanner()创建非常基本的左/右等功率平移; [英] How to create very basic left/right equal power panning with createPanner();
问题描述
我正在查看网络音频API规范,并且平移节点使用三个值来创建声音的3D频谱。我想知道为了创建一个基本的2D等功率panner,程序员需要进行公式编程来扩展这个......或者如果我过度思考它并且有一种更简单的方法可以做到这一点。
I am looking at the web audio API spec and the panning node uses three values to create a 3D spectrum for sound. I was wondering if in order to create a basic 2D "equal power" panner the programmer needs to do the formulaic programming to scale this ... or if I am over thinking it and there is a simpler way to do it.
编辑
推荐答案
我仍然可以通过仅将第一个参数更改为setPosition()并将其他参数保持为零来获得平移效果。
I can still get a panning effect by changing only the first argument to setPosition() and keeping other arguments zero.
<!DOCTYPE html>
<html>
<head>
<script>
var c = new webkitAudioContext();
var s = c.createBufferSource();
var g = c.createGainNode();
var p = c.createPanner();
s.connect(g);
g.connect(p);
p.connect(c.destination);
function play(e) {
var fr = new FileReader();
var file = document.getElementById("file").files[0];
fr.onload = function(e) {
c.decodeAudioData(e.target.result,
function (buf) {
s.buffer = buf;
g.gain.value = 0.5;
s.noteOn(0)
},
function () {
console.error('decodeAudioData failed.');
}
);
};
fr.readAsArrayBuffer(file);
}
function pan(range) {
var x = Math.sin(range.value * (Math.PI / 180));
p.setPosition(x, 0, 0);
}
</script>
</head>
<body>
Choose your MP3 file:<br>
<input type="file" id="file" name="file" /><br>
<input type="submit" id="go" onclick="play()" value="Play" /><br>
L<input type="range" min="-45" max="45" value="0" onchange="pan(this);">R
</body>
</html>
但要获得自然的平移效果,您还需要指定第三个参数。
But to get a natural panning effect, you need to specify the third argument as well.
function pan(range) {
var xDeg = parseInt(range.value);
var zDeg = xDeg + 90;
if (zDeg > 90) {
zDeg = 180 - zDeg;
}
var x = Math.sin(xDeg * (Math.PI / 180));
var z = Math.sin(zDeg * (Math.PI / 180));
p.setPosition(x, 0, z);
}
这篇关于如何使用createPanner()创建非常基本的左/右等功率平移;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!