我希望能够使用单个音频参数来控制其他几个节点的输入。例如,这个参数可能是一个频率,我想把它发送到一个振荡器的频率,也把它发送到一个2x增益,并把它发送到另一个振荡器的频率。
但是,当我修改offset
的值时,我没有看到参数的值有任何变化。
这里有一个简单的例子,你可以在这里看到:https://jsfiddle.net/j70fm4pr/2/
function log(message) {
const d = document.createElement('div');
d.innerText = message;
document.body.appendChild(d);
}
const b = document.createElement('button');
b.textContent = 'Test';
b.addEventListener('click', (e) => {
log('Started.');
const ctx = new AudioContext();
const cNode = new ConstantSourceNode(ctx, {offset: 0.5});
cNode.offset.setValueAtTime(1.0, ctx.currentTime);
log(`Value: ${cNode.offset.value}`);
setTimeout(() => {log(`Value: ${cNode.offset.value}`);}, 1000);
});
document.body.appendChild(b);
实际输出:
Initial value: 0.5
Later value: 0.5
预期输出:实际输出:
Initial value: 0.5
Later value: 1
WebAudio API实现(至少在Chrome on Linux)不会更新任何计划值,除非它们已经启动并连接到目的地。
const ctx = new AudioContext();
const cNode = new ConstantSourceNode(ctx, {offset: 0.5});
cNode.offset.setValueAtTime(1.0, ctx.currentTime);
// Start the constant source node.
cNode.start();
// Create a GainNode so that we can connect the constant source node
// without having any impact on the final output.
const g = new GainNode(ctx, {gain: 0.0});
cNode.connect(g);
// Actually connect the gain node to the destination. Without this,
// the scheduled change has no effect.
g.connect(ctx.destination);
// Alternatively, you could use an Analyzer node as the target.
log(`Initial value: ${cNode.offset.value}`);
setTimeout(() => {log(`Later value: ${cNode.offset.value}`);}, 1000);
注意:您也可以将ConstantSourceNode连接到连接到输出的节点的参数:
const g = new GainNode(ctx, {gain: 0.0});
cNode.connect(g.gain);
g.connect(ctx.destination);
输出:
Initial value: 0.5
Later value: 1
我已经更新了提琴在这里:https://jsfiddle.net/h1xk5d04/和连接到一个参数在这里:https://jsfiddle.net/h1xk5d04/1/