| 1 |
/* |
| 2 |
* MDAudioUtility.h |
| 3 |
* Alchemusica |
| 4 |
* |
| 5 |
* Created by Toshi Nagata on 09/11/05. |
| 6 |
* Copyright 2009-2011 Toshi Nagata. All rights reserved. |
| 7 |
* |
| 8 |
This program is free software; you can redistribute it and/or modify |
| 9 |
it under the terms of the GNU General Public License as published by |
| 10 |
the Free Software Foundation version 2 of the License. |
| 11 |
|
| 12 |
This program is distributed in the hope that it will be useful, |
| 13 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
GNU General Public License for more details. |
| 16 |
*/ |
| 17 |
|
| 18 |
#ifndef __MDAudioUtility__ |
| 19 |
#define __MDAudioUtility__ |
| 20 |
|
| 21 |
#include <CoreAudio/CoreAudio.h> |
| 22 |
#include <CoreAudio/CoreAudioTypes.h> |
| 23 |
#include <Carbon/Carbon.h> // for CompareAndSwap |
| 24 |
|
| 25 |
enum { |
| 26 |
kMDRingBufferError_WayBehind = -2, /* both fetch times are earlier than buffer start time */ |
| 27 |
kMDRingBufferError_SlightlyBehind = -1, /* fetch start time is earlier than buffer start time (fetch end time OK) */ |
| 28 |
kMDRingBufferError_OK = 0, |
| 29 |
kMDRingBufferError_SlightlyAhead = 1, /* fetch end time is later than buffer end time (fetch start time OK) */ |
| 30 |
kMDRingBufferError_WayAhead = 2, /* both fetch times are later than buffer end time */ |
| 31 |
kMDRingBufferError_TooMuch = 3, /* fetch start time is earlier than buffer start time and fetch end time is later than buffer end time */ |
| 32 |
kMDRingBufferError_CPUOverload = 4, /* the reader is unable to get enough CPU cycles to capture a consistent snapshot of the time bounds */ |
| 33 |
kMDRingBufferError_BufferNotLargeEnough = 5, |
| 34 |
kMDRingBufferError_OutOfMemory = 6, |
| 35 |
kMDRingBufferError_NumberBuffersMismatch = 7 |
| 36 |
}; |
| 37 |
|
| 38 |
typedef SInt64 MDSampleTime; |
| 39 |
|
| 40 |
#define kMDRingTimeBoundsQueueSize 32 |
| 41 |
#define kMDRingTimeBoundsQueueMask (kMDRingTimeBoundsQueueSize - 1) |
| 42 |
|
| 43 |
/* range of valid sample time in the buffer */ |
| 44 |
typedef struct MDTimeBounds { |
| 45 |
volatile MDSampleTime startTime; |
| 46 |
volatile MDSampleTime endTime; |
| 47 |
volatile UInt32 updateCounter; |
| 48 |
} MDTimeBounds; |
| 49 |
|
| 50 |
struct MDRingBuffer { |
| 51 |
Byte ** buffers; |
| 52 |
int numberChannels; |
| 53 |
int numberBuffers; |
| 54 |
UInt32 bytesPerFrame; |
| 55 |
UInt32 capacityFrames; |
| 56 |
UInt32 capacityFramesMask; |
| 57 |
UInt32 capacityBytes; |
| 58 |
|
| 59 |
MDTimeBounds timeBoundsQueue[kMDRingTimeBoundsQueueSize]; |
| 60 |
UInt32 timeBoundsQueuePtr; |
| 61 |
}; |
| 62 |
|
| 63 |
typedef struct MDRingBuffer MDRingBuffer; |
| 64 |
|
| 65 |
MDRingBuffer *MDRingBufferNew(void); |
| 66 |
int MDRingBufferAllocate(MDRingBuffer *ring, int nChannels, UInt32 bytesPerFrame, UInt32 capacityFrames); |
| 67 |
void MDRingBufferDeallocate(MDRingBuffer *ring); |
| 68 |
void MDRingBufferRelease(MDRingBuffer *ring); |
| 69 |
|
| 70 |
int MDRingBufferStore(MDRingBuffer *ring, const AudioBufferList *abl, UInt32 nFrames, MDSampleTime frameNumber); |
| 71 |
int MDRingBufferFetch(MDRingBuffer *ring, AudioBufferList *abl, UInt32 nFrames, MDSampleTime frameNumber, bool aheadOK); |
| 72 |
int MDRingBufferGetTimeBounds(MDRingBuffer *ring, MDSampleTime *startTime, MDSampleTime *endTime); |
| 73 |
int MDRingBufferFrameOffset(MDRingBuffer *ring, MDSampleTime frameNumber); |
| 74 |
|
| 75 |
int MDRingBufferCheckTimeBounds(MDRingBuffer *ring, MDSampleTime startRead, MDSampleTime endRead, bool aheadOK); |
| 76 |
|
| 77 |
MDSampleTime MDRingBufferStartTime(MDRingBuffer *ring); |
| 78 |
MDSampleTime MDRingBufferEndTime(MDRingBuffer *ring); |
| 79 |
void MDRingBufferSetTimeBounds(MDRingBuffer *ring, MDSampleTime startTime, MDSampleTime endTime); |
| 80 |
|
| 81 |
#endif /* __MDAudioUtility__ */ |