pa_rec refactored
authordrowe67 <drowe67@01035d8c-6547-0410-b346-abe4f91aad63>
Fri, 6 Jul 2012 00:54:41 +0000 (00:54 +0000)
committerdrowe67 <drowe67@01035d8c-6547-0410-b346-abe4f91aad63>
Fri, 6 Jul 2012 00:54:41 +0000 (00:54 +0000)
git-svn-id: https://svn.code.sf.net/p/freetel/code@581 01035d8c-6547-0410-b346-abe4f91aad63

codec2-dev/unittest/Makefile.am
codec2-dev/unittest/pa_rec.c

index 26856c27c3df98a5dc37602826b195d111c256ed..77d13f0115a0a3a46efbc83355a8c83b9fb44fbf 100644 (file)
@@ -69,6 +69,6 @@ tlspsens_LDADD = $(lib_LTLIBRARIES)
 tlspsens_LDFLAGS = $(LIBS)
 
 pa_rec_SOURCES = pa_rec.c
-pa_rec_LDADD = $(lib_LTLIBRARIES) 
+pa_rec_LDADD = $(lib_LTLIBRARIES) -lportaudio
 pa_rec_LDFLAGS = $(LIBS)
 
index 514098d313f222897ae79cc35eb89f909a454153..ad87aef894a35502185726ab762935edd24c2850 100644 (file)
@@ -39,6 +39,7 @@
  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  */
 
+#include <assert.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include "portaudio.h"
 
 typedef short SAMPLE;
 
-typedef struct
-{
-    int          frameIndex;       /* Index into sample array. */
-    int          maxFrameIndex;
-    SAMPLE      *recordedSamples;
-}
-paTestData;
+typedef struct {
+    FILE               *fout;
+    int                 framesLeft;
+} paTestData;
+
 
 /* This routine will be called by the PortAudio engine when audio is available.
 ** It may be called at interrupt level on some machines so don't do anything
@@ -70,47 +69,40 @@ static int recordCallback( const void *inputBuffer, void *outputBuffer,
                            void *userData )
 {
     paTestData *data = (paTestData*)userData;
-    const SAMPLE *rptr = (const SAMPLE*)inputBuffer;
-    SAMPLE *wptr = &data->recordedSamples[data->frameIndex /** NUM_CHANNELS*/];
-    long framesToCalc;
-    long i;
-    int finished;
-    unsigned long framesLeft = data->maxFrameIndex - data->frameIndex;
+    FILE       *fout = data->fout;
+    int         framesToCopy;
+    int         i;
+    int         finished;
+    short       buf[FRAMES_PER_BUFFER];
+    short      *rptr = (short*)inputBuffer;
 
     (void) outputBuffer; /* Prevent unused variable warnings. */
     (void) timeInfo;
     (void) statusFlags;
     (void) userData;
 
-    if( framesLeft < framesPerBuffer )
-    {
-        framesToCalc = framesLeft;
+    if (data->framesLeft < framesPerBuffer) {
+        framesToCopy = data->framesLeft;
         finished = paComplete;
-    }
-    else
-    {
-        framesToCalc = framesPerBuffer;
+    } 
+    else {
+        framesToCopy = framesPerBuffer;
         finished = paContinue;
     }
+    data->framesLeft -= framesToCopy;
 
-    if( inputBuffer == NULL )
-    {
-        for( i=0; i<framesToCalc; i++ )
-        {
-            *wptr++ = SAMPLE_SILENCE;  /* left */
-            //if( NUM_CHANNELS == 2 ) *wptr++ = SAMPLE_SILENCE;  /* right */
-        }
-    }
-    else
-    {
-        for( i=0; i<framesToCalc; i++ )
-        {
-            *wptr++ = *rptr;  /* left */
-           rptr += 2;
-            //if( NUM_CHANNELS == 2 ) *wptr++ = *rptr++;  /* right */
-        }
-    }
-    data->frameIndex += framesToCalc;
+    assert(inputBuffer != NULL);
+
+    /* just use left channel */
+
+    for(i=0; i<framesToCopy; i++,rptr+=2)
+       buf[i] = *rptr; 
+
+    /* note Portaudio doc doesn't rec making systems calls in this
+       callback but seems to work OK */
+
+    fwrite(buf, sizeof(short), framesToCopy, fout);
+    
     return finished;
 }
 
@@ -128,33 +120,20 @@ int main(int argc, char *argv[])
     SAMPLE              max, val;
     double              average;
     int                 numSecs;
-    FILE               *fout;
 
     if (argc != 3) {
        printf("usage: %s rawFile time(s)\n", argv[0]);
        exit(0);
     }
 
-    fout = fopen(argv[1], "wt");
-    if (fout == NULL) {
+    data.fout = fopen(argv[1], "wt");
+    if (data.fout == NULL) {
        printf("Error opening output raw file %s\n", argv[1]);
        exit(1);
     }
 
     numSecs = atoi(argv[2]);
-    printf("patest_record.c\n"); fflush(stdout);
-
-    data.maxFrameIndex = totalFrames = numSecs * SAMPLE_RATE; /* Record for a few seconds. */
-    data.frameIndex = 0;
-    numSamples = totalFrames/* * NUM_CHANNELS*/;
-    numBytes = numSamples * sizeof(SAMPLE);
-    data.recordedSamples = (SAMPLE *) malloc( numBytes ); /* From now on, recordedSamples is initialised. */
-    if( data.recordedSamples == NULL )
-    {
-        printf("Could not allocate record array.\n");
-        goto done;
-    }
-    for( i=0; i<numSamples; i++ ) data.recordedSamples[i] = 0;
+    data.framesLeft = numSecs * SAMPLE_RATE;
 
     err = Pa_Initialize();
     if( err != paNoError ) goto done;
@@ -184,28 +163,21 @@ int main(int argc, char *argv[])
 
     err = Pa_StartStream( stream );
     if( err != paNoError ) goto done;
-    printf("\n=== Now recording!! Please speak into the microphone. ===\n"); fflush(stdout);
 
     while( ( err = Pa_IsStreamActive( stream ) ) == 1 )
     {
-        Pa_Sleep(1000);
-        printf("index = %d\n", data.frameIndex ); fflush(stdout);
+        Pa_Sleep(100);
     }
     if( err < 0 ) goto done;
 
     err = Pa_CloseStream( stream );
     if( err != paNoError ) goto done;
 
-    /* Write recorded data to a file. */
-
-    fwrite( data.recordedSamples, /*NUM_CHANNELS * */sizeof(SAMPLE), totalFrames, fout );
-    fclose( fout );
+    fclose(data.fout);
 
 
 done:
     Pa_Terminate();
-    if( data.recordedSamples )       /* Sure it is NULL or valid. */
-        free( data.recordedSamples );
     if( err != paNoError )
     {
         fprintf( stderr, "An error occured while using the portaudio stream\n" );