Web Audio API:性能问题?

时间:2020-02-14 19:56:21

标签: javascript web-audio-api

我这里有一个小编钟机。它可以一次生成多个泛音,并在每个泛音上使用可变增益以产生丰富的铃声。我调用一个函数来依次播放其中的一系列。但是,如果我快速按下按钮几次,声音输出就会停止,好像它的内存不足:

https://codepen.io/ophello/pen/OJVRMQe

function ding(freq) {
    var toneGen = new AudioContext()
    var duration = 15
    var T = toneGen.currentTime
    var overtones = [1,2.76,5.4,8.93,13.34,18.64]
    overtones.forEach(function(overtone,i) {
        var osc = toneGen.createOscillator()
        osc.type = "sine"
        var freqVal = freq * overtone
        if (freqVal <= 22050) {
            osc.frequency.value = freq * overtone
            var envelope = toneGen.createGain()
            osc.connect(envelope)
            envelope.gain.setValueAtTime(0.1/Math.pow((i+1),5), T)
            envelope.gain.exponentialRampToValueAtTime(0.00001, T + duration)
            envelope.connect(toneGen.destination)
            osc.start(T)
            osc.stop(T + duration)
        }
    })
}

var hzValues = [216, 288, 324, 405, 432, 648]

$("#b1").mousedown(() => { 
    hzValues.forEach((thisHz,i) => { 
        setTimeout(() => { ding(thisHz) }, 100 * i); 
    })
})

为什么几次按下后该键停止工作?我在这里错过了重要的东西吗?我不知道该在控制台中寻找任何问题。这是内存问题吗?

1 个答案:

答案 0 :(得分:1)

由于上面的dandavis,我通过添加超时功能来实现此目的:

library(data.table)
setDT(df1)[,  P1 := sum(P1[Condition == 0]), .(Col1, Col2)][, 
     lapply(.SD, sum) , .(Col1, Col2, P1), .SDcols = P2:P4]

更新:通过将音频上下文移至主要功能之外而得到改进!