/
CoreAudioInputProcessor.mm
361 lines (281 loc) · 9 KB
/
CoreAudioInputProcessor.mm
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
//
// CoreAudioInputProcessor.m
// Oscilloscope
//
// Created by koji on 11/01/20.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import "CoreAudioInputProcessor.h"
#include <AudioToolbox/AudioFormat.h>
#include "util.h"
#define SUCCEEDED(result) (result == noErr)
#define FAILED(result) (result != noErr)
//vector as smart pointer for C-arrays
#include <vector>
#define Array std::vector //oops, need temlate typedef of C++0x,,
#pragma mark -
//callback for audio unit
static OSStatus InputProc(
void *inRefCon,
AudioUnitRenderActionFlags *ioActionFlags,
const AudioTimeStamp *inTimeStamp,
UInt32 inBusNumber,
UInt32 inNumberFrames,
AudioBufferList * ioData)
{
CoreAudioInputProcessor *processor = (CoreAudioInputProcessor *)inRefCon;
return [processor inputComming:ioActionFlags :inTimeStamp :inBusNumber :inNumberFrames :ioData];
}
@implementation CoreAudioInputProcessor
-(void) allocateTempBuffer{
//main code is copied from RecordAudioToFile sample.
_tempBufferList = (AudioBufferList *)malloc(sizeof(AudioBufferList) + sizeof(AudioBuffer)); //two channel
if (_tempBufferList == NULL){
NSLog(@"failed to allocalte bufferlist");
return ;
}
//get bytesize for each frame
UInt32 frames = 0;
UInt32 size = sizeof(UInt32);
//get the size form device
OSStatus result = AudioUnitGetProperty(_inputUnit, kAudioDevicePropertyBufferFrameSize,
kAudioUnitScope_Global, 0, &frames, &size);
IF_FAILED(result,"can not get buffer frame size"){
return;
}
NSLog(@"frame size = %u", frames);
UInt32 byteSize = frames * sizeof(float);
_tempBufferList->mNumberBuffers = 2; //2ch
for (int i = 0; i < 2 ; i++){
_tempBufferList->mBuffers[i].mNumberChannels = 1;
_tempBufferList->mBuffers[i].mDataByteSize = byteSize;
_tempBufferList->mBuffers[i].mData = malloc(byteSize);
if (_tempBufferList->mBuffers[i].mData == NULL){
NSLog(@"failed to allocate buffer memory, byteSize = %d", byteSize);
return;
}
}
}
//find the audio device which name is SoundFlower
-(AudioDeviceID) getSoundFlowerDeviceID{
OSStatus result = noErr;
UInt32 propSize;
result = AudioHardwareGetPropertyInfo(kAudioHardwarePropertyDevices,&propSize,NULL);
if(FAILED(result)){
NSLog(@"failed to get device list err = %d\n", result);
return 0;
}
int nDevices = propSize / sizeof(AudioDeviceID);
Array<AudioDeviceID> deviceIDs = std::vector<AudioDeviceID>(nDevices);
result = AudioHardwareGetProperty(kAudioHardwarePropertyDevices, &propSize, &deviceIDs[0]);
if(FAILED(result)){
NSLog(@"failed to get device list err = %d\n", result);
return 0;
}
for (int i = 0; i < nDevices; i++){
UInt32 size = 256;
char name[256];
result = AudioDeviceGetProperty(deviceIDs[i], 0,1/*input side*/,kAudioDevicePropertyDeviceName, &size, name);
NSLog(@"Device: %ld, name = %s", deviceIDs[i], name);
if (strcmp("Soundflower (2ch)", name) == 0){
AudioDeviceID sf_id = deviceIDs[i];
NSLog(@"found sound flower: AudioDeviceID=%ld", sf_id);
return sf_id;
}
}
return 0;
}
-(bool)setInputDevice{
OSStatus result = noErr;
//we should enable input and disable output at first.. shit! see TN2091.
{
UInt32 enableIO = 1;
result = AudioUnitSetProperty(_inputUnit,
kAudioOutputUnitProperty_EnableIO,
kAudioUnitScope_Input,
1, // input element
&enableIO,
sizeof(enableIO));
IF_FAILED(result, "failed to enable input."){
return false;
}
enableIO = 0;
AudioUnitSetProperty(_inputUnit,
kAudioOutputUnitProperty_EnableIO,
kAudioUnitScope_Output,
0, //output element
&enableIO,
sizeof(enableIO));
IF_FAILED(result, "failed to disable output."){
return false;
}
}
AudioDeviceID in = [self getSoundFlowerDeviceID];
//set the current to specified in
{
UInt32 size = sizeof(AudioDeviceID);
//should we init something??
result = AudioUnitSetProperty(_inputUnit,
kAudioOutputUnitProperty_CurrentDevice,
kAudioUnitScope_Global,
0,
&in,
size);
IF_FAILED(result, "##Failed to set input device to sound flower!!!"){
return false;
}
}
return true;
}
-(bool)setFormat{
AudioStreamBasicDescription format;
UInt32 size = sizeof(AudioStreamBasicDescription);
OSStatus result = noErr;
result = AudioUnitGetProperty(_inputUnit, kAudioUnitProperty_StreamFormat,
kAudioUnitScope_Input,
1, /*input element*/
&format,
&size);
IF_FAILED(result ," failed to get stream format"){
return false;
}
dump_struct(format);
NSLog(@"format = %@", EnumToFOURCC(format.mFormatID));
if (format.mFormatFlags & kAudioFormatFlagIsFloat){
NSLog(@"format is float");
}
if (format.mFormatFlags & kAudioFormatFlagIsNonInterleaved){
NSLog(@"format is non-interleaved");
}else{
NSLog(@"format is interleaved");
}
// i want to use non-interleaved stereo float(32bit).
AudioStreamBasicDescription newFormat;
memcpy(&newFormat, &format, sizeof( newFormat ));
newFormat.mFormatFlags = kAudioFormatFlagIsFloat | kAudioFormatFlagIsPacked | kAudioFormatFlagIsNonInterleaved;
newFormat.mBytesPerPacket = 4; //for non-interleaved, this should be ok see AudioInputProc sample in Core Audio SDK
newFormat.mFramesPerPacket = 1;
newFormat.mBytesPerFrame = 4;
newFormat.mChannelsPerFrame = 2; //note a packet include frames
newFormat.mBitsPerChannel = 32;
//set the output side of input element(1)
result = AudioUnitSetProperty(_inputUnit, kAudioUnitProperty_StreamFormat,
kAudioUnitScope_Output,
1, /*input element*/
&newFormat,
size);
IF_FAILED(result ," failed to set stream format"){
if (result == kAudioFormatUnsupportedDataFormatError){
NSLog(@"Unsupported format error");
}
return false;
}
return true;
}
-(bool)setCallback{
AURenderCallbackStruct input;
input.inputProc = InputProc;
input.inputProcRefCon = self;
OSStatus result = noErr;
result = AudioUnitSetProperty(
_inputUnit,
kAudioOutputUnitProperty_SetInputCallback,
kAudioUnitScope_Global, //somehow, this is global task
0, //so, ignore this.
&input,
sizeof(input));
IF_FAILED(result , "failed to set callback!!"){
return false;
}
NSLog(@"succeeded to set callback. now ready to go");
return true;
}
-(bool) initProcessor{
NSLog(@"init");
OSStatus result = noErr;
ComponentDescription desc;
desc.componentType = kAudioUnitType_Output;
desc.componentSubType = kAudioUnitSubType_HALOutput;
desc.componentManufacturer = kAudioUnitManufacturer_Apple;
desc.componentFlags = 0;
desc.componentFlagsMask = 0;
Component comp = FindNextComponent(NULL, &desc);
if (comp == NULL){
NSLog(@"failed to get HALOutput Audio Component\n");
}
result = OpenAComponent(comp, &_inputUnit);
if (_inputUnit == NULL){
NSLog(@"failed to open Audio HAL Unit. OpenAComponent err = %ld\n", result);
return false;
}
result = AudioUnitInitialize(_inputUnit);
if (FAILED(result)){
NSLog(@"failed to init Audio HAL Unit. er = %ld\n", result);
return false;
}
if (![self setInputDevice]) return false;
if (![self setFormat]) return false;
if (![self setCallback]) return false;
[self allocateTempBuffer];
return true;
}
-(bool) start{
NSLog(@"start");
OSStatus result = noErr;
result = AudioUnitInitialize(_inputUnit);
IF_FAILED(result, "failed to initialize in start()"){
return false;
}
result = AudioOutputUnitStart(_inputUnit);
IF_FAILED(result, "failed to AudioOutputUnitStart() in start()"){
return false;
}
NSLog(@"successfully startd Audio Unit Engine for Record");
return true;
}
-(bool) stop{
NSLog(@"%s", _cmd);
OSStatus result = noErr;
result = AudioOutputUnitStop(_inputUnit);
IF_FAILED(result, "failed to AudioOutputUnitStop() in stop()"){
return false;
}
return true;
}
- (OSStatus) inputComming:(AudioUnitRenderActionFlags *)ioActionFlags :(const AudioTimeStamp *) inTimeStamp:
(UInt32) inBusNumber: (UInt32) inNumberFrames :(AudioBufferList *)ioData{
//NSLog(@"inputComming, bus number = %u, frames = %u, flag=%d",inBusNumber, inNumberFrames, *ioActionFlags );
OSStatus result = AudioUnitRender(
_inputUnit,
ioActionFlags,
inTimeStamp,
inBusNumber,
inNumberFrames,
_tempBufferList //TODO: check Buffer.mData with NULL may OK..
);
if (result != noErr){
NSLog(@"failed to AudioUnitRender, err = %lu", result);
}
float *temp_left = (float *)(_tempBufferList->mBuffers[0].mData);
float *temp_right = (float *)(_tempBufferList->mBuffers[1].mData);
static UInt32 count = 0;
if ((count % 1000) == 0){
NSLog(@"input: %d frames, first sample is %+6.5f", inNumberFrames, temp_left[0]); //-0.11096, +0.22430,, etc
}
count++;
@synchronized(self){
for (int i = 0 ; i < inNumberFrames; i++){
left.push_back(temp_left[i]);
right.push_back(temp_right[i]);
}
}
return noErr;
}
#pragma mark -- accessors--
-(std::vector<float> *) left{
return &left;
}
-(std::vector<float> *) right{
return &right;
}
@end