AEUtilities.h
Go to the documentation of this file.
1 //
2 // AEUtilities.h
3 // The Amazing Audio Engine
4 //
5 // Created by Michael Tyson on 23/03/2012.
6 //
7 // This software is provided 'as-is', without any express or implied
8 // warranty. In no event will the authors be held liable for any damages
9 // arising from the use of this software.
10 //
11 // Permission is granted to anyone to use this software for any purpose,
12 // including commercial applications, and to alter it and redistribute it
13 // freely, subject to the following restrictions:
14 //
15 // 1. The origin of this software must not be misrepresented; you must not
16 // claim that you wrote the original software. If you use this software
17 // in a product, an acknowledgment in the product documentation would be
18 // appreciated but is not required.
19 //
20 // 2. Altered source versions must be plainly marked as such, and must not be
21 // misrepresented as being the original software.
22 //
23 // 3. This notice may not be removed or altered from any source distribution.
24 //
25 
26 #import <AudioToolbox/AudioToolbox.h>
27 #import <Foundation/Foundation.h>
28 
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32 
33 #pragma mark - AudioBufferList Utilities
34 
35 
36 
47 AudioBufferList *AEAudioBufferListCreate(AudioStreamBasicDescription audioFormat, int frameCount);
48 #define AEAllocateAndInitAudioBufferList AEAudioBufferListCreate // Legacy alias
49 
64 #define AEAudioBufferListCreateOnStack(name, audioFormat) \
65  int name ## _numberBuffers = audioFormat.mFormatFlags & kAudioFormatFlagIsNonInterleaved \
66  ? audioFormat.mChannelsPerFrame : 1; \
67  char name ## _bytes[sizeof(AudioBufferList)+(sizeof(AudioBuffer)*(name ## _numberBuffers-1))]; \
68  memset(&name ## _bytes, 0, sizeof(name ## _bytes)); \
69  AudioBufferList * name = (AudioBufferList*)name ## _bytes; \
70  name->mNumberBuffers = name ## _numberBuffers;
71 
87 #define AEAudioBufferListCopyOnStack(name, sourceBufferList, offsetBytes) \
88  char name ## _bytes[sizeof(AudioBufferList)+(sizeof(AudioBuffer)*(sourceBufferList->mNumberBuffers-1))]; \
89  memcpy(name ## _bytes, sourceBufferList, sizeof(name ## _bytes)); \
90  AudioBufferList * name = (AudioBufferList*)name ## _bytes; \
91  for ( int i=0; i<name->mNumberBuffers; i++ ) { \
92  name->mBuffers[i].mData = (char*)name->mBuffers[i].mData + offsetBytes; \
93  name->mBuffers[i].mDataByteSize -= offsetBytes; \
94  }
95 
105 AudioBufferList *AEAudioBufferListCopy(const AudioBufferList *original);
106 #define AECopyAudioBufferList AEAudioBufferListCopy // Legacy alias
107 
114 void AEAudioBufferListFree(AudioBufferList *bufferList);
115 #define AEFreeAudioBufferList AEAudioBufferListFree // Legacy alias
116 
128 UInt32 AEAudioBufferListGetLength(const AudioBufferList *bufferList,
129  AudioStreamBasicDescription audioFormat,
130  int *oNumberOfChannels);
131 #define AEGetNumberOfFramesInAudioBufferList AEAudioBufferListGetLength // Legacy alias
132 
143 void AEAudioBufferListSetLength(AudioBufferList *bufferList,
144  AudioStreamBasicDescription audioFormat,
145  UInt32 frames);
146 
157 void AEAudioBufferListOffset(AudioBufferList *bufferList,
158  AudioStreamBasicDescription audioFormat,
159  UInt32 frames);
160 
169 void AEAudioBufferListSilence(const AudioBufferList *bufferList,
170  AudioStreamBasicDescription audioFormat,
171  UInt32 offset,
172  UInt32 length);
173 
185 static inline size_t AEAudioBufferListGetStructSize(const AudioBufferList *bufferList) {
186  return sizeof(AudioBufferList) + (bufferList->mNumberBuffers-1) * sizeof(AudioBuffer);
187 }
188 
190 #pragma mark - AudioStreamBasicDescription Utilities
191 
192 
193 
197 extern const AudioStreamBasicDescription AEAudioStreamBasicDescriptionNonInterleavedFloatStereo;
198 
202 extern const AudioStreamBasicDescription AEAudioStreamBasicDescriptionNonInterleaved16BitStereo;
203 
207 extern const AudioStreamBasicDescription AEAudioStreamBasicDescriptionInterleaved16BitStereo;
208 
212 typedef enum {
217 
228  BOOL interleaved,
229  int numberOfChannels,
230  double sampleRate);
231 
238 void AEAudioStreamBasicDescriptionSetChannelsPerFrame(AudioStreamBasicDescription *audioDescription, int numberOfChannels);
239 
241 #pragma mark - Time Utilities
242 
243 
244 
248 void AETimeInit(void);
249 
253 uint64_t AECurrentTimeInHostTicks(void);
254 
258 double AECurrentTimeInSeconds(void);
259 
266 uint64_t AEHostTicksFromSeconds(double seconds);
267 
274 double AESecondsFromHostTicks(uint64_t ticks);
275 
277 #pragma mark - Other Utilities
278 
279 
280 
289 AudioComponentDescription AEAudioComponentDescriptionMake(OSType manufacturer, OSType type, OSType subtype);
290 
297 BOOL AERateLimit(void);
298 
305 #define AECheckOSStatus(result,operation) (_AECheckOSStatus((result),(operation),strrchr(__FILE__, '/')+1,__LINE__))
306 static inline BOOL _AECheckOSStatus(OSStatus result, const char *operation, const char* file, int line) {
307  if ( result != noErr ) {
308  if ( AERateLimit() ) {
309  int fourCC = CFSwapInt32HostToBig(result);
310  if ( isascii(((char*)&fourCC)[0]) && isascii(((char*)&fourCC)[1]) && isascii(((char*)&fourCC)[2]) ) {
311  NSLog(@"%s:%d: %s: '%4.4s' (%d)", file, line, operation, (char*)&fourCC, (int)result);
312  } else {
313  NSLog(@"%s:%d: %s: %d", file, line, operation, (int)result);
314  }
315  }
316  return NO;
317  }
318  return YES;
319 }
320 
322 
323 #ifdef __cplusplus
324 }
325 #endif