]> Repos - portaudio/commitdiff
Reorganized code so that it is easier to test in a loop.
authorphilburk <philburk@0f58301d-fd10-0410-b4af-bbb618454e57>
Thu, 2 May 2002 20:15:40 +0000 (20:15 +0000)
committerphilburk <philburk@0f58301d-fd10-0410-b4af-bbb618454e57>
Thu, 2 May 2002 20:15:40 +0000 (20:15 +0000)
pa_tests/debug_record.c

index 6acd16137509ee7edb78217c35d35273b9b28cb1..57b11f581fc7d42f1c4e2839c5f3524f9521a151 100644 (file)
@@ -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; i<numSamples; i++ ) data.recordedSamples[i] = 0;
-    err = Pa_Initialize();
-    if( err != paNoError ) goto error;
+    int        i;
 
     /* Record some audio. */
     err = Pa_OpenStream(
               &stream,
               Pa_GetDefaultInputDeviceID(),
-              data.samplesPerFrame,               /* stereo input */
+              dataPtr->samplesPerFrame,               /* 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; i<dataPtr->numSamples; 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<numSamples; i++ )
-        {
-            posVal = data.recordedSamples[i];
-            if( posVal < 0 ) posVal = -posVal;
-            if( posVal > 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<data.numSamples; i++ ) data.recordedSamples[i] = 0;
+
+    err = Pa_Initialize();
+    if( err != paNoError ) goto error;
+
+    for( i=0; i<2; i++ )
     {
-        FILE  *fid;
-        fid = fopen("recorded.raw", "wb");
-        if( fid == NULL )
-        {
-            printf("Could not open file.");
-        }
-        else
-        {
-            fwrite( data.recordedSamples, data.samplesPerFrame * sizeof(SAMPLE), totalFrames, fid );
-            fclose( fid );
-            printf("Wrote data to 'recorded.raw'\n");
-        }
+        err = TestRecording( &data );
+        if( err != paNoError ) goto error;
+
+        err = TestPlayback( &data );
+        if( err != paNoError ) goto error;
     }
-#endif
+
     free( data.recordedSamples );
     Pa_Terminate();
     return 0;
+
 error:
     Pa_Terminate();
     fprintf( stderr, "An error occured while using the portaudio stream\n" );