-
Notifications
You must be signed in to change notification settings - Fork 1
/
AKAudioUnit.h
170 lines (156 loc) · 6.74 KB
/
AKAudioUnit.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
//
// AKAudioUnit.h
// AudioKit
//
// Created by Aurelius Prochazka on 11/28/16.
// Copyright © 2016 AudioKit. All rights reserved.
//
#import <AudioToolbox/AudioToolbox.h>
@interface AKAudioUnit : AUAudioUnit
@property AUAudioUnitBus *outputBus;
@property AUAudioUnitBusArray *inputBusArray;
@property AVAudioFormat *defaultFormat;
- (void)start;
- (void)stop;
- (BOOL)isPlaying;
- (BOOL)isSetUp;
@property double rampTime;
@end
@interface AUParameter(Ext)
-(instancetype)init:(NSString *)identifier
name:(NSString *)name
address:(AUParameterAddress)address
min:(AUValue)min
max:(AUValue)max
unit:(AudioUnitParameterUnit)unit;
+(instancetype)parameter:(NSString *)identifier
name:(NSString *)name
address:(AUParameterAddress)address
min:(AUValue)min
max:(AUValue)max
unit:(AudioUnitParameterUnit)unit;
+(instancetype)frequency:(NSString *)identifier
name:(NSString *)name
address:(AUParameterAddress)address;
@end
@interface AUParameterTree(Ext)
+(instancetype)tree:(NSArray<AUParameterNode *> *)children;
@end
#define standardKernelPassthroughs() \
- (void)start { _kernel.start(); } \
- (void)stop { _kernel.stop(); } \
- (BOOL)isPlaying { return _kernel.started; } \
- (BOOL)isSetUp { return _kernel.resetted; }
#define standardSetup(str) \
self.rampTime = AKSettings.rampTime; \
self.defaultFormat = [[AVAudioFormat alloc] initStandardFormatWithSampleRate:AKSettings.sampleRate \
channels:AKSettings.numberOfChannels]; \
_kernel.init(self.defaultFormat.channelCount, self.defaultFormat.sampleRate); \
_inputBus.init(self.defaultFormat, 8); \
self.inputBusArray = [[AUAudioUnitBusArray alloc] initWithAudioUnit:self \
busType:AUAudioUnitBusTypeInput \
busses:@[_inputBus.bus]];
#define parameterTreeBlock(str) \
__block AK##str##DSPKernel *blockKernel = &_kernel; \
self.parameterTree.implementorValueObserver = ^(AUParameter *param, AUValue value) { \
blockKernel->setParameter(param.address, value); \
}; \
self.parameterTree.implementorValueProvider = ^(AUParameter *param) { \
return blockKernel->getParameter(param.address); \
};
#define AUAudioUnitOverrides(str) \
\
- (BOOL)allocateRenderResourcesAndReturnError:(NSError **)outError { \
if (![super allocateRenderResourcesAndReturnError:outError]) { \
return NO; \
} \
if (self.outputBus.format.channelCount != _inputBus.bus.format.channelCount) { \
if (outError) { \
*outError = [NSError errorWithDomain:NSOSStatusErrorDomain \
code:kAudioUnitErr_FailedInitialization \
userInfo:nil]; \
} \
self.renderResourcesAllocated = NO; \
return NO; \
} \
_inputBus.allocateRenderResources(self.maximumFramesToRender); \
_kernel.init(self.outputBus.format.channelCount, self.outputBus.format.sampleRate); \
_kernel.reset(); \
return YES; \
} \
\
- (void)deallocateRenderResources { \
[super deallocateRenderResources]; \
_kernel.destroy(); \
_inputBus.deallocateRenderResources(); \
} \
\
- (AUInternalRenderBlock)internalRenderBlock { \
__block AK##str##DSPKernel *state = &_kernel; \
__block BufferedInputBus *input = &_inputBus; \
return ^AUAudioUnitStatus( \
AudioUnitRenderActionFlags *actionFlags, \
const AudioTimeStamp *timestamp, \
AVAudioFrameCount frameCount, \
NSInteger outputBusNumber, \
AudioBufferList *outputData, \
const AURenderEvent *realtimeEventListHead, \
AURenderPullInputBlock pullInputBlock) { \
AudioUnitRenderActionFlags pullFlags = 0; \
AUAudioUnitStatus err = input->pullInput(&pullFlags, timestamp, frameCount, 0, pullInputBlock); \
if (err != 0) { \
return err; \
} \
AudioBufferList *inAudioBufferList = input->mutableAudioBufferList; \
AudioBufferList *outAudioBufferList = outputData; \
if (outAudioBufferList->mBuffers[0].mData == nullptr) { \
for (UInt32 i = 0; i < outAudioBufferList->mNumberBuffers; ++i) { \
outAudioBufferList->mBuffers[i].mData = inAudioBufferList->mBuffers[i].mData; \
} \
} \
state->setBuffers(inAudioBufferList, outAudioBufferList); \
state->processWithEvents(timestamp, frameCount, realtimeEventListHead); \
return noErr; \
}; \
}
#define AUAudioUnitGeneratorOverrides(str) \
\
- (BOOL)allocateRenderResourcesAndReturnError:(NSError **)outError { \
if (![super allocateRenderResourcesAndReturnError:outError]) { \
return NO; \
} \
_inputBus.allocateRenderResources(self.maximumFramesToRender); \
_kernel.init(self.outputBus.format.channelCount, self.outputBus.format.sampleRate); \
_kernel.reset(); \
return YES; \
} \
\
- (void)deallocateRenderResources { \
[super deallocateRenderResources]; \
_kernel.destroy(); \
_inputBus.deallocateRenderResources(); \
} \
\
- (AUInternalRenderBlock)internalRenderBlock { \
__block AK##str##DSPKernel *state = &_kernel; \
__block BufferedInputBus *input = &_inputBus; \
return ^AUAudioUnitStatus( \
AudioUnitRenderActionFlags *actionFlags, \
const AudioTimeStamp *timestamp, \
AVAudioFrameCount frameCount, \
NSInteger outputBusNumber, \
AudioBufferList *outputData, \
const AURenderEvent *realtimeEventListHead, \
AURenderPullInputBlock pullInputBlock) { \
AudioBufferList *inAudioBufferList = input->mutableAudioBufferList; \
AudioBufferList *outAudioBufferList = outputData; \
if (outAudioBufferList->mBuffers[0].mData == nullptr) { \
for (UInt32 i = 0; i < outAudioBufferList->mNumberBuffers; ++i) { \
outAudioBufferList->mBuffers[i].mData = inAudioBufferList->mBuffers[i].mData; \
} \
} \
state->setBuffer(outAudioBufferList); \
state->processWithEvents(timestamp, frameCount, realtimeEventListHead); \
return noErr; \
}; \
}