2

我正在使用WebAudioAPIand创建复音合成器WebMIDIAPI。我的两个振荡器中的每一个都有一个增益节点,然后将其连接到主增益节点。

我想知道如何在发布后正确停止(并在必要时删除?)振荡器。我不确定是否有必要从阵列中调用oscillator.stop()和振荡器。delete

如果我这样做,释放信封不起作用并且音符立即停止,如果我不这样做,释放信封确实有效,但音符有时可以继续播放。

编辑:似乎当该.stop()功能未实现并且两个音符同时播放时,其中一个振荡器将始终保持打开状态。不知道是我的代码还是??

我的noteOff功能代码如下:

/**
 * Note is being released
 */
this.noteOff = function (frequency, velocity, note){

    var now = this.context.currentTime;

    // Get the release values
    var osc1ReleaseVal = now + this.osc1Release;
    var osc2ReleaseVal = now + this.osc2Release;

    // Cancel scheduled values
    this.oscGain.gain.cancelScheduledValues(now);
    this.osc2Gain.gain.cancelScheduledValues(now);

    // Set the value
    this.oscGain.gain.setValueAtTime(this.oscGain.gain.value, now);
    this.osc2Gain.gain.setValueAtTime(this.osc2Gain.gain.value, now);

    // Release the note
    this.oscGain.gain.linearRampToValueAtTime(0.0, osc1ReleaseVal);
    this.osc2Gain.gain.linearRampToValueAtTime(0.0, osc2ReleaseVal);

    // ----- IF I COMMENT THE `forEach` Loop the release works correctly but with side-effects!
    // Stop the oscillators
    this.oscillators[frequency].forEach(function (oscillator) {
        oscillator.stop(now);
        oscillator.disconnect();
        delete oscillator;
    });
};

任何帮助将不胜感激,谢谢!

4

1 回答 1

1

不要使用oscillator.stop(now). 用于oscillator.stop(osc1ReleaseVal)安排振荡器在增益变为 0 的同时停止。

您不必断开和删除振荡器。一旦停止,振荡器可以自行断开与增益节点的连接。如果你放弃对振荡器的引用,它可能会被垃圾收集。

于 2017-04-25T14:45:32.757 回答