SDL_Events and Portaudio?

This will loop infinitely. finished never gets sent to 1, because the
key-down never gets handled, because it never goes back to the
SDL_PollEvent. You seem to be thinking the event loop will run in
parallel automatically, but it won’t. If PortAudio is using a callback
already, you may be able to just return to the event loop during that time,
and have a different function called to close the stream and free the
buffer when finished. Failing that, you may be able to run the audio in a
separate thread; see http://sdldoc.csn.ul.ie/thread.php (may be slightly
out of date).

Note that SDL also provides its own audio functionality, which
uses a callback interface and runs in its own thread automatically. See
http://sdldoc.csn.ul.ie/audio.php (again, may be slightly out of date).

—> Drake WilsonOn Thu, Jun 10, 2004 at 09:18:55PM +0200, Maciej Szupienko wrote:

while(finished==0) {}

Hello,

I have problem with events using PortAudio and SDL libraries. In this code:

while(done == 0)

{

while(SDL_PollEvent(&event))

{

if(event.type == SDL_QUIT) done=1;

if(event.type == SDL_KEYDOWN)

{

if(event.key.keysym.sym == SDLK_UP)   snd.init();       



if(event.key.keysym.sym == SDLK_DOWN)     finished=1;

}

}

When I press UP Array the function snd.init() is called. But meanwhile I
can’t press any other keys (for example DOWN Array) to finish this function.
In function snd.init() I’m opening an audio stream:

void SOUND_echo::init()

{

numBytes = buf_len * sizeof(float);

circ_buf = (float *) malloc( numBytes );

finished=0;                                //1 konczy program

   

Pa_OpenStream( &stream, Pa_GetDefaultInputDeviceID(), NUM_CHANNELS,

PA_SAMPLE_TYPE, NULL,

          Pa_GetDefaultOutputDeviceID(), NUM_CHANNELS, PA_SAMPLE_TYPE,

NULL, samp_freq,

          FRAMES_PER_BUFFER, 0, 0, AudioCallback, NULL);        



Pa_StartStream( stream );



while(finished==0) {}



Pa_CloseStream( stream );



free(circ_buf);

}

And the function AudioCallback should finish when (finished == 0):

int SOUND_echo::AudioCallback( void *inputBuffer, void *outputBuffer,
unsigned long framesPerBuffer,

                           PaTimestamp outTime, void *userData )

{

float *input = (float*)inputBuffer;                           

float *output = (float*)outputBuffer; 

int m=0;



for(m=0; m<framesPerBuffer; m++)

 {     

  circ_buf[bindex] = input[m]; 

   

  if(index<buf_len-1)

  {

    index++;    

    output[m] = (volume * circ_buf[bindex]) / 10;

   }

  else                 

  { 

    index=0;     

    output[m] = (volume * circ_buf[bindex]) / 10;

  }

 }

 return finished;

}

There has to be some way to control this function using SDL events? I want
to start and stop it using UP Array and DOWN Array respectively.

Thanks for any help

Maciek