我正在使用opencv(用於對象識別)與portaudio結合播放基於視頻輸入的聲音。基本上,我的目標是以不同的速率播放一定音高/頻率的正弦波音。它有效,但結果非常不可預測。有時候音頻回放會起作用(程序運行緩慢,但工作起來),有時候不會發生音頻回放。簡而言之,這是我的程序所做的:如何避免使用PortAudio和OpenCV進行不一致的音頻播放?
開始攝像頭圖像 - >獲取攝像頭圖像 - >在圖像中選擇區域 - >返回視頻輸入 - > while(frame exists) - >跟蹤對象位置 - >初始化端口音頻工具 - >播放基於位置的聲音 - >終止Portaudio工具
我似乎無法弄清爲什麼音頻播放不一致。你們有沒有任何提示?我一直在閱讀,我的想法是,這是一個延遲問題,但我真的沒有經歷過這件事。當我在沒有opencv的情況下使用portaudio時,沒有延遲問題發生,所以我知道它與將這兩者結合起來。任何幫助表示讚賞。
while (frame)
{
cvCopyImage(frame, drawImg);
// process
track(frame);
// get result
CvRect r;
float confidence;
bool valid;
/* getRoi tells us if the region being tracked on the screen
* is the same region that we chose prior to entering this while loop
*/
getRoi(&r, &confidence, &valid);
// show
cvDrawRect(drawImg, cvPoint(r.x, r.y),
cvPoint(r.x + r.width - 1, r.y + r.height - 1),
valid ? cvScalar(0, 255, 0) : cvScalar(0, 255, 255),
2
);
writeLogo(drawImg,"USC-IRIS");
int xpos = r.x;
int ypos = r.y;
cvShowImage("Tracking", drawImg);
cout << "valid " << valid << endl;
cout << "conf val " << confidence << endl;
cout << "xpos, ypos " << xpos << ", " << ypos << endl;
//If the region on the screen is the region we chose
//then we should play specific sounds
if(valid){
sI->soundWrite(xpos, ypos);
float freq = sI->getFreq();
int amp = sI->getAmp();
float pulse = sI->getPulse();
switch(amp){
case 0:
//printf("Hear sound in both ears.\n");
data.targetBalance = .5;
break;
case 1:
//printf("Hear sound in left ear.\n");
data.targetBalance = 0;
break;
case 2:
//printf("Hear sound in right ear.\n");
data.targetBalance = 1;
break;
default:
//printf("Incorrect value for amp (left/right sound indicator)");
data.targetBalance = .5;
break;
}
err = Pa_Initialize(); //scan for available devices i.e. audio jack, headphones
if(err != paNoError) {
printf("init\n");
goto error;
}
//open the sound stream for processing
err = Pa_OpenDefaultStream(&stream, 0, 2, paFloat32, SAMPLE_RATE,
256, patestCallback, &data); //open the sound stream for processing
if(err != paNoError) {
printf("open\n");
goto error;
}
//start the stream (i.e. play sound) if no errors
err = Pa_StartStream(stream);
if(err != paNoError) {
printf("start\n");
goto error;
}
//check which ear(s) the sound should be played to
//hold that tone for a certain amount of time (pulse*200 millisec)
Pa_Sleep(pulse*200);
cout << "pulse: " << pulse << endl << "freq: " << freq << endl;
cout << "amp: " << amp << endl;
//stop the stream (i.e. stop playing sound)
err = Pa_StopStream(stream);
if(err != paNoError) {
printf("stop\n");
goto error;
}
err = Pa_CloseStream(stream);
if(err != paNoError) {
printf("close\n");
goto error;
}
err = Pa_Terminate();
if(err != paNoError) {
printf("term\n");
goto error;
}
}
int key = cvWaitKey(1);
// write
if (output_txt)
fprintf(output_txt, "%d %d %d %d\n", r.x, r.y, r.width, r.height);
if (output_avi)
cvWriteFrame(output_avi, drawImg);
// next
if (key == 'q'||key=='Q')
break;
frame = cvQueryFrame(capture);
}
這樣做時的CPU負載是多少?這可能只是一個負載問題?如果降低幀率會發生什麼? – 2012-02-23 08:56:13
這不是負載,因爲降低幀速率/ framesPerBuff沒有幫助。我想通了,但我會把它發佈在這個頁面上。謝謝您的幫助。 – nmante 2012-03-09 07:10:32