如何在 p5.js 中定向声音 - 双耳声音
How to direct sound in p5.js - Binaural sound
我正在尝试引导声音,
如何将声音指向圆圈所在的位置?考虑到圆圈将要改变位置(我想用双耳声代替对面的声音,不知道有没有更简单的方法)
该代码仅适用于移动设备。
谢谢你。
不完全清楚你在问什么,但如果你想根据空间排列调整来自哪个通道(右与左)的特定声音的多少,你可以使用 Panner3D。
let osc, pan;
let playing = false;
let direction = 0;
function setup() {
createCanvas(windowWidth, windowHeight);
angleMode(DEGREES);
osc = new p5.Oscillator('sine');
osc.freq(261.625565);
osc.disconnect();
pan = new p5.Panner3D();
pan.process(osc);
pan.setFalloff(Math.min(width, height) / 2, 1);
// By default the sound source will be omni-directional
pan.panner.coneInnerAngle = 60;
pan.panner.coneOuterAngle = 360;
// You can also adjust how quite the sound outside the
// outer code is with coneOuterGain which defaults to 0
}
function doubleClicked() {
if (!playing) {
playing = true;
osc.start();
osc.amp(0.3, 0.1);
} else {
playing = false;
osc.amp(0, 0.1);
}
}
function mouseMoved() {
if (pan) {
pan.set(mouseX - width / 2, 0, mouseY - height / 2, 0.1);
}
}
function mouseDragged() {
mouseMoved();
}
function draw() {
background(100);
circle(width / 2, height / 2, 20);
arc(mouseX, mouseY, 40, 40, direction - 20, direction + 20);
if (mouseIsPressed) {
direction = (direction + 3) % 360;
pan.orient(cos(direction), 0, sin(direction), 0.1);
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.4.0/p5.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.4.0/addons/p5.sound.min.js"></script>
我正在尝试引导声音, 如何将声音指向圆圈所在的位置?考虑到圆圈将要改变位置(我想用双耳声代替对面的声音,不知道有没有更简单的方法) 该代码仅适用于移动设备。 谢谢你。
不完全清楚你在问什么,但如果你想根据空间排列调整来自哪个通道(右与左)的特定声音的多少,你可以使用 Panner3D。
let osc, pan;
let playing = false;
let direction = 0;
function setup() {
createCanvas(windowWidth, windowHeight);
angleMode(DEGREES);
osc = new p5.Oscillator('sine');
osc.freq(261.625565);
osc.disconnect();
pan = new p5.Panner3D();
pan.process(osc);
pan.setFalloff(Math.min(width, height) / 2, 1);
// By default the sound source will be omni-directional
pan.panner.coneInnerAngle = 60;
pan.panner.coneOuterAngle = 360;
// You can also adjust how quite the sound outside the
// outer code is with coneOuterGain which defaults to 0
}
function doubleClicked() {
if (!playing) {
playing = true;
osc.start();
osc.amp(0.3, 0.1);
} else {
playing = false;
osc.amp(0, 0.1);
}
}
function mouseMoved() {
if (pan) {
pan.set(mouseX - width / 2, 0, mouseY - height / 2, 0.1);
}
}
function mouseDragged() {
mouseMoved();
}
function draw() {
background(100);
circle(width / 2, height / 2, 20);
arc(mouseX, mouseY, 40, 40, direction - 20, direction + 20);
if (mouseIsPressed) {
direction = (direction + 3) % 360;
pan.orient(cos(direction), 0, sin(direction), 0.1);
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.4.0/p5.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.4.0/addons/p5.sound.min.js"></script>