有没有办法 select 用变量代替节点名称的网络音频节点?

Is there a way to select a web audio node with a variable in place of the nodes name?

我有一组 5 个振荡器节点,希望能够 select 特定节点的波形。所有振荡器都具有 osc1、osc2、osc3 等名称,我希望能够 select 我正在使用元素 id(1、2、3 等)更改哪个振荡器

这是我认为可行的方法:

var oscNum = ("osc"+eleNum)

if (element.value == 1)
{
    oscNum.type = 'square';
}

else if (element.value == 2)
{
oscNum.type = "sawtooth";
}

else if (element.value == 3)
{
oscNum.type = "triangle";
}

else if (element.value == 0)
{
oscNum.type = "sine";           
}

但是它没有,我想知道是否有任何方法可以使用 var.type 而不是 osc1.type 到 select 节点。

在您的示例中,第一行代码正在计算一个字符串。假设 eleNum 等于 3。那么 oscNum 的值将只是字符串 "osc3".

如果您想 select 您的振荡器的索引,您可以使用数组来存储它们。

const oscillators = [ osc0, osc1, osc2, osc3 ];

稍后您可以通过索引select一个单独的振荡器并设置类型。

oscillators[eleNum].type = "triangle";