-
Notifications
You must be signed in to change notification settings - Fork 5
/
AudioFileInputPatch.m
479 lines (420 loc) · 15.3 KB
/
AudioFileInputPatch.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
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
#import "AudioFileInputPatch.h"
#ifndef __LP64__
//////////
//
// GetMovieFromCFURLRef
// Instantiate a QuickTime movie for a QuickTime movie file CFURL.
//
//////////
static OSErr GetMovieFromCFURLRef(CFURLRef inURLRef, Movie *outMovieRef)
{
Handle outDataRef;
OSType outDataRefType;
// first create a QuickTime data reference for our CFURL
OSErr err = QTNewDataReferenceFromCFURL(inURLRef,
0,
&outDataRef,
&outDataRefType);
if(err != noErr)
return err;
DataReferenceRecord dataRefRecord;
Boolean active = true;
dataRefRecord.dataRefType = outDataRefType;
dataRefRecord.dataRef = outDataRef;
QTNewMoviePropertyElement newMovieProperties[] =
{
{kQTPropertyClass_DataLocation, kQTDataLocationPropertyID_DataReference, sizeof(dataRefRecord), &dataRefRecord, 0},
{kQTPropertyClass_NewMovieProperty, kQTNewMoviePropertyID_Active, sizeof(active), &active, 0},
};
// instantiate a QuickTime movie from our CFURL data reference
err = NewMovieFromProperties(sizeof(newMovieProperties) / sizeof(newMovieProperties[0]), newMovieProperties, 0, nil, outMovieRef);
DisposeHandle(outDataRef);
return err;
}
#endif
@implementation AudioFileInputPatch : QCPatch
+ (QCPatchExecutionMode)executionModeWithIdentifier:(id)fp8
{
return kQCPatchExecutionModeProvider;
}
+ (BOOL)allowsSubpatchesWithIdentifier:(id)fp8
{
return NO;
}
+ (QCPatchTimeMode)timeModeWithIdentifier:(id)fp8
{
return kQCPatchTimeModeTimeBase;
}
- (id)initWithIdentifier:(id)fp8
{
if(self=[super initWithIdentifier:fp8])
{
[[self userInfo] setObject: @"Kineme Audio File Input" forKey: @"name"];
[inputSampleBuffer setIndexValue:1]; // 512
_requestedFrames = 512;
[inputSampleBuffer setMaxIndexValue:8];
[inputFrequencyMode setIndexValue:0];
[inputFrequencyMode setMaxIndexValue:3];
}
return self;
}
- (BOOL)setup:(QCOpenGLContext *)context
{
_fft = [[AudioToolsFFT alloc] initWithFrameSize:_requestedFrames];
return YES;
}
- (void)cleanup:(QCOpenGLContext *)context
{
if(audioFile)
{
ExtAudioFileDispose(audioFile);
audioFile = NULL;
}
#ifndef __LP64__
if(movieFile)
{
MovieAudioExtractionEnd(extractionSessionRef);
DisposeMovie(movieFile);
movieFile = NULL;
}
#endif
if(mBufferList && mBufferList->mBuffers[0].mData)
free(mBufferList->mBuffers[0].mData);
if(mBufferList)
free(mBufferList);
mBufferList = NULL;
[_fft release];
}
- (void)enable:(QCOpenGLContext *)context
{
cs = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB);
}
- (void)disable:(QCOpenGLContext *)context
{
CGColorSpaceRelease(cs);
}
- (BOOL)execute:(QCOpenGLContext *)context time:(double)time arguments:(NSDictionary *)arguments
{
int requestedFrames = 0x100 << [inputSampleBuffer indexValue];
bool requestedFramesUpdated = NO;
if( requestedFrames != _requestedFrames)
{
[_fft release];
_fft = [[AudioToolsFFT alloc] initWithFrameSize:requestedFrames];
_requestedFrames = requestedFrames;
requestedFramesUpdated = YES;
}
if( [inputPath wasUpdated] || requestedFramesUpdated )
{
NSString *path = KIExpandPath(self, [inputPath stringValue]);
NSURL *url = [NSURL fileURLWithPath: path];
if(audioFile)
{
ExtAudioFileDispose(audioFile);
audioFile = NULL;
if(mBufferList && mBufferList->mBuffers[0].mData)
free(mBufferList->mBuffers[0].mData);
if(mBufferList)
free(mBufferList);
mBufferList = NULL;
}
#ifndef __LP64__
if(movieFile)
{
MovieAudioExtractionEnd(extractionSessionRef);
DisposeMovie(movieFile);
movieFile = NULL;
if(mBufferList && mBufferList->mBuffers[0].mData)
free(mBufferList->mBuffers[0].mData);
if(mBufferList)
free(mBufferList);
mBufferList = NULL;
}
#endif
CFStringRef itemUTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, (CFStringRef)[path pathExtension], NULL);
if(UTTypeConformsTo(itemUTI, kUTTypeAudio))
{
if(ExtAudioFileOpenURL((CFURLRef)url, &audioFile))
{
[self logMessage:@"Error opening '%@'", [inputPath stringValue]];
audioFile = NULL;
return YES;
}
// See http://lists.apple.com/archives/Coreaudio-api/2006/Nov/msg00116.html for 64-bit insanity on this
mBufferList = (AudioBufferList*)calloc(1,sizeof(AudioBufferList));
AudioStreamBasicDescription fileAsbd, clientAsbd;
UInt32 size = sizeof(fileAsbd);
bzero(&fileAsbd,size);
bzero(&clientAsbd, size);
if(ExtAudioFileGetProperty(audioFile, kExtAudioFileProperty_FileDataFormat, &size, &fileAsbd))
{
[self logMessage: @"Error reading audio file properties!"];
ExtAudioFileDispose(audioFile);
audioFile = NULL;
return YES;
}
mBufferList->mBuffers[0].mNumberChannels = 2;
mBufferList->mBuffers[0].mDataByteSize = requestedFrames*4*fileAsbd.mChannelsPerFrame;
mBufferList->mBuffers[0].mData = malloc(requestedFrames*4*fileAsbd.mChannelsPerFrame);
mBufferList->mBuffers[0].mNumberChannels = fileAsbd.mChannelsPerFrame;
sampleRate = fileAsbd.mSampleRate;
clientAsbd.mSampleRate = fileAsbd.mSampleRate;
#if defined (__ppc__) || (__ppc64__)
clientAsbd.mFormatFlags = kAudioFormatFlagIsFloat | kAudioFormatFlagIsPacked | kAudioFormatFlagIsBigEndian;
#elif (__i386__) || (__x86_64__)
clientAsbd.mFormatFlags = kAudioFormatFlagIsFloat | kAudioFormatFlagIsPacked;
#else
#error what arch is this?
#endif
clientAsbd.mFormatID = kAudioFormatLinearPCM;
clientAsbd.mChannelsPerFrame = fileAsbd.mChannelsPerFrame;
clientAsbd.mFramesPerPacket = 1;
clientAsbd.mBytesPerFrame = sizeof(float) * fileAsbd.mChannelsPerFrame;
clientAsbd.mBytesPerPacket = clientAsbd.mBytesPerFrame;
clientAsbd.mBitsPerChannel = sizeof(float) * 8;
if(fileAsbd.mFormatID == kAudioFormatMPEGLayer3)
isMP3 = YES;
else
isMP3 = NO;
if(ExtAudioFileSetProperty(audioFile, kExtAudioFileProperty_ClientDataFormat, sizeof(clientAsbd), &clientAsbd))
{
[self logMessage: @"Error setting client properties!"];
ExtAudioFileDispose(audioFile);
audioFile = NULL;
return YES;
}
AudioConverterRef acRef;
UInt32 acrsize=sizeof(AudioConverterRef);
if(ExtAudioFileGetProperty(audioFile, kExtAudioFileProperty_AudioConverter, &acrsize, &acRef))
{
[self logMessage: @"Error getting audio converter!"];
ExtAudioFileDispose(audioFile);
audioFile = NULL;
return YES;
}
AudioConverterPrimeInfo primeInfo;
UInt32 piSize=sizeof(AudioConverterPrimeInfo);
if(AudioConverterGetProperty(acRef, kAudioConverterPrimeInfo, &piSize, &primeInfo) != kAudioConverterErr_PropertyNotSupported)
headerFrames = primeInfo.leadingFrames;
else
headerFrames = 0;
}
else if(UTTypeConformsTo(itemUTI, kUTTypeMovie))
{
#ifndef __LP64__
if(GetMovieFromCFURLRef((CFURLRef)url, &movieFile) == noErr)
MovieAudioExtractionBegin(movieFile, 0, &extractionSessionRef);
else
{
[self logMessage:@"Error opening '%@'", [inputPath stringValue]];
movieFile = NULL;
return YES;
}
// See http://lists.apple.com/archives/Coreaudio-api/2006/Nov/msg00116.html for 64-bit insanity on this
mBufferList = (AudioBufferList*)calloc(1,sizeof(AudioBufferList));
OSStatus err;
Boolean allChannelsDiscrete = true;
// disable mixing of audio channels
err = MovieAudioExtractionSetProperty(extractionSessionRef,
kQTPropertyClass_MovieAudioExtraction_Movie,
kQTMovieAudioExtractionMoviePropertyID_AllChannelsDiscrete,
sizeof (Boolean), &allChannelsDiscrete);
AudioStreamBasicDescription asbd;
// Get the default audio extraction ASBD
err = MovieAudioExtractionGetProperty(extractionSessionRef,
kQTPropertyClass_MovieAudioExtraction_Audio,
kQTMovieAudioExtractionAudioPropertyID_AudioStreamBasicDescription,
sizeof (asbd), &asbd, nil);
mBufferList->mBuffers[0].mNumberChannels = 2;
mBufferList->mBuffers[0].mDataByteSize = requestedFrames*4*asbd.mChannelsPerFrame;
mBufferList->mBuffers[0].mData = malloc(requestedFrames*4*asbd.mChannelsPerFrame);
mBufferList->mBuffers[0].mNumberChannels = asbd.mChannelsPerFrame;
#if defined (__ppc__) || (__ppc64__)
asbd.mFormatFlags = kAudioFormatFlagIsFloat | kAudioFormatFlagIsPacked | kAudioFormatFlagIsBigEndian;
#elif (__i386__) || (__x86_64__)
asbd.mFormatFlags = kAudioFormatFlagIsFloat | kAudioFormatFlagIsPacked;
#else
#error what arch is this?
#endif
asbd.mBitsPerChannel = sizeof(float) * 8;
asbd.mBytesPerFrame = sizeof(float) * asbd.mChannelsPerFrame;
asbd.mBytesPerPacket = asbd.mBytesPerFrame;
// Set the new audio extraction ASBD (ensure float samples)
err = MovieAudioExtractionSetProperty(extractionSessionRef,
kQTPropertyClass_MovieAudioExtraction_Audio,
kQTMovieAudioExtractionAudioPropertyID_AudioStreamBasicDescription,
sizeof (asbd), &asbd);
//NSLog(@"set err: %i", err);
#endif
}
CFRelease(itemUTI);
}
if(audioFile)
{
UInt32 numFrames = requestedFrames;
mBufferList->mNumberBuffers = 1;
mBufferList->mBuffers[0].mDataByteSize = requestedFrames*4 * mBufferList->mBuffers[0].mNumberChannels;
// clear so if we go over we get proper zeros at the end if we have a partial packet
bzero(mBufferList->mBuffers[0].mData,mBufferList->mBuffers[0].mNumberChannels * requestedFrames * sizeof(float));
if(isMP3)
{
// MP3 is dumb, and produces silence for the first few hundred samples after a seek.
// to work around that, we seek to 1024 samples before our desired point, and decode twice to catch up
// with a populated decoder stream.
if(ExtAudioFileSeek(audioFile, MAX(time * sampleRate + headerFrames - 1024, headerFrames)) )
{
[self logMessage: @"Error seeking to current frame...!"];
return YES;
}
ExtAudioFileRead(audioFile, &numFrames, mBufferList);
numFrames = 512;
ExtAudioFileRead(audioFile, &numFrames, mBufferList);
numFrames = 512;
}
else
{
if(ExtAudioFileSeek(audioFile, time * sampleRate + headerFrames))
{
[self logMessage: @"Error seeking to current frame...!"];
return YES;
}
}
if(ExtAudioFileRead(audioFile, &numFrames, mBufferList) || numFrames != requestedFrames)
{
// if we can't read all samples (EOF or error), manually set our buffer size stuff
mBufferList->mBuffers[0].mDataByteSize = mBufferList->mBuffers[0].mNumberChannels * requestedFrames * sizeof(float);
}
}
#ifndef __LP64__
else if(movieFile)
{
OSStatus err;
TimeRecord timeRec;
timeRec.scale = GetMovieTimeScale(movieFile);
timeRec.base = NULL;
timeRec.value.hi = 0;
timeRec.value.lo = time * timeRec.scale;
// Set the extraction current time. The duration will
// be determined by how much is pulled.
err = MovieAudioExtractionSetProperty(extractionSessionRef,
kQTPropertyClass_MovieAudioExtraction_Movie,
kQTMovieAudioExtractionMoviePropertyID_CurrentTime,
sizeof(TimeRecord), &timeRec);
if(err!= noErr)
NSLog(NSLocalizedString(@"error setting time: %i", @""), err);
UInt32 flags = 0;
UInt32 numFrames = requestedFrames;
mBufferList->mNumberBuffers = 1;
mBufferList->mBuffers[0].mDataByteSize = requestedFrames*4 * mBufferList->mBuffers[0].mNumberChannels;
// clear so if we go over we get proper zeros at the end if we have a partial packet
bzero(mBufferList->mBuffers[0].mData, mBufferList->mBuffers[0].mNumberChannels * requestedFrames * sizeof(float));
err = MovieAudioExtractionFillBuffer(extractionSessionRef, &numFrames, mBufferList, &flags);
if (flags & kQTMovieAudioExtractionComplete)
{
// extraction complete!
//NSLog(@"extraction complete!");
}
//NSLog(@"numFrames: %i flags: %x err: %i", numFrames, flags, err);
}
#endif
{
if(mBufferList && mBufferList->mBuffers[0].mNumberChannels)
{
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 currentChannel=0;
{
AudioBuffer audioBuffer = mBufferList->mBuffers[0];
float *sampleData = (float*)audioBuffer.mData;
float max;
unsigned int dataSize = audioBuffer.mDataByteSize/(audioBuffer.mNumberChannels * sizeof(float));
unsigned int freqMode = [inputFrequencyMode indexValue];
for(j = 0; j < audioBuffer.mNumberChannels; ++j)
{
NSMutableArray *channel = [[NSMutableArray alloc] initWithCapacity: dataSize];
IMP addObject = [channel methodForSelector:@selector(addObject:)];
unsigned int offset = j;
max = 0;
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 stringWithFormat:@"channel%02i",currentChannel];
[data addMember: chan forKey: channelNumber];
[peak addMember: [NSNumber numberWithFloat: max] forKey: channelNumber];
[freq addMember: fchan forKey: channelNumber];
++currentChannel;
[chan release];
[fchan release];
}
}
[outputWaveform setStructureValue: data];
[outputPeaks setStructureValue: peak];
[outputFrequency setStructureValue: freq];
[data release];
[peak release];
[freq release];
QCImagePixelBuffer *pb = [[context imageManager] createPixelBufferWithFormat: [QCPixelFormat pixelFormatARGB8]
pixelsWide: requestedFrames
pixelsHigh: mBufferList->mBuffers[0].mNumberChannels
options: nil];
unsigned int *audioImageData = (unsigned int*)[pb baseAddress];
unsigned int rowBytes = [pb bytesPerRow]/sizeof(unsigned int);
[pb beginUpdatePixels: FALSE colorSpace: cs];
float *floatData = (float*)mBufferList->mBuffers[0].mData;
unsigned int stride = mBufferList->mBuffers[0].mNumberChannels;
for(j=0;j<mBufferList->mBuffers[0].mNumberChannels;++j)
{
unsigned int offset = j;
for(i=0;i<requestedFrames;++i)
{
unsigned int value = (127.f * (1.0f + floatData[offset]));
value *= 0x01010101;
audioImageData[i] = value;
offset += stride;
}
audioImageData += rowBytes;
}
[pb endUpdatePixels];
QCImage *audioImage = [[QCImage allocWithZone:NULL] initWithQCImageBuffer: pb options: nil];
[outputWaveformImage setImageValue: audioImage];
[audioImage release];
[pb release];
}
else // no buffers = no/invalid input file
{
[outputWaveformImage setImageValue:nil];
[outputWaveform setStructureValue:nil];
[outputPeaks setStructureValue:nil];
[outputFrequency setStructureValue:nil];
}
}
return YES;
}
@end