Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/162.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在windows phone中使用libsamplerate重新采样音频_C++_Resampling_Wasapi_Audio Capture - Fatal编程技术网

C++ 在windows phone中使用libsamplerate重新采样音频

C++ 在windows phone中使用libsamplerate重新采样音频,c++,resampling,wasapi,audio-capture,C++,Resampling,Wasapi,Audio Capture,我试图在一个使用WASAPI的windows phone项目中,使用libsamplerate将捕获的2声道/48khz/32位音频重新采样为1声道/8khz/32位 我需要通过重新采样从960个原始帧中获取160帧。使用GetBuffer方法捕获音频后,我将捕获的7680字节字节的字节数组发送到以下方法: void BackEndAudio::ChangeSampleRate(BYTE* buf) { int er2; st=src_new(2,1,&er2); //SRC_DATA

我试图在一个使用WASAPI的windows phone项目中,使用libsamplerate将捕获的2声道/48khz/32位音频重新采样为1声道/8khz/32位

我需要通过重新采样从960个原始帧中获取160帧。使用
GetBuffer
方法捕获音频后,我将捕获的7680字节字节的字节数组发送到以下方法:

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;

}
void BackEndAudio::ChangeSampleRate(字节*buf)
{
int-er2;
st=src_新(2,1和er2);
//之前定义的SRC_数据sd
sd=新的SRC_数据;
字节*onechbuf=新字节[3840];
int outputIndex=0;
//将立体声转换为单声道
对于(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];
}
浮动*res1=新浮动[960];
res1=(float*)onechbuf;
浮动*res2=新浮动[160];
//更换取样器
sd->data_in=res1;
sd->data\u out=res2;
sd->input_frames=960;
sd->输出帧=160;
sd->src_比率=(双)1/6;
sd->输入的结束=1;
int er=src_过程(st,sd);
transportController->WriteAudio((字节*)RES2640);
删除[]onechbuf;
src_删除(st);
删除sd;
}
src_process方法不返回任何错误,
sd->input_frames\u used
设置为960,
sd->output_frames\u gen
设置为159,但渲染输出仅为噪波。 我在实时VoIP应用程序中使用该代码。
问题的根源是什么?

我发现了问题。我不应该创建一个新的
SRC_STATE
对象,并通过调用
st=SRC_new(2,1和er2)在每次调用函数时删除它
src_删除(st)
但是调用它们一次就足够整个音频重新采样了。另外,对于
SRC\u数据
,也不需要使用指针。我修改了我的代码如下,它现在工作良好

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);
}
void BackEndAudio::ChangeSampleRate(字节*buf)
{
字节*onechbuf=新字节[3840];
int outputIndex=0;
//将立体声转换为单声道
对于(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];
}
浮动*out=新浮动[160];
//更换取样器
sd.data_in=(float*)onechbuf;
sd.data_out=out;
sd.input_frames=960;
sd.输出_帧=160;
标准差:src_比率=(双)1/6;
_输入的sd.end_=0;
int er=src_过程(st和sd);
}

您是否先将整数转换为浮点采样?也就是说,
buf
包含整数样本还是浮点样本?这里有内存泄漏:
float*res1=newfloat[960]因为您立即重新分配给<代码> RES1,然后松开分配给内存的唯一指针。@ Nabla谢谢您的提示。我不太擅长C++。但是我不认为这是主要问题。@哈希尼不,不是,这就是为什么我把它作为评论发表的原因。但当你的程序占用了你所有的系统内存时,你迟早会遇到麻烦。你是如何生成一个简单的dll的?我们可以直接获取winmd文件吗?