Jamoma API  0.6.0.a19
CARingBuffer.h
1 /* Copyright 2007 Apple Inc. All Rights Reserved.
2 
3  Disclaimer: IMPORTANT: This Apple software is supplied to you by
4  Apple Inc. ("Apple") in consideration of your agreement to the
5  following terms, and your use, installation, modification or
6  redistribution of this Apple software constitutes acceptance of these
7  terms. If you do not agree with these terms, please do not use,
8  install, modify or redistribute this Apple software.
9 
10  In consideration of your agreement to abide by the following terms, and
11  subject to these terms, Apple grants you a personal, non-exclusive
12  license, under Apple's copyrights in this original Apple software (the
13  "Apple Software"), to use, reproduce, modify and redistribute the Apple
14  Software, with or without modifications, in source and/or binary forms;
15  provided that if you redistribute the Apple Software in its entirety and
16  without modifications, you must retain this notice and the following
17  text and disclaimers in all such redistributions of the Apple Software.
18  Neither the name, trademarks, service marks or logos of Apple Inc.
19  may be used to endorse or promote products derived from the Apple
20  Software without specific prior written permission from Apple. Except
21  as expressly stated in this notice, no other rights or licenses, express
22  or implied, are granted by Apple herein, including but not limited to
23  any patent rights that may be infringed by your derivative works or by
24  other works in which the Apple Software may be incorporated.
25 
26  The Apple Software is provided by Apple on an "AS IS" basis. APPLE
27  MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
28  THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
29  FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
30  OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
31 
32  IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
33  OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35  INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
36  MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
37  AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
38  STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
39  POSSIBILITY OF SUCH DAMAGE.
40 */
41 #if !defined(__COREAUDIO_USE_FLAT_INCLUDES__)
42  #include <CoreAudio/CoreAudioTypes.h>
43 #else
44  #include <CoreAudioTypes.h>
45 #endif
46 
47 
48 #ifndef CARingBuffer_Header
49 #define CARingBuffer_Header
50 
51 enum {
52  kCARingBufferError_WayBehind = -2, // both fetch times are earlier than buffer start time
53  kCARingBufferError_SlightlyBehind = -1, // fetch start time is earlier than buffer start time (fetch end time OK)
54  kCARingBufferError_OK = 0,
55  kCARingBufferError_SlightlyAhead = 1, // fetch end time is later than buffer end time (fetch start time OK)
56  kCARingBufferError_WayAhead = 2, // both fetch times are later than buffer end time
57  kCARingBufferError_TooMuch = 3, // fetch start time is earlier than buffer start time and fetch end time is later than buffer end time
58  kCARingBufferError_CPUOverload = 4 // the reader is unable to get enough CPU cycles to capture a consistent snapshot of the time bounds
59 };
60 
61 typedef SInt32 CARingBufferError;
62 
63 const UInt32 kGeneralRingTimeBoundsQueueSize = 32;
64 const UInt32 kGeneralRingTimeBoundsQueueMask = kGeneralRingTimeBoundsQueueSize - 1;
65 
66 class CARingBuffer {
67 public:
68  typedef SInt64 SampleTime;
69 
70  CARingBuffer();
71  ~CARingBuffer();
72 
73  void Allocate(int nChannels, UInt32 bytesPerFrame, UInt32 capacityFrames);
74  // capacityFrames will be rounded up to a power of 2
75  void Deallocate();
76 
77  CARingBufferError Store(const AudioBufferList *abl, UInt32 nFrames, SampleTime frameNumber);
78  // Copy nFrames of data into the ring buffer at the specified sample time.
79  // The sample time should normally increase sequentially, though gaps
80  // are filled with zeroes. A sufficiently large gap effectively empties
81  // the buffer before storing the new data.
82 
83  // If frameNumber is less than the previous frame number, the behavior is undefined.
84 
85  // Return false for failure (buffer not large enough).
86 
87  CARingBufferError Fetch(AudioBufferList *abl, UInt32 nFrames, SampleTime frameNumber, bool aheadOK);
88  // will alter mNumDataBytes of the buffers
89 
90  CARingBufferError GetTimeBounds(SampleTime &startTime, SampleTime &endTime);
91 
92 protected:
93 
94  int FrameOffset(SampleTime frameNumber) { return (frameNumber % mCapacityFrames) * mBytesPerFrame; }
95 
96  CARingBufferError CheckTimeBounds(SampleTime& startRead, SampleTime& endRead);
97 
98  // these should only be called from Store.
99  SampleTime StartTime() const { return mTimeBoundsQueue[mTimeBoundsQueuePtr & kGeneralRingTimeBoundsQueueMask].mStartTime; }
100  SampleTime EndTime() const { return mTimeBoundsQueue[mTimeBoundsQueuePtr & kGeneralRingTimeBoundsQueueMask].mEndTime; }
101  void SetTimeBounds(SampleTime startTime, SampleTime endTime);
102 
103 protected:
104  Byte ** mBuffers; // allocated in one chunk of memory
105  int mNumberChannels;
106  UInt32 mBytesPerFrame; // within one deinterleaved channel
107  UInt32 mCapacityFrames; // per channel, must be a power of 2
108  //UInt32 mCapacityFramesMask;
109  UInt32 mCapacityBytes; // per channel
110 
111  // range of valid sample time in the buffer
112  typedef struct {
113  volatile SampleTime mStartTime;
114  volatile SampleTime mEndTime;
115  volatile UInt32 mUpdateCounter;
116  } TimeBounds;
117 
118  CARingBuffer::TimeBounds mTimeBoundsQueue[kGeneralRingTimeBoundsQueueSize];
119  UInt32 mTimeBoundsQueuePtr;
120 };
121 
122 
123 
124 #endif