api - 为什么GainNode连接不起作用?

标签 api audio web web-audio-api

我正在使用Web音频API从声音文件中获取频率数据。基本上,我已经实现了此example中显示的代码,我想要添加的代码是gainNode,因此我可以在代码的任何位置控制音量,但是我所做的连接有问题,其他一切都很好。

音量部分是我对原始代码所做的唯一更改:

request.onload = function() {
    context.decodeAudioData(
        request.response,
        function(buffer) {
            if(!buffer) {
                $('#info').text('Error decoding file data');
                return;
            }

            sourceJs = context.createJavaScriptNode(2048);
            sourceJs.buffer = buffer;
            sourceJs.connect(context.destination);
            analyser = context.createAnalyser();
            analyser.smoothingTimeConstant = 0.6;
            analyser.fftSize = 512;

            source = context.createBufferSource();
            source.buffer = buffer;
            source.loop = true;

            source.connect(analyser);
            analyser.connect(sourceJs);
            source.connect(context.destination);

                       //////////////////////////////////// 
                       //////////VOLUME////////////////////                            
                       gainNode = context.createGain();                        
                       source.connect(gainNode);                         
                       gainNode.connect(context.destination);
                       //////////////////////////////////////

            sourceJs.onaudioprocess = function(e) {
                array = new Uint8Array(analyser.frequencyBinCount);
                analyser.getByteFrequencyData(array);
                boost = 0;
                for (var i = 0; i < array.length; i++) {
                    boost += array[i];
                }
                boost = boost / array.length;
            };



            // popup
            //aca avisa cuando ya cargo el buffer
        },
        function(error) {
            $('#info').text('Decoding error:' + error);
        }
    );
};

然后我用它来关闭音量但不起作用:... gainNode.gain.value = 0;

最佳答案

您需要将增益节点作为source输出连接的唯一对象。

现在,它具有多个连接,您的增益节点只是其中之一-这意味着它不会影响整个信号。

关于api - 为什么GainNode连接不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17248166/

相关文章:

html - 如何在 chrome <HTML> 中显示图标

javascript - Uncaught ReferenceError : function is not defined; trying to build weather site

node.js - 基于 Content-Type header 的 Expressjs 路由器

python - numpy RFFT/IRFFT音量

c# - 将 MIDI 字节数组转换为 MP3 字节数组

java - 处理 - 根据 mousePressed 播放不同的音频

c# - 即使 ID 存在,Findcontrol 也返回 null

javascript - Google Maps API v3 For 循环问题

python - 使用 Python 访问 Azure API

ruby-on-rails - 在设计 [Rails 4] 之上滚动 token 认证机制