-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathAudioInputPatch.m
332 lines (287 loc) · 9.13 KB
/
AudioInputPatch.m
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
#import "AudioInputPatch.h"
static OSStatus audioBufferHandler(AudioDeviceID inDevice,
const AudioTimeStamp* inNow,
const AudioBufferList* inInputData,
const AudioTimeStamp* inInputTime,
AudioBufferList* outOutputData,
const AudioTimeStamp* inOutputTime,
void* inClientData)
{
// we might've (accidentally) selected an audio device with no input channels
if( inInputData->mNumberBuffers > 0 )
{
// NSLog(@"read stuff! (%i buffers)", inInputData->mNumberBuffers);
AudioInputPatch *ai = (AudioInputPatch*)inClientData;
NSAutoreleasePool *p = [[NSAutoreleasePool allocWithZone:NULL] init];
[ai setAudioData:inInputData];
[p drain];
}
return 0;
}
@implementation AudioInputPatch : QCPatch
+ (QCPatchExecutionMode)executionModeWithIdentifier:(id)fp8
{
return kQCPatchExecutionModeProvider;
}
+ (BOOL)allowsSubpatchesWithIdentifier:(id)fp8
{
return NO;
}
+ (QCPatchTimeMode)timeModeWithIdentifier:(id)fp8
{
return kQCPatchTimeModeTimeBase;
}
- (void)setAudioData:(const AudioBufferList *)audioBufferList
{
QCStructure *data = [[QCStructure allocWithZone:NULL] init], *chan, *fchan;
QCStructure *peak = [[QCStructure allocWithZone:NULL] init];
QCStructure *freq = [[QCStructure allocWithZone:NULL] init];
unsigned int i, j;
NSUInteger currentBuffer;
NSUInteger currentChannel=0;
unsigned int freqMode = [inputFrequencyMode indexValue];
NSArray *channel_strings = [[[inputChannels stringValue] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] componentsSeparatedByString:@","];
for(currentBuffer=0;currentBuffer<audioBufferList->mNumberBuffers;++currentBuffer)
{
AudioBuffer audioBuffer = audioBufferList->mBuffers[currentBuffer];
float *sampleData = (float*)audioBuffer.mData;
float max;
unsigned int dataSize = audioBuffer.mDataByteSize/(audioBuffer.mNumberChannels * sizeof(float));
for(j = 0; j < audioBuffer.mNumberChannels; ++j)
{
if(![[inputChannels stringValue] isEqualToString:@"all"])
if(![channel_strings containsObject:[NSString stringWithFormat:@"%u",j]]) {
continue;
}
NSMutableArray *channel = [[NSMutableArray allocWithZone:NULL] initWithCapacity: dataSize];
unsigned int offset = j;
max = 0;
IMP addObject = [channel methodForSelector:@selector(addObject:)];
for(i=0; i< dataSize; ++i)
{
CFNumberRef num = CFNumberCreate(NULL, kCFNumberFloatType, &sampleData[offset]);
addObject(channel, @selector(addObject:), num);
CFRelease(num);
if( fabsf(sampleData[offset]) > max)
max = fabsf(sampleData[offset]);
offset += audioBuffer.mNumberChannels;
}
NSArray *freqChannel;
if( freqMode )
{
freqChannel = [_fft
frequenciesForSampleData:sampleData
numFrames:dataSize
usingChannel:j
ofChannels:audioBuffer.mNumberChannels
frequencyMode:freqMode
];
}
else
freqChannel = nil;
chan = [[QCStructure allocWithZone:NULL] initWithArray: channel];
fchan = [[QCStructure allocWithZone:NULL] initWithArray: freqChannel];
[channel release];
[freqChannel release];
NSString *channelNumber = [[NSString allocWithZone:NULL] initWithFormat:@"channel%02i",currentChannel];
[data addMember: chan forKey: channelNumber];
[peak addMember: [NSNumber numberWithFloat: max] forKey: channelNumber];
[freq addMember: fchan forKey: channelNumber];
[channelNumber release];
++currentChannel;
[chan release];
[fchan release];
}
}
QCStructure *oldAudioFreq = audioFreq;
QCStructure *oldAudioData = audioData;
QCStructure *oldAudioPeaks = audioPeaks;
pthread_mutex_lock(&dataLock);
{
audioFreq = freq;
audioData = data;
audioPeaks = peak;
}
pthread_mutex_unlock(&dataLock);
[oldAudioFreq release];
[oldAudioData release];
[oldAudioPeaks release];
}
- (id)initWithIdentifier:(id)fp8
{
if(self=[super initWithIdentifier:fp8])
{
//[self connect];
pf = [QCPixelFormat pixelFormatARGB8];
[[self userInfo] setObject:@"Kineme Audio Input" forKey:@"name"];
[inputFrequencyMode setIndexValue:0];
[inputFrequencyMode setMaxIndexValue:3];
[inputChannels setStringValue:@"all"];
}
return self;
}
- (BOOL)setup:(QCOpenGLContext *)context
{
pthread_mutex_init(&dataLock, NULL);
_fft = [[AudioToolsFFT alloc] initWithFrameSize:512];
[self setDeviceUID:[inputDeviceUID stringValue]];
[self connect];
return YES;
}
- (void)cleanup:(QCOpenGLContext *)context
{
AudioDeviceDestroyIOProcID(device, procID);
[_fft release];
pthread_mutex_destroy(&dataLock);
}
- (void)enable:(QCOpenGLContext *)context
{
cs = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB);
AudioDeviceStart(device, procID);
}
- (void)disable:(QCOpenGLContext *)context
{
AudioDeviceStop(device, procID);
CGColorSpaceRelease(cs);
}
- (void)connect
{
if(device)
{
AudioDeviceStop(device, procID);
AudioDeviceDestroyIOProcID(device, procID);
}
UInt32 size, count;
//device = kAudioHardwarePropertyDefaultInputDevice;//devices[0];
size = sizeof(AudioDeviceID);
if( (deviceUID == nil) || ([deviceUID length] == 0) )
{
//NSLog(@"using default input device");
AudioHardwareGetProperty(kAudioHardwarePropertyDefaultInputDevice, &size, &device);
}
else
{
NSString *uid = nil;
unsigned int i;
AudioHardwareGetPropertyInfo(kAudioHardwarePropertyDevices, &size, NULL);
//NSLog(@"%i devices", size / sizeof(AudioDeviceID));
count = size/sizeof(AudioDeviceID);
AudioDeviceID devices[count];
AudioHardwareGetProperty(kAudioHardwarePropertyDevices,&size, &devices);
for(i=0; i < count; ++i)
{
size = sizeof(NSString*);
if(AudioDeviceGetProperty(devices[i],
0, YES,
kAudioDevicePropertyDeviceUID,
&size, &uid))
NSLog(NSLocalizedString(@"Error getting device uid", @""));
if([uid isEqual: deviceUID])
{
//NSLog(@"found matching device %i %@", devices[i],uid);
device = devices[i];
}
}
}
//AudioDeviceAddIOProc(device, audioBufferHandler, self);
//AudioDeviceStart(device, audioBufferHandler);
if(AudioDeviceCreateIOProcID(device,
audioBufferHandler,
self,
&procID))
NSLog(NSLocalizedString(@"Error creating IOProc", @""));
//AudioDeviceStart(device, procID);
AudioStreamBasicDescription recordFormat;
size = sizeof(recordFormat);
AudioHardwareGetProperty(kAudioDevicePropertyStreamFormat, &size, &recordFormat);
if(recordFormat.mFormatFlags & kLinearPCMFormatFlagIsFloat == FALSE)
{
NSLog(NSLocalizedString(@"QCAudioInput: non-floating samples not currently supported", @""));
}
//NSLog(@"float samples: %i",recordFormat.mFormatFlags & kLinearPCMFormatFlagIsFloat);
//AudioDeviceStart(device, procID);
}
- (BOOL)execute:(QCOpenGLContext *)context time:(double)time arguments:(NSDictionary *)arguments
{
if( [inputDeviceUID wasUpdated] )
[self setDeviceUID:[inputDeviceUID stringValue]];
if(audioData)
{
QCStructure *localAudioData;
QCStructure *localAudioPeaks;
QCStructure *localFreqData;
pthread_mutex_lock(&dataLock);
{
localAudioData = [audioData retain];
localAudioPeaks = [audioPeaks retain];
localFreqData = [audioFreq retain];
}
pthread_mutex_unlock(&dataLock);
/* This is usually true, but when it's not, we can skip everything.
if we're in non-vblsync mode, this is false most of the time (until our framerate drops to ~80-90 fps) */
BOOL needsUpdate = ([outputWaveform structureValue] != localAudioData);
if(needsUpdate)
{
[outputWaveform setStructureValue: localAudioData];
[outputPeaks setStructureValue: localAudioPeaks];
[outputFrequency setStructureValue: localFreqData];
unsigned int i, j, count;
unsigned int channelCount = [localAudioData count];
count = [[localAudioData memberAtIndex:0] count];
id audioDataMember;
unsigned int value;
QCImagePixelBuffer *pb;
pb = [[context imageManager] createPixelBufferWithFormat: pf
pixelsWide: count
pixelsHigh: channelCount
options: nil];
unsigned int rowBytes = [pb bytesPerRow]/sizeof(unsigned int);
unsigned int *audioImageData = (unsigned int*)[pb baseAddress];
[pb beginUpdatePixels:FALSE colorSpace: cs];
for(j=0;j<channelCount;++j)
{
audioDataMember = [localAudioData memberAtIndex:j];
i = 0;
for(NSNumber *val in (NSArray*)[audioDataMember _list])
{
value = 127.f*(1.f+[val floatValue]);
value *= 0x01010101;
audioImageData[i] = value;
++i;
}
audioImageData += rowBytes;
}
[pb endUpdatePixels];
QCImage *audioImage = [[QCImage allocWithZone:NULL] initWithQCImageBuffer: pb options: nil];
[outputWaveformImage setImageValue: audioImage];
[pb release];
[audioImage release];
}
[localAudioData release];
[localFreqData release];
[localAudioPeaks release];
}
else // no data
{
[outputWaveform setStructureValue:nil];
[outputPeaks setStructureValue:nil];
[outputFrequency setStructureValue:nil];
[outputWaveformImage setImageValue:nil];
}
return YES;
}
- (void)setDeviceUID:(NSString*)uid
{
if([uid isEqual:deviceUID] == FALSE)
{
[deviceUID release];
deviceUID = [uid retain];
[self connect]; // stops the old device
AudioDeviceStart(device, procID);
}
}
-(NSString*)deviceUID
{
return deviceUID;
}
@end