WebAudio:振荡器总数和性能 (Javascript)

WebAudio: total oscillator count and performance (Javascript)

我正在创建大量此类 WebAudio 乐器对象:

var context = new AudioContext();

Inst = function (id, param){
  var inst = {
    id = id,
    param = param
  }
  inst.osc = context.createOscillator();
  inst.gain = context.createGain();
  inst.osc.connect(inst.gain);
  inst.gain.connect(context.destination)
  inst.gain.gain.value = 0;
  inst.osc.start();

  inst.playTone = function () {
    inst.gain.gain.setTargetAtTime(1, context.currentTime, 0.1);        
  } 
  inst.stopTone = function () {
    inst.gain.gain.setTargetAtTime(0, context.currentTime, 0.1);        
  }     
instList[id] = inst;    
}

如您所见,我只是提高和降低了音量。在 inst. 函数中使用 .start().stop() 是有问题的,因为 .stop 会终止振荡器,并且用户将与这些对象重复交互。

我想知道,AudioContext 中是否可以处理最大数量的振荡器?如果我启动一大堆振荡器并且从不停止它们,是否存在一些潜在的性能问题?

(旁注:如果这是推荐的方法,我愿意接受有关如何每次动态创建新振荡器的建议......我还没有弄清楚,毕竟我不是当然需要它。)

您可以有效使用的振荡器数量仅受您机器的强大程度的限制。低功率机器在出现故障之前只能支持几个同步振荡器。高性能机器可以处理更多。

你不想永远离开振荡器 运行,因为它们会耗尽所有可用的 CPU。

由于一段时间后增益变为零,您可以安排振荡器在增益变为零后停止。一般的经验法则是使用您在 setTargetAtTime 中使用的时间常数的 5-10 倍。所以在 stopTone 中包含类似的内容:

inst.osc.stop(context.currentTime + 0.1*10)

其中 0.1 是 setTargetAtTime 中使用的时间常数值。

这将停止振荡器,因此它们不会吸收所有 CPU 播放到零增益增益节点。