Javascript 如何在使用compressor.reduction.value监视压缩减少时修复冻结的div

Javascript 如何在使用compressor.reduction.value监视压缩减少时修复冻结的div,javascript,settimeout,html5-audio,web-audio-api,Javascript,Settimeout,Html5 Audio,Web Audio Api,我的问题如下。 我正在尝试将compressor节点的compressor.reduction.value连接到div的高度,以便动态监视压缩效果。这个很好用。问题是当音频信号停止时,div冻结在其当前位置。我希望潜水舱不要冻结,它的高度归零。我解决这个问题的方法是使用一个setInterval来检查div的高度,如果它保持在完全相同的高度超过几秒钟,则显示设置为none,从而有效地隐藏div。现在我的问题有两个。首先,如果有更好的方法,请分享,但不管怎样,有一件小事情让我恼火,我想不出来。当我

我的问题如下。 我正在尝试将compressor节点的compressor.reduction.value连接到div的高度,以便动态监视压缩效果。这个很好用。问题是当音频信号停止时,div冻结在其当前位置。我希望潜水舱不要冻结,它的高度归零。我解决这个问题的方法是使用一个setInterval来检查div的高度,如果它保持在完全相同的高度超过几秒钟,则显示设置为none,从而有效地隐藏div。现在我的问题有两个。首先,如果有更好的方法,请分享,但不管怎样,有一件小事情让我恼火,我想不出来。当我这样写代码时,它就工作了。但是,它看起来有点难看,因为压缩器节点在播放功能之外

var compressor = audioContext.createDynamicsCompressor();

soundObj.play = function() {

    $(".compression-meter").css("display", "block");
    var playSound = audioContext.createBufferSource();
    compressor.threshold.value = -40;
    compressor.ratio.value = 20;

    playSound.buffer = soundObj.soundToPlay;
    playSound.connect(compressor);
    compressor.connect(audioContext.destination)
    playSound.start(audioContext.currentTime);
    compReductionMeter()
}

/*______________ Compressor metering __________*/

var cachedMeterValue = null

function compReductionMeter() {
    cachedMeterValue = $(".compression-meter").height()
    var reduction = compressor.reduction.value;
    var bar = $(".compression-meter");
    bar.height((-1 * reduction) + '%');
    requestAnimationFrame(compReductionMeter);

};



window.setInterval(function() {
    if ($(".compression-meter").height() == cachedMeterValue) {
        console.log("checking compression meter height when matched with cachedMeterValue.It is " + $(".compression-meter").height())
        $(".compression-meter").css("display", "none")
    }
}, 2000);
当我写这样的代码时,div甚至没有出现,我不知道为什么。从我的观点来看,它“应该”起作用,我真的很想知道为什么它不起作用,以及我遗漏了什么

   soundObj.play = function() {

            $(".compression-meter").css("display", "block");
            var playSound = audioContext.createBufferSource();
            var compressor = audioContext.createDynamicsCompressor(); // modified placement
            compressor.threshold.value = -40;
            compressor.ratio.value = 20;

            playSound.buffer = soundObj.soundToPlay;
            playSound.connect(compressor);
            compressor.connect(audioContext.destination)
            playSound.start(audioContext.currentTime);
            compReductionMeter(compressor.reduction.value)            // modified - added argument
        }

        /*______________ Compressor metering __________*/

        var cachedMeterValue = null

        function compReductionMeter(compVal) {                        // modified - added parameter
            cachedMeterValue = $(".compression-meter").height()
            var reduction = compVal;                                  // modified - is now param value
            var bar = $(".compression-meter");
            bar.height((-1 * reduction) + '%');
            requestAnimationFrame(compReductionMeter);

        };



        window.setInterval(function() {
            if ($(".compression-meter").height() == cachedMeterValue) {
                console.log("checking compression meter height when matched with cachedMeterValue.It is " + $(".compression-meter").height())
                $(".compression-meter").css("display", "none")
            }
        }, 2000);

谢谢。

不幸的是,
DynamicCompressorNode
的当前设计使增益减少值在源节点的流停止时不会被更新。也就是说,GR值仅在活动音频流运行时更新
AnalyserNode
也有同样的问题

如果您的音频图仅使用单个源节点,则可以从源节点使用
.onended
事件将DIV的高度归零。但是,如果您更喜欢使用复杂的音频图,那么它将更加复杂


这里有一个可能的黑客程序,可以将压缩机和分析仪的零点设置为零。创建一个全零的新缓冲区。将其分配给新的AudioBufferSourceNode。将该节点连接至压缩机和/或分析仪,并计划在源结束时(或稍早)启动源。这应保持压缩机/分析仪节点处理,以便GR值和分析仪节点降至零


实际上我并没有尝试过这一点。

DynamicComporessorNode中的这一烦恼将在Chrome版本M40中修复

我很好奇,一旦信号下降到某个阈值以下,是否有一种(我敢说是简单的..ha!)方法将一些死噪声附加到缓冲流的末尾,作为使com减少值或analyzer节点降至“零”的一种手段。我认为我在上面代码中的方法更简单,但也许用另一种方法实现的实用程序会有所帮助。