프로젝트의 경우 자바 서버에서 웹 소켓을 통해 라이브 오디오 스트림을 가져옵니다. 서버에서 샘플을 16 비트/8000hz/모노 형식으로 8 비트 부호있는 바이트 값으로 처리합니다 (한 샘플을 구성하는 2 바이트). 그러나 브라우저에서 지원되는 가장 낮은 샘플은 22050hz입니다. 그래서 제 생각은 기존의 8000에서 32000 hz까지 "단순하게"업 샘플링하는 것이 었습니다. 이것은 지원되고 쉽게 계산할 수있는 것처럼 보입니다.업 샘플링 WebAudioApi를 사용하여 자바 스크립트 PCM 데이터
지금까지 linear upsampling과 cosine interpolation을 시도했지만 둘 모두 작동하지 않았습니다. 정말로 왜곡 된 소리 외에도 첫 번째 소리는 약간의 잡음을 더했습니다. Chrome의 WebAudioAPI에도 문제가있을 수 있지만 적어도 소리는 들리고 간신히 알아볼 수 있어야합니다. 그래서 나는 코덱이나 엔디안 문제가 없다고 생각합니다.
다음은 사운드 데이터가있는 바이너리 패킷을 수신 할 때 실행되는 완전한 코드입니다. 나는 단순함을 위해서 새로운 버퍼와 버퍼 소스를 만들고있다. (예, 성능에는 좋지 않다.) data
은 ArrayBuffer입니다. 먼저, 샘플을 Float으로 변환합니다. 그런 다음 업 샘플링합니다.
//endianess-aware buffer view
var bufferView=new DataView(data),
//the audio buffer to set for output
buffer=_audioContext.createBuffer(1,640,32000),
//reference to underlying buffer array
buf=buffer.getChannelData(0),
floatBuffer8000=new Float32Array(160);
//16Bit => Float
for(var i=0,j=null;i<160;i++){
j=bufferView.getInt16(i*2,false);
floatBuffer8000[i]=(j>0)?j/32767:j/-32767;
}
//convert 8000 => 32000
var point1,point2,point3,point4,mu=0.2,mu2=(1-Math.cos(mu*Math.PI))/2;
for(var i=0,j=0;i<160;i++){
//index for dst buffer
j=i*4;
//the points to interpolate between
point1=floatBuffer8000[i];
point2=(i<159)?floatBuffer8000[i+1]:point1;
point3=(i<158)?floatBuffer8000[i+2]:point1;
point4=(i<157)?floatBuffer8000[i+3]:point1;
//interpolate
point2=(point1*(1-mu2)+point2*mu2);
point3=(point2*(1-mu2)+point3*mu2);
point4=(point3*(1-mu2)+point4*mu2);
//put data into buffer
buf[j]=point1;
buf[j+1]=point2;
buf[j+2]=point3;
buf[j+3]=point4;
}
//playback
var node=_audioContext.createBufferSource(0);
node.buffer=buffer;
node.connect(_audioContext.destination);
node.noteOn(_audioContext.currentTime);
샘플 속도를 다른 속도로 변환하려면 어떻게해야합니까? 뿐만 아니라 8000에서 32000 – noamtcohen