网络音频增益节点不完全静音

时间:2017-07-03 10:14:06

标签: javascript audio web-audio-api

我一直试图在我的应用中完全过滤,衰减或静音音频。使用此代码,我已经能够使用我找到的解决方案略微安静我的音频,但设置gainNode.gain.value = 0实际上并没有将它设置为0,负整数使它更响亮。我认为我可以通过2条路径来播放声音,并且增益节点只是其中之一,但我不知道如何隔离它。我添加的滤镜到目前为止都没有任何作用,但我只是希望能够将声音剪掉。

window.onload = function () {
var volumeBars = {
    mono: document.getElementById("monoFill")
};

document.getElementById("open-file").onchange = function (evt) {
    var file = evt.target.files[0];
    var reader = new FileReader();
    reader.onload = function (e) {
        playSound(e.target.result);
    }
    reader.readAsArrayBuffer(file);
}
document.getElementById("open-file1").onchange = function (evt) {
    var file = evt.target.files[0];
    var reader = new FileReader();
    reader.onload = function (e) {
        playSound1(e.target.result);
    }
    reader.readAsArrayBuffer(file);
}

var context = new AudioContext();
var context1 = new AudioContext();

var playbackControl = document.querySelector('.playback-rate-control');
    var playbackValue = document.querySelector('.playback-rate-value');
    var panControl = document.querySelector('.panning-control')
    var panValue = document.querySelector('.panning-value');
    var mute = document.querySelector('.mute');
    var gainSlider = document.getElementById("gainSlider");
    var filtControl = document.querySelector(".filter-control")
    var filtValue = document.querySelector(".filter-value");

    function playSound(arraybuffer) {
        context.close();
        context = new AudioContext();

        var source = context.createBufferSource();
        var gainNode = context.createGain();
        var panNode = context.createStereoPanner();
        var filter = context.createBiquadFilter();
        filter.type = 'lowpass';



        source.connect(panNode);
        panNode.connect(context.destination);
        context.decodeAudioData(arraybuffer, function (buffer) {
            source.buffer = buffer;
            source.loop = true;
            source.playbackRate.value = playbackControl.value;


            source.connect(gainNode);

            gainNode.connect(context.destination);
            source.connect(filter);
            filter.connect(gainNode);

            //speed slider
            playbackControl.oninput = function () {
                source.playbackRate.value = playbackControl.value;
                playbackValue.innerHTML = playbackControl.value;
            }
            //pan slider
            panControl.oninput = function () {
                panNode.pan.value = panControl.value;
                //   panValue.innerHTML = panControl.value;
            }
            //filter slider
            filtControl.oninput = function () {
                filter.frequency.value = filtControl.value;
                filtValue.innerHTML = filtControl.value;
            }

            gainSlider.addEventListener('change', function () {
                gainNode.gain = this.value;
            });

            mute.onclick = voiceMute;

            function voiceMute() {
                if (mute.id == "") {
                    gainNode.gain.value = 0;
                    mute.id = "activated";
                    mute.innerHTML = "Unmute";
                } else {
                    gainNode.gain.value = 1;
                    mute.id = "";
                    mute.innerHTML = "Mute";
                }
            }
        });
<div>
  <button class="mute">Mute button</button>
</div>
<p><input class="filter-control" type="range" min="0" max="400" step="1" value="400">
<span class="filter-value">1.0</span></p>
<p><input id="gainSlider" type="range" min="0" max="1" step=".05" value="1">Volume</p>
<p><input class="playback-rate-control" type="range" min="0.25" max="3" step="0.01" value="1">
<span class="playback-rate-value">1.0</span></p>
<p><input class="panning-control" type="range" min="-1" max="1" step=".05" value="0">Pan</p>

1 个答案:

答案 0 :(得分:0)

添加断开节点的功能以防止音频到达目的地

pause=function(){
filter.disconnect();
};
// and to play
var play=function(){
filter.connect(gainNode);
};