[英]HTML5 Web Audio API has strange noise when playing PCM data with AudioBuffer
我正在嘗試使用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();
}
您需要平滑波的開始/結束以防止點擊
添加這個:
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 }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.