CARingBufferError CARingBuffer::Fetch(AudioBufferList *abl, UInt32 nFrames, SampleTime startRead) { SampleTime endRead = startRead + nFrames; SampleTime startRead0 = startRead; SampleTime endRead0 = endRead; CARingBufferError err = ClipTimeBounds(startRead, endRead); SampleTime readSizeFrames = endRead - startRead; if (err) { if ( readSizeFrames <= 0 ) { CARB_DEBUG( "POS1 read size frames too little. (%ld)\n", err ); return err; } } SInt32 destStartFrameOffset = startRead - startRead0; if ( destStartFrameOffset > 0 ) { CARB_DEBUG( "Fetch - Zeroing start bound\n" ); ZeroABL(abl, 0, destStartFrameOffset * mBytesPerFrame); } SInt32 destEndSize = endRead0 - endRead; if ( destEndSize > 0 ) { CARB_DEBUG( "Fetch - Zeroing end bound (%ld frames off)\n", destEndSize ); ZeroABL(abl, ( destStartFrameOffset + readSizeFrames ) * mBytesPerFrame, destEndSize * mBytesPerFrame); } Byte **buffers = mBuffers; int offset0 = FrameOffset(startRead); int offset1 = FrameOffset(endRead); int destStartByteOffset = destStartFrameOffset * mBytesPerFrame; int nbytes; if ( offset0 < offset1 ) { nbytes = offset1 - offset0; FetchABL( abl, destStartByteOffset , buffers, offset0, nbytes ); } else { nbytes = mCapacityBytes - offset0; FetchABL( abl, destStartByteOffset , buffers, offset0, nbytes ); FetchABL( abl, destStartByteOffset + nbytes, buffers, 0 , offset1 ); nbytes += offset1; } int nchannels = abl->mNumberBuffers; AudioBuffer *dest = abl->mBuffers; while (--nchannels >= 0) { dest->mDataByteSize = nbytes; dest++; } OSStatus err2 = ClipTimeBounds( startRead, endRead ); err2 = worse( err, err2 ); readSizeFrames = endRead - startRead; if ( err2 ) { if ( readSizeFrames <= 0 ) { CARB_DEBUG( "POS2 read size frames too little. (%ld)\n", err2 ); return err2; } } if ( err2 ) { CARB_DEBUG( "Returning error %ld.\n", err2 ); } return err2; }
CARingBufferError CARingBuffer::Fetch(AudioBufferList *abl, UInt32 nFrames, SampleTime startRead, bool outOfBoundsOK) { SampleTime endRead = startRead + nFrames; SampleTime startRead0 = startRead; SampleTime endRead0 = endRead; SampleTime size; CARingBufferError err = CheckTimeBounds(startRead, endRead); size = endRead - startRead; if (err) { if (!outOfBoundsOK) return err; if (size <= 0) return err; // there is nothing to read } SInt32 destStartOffset = startRead - startRead0; if (destStartOffset > 0) { ZeroABL(abl, 0, destStartOffset * mBytesPerFrame); } SInt32 destEndSize = endRead0 - endRead; if (destEndSize > 0) { ZeroABL(abl, destStartOffset + size, destEndSize * mBytesPerFrame); } Byte **buffers = mBuffers; int offset0 = FrameOffset(startRead); int offset1 = FrameOffset(endRead); int nbytes; if (offset0 < offset1) { FetchABL(abl, destStartOffset, buffers, offset0, nbytes = offset1 - offset0); } else { nbytes = mCapacityBytes - offset0; FetchABL(abl, destStartOffset, buffers, offset0, nbytes); FetchABL(abl, destStartOffset + nbytes, buffers, 0, offset1); nbytes += offset1; } int nchannels = abl->mNumberBuffers; AudioBuffer *dest = abl->mBuffers; while (--nchannels >= 0) { dest->mDataByteSize = nbytes; dest++; } // have to check bounds again because the data may have been overwritten before we could finish reading it. OSStatus err2 = CheckTimeBounds(startRead, endRead); err2 = worse(err, err2); size = endRead - startRead; if (err2) { if (!outOfBoundsOK) return err2; if (size <= 0) return err2; // there is nothing to read } return err2; }
CARingBuffer::Error CARingBuffer::fetch(AudioBufferList* list, size_t nFrames, uint64_t startRead) { if (!nFrames) return Ok; startRead = std::max<uint64_t>(0, startRead); uint64_t endRead = startRead + nFrames; uint64_t startRead0 = startRead; uint64_t endRead0 = endRead; clipTimeBounds(startRead, endRead); if (startRead == endRead) { ZeroABL(list, 0, nFrames * m_bytesPerFrame); return Ok; } size_t byteSize = static_cast<size_t>((endRead - startRead) * m_bytesPerFrame); size_t destStartByteOffset = static_cast<size_t>(std::max<uint64_t>(0, (startRead - startRead0) * m_bytesPerFrame)); if (destStartByteOffset > 0) ZeroABL(list, 0, std::min<size_t>(nFrames * m_bytesPerFrame, destStartByteOffset)); size_t destEndSize = static_cast<size_t>(std::max<uint64_t>(0, endRead0 - endRead)); if (destEndSize > 0) ZeroABL(list, destStartByteOffset + byteSize, destEndSize * m_bytesPerFrame); Byte **buffers = static_cast<Byte**>(m_buffers->data()); size_t offset0 = frameOffset(startRead); size_t offset1 = frameOffset(endRead); size_t nbytes; if (offset0 < offset1) { nbytes = offset1 - offset0; FetchABL(list, destStartByteOffset, buffers, offset0, nbytes); } else { nbytes = m_capacityBytes - offset0; FetchABL(list, destStartByteOffset, buffers, offset0, nbytes); FetchABL(list, destStartByteOffset + nbytes, buffers, 0, offset1); nbytes += offset1; } int channelCount = list->mNumberBuffers; AudioBuffer* dest = list->mBuffers; while (--channelCount >= 0) { dest->mDataByteSize = nbytes; dest++; } return Ok; }
CARingBufferError CARingBuffer::Fetch(AudioBufferList *abl, UInt32 nFrames, SampleTime startRead) { SampleTime endRead = startRead + nFrames; SampleTime startRead0 = startRead; SampleTime endRead0 = endRead; SampleTime size; CARingBufferError err = ClipTimeBounds(startRead, endRead); if (err) return err; size = endRead - startRead; SInt32 destStartOffset = startRead - startRead0; if(destStartOffset > nFrames) return kCARingBufferError_CPUOverload; if (destStartOffset > 0) { ZeroABL(abl, 0, destStartOffset * mBytesPerFrame); } SInt32 destEndSize = endRead0 - endRead; if (destEndSize > 0) { ZeroABL(abl, destStartOffset + size, destEndSize * mBytesPerFrame); } Byte **buffers = mBuffers; int offset0 = FrameOffset(startRead); int offset1 = FrameOffset(endRead); int nbytes; if (offset0 < offset1) { FetchABL(abl, destStartOffset, buffers, offset0, nbytes = offset1 - offset0); } else { nbytes = mCapacityBytes - offset0; FetchABL(abl, destStartOffset, buffers, offset0, nbytes); FetchABL(abl, destStartOffset + nbytes, buffers, 0, offset1); nbytes += offset1; } int nchannels = abl->mNumberBuffers; AudioBuffer *dest = abl->mBuffers; while (--nchannels >= 0) { dest->mDataByteSize = nbytes; dest++; } return noErr; }
FSTCARingBufferError FSTCARingBuffer::Fetch(AudioBufferList *abl, UInt32 nFrames, SampleTime startRead) { SampleTime endRead = startRead + nFrames; SampleTime startRead0 = startRead; SampleTime endRead0 = endRead; SampleTime size; FSTCARingBufferError err = ClipTimeBounds(startRead, endRead); if (err) return err; size = endRead - startRead; SInt32 idx, destStartOffset = startRead - startRead0; if(mBufferList) { for(idx=0; idx<mBufferList->mNumberBuffers; idx++) { AudioBuffer * storeBuf = &mBufferList->mBuffers[idx], * fetchBuf = &abl->mBuffers[idx]; mCapacityBytes = sizeof(float)*storeBuf->mNumberChannels * mCapacityFrames; if (destStartOffset > 0) { ZeroAB(fetchBuf, 0, destStartOffset * sizeof(float)*fetchBuf->mNumberChannels); } SInt32 destEndSize = endRead0 - endRead; if (destEndSize > 0) { ZeroAB(fetchBuf, (destStartOffset + size) * sizeof(float)*fetchBuf->mNumberChannels, destEndSize * sizeof(float)*fetchBuf->mNumberChannels); } int offset0 = startRead * sizeof(float) * storeBuf->mNumberChannels; int offset1 = endRead * sizeof(float) * storeBuf->mNumberChannels; int nbytes; if(offset0 == offset1 == 0) { nbytes = 0; } if (offset0 < offset1) { FetchAB(fetchBuf, destStartOffset, storeBuf, offset0, nbytes = offset1 - offset0); } else { nbytes = mCapacityBytes - offset0; FetchAB(fetchBuf, destStartOffset, storeBuf, offset0, nbytes); FetchAB(fetchBuf, destStartOffset + nbytes, storeBuf, 0, offset1); nbytes += offset1; } fetchBuf->mDataByteSize = nbytes; } } else { if (destStartOffset > 0) { ZeroABL(abl, 0, destStartOffset * mBytesPerFrame); } SInt32 destEndSize = endRead0 - endRead; if (destEndSize > 0) { ZeroABL(abl, destStartOffset + size, destEndSize * mBytesPerFrame); } Byte **buffers = mBuffers; int offset0 = FrameOffset(startRead); int offset1 = FrameOffset(endRead); int nbytes; if (offset0 < offset1) { FetchBufs(abl, destStartOffset, buffers, offset0, nbytes = offset1 - offset0); } else { nbytes = mCapacityBytes - offset0; FetchBufs(abl, destStartOffset, buffers, offset0, nbytes); FetchBufs(abl, destStartOffset + nbytes, buffers, 0, offset1); nbytes += offset1; } int nchannels = abl->mNumberBuffers; AudioBuffer *dest = abl->mBuffers; while (--nchannels >= 0) { dest->mDataByteSize = nbytes; dest++; } } return noErr; }