]> Repos - portaudio/commitdiff
fix ringBufferIOProc
authorJohn Melas <john@jmelas.gr>
Sun, 20 Dec 2020 12:35:24 +0000 (14:35 +0200)
committerPhil Burk <philburk@mobileer.com>
Thu, 21 Jan 2021 02:21:30 +0000 (18:21 -0800)
src/hostapi/coreaudio/pa_mac_core.c

index 24883a8542604f69023385339be47584396d1528..e6cb4db5e2e777a7ddac080115ecb671ffc7e886 100644 (file)
@@ -2136,29 +2136,35 @@ static OSStatus ringBufferIOProc(
     AudioStreamPacketDescription** outDataPacketDescription,
     void*                          inUserData)
 {
-   UInt32 * ioDataSize = &ioData->mBuffers[0].mDataByteSize;
-   void ** outData = &ioData->mBuffers[0].mData;
+   VVDBUG(("ringBufferIOProc()\n"));
 
-   void *dummyData;
-   ring_buffer_size_t dummySize;
    PaUtilRingBuffer *rb = (PaUtilRingBuffer *) inUserData;
 
-   VVDBUG(("ringBufferIOProc()\n"));
-
    if( PaUtil_GetRingBufferReadAvailable( rb ) == 0 ) {
-      *outData = NULL;
-      *ioDataSize = 0;
+      ioData->mBuffers[0].mData = NULL;
+      ioData->mBuffers[0].mDataByteSize = 0;
+      *ioNumberDataPackets = 0;
+      VVDBUG(("Ring buffer empty!\n"));
       return RING_BUFFER_EMPTY;
    }
-   assert(sizeof(UInt32) == sizeof(ring_buffer_size_t));
-   assert( ( (*ioDataSize) / rb->elementSizeBytes ) * rb->elementSizeBytes == (*ioDataSize) ) ;
-   (*ioDataSize) /= rb->elementSizeBytes ;
-   PaUtil_GetRingBufferReadRegions( rb, *ioDataSize,
-                                    outData, (ring_buffer_size_t *)ioDataSize, 
+
+   UInt32 packetSize = sizeof(float) * ioData->mBuffers[0].mNumberChannels;
+   UInt32 dataSize = *ioNumberDataPackets * packetSize;
+   assert(dataSize % rb->elementSizeBytes == 0);
+   UInt32 rbElements = dataSize / rb->elementSizeBytes;
+   ring_buffer_size_t rbElementsRead = rbElements;
+   void *dummyData;
+   ring_buffer_size_t dummySize;
+   PaUtil_GetRingBufferReadRegions( rb, rbElements,
+                                    &ioData->mBuffers[0].mData, &rbElementsRead,
                                     &dummyData, &dummySize );
-   assert( *ioDataSize );
-   PaUtil_AdvanceRingBufferReadIndex( rb, *ioDataSize );
-   (*ioDataSize) *= rb->elementSizeBytes ;
+   assert(rbElementsRead > 0);
+   VVDBUG(("RingBuffer read elements %u of %u\n", rbElementsRead, rbElements));
+   PaUtil_AdvanceRingBufferReadIndex( rb, rbElementsRead );
+
+   UInt32 bytesRead = rbElementsRead * rb->elementSizeBytes;
+   ioData->mBuffers[0].mDataByteSize = bytesRead;
+   *ioNumberDataPackets = bytesRead / packetSize;
 
    return noErr;
 }