From b5a2c9d6d83828af8a9c7d06d19bd130812b588a Mon Sep 17 00:00:00 2001 From: philburk Date: Thu, 2 May 2002 20:15:40 +0000 Subject: [PATCH] Reorganized code so that it is easier to test in a loop. --- pa_tests/debug_record.c | 158 +++++++++++++++++++++++++--------------- 1 file changed, 98 insertions(+), 60 deletions(-) diff --git a/pa_tests/debug_record.c b/pa_tests/debug_record.c index 6acd161..57b11f5 100644 --- a/pa_tests/debug_record.c +++ b/pa_tests/debug_record.c @@ -58,6 +58,7 @@ typedef struct long frameIndex; /* Index into sample array. */ long maxFrameIndex; long samplesPerFrame; + long numSamples; SAMPLE *recordedSamples; } paTestData; @@ -155,41 +156,19 @@ static int playCallback( void *inputBuffer, void *outputBuffer, } return finished; } -/*******************************************************************/ -int main(void); -int main(void) + +/****************************************************************/ +PaError TestRecording( paTestData *dataPtr ) { PortAudioStream *stream; PaError err; - paTestData data; - long totalFrames; - long numSamples; - long numBytes; - long i; - printf("patest_record.c\n"); fflush(stdout); - - data.frameIndex = 0; - data.samplesPerFrame = 2; - data.maxFrameIndex = totalFrames = NUM_SECONDS*SAMPLE_RATE; - - printf("totalFrames = %d\n", totalFrames ); fflush(stdout); - numSamples = totalFrames * data.samplesPerFrame; - numBytes = numSamples * sizeof(SAMPLE); - data.recordedSamples = (SAMPLE *) malloc( numBytes ); - if( data.recordedSamples == NULL ) - { - printf("Could not allocate record array.\n"); - exit(1); - } - for( i=0; isamplesPerFrame, /* stereo input */ PA_SAMPLE_TYPE, NULL, paNoDevice, @@ -201,7 +180,7 @@ int main(void) NUM_REC_BUFS, /* number of buffers, if zero then use default minimum */ paClipOff, /* we won't output out of range samples so don't bother clipping them */ recordCallback, - &data ); + dataPtr ); if( err != paNoError ) goto error; err = Pa_StartStream( stream ); if( err != paNoError ) goto error; @@ -214,19 +193,64 @@ int main(void) printf("Stream inactive!\n"); break; } - if( data.maxFrameIndex <= data.frameIndex ) + if( dataPtr->maxFrameIndex <= dataPtr->frameIndex ) { printf("Buffer recording complete.\n"); break; } Pa_Sleep(100); - printf("index = %d\n", data.frameIndex ); fflush(stdout); + printf("index = %d\n", dataPtr->frameIndex ); fflush(stdout); } + printf("Finished loop. Close stream.\n"); fflush(stdout); + err = Pa_CloseStream( stream ); if( err != paNoError ) goto error; + + printf("Done.\n"); fflush(stdout); + { + SAMPLE max = 0; + SAMPLE posVal; + int i; + for( i=0; inumSamples; i++ ) + { + posVal = dataPtr->recordedSamples[i]; + if( posVal < 0 ) posVal = -posVal; + if( posVal > max ) max = posVal; + } + printf("Largest recorded sample = %d\n", max ); + } + /* Write recorded data to a file. */ +#if 0 + { + FILE *fid; + fid = fopen("recorded.raw", "wb"); + if( fid == NULL ) + { + printf("Could not open file."); + } + else + { + fwrite( dataPtr->recordedSamples, dataPtr->samplesPerFrame * sizeof(SAMPLE), totalFrames, fid ); + fclose( fid ); + printf("Wrote data to 'recorded.raw'\n"); + } + } +#endif + +error: + return err; +} + +/****************************************************************/ +PaError TestPlayback( paTestData *dataPtr ) +{ + PortAudioStream *stream; + PaError err; + int i; + /* Playback recorded data. */ - data.frameIndex = 0; + dataPtr->frameIndex = 0; printf("Begin playback.\n"); fflush(stdout); err = Pa_OpenStream( &stream, @@ -235,7 +259,7 @@ int main(void) PA_SAMPLE_TYPE, NULL, Pa_GetDefaultOutputDeviceID(), - data.samplesPerFrame, /* stereo output */ + dataPtr->samplesPerFrame, /* stereo output */ PA_SAMPLE_TYPE, NULL, SAMPLE_RATE, @@ -243,7 +267,7 @@ int main(void) 0, /* number of buffers, if zero then use default minimum */ paClipOff, /* we won't output out of range samples so don't bother clipping them */ playCallback, - &data ); + dataPtr ); if( err != paNoError ) goto error; err = Pa_StartStream( stream ); if( err != paNoError ) goto error; @@ -251,43 +275,57 @@ int main(void) for( i=0; i<(NUM_SECONDS*1000/SLEEP_DUR_MSEC); i++ ) { Pa_Sleep(100); - printf("index = %d\n", data.frameIndex ); + printf("index = %d\n", dataPtr->frameIndex ); } err = Pa_CloseStream( stream ); if( err != paNoError ) goto error; - printf("Done.\n"); fflush(stdout); + +error: + return err; +} +/*******************************************************************/ +int main(void); +int main(void) +{ + PaError err; + paTestData data; + long totalFrames; + long numBytes; + long i; + printf("patest_record.c\n"); fflush(stdout); + + data.frameIndex = 0; + data.samplesPerFrame = 2; + data.maxFrameIndex = totalFrames = NUM_SECONDS*SAMPLE_RATE; + + printf("totalFrames = %d\n", totalFrames ); fflush(stdout); + data.numSamples = totalFrames * data.samplesPerFrame; + + numBytes = data.numSamples * sizeof(SAMPLE); + data.recordedSamples = (SAMPLE *) malloc( numBytes ); + if( data.recordedSamples == NULL ) { - SAMPLE max = 0; - SAMPLE posVal; - int i; - for( i=0; i max ) max = posVal; - } - printf("Largest recorded sample = %d\n", max ); + printf("Could not allocate record array.\n"); + exit(1); } - /* Write recorded data to a file. */ -#if 1 + for( i=0; i