int main(void)
{
    PaStreamParameters outputParameters;
    PaStream *stream;
    PaError err;
    paTestData data;
    PaTime startTime;

    printf("PortAudio Test: output sine wave. SR = %d, BufSize = %d\n", SAMPLE_RATE, FRAMES_PER_BUFFER);

    data.left_phase = data.right_phase = 0;

    err = Pa_Initialize();
    if( err != paNoError ) goto error;

    outputParameters.device = Pa_GetDefaultOutputDevice(); /* default output device */
    outputParameters.channelCount = 2;       /* stereo output */
    outputParameters.sampleFormat = paFloat32; /* 32 bit floating point output */
    outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency;
    outputParameters.hostApiSpecificStreamInfo = NULL;

    err = Pa_OpenStream(
              &stream,
              NULL, /* no input */
              &outputParameters,
              SAMPLE_RATE,
              FRAMES_PER_BUFFER,
              paClipOff,      /* we won't output out of range samples so don't bother clipping them */
              patestCallback,
              &data );
    if( err != paNoError ) goto error;
          
    /* Watch until sound is halfway finished. */
    printf("Play for %d seconds.\n", NUM_SECONDS/2 ); fflush(stdout);

    data.outTime = -1.0; /* mark time for callback as undefined */
    err = Pa_StartStream( stream );
    if( err != paNoError ) goto error;

    startTime = Pa_GetStreamTime( stream );

    do
    {
        ReportStreamTime( stream, &data );
        Pa_Sleep(100);
    } while( (Pa_GetStreamTime( stream ) - startTime) < (NUM_SECONDS/2) );
    
    /* Stop sound for 2 seconds. */
    err = Pa_StopStream( stream );
    if( err != paNoError ) goto error;
    
    printf("Pause for 2 seconds.\n"); fflush(stdout);
    Pa_Sleep( 2000 );

    data.outTime = -1.0; /* mark time for callback as undefined */
    err = Pa_StartStream( stream );
    if( err != paNoError ) goto error;

    startTime = Pa_GetStreamTime( stream );
    
    printf("Play until sound is finished.\n"); fflush(stdout);
    do
    {
        ReportStreamTime( stream, &data );
        Pa_Sleep(100);
    } while( (Pa_GetStreamTime( stream ) - startTime) < (NUM_SECONDS/2) );
    
    err = Pa_CloseStream( stream );
    if( err != paNoError ) goto error;

    Pa_Terminate();
    printf("Test finished.\n");
    return err;
    
error:
    Pa_Terminate();
    fprintf( stderr, "An error occured while using the portaudio stream\n" );
    fprintf( stderr, "Error number: %d\n", err );
    fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );
    return err;
}
int main(void)
{
    PortAudioStream *stream;
    PaError err;
    paTestData DATA;
    int i;
    int totalSamps;
    printf("PortAudio Test: output sine wave. SR = %d, BufSize = %d\n", SAMPLE_RATE, FRAMES_PER_BUFFER);
    /* initialise sinusoidal wavetable */
    for( i=0; i<TABLE_SIZE; i++ )
    {
        DATA.sine[i] = (float) sin( ((double)i/(double)TABLE_SIZE) * M_PI * 2. );
    }
    DATA.left_phase = DATA.right_phase = 0;
    DATA.framesToGo = totalSamps =  NUM_SECONDS * SAMPLE_RATE; /* Play for a few seconds. */
    err = Pa_Initialize();
    if( err != paNoError ) goto error;
    err = Pa_OpenStream(
              &stream,
              paNoDevice,/* default input device */
              0,              /* no input */
              paFloat32,  /* 32 bit floating point input */
              NULL,
              Pa_GetDefaultOutputDeviceID(), /* default output device */
              2,          /* stereo output */
              paFloat32,      /* 32 bit floating point output */
              NULL,
              SAMPLE_RATE,
              FRAMES_PER_BUFFER,            /* frames per buffer */
              NUM_BUFFERS,              /* number of buffers, if zero then use default minimum */
              paClipOff,      /* we won't output out of range samples so don't bother clipping them */
              patestCallback,
              &DATA );
    if( err != paNoError ) goto error;
    
    DATA.outTime = -1.0; // mark time for callback as undefined
    err = Pa_StartStream( stream );
    if( err != paNoError ) goto error;
    
    /* Watch until sound is halfway finished. */
    printf("Play for %d seconds.\n", NUM_SECONDS/2 ); fflush(stdout);
    do
    {
        ReportStreamTime( stream, &DATA );
        Pa_Sleep(100);
    } while( Pa_StreamTime( stream ) < (totalSamps/2) );
    
    /* Stop sound until ENTER hit. */
    err = Pa_StopStream( stream );
    if( err != paNoError ) goto error;
    printf("Pause for 2 seconds.\n"); fflush(stdout);
    Pa_Sleep( 2000 );

    DATA.outTime = -1.0; // mark time for callback as undefined
    err = Pa_StartStream( stream );
    if( err != paNoError ) goto error;
    
    printf("Play until sound is finished.\n"); fflush(stdout);
    do
    {
        ReportStreamTime( stream, &DATA );
        Pa_Sleep(100);
    } while( Pa_StreamActive( stream ) );
    
    err = Pa_CloseStream( stream );
    if( err != paNoError ) goto error;
    Pa_Terminate();
    printf("Test finished.\n");
    return err;
error:
    Pa_Terminate();
    fprintf( stderr, "An error occured while using the portaudio stream\n" );
    fprintf( stderr, "Error number: %d\n", err );
    fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );
    return err;
}