-
Notifications
You must be signed in to change notification settings - Fork 288
Expand file tree
/
Copy pathPdAudioUnit.m
More file actions
247 lines (210 loc) · 9.07 KB
/
Copy pathPdAudioUnit.m
File metadata and controls
247 lines (210 loc) · 9.07 KB
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
//
// PdAudioUnit.m
// libpd
//
// Created on 29/09/11.
//
// For information on usage and redistribution, and for a DISCLAIMER OF ALL
// WARRANTIES, see the file, "LICENSE.txt," in this distribution.
//
#import "PdAudioUnit.h"
#import "PdBase.h"
#import "AudioHelpers.h"
#import <AudioToolbox/AudioToolbox.h>
static const AudioUnitElement kInputElement = 1;
static const AudioUnitElement kOutputElement = 0;
@interface PdAudioUnit () {
@private
BOOL inputEnabled_;
BOOL initialized_;
int blockSizeAsLog_;
}
- (BOOL)initAudioUnitWithSampleRate:(Float64)sampleRate numberChannels:(int)numChannels inputEnabled:(BOOL)inputEnabled;
- (void)destroyAudioUnit;
- (AudioComponentDescription)ioDescription;
@end
@implementation PdAudioUnit
@synthesize audioUnit = audioUnit_;
@synthesize active = active_;
#pragma mark - Init / Dealloc
- (id)init {
self = [super init];
if (self) {
initialized_ = NO;
active_ = NO;
blockSizeAsLog_ = log2int([PdBase getBlockSize]);
}
return self;
}
- (void)dealloc {
[self destroyAudioUnit];
[super dealloc];
}
#pragma mark - Public Methods
- (void)setActive:(BOOL)active {
if (!initialized_) {
return;
}
if (active == active_) {
return;
}
if (active) {
AU_RETURN_IF_ERROR(AudioOutputUnitStart(audioUnit_));
} else {
AU_RETURN_IF_ERROR(AudioOutputUnitStop(audioUnit_));
}
active_ = active;
}
- (int)configureWithSampleRate:(Float64)sampleRate numberChannels:(int)numChannels inputEnabled:(BOOL)inputEnabled {
Boolean wasActive = self.isActive;
inputEnabled_ = inputEnabled;
if (![self initAudioUnitWithSampleRate:sampleRate numberChannels:numChannels inputEnabled:inputEnabled_]) {
return -1;
}
[PdBase openAudioWithSampleRate:sampleRate inputChannels:(inputEnabled_ ? numChannels : 0) outputChannels:numChannels];
[PdBase computeAudio:YES];
self.active = wasActive;
return 0;
}
- (void)print {
if (!initialized_) {
AU_LOG(@"Audio Unit not initialized");
return;
}
UInt32 sizeASBD = sizeof(AudioStreamBasicDescription);
if (inputEnabled_) {
AudioStreamBasicDescription inputStreamDescription;
memset (&inputStreamDescription, 0, sizeof(inputStreamDescription));
AU_RETURN_IF_ERROR(AudioUnitGetProperty(audioUnit_,
kAudioUnitProperty_StreamFormat,
kAudioUnitScope_Output,
kInputElement,
&inputStreamDescription,
&sizeASBD));
AU_LOG(@"input ASBD:");
AU_LOG(@" mSampleRate: %.0fHz", inputStreamDescription.mSampleRate);
AU_LOG(@" mChannelsPerFrame: %lu", inputStreamDescription.mChannelsPerFrame);
AU_LOGV(@" mFormatID: %lu", inputStreamDescription.mFormatID);
AU_LOGV(@" mFormatFlags: %lu", inputStreamDescription.mFormatFlags);
AU_LOGV(@" mBytesPerPacket: %lu", inputStreamDescription.mBytesPerPacket);
AU_LOGV(@" mFramesPerPacket: %lu", inputStreamDescription.mFramesPerPacket);
AU_LOGV(@" mBytesPerFrame: %lu", inputStreamDescription.mBytesPerFrame);
AU_LOGV(@" mBitsPerChannel: %lu", inputStreamDescription.mBitsPerChannel);
} else {
AU_LOG(@"no input ASBD");
}
AudioStreamBasicDescription outputStreamDescription;
memset(&outputStreamDescription, 0, sizeASBD);
AU_RETURN_IF_ERROR(AudioUnitGetProperty(audioUnit_,
kAudioUnitProperty_StreamFormat,
kAudioUnitScope_Input,
kOutputElement,
&outputStreamDescription,
&sizeASBD));
AU_LOG(@"output ASBD:");
AU_LOG(@" mSampleRate: %.0fHz", outputStreamDescription.mSampleRate);
AU_LOG(@" mChannelsPerFrame: %lu", outputStreamDescription.mChannelsPerFrame);
AU_LOGV(@" mFormatID: %lu", outputStreamDescription.mFormatID);
AU_LOGV(@" mFormatFlags: %lu", outputStreamDescription.mFormatFlags);
AU_LOGV(@" mBytesPerPacket: %lu", outputStreamDescription.mBytesPerPacket);
AU_LOGV(@" mFramesPerPacket: %lu", outputStreamDescription.mFramesPerPacket);
AU_LOGV(@" mBytesPerFrame: %lu", outputStreamDescription.mBytesPerFrame);
AU_LOGV(@" mBitsPerChannel: %lu", outputStreamDescription.mBitsPerChannel);
}
// sets the format to 32 bit, floating point, linear PCM, interleaved
- (AudioStreamBasicDescription)ASBDForSampleRate:(Float64)sampleRate numberChannels:(UInt32)numberChannels {
const int kFloatSize = 4;
const int kBitSize = 8;
AudioStreamBasicDescription description;
memset(&description, 0, sizeof(description));
description.mSampleRate = sampleRate;
description.mFormatID = kAudioFormatLinearPCM;
description.mFormatFlags = kAudioFormatFlagsNativeFloatPacked;
description.mBytesPerPacket = kFloatSize * numberChannels;
description.mFramesPerPacket = 1;
description.mBytesPerFrame = kFloatSize * numberChannels;
description.mChannelsPerFrame = numberChannels;
description.mBitsPerChannel = kFloatSize * kBitSize;
return description;
}
#pragma mark - AURenderCallback
static OSStatus AudioRenderCallback(void *inRefCon,
AudioUnitRenderActionFlags *ioActionFlags,
const AudioTimeStamp *inTimeStamp,
UInt32 inBusNumber,
UInt32 inNumberFrames,
AudioBufferList *ioData) {
PdAudioUnit *pdAudioUnit = (PdAudioUnit *)inRefCon;
Float32 *auBuffer = (Float32 *)ioData->mBuffers[0].mData;
if (pdAudioUnit->inputEnabled_) {
AudioUnitRender(pdAudioUnit->audioUnit_, ioActionFlags, inTimeStamp, kInputElement, inNumberFrames, ioData);
}
int ticks = inNumberFrames >> pdAudioUnit->blockSizeAsLog_; // this is a faster way of computing (inNumberFrames / blockSize)
[PdBase processFloatWithInputBuffer:auBuffer outputBuffer:auBuffer ticks:ticks];
return noErr;
}
- (AURenderCallback)renderCallback {
return AudioRenderCallback;
}
#pragma mark - Private
- (void)destroyAudioUnit {
if (!initialized_) {
return;
}
self.active = NO;
initialized_ = NO;
AU_RETURN_IF_ERROR(AudioUnitUninitialize(audioUnit_));
AU_RETURN_IF_ERROR(AudioComponentInstanceDispose(audioUnit_));
AU_LOGV(@"destroyed audio unit");
}
- (BOOL)initAudioUnitWithSampleRate:(Float64)sampleRate numberChannels:(int)numChannels inputEnabled:(BOOL)inputEnabled {
[self destroyAudioUnit];
AudioComponentDescription ioDescription = [self ioDescription];
AudioComponent audioComponent = AudioComponentFindNext(NULL, &ioDescription);
AU_RETURN_FALSE_IF_ERROR(AudioComponentInstanceNew(audioComponent, &audioUnit_));
AudioStreamBasicDescription streamDescription = [self ASBDForSampleRate:sampleRate numberChannels:numChannels];
if (inputEnabled) {
UInt32 enableInput = 1;
AU_RETURN_FALSE_IF_ERROR(AudioUnitSetProperty(audioUnit_,
kAudioOutputUnitProperty_EnableIO,
kAudioUnitScope_Input,
kInputElement,
&enableInput,
sizeof(enableInput)));
AU_RETURN_FALSE_IF_ERROR(AudioUnitSetProperty(audioUnit_,
kAudioUnitProperty_StreamFormat,
kAudioUnitScope_Output, // Output scope because we're defining the output of the input element _to_ our render callback
kInputElement,
&streamDescription,
sizeof(streamDescription)));
}
AU_RETURN_FALSE_IF_ERROR(AudioUnitSetProperty(audioUnit_,
kAudioUnitProperty_StreamFormat,
kAudioUnitScope_Input, // Input scope because we're defining the input of the output element _from_ our render callback.
kOutputElement,
&streamDescription,
sizeof(streamDescription)));
AURenderCallbackStruct callbackStruct;
callbackStruct.inputProc = self.renderCallback;
callbackStruct.inputProcRefCon = self;
AU_RETURN_FALSE_IF_ERROR(AudioUnitSetProperty(audioUnit_,
kAudioUnitProperty_SetRenderCallback,
kAudioUnitScope_Input,
kOutputElement,
&callbackStruct,
sizeof(callbackStruct)));
AU_RETURN_FALSE_IF_ERROR(AudioUnitInitialize(audioUnit_));
initialized_ = YES;
AU_LOGV(@"initialized audio unit");
return true;
}
- (AudioComponentDescription)ioDescription {
AudioComponentDescription description;
description.componentType = kAudioUnitType_Output;
description.componentSubType = kAudioUnitSubType_RemoteIO;
description.componentManufacturer = kAudioUnitManufacturer_Apple;
description.componentFlags = 0;
description.componentFlagsMask = 0;
return description;
}
@end