1
由於我是PortAudio的新手,我嘗試了一個來自Internet的示例程序。該程序能夠通過回調函數記錄麥克風的輸入。PortAudio通過音頻數據迭代
我想獲得表示爲數字值(例如浮子)所記錄的音頻的每一個樣品。我無法確定麥克風錄製數據的存儲位置。
這是回調函數:
static int recordCallback(const void *inputBuffer, void *outputBuffer,
unsigned long framesPerBuffer,
const PaStreamCallbackTimeInfo* timeInfo,
PaStreamCallbackFlags statusFlags,
void *userData)
{
paTestData *data = (paTestData*)userData;
const SAMPLE *rptr = (const SAMPLE*)inputBuffer;
SAMPLE *wptr = &data->recordedSamples[data->frameIndex * NUM_CHANNELS];
long framesToCalc;
long i;
int finished;
unsigned long framesLeft = data->maxFrameIndex - data->frameIndex;
(void)outputBuffer; /* Prevent unused variable warnings. */
(void)timeInfo;
(void)statusFlags;
(void)userData;
if (framesLeft < framesPerBuffer)
{
framesToCalc = framesLeft;
finished = paComplete;
}
else
{
framesToCalc = framesPerBuffer;
finished = paContinue;
}
if (inputBuffer == NULL)
{
for (i = 0; i<framesToCalc; i++)
{
*wptr++ = SAMPLE_SILENCE; /* left */
if (NUM_CHANNELS == 2) *wptr++ = SAMPLE_SILENCE; /* right */
}
}
else
{
cout << endl << "SAMPLE" << endl;
for (i = 0; i<framesToCalc; i++)
{
*wptr++ = *rptr++; /* left */
//cout << rptr<<endl;
if (NUM_CHANNELS == 2) *wptr++ = *rptr++; /* right */
}
}
data->frameIndex += framesToCalc;
return finished;
}
音頻輸入流在這裏初始化:
err = Pa_OpenStream(
&stream,
&inputParameters,
NULL, /* &outputParameters, */
SAMPLE_RATE,
FRAMES_PER_BUFFER,
paClipOff, /* we won't output out of range samples so don't bother clipping them */
recordCallback,
&data);
你能告訴我在初始化(第二代碼剪切)的哪個參數是inputBuffer?我現在明白了它在回調中的位置。 – bingoStack
@bingoStack初始化時沒有輸入緩衝區。它將通過回調函數傳遞給您。您可以通過inputParameters結構來調整,以告知您需要多少個通道以及示例格式,請參閱http://portaudio.com/docs/v19-doxydocs/structPaStreamParameters.html – Archie
謝謝!雖然,我無法讀取數據。我剛剛添加了'cout << *(float *)(inputBuffer)<< endl';在回調中返回完成之前,我得到0或+/- 3.0517e-005。哪裏不對? – bingoStack