-
Notifications
You must be signed in to change notification settings - Fork 2
/
PKAudioEffect.cpp
248 lines (202 loc) · 5.34 KB
/
PKAudioEffect.cpp
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/*
* PKAudioEffect.cpp
* PlayerKit
*
* Created by Peter MacWhinnie on 10/16/10.
* Copyright 2010 __MyCompanyName__. All rights reserved.
*
*/
#import "PKAudioEffect.h"
#import "PKAudioPlayerInternal.h"
struct PKAudioEffect
{
AUNode node;
PKAudioPlayerEngine *engine;
};
#pragma mark Lifecycle
PK_EXTERN PKAudioEffectRef PKAudioEffectCreate(AudioComponentDescription description, CFErrorRef *outError)
{
AUNode node = 0;
try
{
node = AudioPlayerState.engine->AddNode(&description);
}
catch (RBException e)
{
if(outError) *outError = e.CopyError();
return NULL;
}
PKAudioEffect *effect = new PKAudioEffect;
effect->node = node;
effect->engine = AudioPlayerState.engine;
effect->engine->Retain();
return effect;
}
PK_EXTERN Boolean PKAudioEffectRemove(PKAudioEffectRef effect, CFErrorRef *outError)
{
try
{
RBParameterAssert(effect);
effect->engine->RemoveNode(effect->node);
effect->engine->Release();
delete effect;
}
catch (RBException e)
{
if(outError) *outError = e.CopyError();
delete effect;
return false;
}
return true;
}
#pragma mark -
#pragma mark Properties
PK_EXTERN CFStringRef PKAudioEffectCopyTitle(PKAudioEffectRef effect)
{
if(effect)
{
try
{
return effect->engine->CopyTitleForNode(effect->node);
}
catch (RBException e)
{
//Ignore it.
}
}
return NULL;
}
#pragma mark -
PK_EXTERN Boolean PKAudioEffectSetClassInfo(PKAudioEffectRef effect, CFPropertyListRef classInfo, CFErrorRef *outError)
{
try
{
RBParameterAssert(effect);
OSStatus error = PKAudioEffectSetProperty(effect,
&classInfo,
sizeof(classInfo),
kAudioUnitProperty_ClassInfo,
kAudioUnitScope_Global,
0);
if(error != noErr)
{
if(outError) *outError = PKCopyError(PKEffectsErrorDomain,
error,
NULL,
CFSTR("Could not set class data. Error code %ld."), error);
return false;
}
}
catch (RBException e)
{
if(outError) *outError = e.CopyError();
return false;
}
return true;
}
PK_EXTERN CFPropertyListRef PKAudioEffectCopyClassInfo(PKAudioEffectRef effect)
{
CFPropertyListRef classInfo = NULL;
UInt32 classInfoSize = sizeof(classInfo);
if(PKAudioEffectCopyProperty(effect, (void **)&classInfo, &classInfoSize, kAudioUnitProperty_ClassInfo, kAudioUnitScope_Global, 0) == noErr)
return classInfo;
return NULL;
}
#pragma mark -
PK_EXTERN Boolean PKAudioEffectSetEnabled(PKAudioEffectRef effect, Boolean enabled, CFErrorRef *outError)
{
UInt32 bypass = !enabled;
OSStatus error = PKAudioEffectSetProperty(effect,
&bypass,
sizeof(bypass),
kAudioUnitProperty_BypassEffect,
kAudioUnitScope_Global,
0);
if(error != noErr)
{
if(outError) *outError = PKCopyError(PKEffectsErrorDomain,
error,
NULL,
CFSTR("Could not set enabled. Error code %ld."), error);
return false;
}
return true;
}
PK_EXTERN Boolean PKAudioEffectIsEnabled(PKAudioEffectRef effect)
{
UInt32 enabled = 0;
UInt32 enabledSize = sizeof(enabled);
PKAudioEffectCopyProperty(effect,
(void **)&enabled,
&enabledSize,
kAudioUnitProperty_BypassEffect,
kAudioUnitScope_Global,
0);
return enabled;
}
#pragma mark -
#pragma mark Audio Unit Properties
PK_EXTERN OSStatus PKAudioEffectSetProperty(PKAudioEffectRef effect, const void *inData, UInt32 inSize, AudioUnitPropertyID inPropertyID, AudioUnitScope inScope, AudioUnitElement element)
{
if(!effect)
return -50 /* paramErr */;
try
{
effect->engine->SetPropertyValue(inData, inSize, inPropertyID, inScope, effect->node, element);
}
catch (RBException e)
{
return e.GetCode();
}
return noErr;
}
PK_EXTERN OSStatus PKAudioEffectCopyProperty(PKAudioEffectRef effect, void **outValue, UInt32 *ioSize, AudioUnitPropertyID inPropertyID, AudioUnitScope inScope, AudioUnitElement element)
{
if(!effect)
return -50 /* paramErr */;
try
{
effect->engine->CopyPropertyValue(outValue, ioSize, inPropertyID, inScope, effect->node, element);
}
catch (RBException e)
{
return e.GetCode();
}
return noErr;
}
#pragma mark -
#pragma mark Audio Unit Parameters
PK_EXTERN OSStatus PKAudioEffectSetParameter(PKAudioEffectRef effect, AudioUnitParameterValue inData, AudioUnitParameterID inPropertyID, AudioUnitScope inScope, UInt32 inBufferOffsetInNumberOfFrames)
{
if(!effect)
return -50 /* paramErr */;
try
{
effect->engine->SetParameterValue(inData, inPropertyID, inScope, effect->node, inBufferOffsetInNumberOfFrames);
}
catch (RBException e)
{
return e.GetCode();
}
return noErr;
}
PK_EXTERN OSStatus PKAudioEffectCopyParameter(PKAudioEffectRef effect, AudioUnitParameterValue *outValue, AudioUnitParameterID inPropertyID, AudioUnitScope inScope)
{
if(!effect)
return -50 /* paramErr */;
try
{
effect->engine->CopyParameterValue(outValue, inPropertyID, inScope, effect->node);
}
catch (RBException e)
{
return e.GetCode();
}
return noErr;
}
#pragma mark -
PK_EXTERN OSStatus PKAudioEffectCopyParameterInfo(PKAudioEffectRef effect, AudioUnitParameterID inPropertyID, AudioUnitParameterInfo *outInfo)
{
UInt32 outInfoSize = sizeof(*outInfo);
return PKAudioEffectCopyProperty(effect, (void **)outInfo, &outInfoSize, kAudioUnitProperty_ParameterInfo, kAudioUnitScope_Global, inPropertyID);
}