使用Web Audio API播放重叠的wav文件时音量会发生变化

时间:2013-07-28 11:46:08

标签: javascript web-audio

在谷歌浏览器中:

播放一个.wav文件,循环播放。另一个.wav文件会不时播放作为声音效果。

播放声音效果时,循环声音的音量会自动降低。体积在约15秒内再次逐渐增加。

(我猜它会自动躲避http://en.wikipedia.org/wiki/Ducking

当播放声音效果时,我不希望循环的音量减小。我该如何防止这种行为?

示例:http://www.matthewgatland.com/games/takedown/play/web/audiofail.html

window.AudioContext = window.AudioContext||window.webkitAudioContext;
var context = new AudioContext();

var play = function (buffer, loop) {
    var source = context.createBufferSource();
    source.buffer = buffer;
    if (loop) source.loop = true;
    source.connect(context.destination);
    source.start(0);
};

var load = function (url, callback) {
    var request = new XMLHttpRequest();
    request.open('GET', url, true);
    request.responseType = 'arraybuffer';
    request.onload = function() {
    context.decodeAudioData(request.response, function(buffer) {
          callback(buffer);
        }, null);
    };
    request.send();
};

var musicSound;
var thudSound;
load("res/snd/music0.wav", function (buffer) {
    musicSound = buffer;
});
load("res/snd/thud0.wav", function (buffer) {
    thudSound = buffer;
});

声音加载后,请致电:

play(musicSound, true); //start the music looping

//each time you call this, the music becomes quiet for a few seconds
play(thudSound, false);

1 个答案:

答案 0 :(得分:1)

在将其放入网站之前,您可能需要进行一些声音设计。我不知道你正在使用什么编辑器,但你可能想要一起编辑声音,以便它们的所有级别更接近原始循环声音的级别。这样他们的水平差异就不会引发自动增益减少。两种声音的组合太大声,因此两者的声音会降低较软声音的水平。因此,如果你将它们放在一起,那么当增益减少开始时,整体差异不应该那么大。

相关问题