我已添加以下代码。我能够使用麦克风捕捉音频,我可以同时捕捉所有音频。我的最终目标是进行实时音频处理。所以在for循环中,我将捕获音频处理它并将输出放在一个文件中。
使用下面的代码,我只能在for循环的第一遍中捕获音频,同样的音频被复制到其余循环中的Cinput
变量中。有没有办法捕获实时音频并使用端口音频以最小延迟进行处理?这是我发现的唯一可以使用的跨平台API。目前我在C语言的Windows 8上使用Visual Studio。但是,稍后我可以处理Linux或Mac。
PaStreamParameters inputparameters;
PaStream* stream;
PaError err = paNoError;
paTestData data;
data.maxFrameIndex = chunkSz * 100;
data.frameIndex = 0;
err = Pa_Initialize();
if (err != paNoError) return;
inputparameters.device = Pa_GetDefaultInputDevice();
if (inputparameters.device == paNoDevice) {
fprintf(stderr, "Error: No default input device.\n");
return;
}
inputparameters.channelCount = 2;
inputparameters.sampleFormat = paInt16;
inputparameters.suggestedLatency = Pa_GetDeviceInfo(inputparameters.device)->defaultLowInputLatency;
inputparameters.hostApiSpecificStreamInfo = NULL;
data.livebuf = (short*)malloc(sizeof(short)*(chunkSz)* 100 * 2);
short *Cinput;
Cinput = (short*)malloc(10*chunkSz * 2 *100* sizeof(short));
memset(Cinput, 0, sizeof(short)*10*100*2*chunkSz);
int newwtemp = 0;
for (int i = 0; i < 10; i++){
err = Pa_OpenStream(&stream, &inputparameters, NULL, 8000, chunkSz * 100, paClipOff, recordCallback, &data);
if (err != paNoError) {
printf("Exiting from Openstream\n");
return;
}
err = Pa_StartStream(stream);
if (err != paNoError) {
printf("Exit from start stream\n");
return; }
//printf("\n\n...................now Recording! please speak into the mike....................\n\n"); fflush(stdout);
while ((err = Pa_IsStreamActive(stream)) == 1)
{
Pa_Sleep(1);
//printf("index = %d\n", data.frameIndex); fflush(stdout);
}
if (err < 0) {
printf("In Is stream active error return\n");
return;
}
for (int k = 0; k < chunkSz*100 * 2; k++){
Cinput[newwtemp] = data.livebuf[k];
// data.livebuf[k] = 0;
newwtemp++;
}
Pa_StopStream(stream);
}
err = Pa_CloseStream(stream);
if (err != paNoError){ return; }
FILE *fid;
fid = fopen("recorded.raw", "wb");
if (fid == NULL)
{
printf("Could not open file.");
}
else
{
fwrite(Cinput, sizeof(short), chunkSz *10* 100*2, fid);
fclose(fid);
printf("Wrote data to 'recorded.raw'\n");
}
//////////////////////////////////////