当前位置:  开发笔记 > 编程语言 > 正文

Web Audio Api:如何添加工作的卷积器?

如何解决《WebAudioApi:如何添加工作的卷积器?》经验,为你挑选了2个好方法。

我想学习/做的事情:如何使用脉冲响应在我的代码沙箱中设置一个简单的工作卷积器(混响).我认为这与设置过滤器类似,但事情似乎有很大不同.

我尝试过:与所有新技术一样,事情变化很快,因此很难知道哪种实现是正确的,哪些是不正确的.我看了无数的WebAudio Api Convolver Tutorials,很多都很老,其他人都在工作,但是太过"臃肿",让人很难理解发生了什么.我试图从mozilla文档中实现一些示例:

我已经看过了:https://developer.mozilla.org/en-US/docs/Web/API/ConvolverNode/buffer

我的问题:如何在下面的上下文中正确集成一个卷积器?正如你所看到的那样,我试过但是无法弄明白这一点.

 window.addEventListener('load', init, false);

function init() {
    setupWebAudio();
}

function setupWebAudio() {
    var audio = document.getElementById('music');
    var context = new AudioContext();
    var source = context.createMediaElementSource(audio);
    var filter = context.createBiquadFilter();
    var convolver = context.createConvolver();
    var inpulseRes = "hall.mp3";

    var hallBuffer = inpulseRes;
    soundSource = context.createBufferSource();
    soundSource.buffer = hallBuffer;
    convolver.buffer = hallBuffer;

    filter.type = 'lowpass';
    filter.frequency.value = 400;

var theParent = document.getElementById("test");
    theParent.addEventListener("mousedown", doSomething, false);
    function doSomething(e) {
        if (e.target !== e.currentTarget) {
            if(e.target == theParent.children[0]){
                filter.frequency.value += 200;
            }
            else if(e.target == theParent.children[1]){
                 filter.frequency.value -= 200;
            }
            else if(e.target == theParent.children[2]){
                 filter.type = 'highpass';
            }               
        }
        e.stopPropagation();
    }

    source.connect(filter);
    source.connect(convolver);
    filter.connect(context.destination);
    audio.play();
}

cwilso.. 8

这是一个非常开放的问题; 你尝试了什么没有用,或者你错过了什么"冲动反应"应该是什么?如果是后者,搜索"脉冲响应文件",你会发现你可以使用的大量免费文件.您还可以将对数衰减曲线上的噪声生成到缓冲区中,您将获得基本的混响效果.创建impulseResponse缓冲区的基本方法:

function impulseResponse( duration, decay, reverse ) {
    var sampleRate = audioContext.sampleRate;
    var length = sampleRate * duration;
    var impulse = audioContext.createBuffer(2, length, sampleRate);
    var impulseL = impulse.getChannelData(0);
    var impulseR = impulse.getChannelData(1);

    if (!decay)
        decay = 2.0;
    for (var i = 0; i < length; i++){
      var n = reverse ? length - i : i;
      impulseL[i] = (Math.random() * 2 - 1) * Math.pow(1 - n / length, decay);
      impulseR[i] = (Math.random() * 2 - 1) * Math.pow(1 - n / length, decay);
    }
    return impulse;
}

你的代码有一个BufferSourceNode 指向同一缓冲区的卷积器,这几乎肯定是错的; 你通常不使用buffersource播放一个脉冲响应文件,你通常不会使用普通的声音文件作为脉冲响应.(如果冲动响应的作用不明确,请在维基百科上查找卷积.)您需要执行以下操作:

function setupWebAudio() {
    var audio = document.getElementById('music');
    var context = new AudioContext();
    var source = context.createMediaElementSource(audio);
    var convolver = context.createConvolver();
    var irRRequest = new XMLHttpRequest();
    irRRequest.open("GET", "hall.mp3", true);
    irRRequest.responseType = "arraybuffer";
    irRRequest.onload = function() {
        context.decodeAudioData( irRRequest.response, 
            function(buffer) { convolver.buffer = buffer; } );
    }
    irRRequest.send();
// note the above is async; when the buffer is loaded, it will take effect, but in the meantime, the sound will be unaffected.

    source.connect( convolver );
    convolver.connect( context.destination );
}


Raymond Toy.. 5

将卷积器的输出连接到某物.你现在拥有的是连接到卷积器的源,但是卷积器没有任何连接.作为第一次切割,convolver.connect(context.destination).



1> cwilso..:

这是一个非常开放的问题; 你尝试了什么没有用,或者你错过了什么"冲动反应"应该是什么?如果是后者,搜索"脉冲响应文件",你会发现你可以使用的大量免费文件.您还可以将对数衰减曲线上的噪声生成到缓冲区中,您将获得基本的混响效果.创建impulseResponse缓冲区的基本方法:

function impulseResponse( duration, decay, reverse ) {
    var sampleRate = audioContext.sampleRate;
    var length = sampleRate * duration;
    var impulse = audioContext.createBuffer(2, length, sampleRate);
    var impulseL = impulse.getChannelData(0);
    var impulseR = impulse.getChannelData(1);

    if (!decay)
        decay = 2.0;
    for (var i = 0; i < length; i++){
      var n = reverse ? length - i : i;
      impulseL[i] = (Math.random() * 2 - 1) * Math.pow(1 - n / length, decay);
      impulseR[i] = (Math.random() * 2 - 1) * Math.pow(1 - n / length, decay);
    }
    return impulse;
}

你的代码有一个BufferSourceNode 指向同一缓冲区的卷积器,这几乎肯定是错的; 你通常不使用buffersource播放一个脉冲响应文件,你通常不会使用普通的声音文件作为脉冲响应.(如果冲动响应的作用不明确,请在维基百科上查找卷积.)您需要执行以下操作:

function setupWebAudio() {
    var audio = document.getElementById('music');
    var context = new AudioContext();
    var source = context.createMediaElementSource(audio);
    var convolver = context.createConvolver();
    var irRRequest = new XMLHttpRequest();
    irRRequest.open("GET", "hall.mp3", true);
    irRRequest.responseType = "arraybuffer";
    irRRequest.onload = function() {
        context.decodeAudioData( irRRequest.response, 
            function(buffer) { convolver.buffer = buffer; } );
    }
    irRRequest.send();
// note the above is async; when the buffer is loaded, it will take effect, but in the meantime, the sound will be unaffected.

    source.connect( convolver );
    convolver.connect( context.destination );
}



2> Raymond Toy..:

将卷积器的输出连接到某物.你现在拥有的是连接到卷积器的源,但是卷积器没有任何连接.作为第一次切割,convolver.connect(context.destination).


是的,你需要使用XMLhttprequest来加载你的hall.mp3文件并使用decodeAudio将mp3转换为缓冲源.或者使用@ cwilso的impulseResponse函数创建一个脉冲响应,用于测试您是否按照自己的方式设置了所需的东西.
推荐阅读
mobiledu2402851173
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有