{\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
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
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
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
(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
(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
/* 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
{\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