简体   繁体   English

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

[英]Volume changing when playing overlapping wav files with Web Audio API

In Google Chrome: 在谷歌浏览器中:

One .wav file is played, looping. 播放一个.wav文件,循环播放。 Another .wav file is played from time to time as a sound effect. 不时播放另一个.wav文件作为声音效果。

When the sound effect plays, the volume of the looping sound automatically decreases. 播放声音效果时,循环声音的音量会自动减小。 The volume gradually increases again over about 15 seconds. 音量在大约15秒内再次逐渐增加。

(I guess it's automatically ducking http://en.wikipedia.org/wiki/Ducking ) (我猜这是自动回避http://en.wikipedia.org/wiki/Ducking

I don't want the volume of the loop to decrease when the sound effect plays. 我不希望在播放声音效果时减少循环的音量。 How can I prevent this behaviour? 我如何防止这种行为?

Example: http://www.matthewgatland.com/games/takedown/play/web/audiofail.html 示例: 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;
});

Once the sounds have loaded, call: 加载声音后,请致电:

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

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

You might have to do some sound design before you put this into your website. 在将其放入您的网站之前,您可能需要做一些声音设计。 I don't know what you are using for an editor but you might want to edit the sounds together so that their over all level is closer to the level of the original looping sound. 我不知道您要使用的编辑器是什么,但是您可能希望一起编辑声音,以使它们的整体音量更接近原始循环声音的音量。 That way their won't be as dramatic a difference in levels that is triggering the automatic gain reduction. 这样,它们不会在触发自动增益降低的电平上产生巨大差异。 The combination of both sounds is too loud so the louder of the two will bring down the level of the softer one. 两种声音的组合太大,因此两者的声音较大会降低较柔和的声音的音量。 So if you bring them closer together in level the overall difference shouldn't be as drastic when or if the gain reduction kicks in. 因此,如果您将它们在水平上拉得更近一些,那么在增益降低开始或消失时,总体差异就不会那么大。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM