]> Repos - portaudio/commitdiff
Cleanup of code
authorrobiwan <robiwan@0f58301d-fd10-0410-b4af-bbb618454e57>
Tue, 10 Jul 2012 04:44:58 +0000 (04:44 +0000)
committerrobiwan <robiwan@0f58301d-fd10-0410-b4af-bbb618454e57>
Tue, 10 Jul 2012 04:44:58 +0000 (04:44 +0000)
examples/paex_record_file.c

index 01cf6fc30e6032bdcf7c6a34d6b6c4d363fa82ed..9e605063855ed653b7735e4a637f7b29be39ee66 100644 (file)
@@ -147,9 +147,6 @@ static int threadFunctionReadFromRawFile(void* ptr)
 {\r
     paTestData* pData = (paTestData*)ptr;\r
 \r
-    /* Mark thread started */\r
-    pData->threadSyncFlag = 0;\r
-\r
     while (1)\r
     {\r
         ring_buffer_size_t elementsInBuffer = PaUtil_GetRingBufferWriteAvailable(&pData->ringBuffer);\r
@@ -171,6 +168,9 @@ static int threadFunctionReadFromRawFile(void* ptr)
                     itemsReadFromFile += (ring_buffer_size_t)fread(ptr[i], pData->ringBuffer.elementSizeBytes, sizes[i], pData->file);\r
                 }\r
                 PaUtil_AdvanceRingBufferWriteIndex(&pData->ringBuffer, itemsReadFromFile);\r
+\r
+                /* Mark thread started here, that way we "prime" the ring buffer before playback */\r
+                pData->threadSyncFlag = 0;\r
             }\r
             else\r
             {\r
@@ -194,16 +194,23 @@ typedef int (*ThreadFunctionType)(void*);
 static PaError startThread( paTestData* pData, ThreadFunctionType fn )\r
 {\r
 #ifdef _WIN32\r
-    pData->threadSyncFlag = 1;\r
-    pData->threadHandle = (void*)_beginthreadex(NULL, 0, fn, pData, 0, NULL);\r
+    pData->threadHandle = (void*)_beginthreadex(NULL, 0, fn, pData, CREATE_SUSPENDED, NULL);\r
     if (pData->threadHandle == NULL) return paUnanticipatedHostError;\r
+\r
+    /* Set file thread to a little higher prio than normal */\r
+    SetThreadPriority(pData->threadHandle, THREAD_PRIORITY_ABOVE_NORMAL);\r
+\r
+    /* Start it up */\r
+    pData->threadSyncFlag = 1;\r
+    ResumeThread(pData->threadHandle);\r
+\r
+#endif\r
+\r
     /* Wait for thread to startup */\r
     while (pData->threadSyncFlag) {\r
         Pa_Sleep(10);\r
     }\r
-    /* Set file thread to a little higher prio than normal */\r
-    SetThreadPriority(pData->threadHandle, THREAD_PRIORITY_ABOVE_NORMAL);\r
-#endif\r
+\r
     return paNoError;\r
 }\r
 \r
@@ -234,6 +241,8 @@ static int recordCallback( const void *inputBuffer, void *outputBuffer,
                            void *userData )\r
 {\r
     paTestData *data = (paTestData*)userData;\r
+    ring_buffer_size_t elementsWriteable = PaUtil_GetRingBufferWriteAvailable(&data->ringBuffer);\r
+    ring_buffer_size_t elementsToWrite = min(elementsWriteable, (ring_buffer_size_t)(framesPerBuffer * NUM_CHANNELS));\r
     const SAMPLE *rptr = (const SAMPLE*)inputBuffer;\r
 \r
     (void) outputBuffer; /* Prevent unused variable warnings. */\r
@@ -241,7 +250,7 @@ static int recordCallback( const void *inputBuffer, void *outputBuffer,
     (void) statusFlags;\r
     (void) userData;\r
 \r
-    data->frameIndex += PaUtil_WriteRingBuffer(&data->ringBuffer, rptr, framesPerBuffer * NUM_CHANNELS);\r
+    data->frameIndex += PaUtil_WriteRingBuffer(&data->ringBuffer, rptr, elementsToWrite);\r
 \r
     return paContinue;\r
 }\r
@@ -266,19 +275,6 @@ static int playCallback( const void *inputBuffer, void *outputBuffer,
     (void) statusFlags;\r
     (void) userData;\r
 \r
-    /* If we have less data in the ring buffer than framesPerBuffer, we must clear the first N frames where\r
-       N = (framesPerBuffer - elementsToRead) */\r
-      \r
-    if ((ring_buffer_size_t)framesPerBuffer > elementsToRead)\r
-    {\r
-        int i;\r
-        const int samplesToClear = framesPerBuffer * NUM_CHANNELS - elementsToRead;\r
-        for (i = 0; i < samplesToClear; ++i)\r
-        {\r
-            *wptr++ = 0;\r
-        }\r
-    }\r
-\r
     data->frameIndex += PaUtil_ReadRingBuffer(&data->ringBuffer, wptr, elementsToRead);\r
 \r
     return data->threadSyncFlag ? paComplete : paContinue;\r
@@ -313,15 +309,16 @@ int main(void)
     /* We set the ring buffer size to about 500 ms */\r
     numSamples = NextPowerOf2((unsigned)(SAMPLE_RATE * 0.5 * NUM_CHANNELS));\r
     numBytes = numSamples * sizeof(SAMPLE);\r
-    data.ringBufferData = (SAMPLE *) PaUtil_AllocateMemory( numBytes ); /* From now on, recordedSamples is initialised. */\r
+    data.ringBufferData = (SAMPLE *) PaUtil_AllocateMemory( numBytes );\r
     if( data.ringBufferData == NULL )\r
     {\r
-        printf("Could not allocate record array.\n");\r
+        printf("Could not allocate ring buffer data.\n");\r
         goto done;\r
     }\r
 \r
     if (PaUtil_InitializeRingBuffer(&data.ringBuffer, sizeof(SAMPLE), numSamples, data.ringBufferData) < 0)\r
     {\r
+        printf("Failed to initialize ring buffer. Size is not power of 2 ??\n");\r
         goto done;\r
     }\r
 \r
@@ -412,26 +409,29 @@ int main(void)
     {\r
         /* Open file again for reading */\r
         data.file = fopen(FILE_NAME, "rb");\r
-        if (data.file == 0) goto done;\r
+        if (data.file != 0)\r
+        {\r
+            /* Start the file reading thread */\r
+            err = startThread(&data, threadFunctionReadFromRawFile);\r
+            if( err != paNoError ) goto done;\r
 \r
-        /* Start the file reading thread */\r
-        err = startThread(&data, threadFunctionReadFromRawFile);\r
-        if( err != paNoError ) goto done;\r
+            err = Pa_StartStream( stream );\r
+            if( err != paNoError ) goto done;\r
 \r
-        err = Pa_StartStream( stream );\r
-        if( err != paNoError ) goto done;\r
-        \r
-        printf("Waiting for playback to finish.\n"); fflush(stdout);\r
+            printf("Waiting for playback to finish.\n"); fflush(stdout);\r
 \r
-        /* The playback will end when EOF is reached */\r
-        while( ( err = Pa_IsStreamActive( stream ) ) == 1 ) {\r
-            printf("index = %d\n", data.frameIndex ); fflush(stdout);\r
-            Pa_Sleep(1000);\r
+            /* The playback will end when EOF is reached */\r
+            while( ( err = Pa_IsStreamActive( stream ) ) == 1 ) {\r
+                printf("index = %d\n", data.frameIndex ); fflush(stdout);\r
+                Pa_Sleep(1000);\r
+            }\r
+            if( err < 0 ) goto done;\r
         }\r
-        if( err < 0 ) goto done;\r
         \r
         err = Pa_CloseStream( stream );\r
         if( err != paNoError ) goto done;\r
+\r
+        fclose(data.file);\r
         \r
         printf("Done.\n"); fflush(stdout);\r
     }\r