[英]Resample audio using libsamplerate in windows phone
我正在嘗試使用WASAPI在Windows Phone項目中使用libsamplerate將捕獲的2channel / 48khz / 32bit音頻重新采樣為1channel / 8khz / 32bit。
我需要通過重新采樣從960個原始幀中獲取160個幀。使用GetBuffer
方法捕獲音頻后,將捕獲的7680字節的BYTE數組發送到以下方法:
void BackEndAudio::ChangeSampleRate(BYTE* buf)
{
int er2;
st=src_new(2,1,&er2);
//SRC_DATA sd defined before
sd=new SRC_DATA;
BYTE *onechbuf = new BYTE[3840];
int outputIndex = 0;
//convert Stereo to Mono
for (int n = 0; n < 7680; n+=8)
{
onechbuf[outputIndex++] = buf[n];
onechbuf[outputIndex++] = buf[n+1];
onechbuf[outputIndex++] = buf[n+2];
onechbuf[outputIndex++] = buf[n+3];
}
float *res1=new float[960];
res1=(float *)onechbuf;
float *res2=new float[160];
//change samplerate
sd->data_in=res1;
sd->data_out=res2;
sd->input_frames=960;
sd->output_frames=160;
sd->src_ratio=(double)1/6;
sd->end_of_input=1;
int er=src_process(st,sd);
transportController->WriteAudio((BYTE *)res2,640);
delete[] onechbuf;
src_delete(st);
delete sd;
}
src_process方法不返回錯誤,並且sd->input_frames_used
設置為960且sd->output_frames_gen
設置為159,但是呈現的輸出僅為噪聲。 我在實時VoIP應用程序中使用代碼。 問題的根源可能是什么?
我發現了問題。我不應該創建一個新的SRC_STATE
對象並在每次調用函數時都通過調用st=src_new(2,1,&er2);
刪除它st=src_new(2,1,&er2);
和src_delete(st);
但是調用它們一次就足以完成整個音頻重采樣了。也不需要為SRC_DATA
使用指針。 我如下修改了我的代碼,現在可以正常工作了。
void BackEndAudio::ChangeSampleRate(BYTE* buf)
{
BYTE *onechbuf = new BYTE[3840];
int outputIndex = 0;
//convert Stereo to Mono
for (int n = 0; n < 7680; n+=8)
{
onechbuf[outputIndex++] = buf[n];
onechbuf[outputIndex++] = buf[n+1];
onechbuf[outputIndex++] = buf[n+2];
onechbuf[outputIndex++] = buf[n+3];
}
float *out=new float[160];
//change samplerate
sd.data_in=(float *)onechbuf;
sd.data_out=out;
sd.input_frames=960;
sd.output_frames=160;
sd.src_ratio=(double)1/6;
sd.end_of_input=0;
int er=src_process(st,&sd);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.