Permalink
Browse files

Merge branch 'RingBuffer-edits'

  • Loading branch information...
Alex Wiltschko Alex Wiltschko
Alex Wiltschko authored and Alex Wiltschko committed Jul 7, 2012
2 parents b056c50 + ecd52c7 commit e395255e349fcfefe588eb3d425cb8ebae00a08c
Showing with 45 additions and 0 deletions.
  1. +2 −0 Novocaine/RingBuffer.h
  2. +43 −0 Novocaine/RingBuffer.mm
View
@@ -41,6 +41,8 @@ class RingBuffer {
void AddNewInterleavedFloatData(const float *newData, const SInt64 numFrames, const SInt64 numChannelsHere);
void FetchInterleavedData(float *outData, SInt64 numFrames, SInt64 numChannels);
void FetchFreshData(float *outData, SInt64 numFrames, SInt64 whichChannel, SInt64 stride);
+ void FetchFreshData2(float *outData, SInt64 numFrames, SInt64 whichChannel, SInt64 stride);
+
void FetchData(float *outData, SInt64 numFrames, SInt64 whichChannel, SInt64 stride);
SInt64 NumNewFrames(SInt64 lastReadFrame, int iChannel = 0);
SInt64 NumUnreadFrames(int iChannel = 0) {return mNumUnreadFrames[iChannel]; }
View
@@ -161,6 +161,49 @@
}
+void RingBuffer::FetchFreshData2(float *outData, SInt64 numFrames, SInt64 whichChannel, SInt64 stride)
+{
+
+ if (mLastWrittenIndex[whichChannel] - numFrames >= 0) { // if we're requesting samples that won't go off the left end of the ring buffer, then go ahead and copy them all out.
+
+ UInt32 idx = mLastWrittenIndex[whichChannel] - numFrames;
+ float zero = 0.0f;
+ vDSP_vsadd(&mData[whichChannel][idx],
+ 1,
+ &zero,
+ outData,
+ stride,
+ numFrames);
+ }
+
+ else { // if we will overrun, then we need to do two separate copies.
+
+ // The copy that bleeds off the left, and cycles back to the right of the ring buffer
+ int numSamplesInFirstCopy = numFrames - mLastWrittenIndex[whichChannel];
+ // The copy that starts at the beginning, and proceeds to the end.
+ int numSamplesInSecondCopy = mLastWrittenIndex[whichChannel];
+
+
+ float zero = 0.0f;
+ UInt32 firstIndex = mSizeOfBuffer - numSamplesInFirstCopy;
+ vDSP_vsadd(&mData[whichChannel][firstIndex],
+ 1,
+ &zero,
+ &outData[0],
+ stride,
+ numSamplesInFirstCopy);
+
+ vDSP_vsadd(&mData[whichChannel][0],
+ 1,
+ &zero,
+ &outData[numSamplesInFirstCopy*stride],
+ stride,
+ numSamplesInSecondCopy);
+
+ }
+
+}
+
void RingBuffer::FetchData(float *outData, SInt64 numFrames, SInt64 whichChannel, SInt64 stride)
{
int idx;

0 comments on commit e395255

Please sign in to comment.