PortAudio: How to get record from mic (get data)
I am trying to use portaudio (being cross platform capability), to read in from microphone, then i want to put that data through a FFT, but i am not so sure how to do it. Alot of people have told me: 1.get the data, 2.apply fft, but the problem is i am not so sure how to get the data, portaudio doesn't have much tutorials on grabbing input from mic, if you know any code, tutorials or any other resource it would be great full. 开发者_高级运维I been searching on this for a while now. Please help
The portaudio distribution has documentation in the form of example C programs. They are in the test
directory and are usually called patest_...
. There is lots of good material there and the docs contain an overview with very short description,
The one you want to look at is patest_record
, which does asynchronous recording via a call-back. This is the way to go if you want to do anything serious, IMHO. But there is also patest_read_record.c
, which does synchronous (blocking) IO.
The code is actually very easy, here are the relevant parts (lots of stuff left out): 1/ you malloc a buffer 2/ you install a callback 3/ in the callback, you copy the data to your buffer
/* 1 */
data.recordedSamples = (SAMPLE *) malloc( numBytes );
/* 2 */
err = Pa_OpenStream(
&stream,
&inputParameters,
NULL, /* &outputParameters, */
SAMPLE_RATE,
FRAMES_PER_BUFFER,
paClipOff,
recordCallback,
&data );
/* 3, in recordCallBack with rptr the input data and wptr our buffer */
for( i=0; i<framesLeft; i++ )
{
*wptr++ = *rptr++; /* left */
if( NUM_CHANNELS == 2 ) *wptr++ = *rptr++; /* right */
}
Again, this is simplified, but you get the idea. There is a fair amount of bookkeeping and the sample code isn't the squeeky cleanest, but it is easy to adapt to your purposes.
精彩评论