使用AudioBuffer播放PCM数据时,HTML5 Web Audio API会产生奇怪的噪音

时间:2016-11-01 00:14:19

标签: javascript html5 audio web-audio-api

我试图使用HTML5 Web Audio缓冲区来播放音符缓冲区,但是当音符开始和结束时,我发现有奇怪的,响亮的,不需要的噪音。有谁知道怎么摆脱它?谢谢!

我在这里有一个有效的例子,我已经重复了这个说明,所以你清楚地说明了这个噪音:

https://jsfiddle.net/charrli/ca48tj23/5/

代码如下。我正在产生正弦波:

   var audioContext = new AudioContext();
var audioBuffer;// = new AudioBuffer();
var audioBufferArray;//: number[];
var audioBufferArrayIndex;//: number;
var frameCount = 8192;
    audioBuffer = audioContext.createBuffer(1, frameCount, 44100);

function play(){
    //audioContext = new AudioContext();
      audioBufferArray = this.audioBuffer.getChannelData(0);
        audioBufferArrayIndex = 0;
        for (var i = 0; i < frameCount; i++) {
            audioBufferArray[i] = Math.sin((i%168)/168.0*Math.PI*2);
        }
        var source = this.audioContext.createBufferSource();
                    // set the buffer in the AudioBufferSourceNode
            source.buffer = this.audioBuffer;
                    // connect the AudioBufferSourceNode to the
                    // destination so we can hear the sound
        source.connect(this.audioContext.destination);
                    // start the source playing
        source.start();
}

1 个答案:

答案 0 :(得分:0)

您需要平滑波的开始/结束以防止点击

添加:

  for (var i = 0; i < 100; i++) {
    audioBufferArray[i] = audioBufferArray[i]*i/100;//fade in
    audioBufferArray[frameCount-i-1] = audioBufferArray[frameCount-i-1]*i/100;//fade out
    }

https://jsfiddle.net/sss1024/ca48tj23/7/