我从服务器获取PCM数据。一个接一个地播放它们的最佳方式是什么?我目前为每个样本批次调用以下函数:
function playSamples( samples ) {
let count = samples.length / 2;
let leftData = new Float32Array( count );
let rightData = new Float32Array( count );
for ( let t = 0; t < count; ++ t ) {
leftData[ t ] = samples[ t * 2 + 0 ] / 0x7FFF;
rightData[ t ] = samples[ t * 2 + 1 ] / 0x7FFF;
}
let audioBuffer = this.context.createBuffer( 2, count, this.frequency );
audioBuffer.getChannelData( 0 ).set( leftData );
audioBuffer.getChannelData( 1 ).set( rightData );
let sourceNode = this.context.createBufferSource( );
sourceNode.buffer = audioBuffer;
sourceNode.connect( this.context.destination );
sourceNode.start( this.endTime );
this.endTime += audioBuffer.duration;
}
基本上,我为每个批次设置了一个新缓冲区,我计划在上一批次结束时播放。它有点工作,但音频不够好,因为我期望来自样本的笨拙连接的很多噪音。有没有更好的方法来做我想要的?