AEAudioController.h
Go to the documentation of this file.
1 //
2 // AEAudioController.h
3 // The Amazing Audio Engine
4 //
5 // Created by Michael Tyson on 25/11/2011.
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 #ifdef __cplusplus
27 extern "C" {
28 #endif
29 
30 #import <AudioToolbox/AudioToolbox.h>
31 #import <AudioUnit/AudioUnit.h>
32 #import <Foundation/Foundation.h>
33 #import "AEMessageQueue.h"
34 
36 
37 #pragma mark - Notifications and constants
38 
64 extern NSString * const AEAudioControllerDidRecreateGraphNotification;
65 extern NSString * const AEAudioControllerErrorOccurredNotification;
66 
70 extern NSString * const AEAudioControllerErrorKey;
71 
75 extern NSString * const AEAudioControllerErrorDomain;
76 enum {
78 };
79 
103 typedef enum {
106 } AEInputMode;
107 
108 #pragma mark - Callbacks and protocols
109 
128 typedef OSStatus (*AEAudioRenderCallback) (__unsafe_unretained id channel,
129  __unsafe_unretained AEAudioController *audioController,
130  const AudioTimeStamp *time,
131  UInt32 frames,
132  AudioBufferList *audio);
133 
135 
145 @protocol AEAudioPlayable <NSObject>
146 
155 @property (nonatomic, readonly) AEAudioRenderCallback renderCallback;
156 
157 @optional
158 
168 - (void)setupWithAudioController:(AEAudioController *)audioController;
169 
179 - (void)teardown;
180 
189 @property (nonatomic, readonly) float volume;
190 
199 @property (nonatomic, readonly) float pan;
200 
201 /*
202  * Whether channel is currently playing
203  *
204  * If this is NO, then the track will be silenced and no further render callbacks
205  * will be performed until set to YES again.
206  *
207  * Changes are tracked by Key-Value Observing, so be sure to send KVO notifications
208  * when the value changes (or use a readwrite property).
209  */
210 @property (nonatomic, readonly) BOOL channelIsPlaying;
211 
212 /*
213  * Whether channel is muted
214  *
215  * If YES, track will be silenced, but render callbacks will continue to be performed.
216  *
217  * Changes are tracked by Key-Value Observing, so be sure to send KVO notifications
218  * when the value changes (or use a readwrite property).
219  */
220 @property (nonatomic, readonly) BOOL channelIsMuted;
221 
222 /*
223  * The audio format for this channel
224  */
225 @property (nonatomic, readonly) AudioStreamBasicDescription audioDescription;
226 
227 @end
228 
229 static void * const AEAudioSourceInput = ((void*)0x01);
230 static void * const AEAudioSourceMainOutput = ((void*)0x02);
231 
252 typedef void (*AEAudioReceiverCallback) (__unsafe_unretained id receiver,
253  __unsafe_unretained AEAudioController *audioController,
254  void *source,
255  const AudioTimeStamp *time,
256  UInt32 frames,
257  AudioBufferList *audio);
258 
260 
261 
270 @protocol AEAudioReceiver <NSObject>
271 
280 @property (nonatomic, readonly) AEAudioReceiverCallback receiverCallback;
281 
282 @optional
283 
293 - (void)setupWithAudioController:(AEAudioController *)audioController;
294 
304 - (void)teardown;
305 
306 @end
307 
319 typedef OSStatus (*AEAudioFilterProducer)(void *producerToken,
320  AudioBufferList *audio,
321  UInt32 *frames);
322 
324 
354 typedef OSStatus (*AEAudioFilterCallback)(__unsafe_unretained id filter,
355  __unsafe_unretained AEAudioController *audioController,
356  AEAudioFilterProducer producer,
357  void *producerToken,
358  const AudioTimeStamp *time,
359  UInt32 frames,
360  AudioBufferList *audio);
361 
363 
376 @protocol AEAudioFilter <NSObject>
377 
386 @property (nonatomic, readonly) AEAudioFilterCallback filterCallback;
387 
388 @optional
389 
399 - (void)setupWithAudioController:(AEAudioController *)audioController;
400 
410 - (void)teardown;
411 
412 @end
413 
414 
429 typedef enum {
433 
454 typedef void (*AEAudioTimingCallback) (__unsafe_unretained id receiver,
455  __unsafe_unretained AEAudioController *audioController,
456  const AudioTimeStamp *time,
457  UInt32 frames,
458  AEAudioTimingContext context);
459 
461 
470 @protocol AEAudioTimingReceiver <NSObject>
471 
480 @property (nonatomic, readonly) AEAudioTimingCallback timingReceiverCallback;
481 
482 @end
483 
484 
490 typedef struct _channel_group_t* AEChannelGroupRef;
491 
492 @class AEAudioController;
493 
494 #pragma mark -
495 
500 typedef enum {
517 
529 @interface AEAudioController : NSObject
530 
531 #pragma mark - Setup and start/stop
532 
533 
534 
540 + (AudioStreamBasicDescription)interleaved16BitStereoAudioDescription;
541 // Soon: __deprecated_msg("use AEAudioStreamBasicDescriptionInterleaved16BitStereo instead");
542 
548 + (AudioStreamBasicDescription)nonInterleaved16BitStereoAudioDescription;
549 // Soon: __deprecated_msg("use AEAudioStreamBasicDescriptionNonInterleaved16BitStereo instead");
550 
556 + (AudioStreamBasicDescription)nonInterleavedFloatStereoAudioDescription;
557 // Soon: __deprecated_msg("use AEAudioStreamBasicDescriptionNonInterleavedFloatStereo instead");
558 
566 
577 - (id)initWithAudioDescription:(AudioStreamBasicDescription)audioDescription;
578 
587 - (id)initWithAudioDescription:(AudioStreamBasicDescription)audioDescription inputEnabled:(BOOL)enableInput;
588 
589 
598 - (id)initWithAudioDescription:(AudioStreamBasicDescription)audioDescription options:(AEAudioControllerOptions)options;
599 
610 - (id)initWithAudioDescription:(AudioStreamBasicDescription)audioDescription inputEnabled:(BOOL)enableInput useVoiceProcessing:(BOOL)useVoiceProcessing __deprecated_msg("use initWithAudioDescription:options: instead");
611 
623 - (id)initWithAudioDescription:(AudioStreamBasicDescription)audioDescription inputEnabled:(BOOL)enableInput useVoiceProcessing:(BOOL)useVoiceProcessing outputEnabled:(BOOL)enableOutput __deprecated_msg("use initWithAudioDescription:options: instead");
624 
631 - (BOOL)start:(NSError**)error;
632 
636 - (void)stop;
637 
648 - (BOOL)setAudioDescription:(AudioStreamBasicDescription)audioDescription error:(NSError**)error;
649 
660 - (BOOL)setInputEnabled:(BOOL)inputEnabled error:(NSError**)error;
661 
672 - (BOOL)setOutputEnabled:(BOOL)outputEnabled error:(NSError**)error;
673 
685 - (BOOL)setAudioDescription:(AudioStreamBasicDescription)audioDescription
686  inputEnabled:(BOOL)inputEnabled
687  outputEnabled:(BOOL)outputEnabled
688  error:(NSError**)error;
689 
690 
692 #pragma mark - Channel and channel group management
693 
694 
695 
703 - (void)addChannels:(NSArray*)channels;
704 
711 - (void)addChannels:(NSArray*)channels toChannelGroup:(AEChannelGroupRef)group;
712 
720 - (void)removeChannels:(NSArray*)channels;
721 
728 - (void)removeChannels:(NSArray*)channels fromChannelGroup:(AEChannelGroupRef)group;
729 
733 - (NSArray*)channels;
734 
741 - (NSArray*)channelsInChannelGroup:(AEChannelGroupRef)group;
742 
754 - (AEChannelGroupRef)createChannelGroup;
755 
765 - (AEChannelGroupRef)createChannelGroupWithinChannelGroup:(AEChannelGroupRef)group;
766 
774 - (void)removeChannelGroup:(AEChannelGroupRef)group;
775 
781 - (NSArray*)topLevelChannelGroups;
782 
789 - (NSArray*)channelGroupsInChannelGroup:(AEChannelGroupRef)group;
790 
797 - (void)setVolume:(float)volume forChannelGroup:(AEChannelGroupRef)group;
798 
805 - (float)volumeForChannelGroup:(AEChannelGroupRef)group;
806 
813 - (void)setPan:(float)pan forChannelGroup:(AEChannelGroupRef)group;
814 
821 - (float)panForChannelGroup:(AEChannelGroupRef)group;
822 
832 - (void)setPlaying:(BOOL)playing forChannelGroup:(AEChannelGroupRef)group;
833 
840 - (BOOL)channelGroupIsPlaying:(AEChannelGroupRef)group;
841 
851 - (void)setMuted:(BOOL)muted forChannelGroup:(AEChannelGroupRef)group;
852 
859 - (BOOL)channelGroupIsMuted:(AEChannelGroupRef)group;
860 
862 #pragma mark - Filters
863 
864 
865 
873 - (void)addFilter:(id<AEAudioFilter>)filter;
874 
891 - (void)addFilter:(id<AEAudioFilter>)filter toChannel:(id<AEAudioPlayable>)channel;
892 
904 - (void)addFilter:(id<AEAudioFilter>)filter toChannelGroup:(AEChannelGroupRef)group;
905 
913 - (void)addInputFilter:(id<AEAudioFilter>)filter;
914 
923 - (void)addInputFilter:(id<AEAudioFilter>)filter forChannels:(NSArray*)channels;
924 
930 - (void)removeFilter:(id<AEAudioFilter>)filter;
931 
938 - (void)removeFilter:(id<AEAudioFilter>)filter fromChannel:(id<AEAudioPlayable>)channel;
939 
946 - (void)removeFilter:(id<AEAudioFilter>)filter fromChannelGroup:(AEChannelGroupRef)group;
947 
953 - (void)removeInputFilter:(id<AEAudioFilter>)filter;
954 
958 - (NSArray*)filters;
959 
965 - (NSArray*)filtersForChannel:(id<AEAudioPlayable>)channel;
966 
972 - (NSArray*)filtersForChannelGroup:(AEChannelGroupRef)group;
973 
977 - (NSArray*)inputFilters;
978 
980 #pragma mark - Output receivers
981 
982 
983 
993 - (void)addOutputReceiver:(id<AEAudioReceiver>)receiver;
994 
1004 - (void)addOutputReceiver:(id<AEAudioReceiver>)receiver forChannel:(id<AEAudioPlayable>)channel;
1005 
1016 - (void)addOutputReceiver:(id<AEAudioReceiver>)receiver forChannelGroup:(AEChannelGroupRef)group;
1017 
1023 - (void)removeOutputReceiver:(id<AEAudioReceiver>)receiver;
1024 
1031 - (void)removeOutputReceiver:(id<AEAudioReceiver>)receiver fromChannel:(id<AEAudioPlayable>)channel;
1032 
1039 - (void)removeOutputReceiver:(id<AEAudioReceiver>)receiver fromChannelGroup:(AEChannelGroupRef)group;
1040 
1044 - (NSArray*)outputReceivers;
1045 
1051 - (NSArray*)outputReceiversForChannel:(id<AEAudioPlayable>)channel;
1052 
1058 - (NSArray*)outputReceiversForChannelGroup:(AEChannelGroupRef)group;
1059 
1061 #pragma mark - Input receivers
1062 
1063 
1064 
1081 - (void)addInputReceiver:(id<AEAudioReceiver>)receiver;
1082 
1103 - (void)addInputReceiver:(id<AEAudioReceiver>)receiver forChannels:(NSArray*)channels;
1104 
1112 - (void)removeInputReceiver:(id<AEAudioReceiver>)receiver;
1113 
1120 - (void)removeInputReceiver:(id<AEAudioReceiver>)receiver fromChannels:(NSArray*)channels;
1121 
1125 - (NSArray*)inputReceivers;
1126 
1128 #pragma mark - Timing receivers
1129 
1130 
1131 
1144 - (void)addTimingReceiver:(id<AEAudioTimingReceiver>)receiver;
1145 
1151 - (void)removeTimingReceiver:(id<AEAudioTimingReceiver>)receiver;
1152 
1156 - (NSArray*)timingReceivers;
1157 
1159 #pragma mark - Realtime/Main thread messaging system
1160 
1161 
1162 
1169 @property (nonatomic, readonly, strong) AEMessageQueue *messageQueue;
1170 
1192 - (void)performAsynchronousMessageExchangeWithBlock:(void (^)())block
1193  responseBlock:(void (^)())responseBlock;
1194 
1220 - (BOOL)performSynchronousMessageExchangeWithBlock:(void (^)())block;
1221 
1260 void AEAudioControllerSendAsynchronousMessageToMainThread(__unsafe_unretained AEAudioController *audioController,
1262  void *userInfo,
1263  int userInfoLength);
1264 
1272 
1276 - (void)endMessageExchangeBlock;
1277 
1279 #pragma mark - Metering
1280 
1281 
1282 
1289 - (void)outputAveragePowerLevel:(Float32*)averagePower peakHoldLevel:(Float32*)peakLevel;
1290 
1298 - (void)outputAveragePowerLevels:(Float32*)averagePowers peakHoldLevels:(Float32*)peakLevels channelCount:(UInt32)count;
1299 
1307 - (void)averagePowerLevel:(Float32*)averagePower peakHoldLevel:(Float32*)peakLevel forGroup:(AEChannelGroupRef)group;
1308 
1318 - (void)averagePowerLevels:(Float32*)averagePowers peakHoldLevels:(Float32*)peakLevels forGroup:(AEChannelGroupRef)group channelCount:(UInt32)count;
1319 
1326 - (void)inputAveragePowerLevel:(Float32*)averagePower peakHoldLevel:(Float32*)peakLevel;
1327 
1335 - (void)inputAveragePowerLevels:(Float32*)averagePowers peakHoldLevels:(Float32*)peakLevels channelCount:(UInt32)count;
1336 
1338 #pragma mark - Utilities
1339 
1340 
1341 
1345 AudioStreamBasicDescription *AEAudioControllerAudioDescription(__unsafe_unretained AEAudioController *audioController);
1346 
1350 AudioStreamBasicDescription *AEAudioControllerInputAudioDescription(__unsafe_unretained AEAudioController *audioController);
1351 
1355 long AEConvertSecondsToFrames(__unsafe_unretained AEAudioController *audioController, NSTimeInterval seconds);
1356 
1360 NSTimeInterval AEConvertFramesToSeconds(__unsafe_unretained AEAudioController *audioController, long frames);
1361 
1365 BOOL AECurrentThreadIsAudioThread(void);
1366 
1368 #pragma mark - Properties
1369 
1377 #if TARGET_OS_IPHONE
1378 @property (nonatomic, assign) NSString * audioSessionCategory;
1379 #endif
1380 
1392 #if TARGET_OS_IPHONE
1393 @property (nonatomic, assign) BOOL allowMixingWithOtherApps;
1394 #endif
1395 
1404 #if TARGET_OS_IPHONE
1405 @property (nonatomic, assign) BOOL useMeasurementMode;
1406 #endif
1407 
1417 #if TARGET_OS_IPHONE
1418 @property (nonatomic, assign) BOOL avoidMeasurementModeForBuiltInSpeaker;
1419 #endif
1420 
1431 #if TARGET_OS_IPHONE
1432 @property (nonatomic, assign) BOOL boostBuiltInMicGainInMeasurementMode;
1433 #endif
1434 
1442 @property (nonatomic, assign) BOOL muteOutput;
1443 
1450 @property (nonatomic, assign) float masterOutputVolume;
1451 
1459 #if TARGET_OS_IPHONE
1460 @property (nonatomic, assign) BOOL enableBluetoothInput;
1461 #endif
1462 
1466 @property (nonatomic, readonly) BOOL inputGainAvailable;
1467 
1473 @property (nonatomic, assign) float inputGain;
1474 
1490 @property (nonatomic, assign) BOOL voiceProcessingEnabled;
1491 
1501 @property (nonatomic, assign) BOOL voiceProcessingOnlyForSpeakerAndMicrophone;
1502 
1513 @property (nonatomic, assign) AEInputMode inputMode;
1514 
1530 @property (nonatomic, strong) NSArray *inputChannelSelection;
1531 
1541 @property (nonatomic, assign) NSTimeInterval preferredBufferDuration;
1542 
1552 @property (nonatomic, readonly) NSTimeInterval currentBufferDuration;
1553 
1560 #if TARGET_OS_IPHONE
1561 @property (nonatomic, readonly) NSTimeInterval inputLatency;
1562 #endif
1563 
1570 #if TARGET_OS_IPHONE
1571 @property (nonatomic, readonly) NSTimeInterval outputLatency;
1572 #endif
1573 
1586 #if TARGET_OS_IPHONE
1587 @property (nonatomic, assign) BOOL automaticLatencyManagement;
1588 #endif
1589 
1595 @property (nonatomic, readonly) BOOL running;
1596 
1602 @property (nonatomic, readonly) BOOL playingThroughDeviceSpeaker;
1603 
1609 @property (nonatomic, readonly) BOOL recordingThroughDeviceMicrophone;
1610 
1616 @property (nonatomic, readonly) BOOL audioInputAvailable;
1617 
1623 @property (nonatomic, readonly) BOOL inputEnabled;
1624 
1630 @property (nonatomic, readonly) BOOL outputEnabled;
1631 
1642 @property (nonatomic, readonly) int numberOfInputChannels;
1643 
1651 @property (nonatomic, readonly) AudioStreamBasicDescription inputAudioDescription;
1652 
1656 @property (nonatomic, readonly) AudioStreamBasicDescription audioDescription;
1657 
1661 @property (nonatomic, readonly) AudioUnit audioUnit;
1662 
1666 @property (nonatomic, readonly) AUGraph audioGraph;
1667 
1668 #pragma mark - Timing
1669 
1687 #if TARGET_OS_IPHONE
1688 NSTimeInterval AEAudioControllerInputLatency(__unsafe_unretained AEAudioController *controller);
1689 #endif
1690 
1708 #if TARGET_OS_IPHONE
1709 NSTimeInterval AEAudioControllerOutputLatency(__unsafe_unretained AEAudioController *controller);
1710 #endif
1711 
1721 AudioTimeStamp AEAudioControllerCurrentAudioTimestamp(__unsafe_unretained AEAudioController *controller);
1722 
1723 @end
1724 
1725 #ifdef __cplusplus
1726 }
1727 #endif