iPlug2 - C++ Audio Plug-in Framework
IPlugAUPlayer.mm
1  /*
2  ==============================================================================
3 
4  This file is part of the iPlug 2 library. Copyright (C) the iPlug 2 developers.
5 
6  See LICENSE.txt for more info.
7 
8  ==============================================================================
9 */
10 
11 #import "IPlugAUPlayer.h"
12 #include "config.h"
13 
14 #if !__has_feature(objc_arc)
15 #error This file must be compiled with Arc. Use -fobjc-arc flag
16 #endif
17 
18 @implementation IPlugAUPlayer
19 {
20  AVAudioEngine* audioEngine;
21  AVAudioUnit* avAudioUnit;
22  UInt32 componentType;
23 }
24 
25 - (instancetype) initWithComponentType: (UInt32) unitComponentType
26 {
27  self = [super init];
28 
29  if (self) {
30  audioEngine = [[AVAudioEngine alloc] init];
31  componentType = unitComponentType;
32  }
33 
34 #if TARGET_OS_IPHONE
35  NSError* error = nil;
36  BOOL success = [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:&error];
37 
38  if (success == NO)
39  NSLog (@"Error setting category: %@", [error localizedDescription]);
40 #endif
41 
42  return self;
43 }
44 
45 - (void) loadAudioUnitWithComponentDescription:(AudioComponentDescription)desc
46  completion:(void (^) (void))completionBlock
47 {
48  [AVAudioUnit instantiateWithComponentDescription:desc options:0
49  completionHandler:^(AVAudioUnit* __nullable audioUnit, NSError* __nullable error)
50  {
51  [self onAudioUnitInstantiated:audioUnit error:error completion:completionBlock];
52  }];
53 }
54 
55 - (void) onAudioUnitInstantiated:(AVAudioUnit* __nullable) audioUnit error:(NSError* __nullable) error completion:(void (^) (void))completionBlock
56 {
57  if (audioUnit == nil)
58  return;
59 
60  avAudioUnit = audioUnit;
61 
62  self.currentAudioUnit = avAudioUnit.AUAudioUnit;
63 
64  AVAudioSession* session = [AVAudioSession sharedInstance];
65  [session setCategory: AVAudioSessionCategoryPlayAndRecord error:&error];
66 // [session overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker error:&error];
67  [session setPreferredSampleRate:44100. error:nil];
68  [session setPreferredIOBufferDuration:0.005 error:nil];
69  AVAudioMixerNode* mainMixer = [audioEngine mainMixerNode];
70  mainMixer.outputVolume = 1;
71  [audioEngine attachNode:avAudioUnit];
72  AVAudioFormat* inputFormat = [avAudioUnit inputFormatForBus:0];
73  AVAudioFormat* outputFormat = [avAudioUnit outputFormatForBus:0];
74 
75  if (inputFormat != nil)
76  [audioEngine connect:audioEngine.inputNode to:avAudioUnit format: inputFormat];
77 
78  [audioEngine connect:avAudioUnit to:audioEngine.outputNode format: outputFormat];
79 
80  [self start];
81 
82  completionBlock();
83 }
84 
85 - (void) start
86 {
87  NSError* error = nil;
88  BOOL success = [[AVAudioSession sharedInstance] setActive:TRUE error:nil];
89 
90  if (success == NO)
91  NSLog (@"Error setting category: %@", [error localizedDescription]);
92 
93  if (![audioEngine startAndReturnError:&error])
94  NSLog (@"engine failed to start: %@", error);
95 }
96 
97 @end