summaryrefslogtreecommitdiff
path: root/libs/appleutility/CoreAudio/PublicUtility/CAAUProcessor.cpp
blob: 1cda39c99d3619deca31a19fc200d1bd450ae5f5 (plain)
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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
/*
     File: CAAUProcessor.cpp
 Abstract: CAAUProcessor.h
  Version: 1.1
 
 Disclaimer: IMPORTANT:  This Apple software is supplied to you by Apple
 Inc. ("Apple") in consideration of your agreement to the following
 terms, and your use, installation, modification or redistribution of
 this Apple software constitutes acceptance of these terms.  If you do
 not agree with these terms, please do not use, install, modify or
 redistribute this Apple software.
 
 In consideration of your agreement to abide by the following terms, and
 subject to these terms, Apple grants you a personal, non-exclusive
 license, under Apple's copyrights in this original Apple software (the
 "Apple Software"), to use, reproduce, modify and redistribute the Apple
 Software, with or without modifications, in source and/or binary forms;
 provided that if you redistribute the Apple Software in its entirety and
 without modifications, you must retain this notice and the following
 text and disclaimers in all such redistributions of the Apple Software.
 Neither the name, trademarks, service marks or logos of Apple Inc. may
 be used to endorse or promote products derived from the Apple Software
 without specific prior written permission from Apple.  Except as
 expressly stated in this notice, no other rights or licenses, express or
 implied, are granted by Apple herein, including but not limited to any
 patent rights that may be infringed by your derivative works or by other
 works in which the Apple Software may be incorporated.
 
 The Apple Software is provided by Apple on an "AS IS" basis.  APPLE
 MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
 THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
 FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
 OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
 
 IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
 OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
 MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
 AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
 STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
 POSSIBILITY OF SUCH DAMAGE.
 
 Copyright (C) 2014 Apple Inc. All Rights Reserved.
 
*/
#include "CAAUProcessor.h"						
#include "CAXException.h"

static OSStatus SilenceInputCallback (void 		*inRefCon, 
					AudioUnitRenderActionFlags *ioActionFlags, 
					const AudioTimeStamp 		*inTimeStamp, 
					UInt32 						inBusNumber, 
					UInt32 						inNumberFrames, 
					AudioBufferList 			*ioData)
{
	AudioBuffer *buf = ioData->mBuffers;
	for (UInt32 i = ioData->mNumberBuffers; i--; ++buf)
		memset((Byte *)buf->mData, 0, buf->mDataByteSize);
		
		//provide a hint that our input data is silent.
	*ioActionFlags &= kAudioUnitRenderAction_OutputIsSilence;
	return noErr;
}

static AURenderCallbackStruct sSilentCallback = { SilenceInputCallback, NULL };


CAAUProcessor::CAAUProcessor (const CAComponent& inComp)
	: mPreflightABL(NULL)
{
	OSStatus result = CAAudioUnit::Open (inComp, mUnit);
	if (result)
		throw result;
	memset (&mUserCallback, 0, sizeof (AURenderCallbackStruct));
	mMaxTailTime = 10.;
}

CAAUProcessor::~CAAUProcessor ()
{
	if (mPreflightABL)
		delete mPreflightABL;
}

inline OSStatus		SetInputCallback (CAAudioUnit &inUnit, AURenderCallbackStruct &inInputCallback)
{
	return inUnit.SetProperty (kAudioUnitProperty_SetRenderCallback, 
											kAudioUnitScope_Input, 
											0,
											&inInputCallback, 
											sizeof(inInputCallback));
}

static AURenderCallbackStruct sRenderCallback;
static OSStatus PrerollRenderProc (	void 						* /*inRefCon*/, 
								AudioUnitRenderActionFlags		* /*inActionFlags*/,
								const AudioTimeStamp 			* /*inTimeStamp*/, 
								UInt32 							/*inBusNumber*/,
								UInt32							/*inNumFrames*/, 
								AudioBufferList 				*ioData)
{
	AudioBuffer *buf = ioData->mBuffers;
	for (UInt32 i = ioData->mNumberBuffers; i--; ++buf)
		memset((Byte *)buf->mData, 0, buf->mDataByteSize);

	return noErr;
}

OSStatus 	Preroll (CAAudioUnit & inAU, UInt32 inFrameSize)
{
	CAStreamBasicDescription desc;
	OSStatus result = inAU.GetFormat (kAudioUnitScope_Input, 0, desc);
	bool hasInput = false;
			//we have input	
	if (result == noErr) 
	{
		sRenderCallback.inputProc = PrerollRenderProc;
		sRenderCallback.inputProcRefCon = 0;
		
		result = inAU.SetProperty (kAudioUnitProperty_SetRenderCallback, kAudioUnitScope_Input, 
								0, &sRenderCallback, sizeof(sRenderCallback));
		if (result) return result;
		hasInput = true;
	}
	
	AudioUnitRenderActionFlags flags = 0;
	AudioTimeStamp time;
	memset (&time, 0, sizeof(time));
	time.mFlags = kAudioTimeStampSampleTimeValid;

	CAStreamBasicDescription outputFormat;
	ca_require_noerr (result = inAU.GetFormat (kAudioUnitScope_Output, 0, outputFormat), home);
	{
		AUOutputBL list (outputFormat, inFrameSize);
		list.Prepare ();
		
		result = inAU.Render (&flags, &time, 0, inFrameSize, list.ABL());
		if (result) { printf("A result %d\n", (int)result); goto home; }
	}

home:
	if (hasInput) {
            // remove our installed callback
		sRenderCallback.inputProc = 0;
		sRenderCallback.inputProcRefCon = 0;
		
		inAU.SetProperty (kAudioUnitProperty_SetRenderCallback, kAudioUnitScope_Input, 
								0, &sRenderCallback, sizeof(sRenderCallback));
	}
	return result;
}


OSStatus		CAAUProcessor::EstablishInputCallback (AURenderCallbackStruct &inInputCallback)
{
	OSStatus result = SetInputCallback (mUnit, inInputCallback);
	if (!result)
		memcpy (&mUserCallback, &inInputCallback, sizeof(AURenderCallbackStruct));
	else
		memset (&mUserCallback, 0, sizeof (AURenderCallbackStruct));
	return result;
}

OSStatus		CAAUProcessor::SetAUPreset (CFPropertyListRef 			inPreset)
{
	return mUnit.SetProperty (kAudioUnitProperty_ClassInfo, 
									kAudioUnitScope_Global, 
									0, 
									&inPreset, 
									sizeof(inPreset));
}

OSStatus		CAAUProcessor::SetAUPresetIndex (SInt32				inPresetIndex)
{
	AUPreset aup;
	aup.presetName = NULL;
	aup.presetNumber = inPresetIndex;
	return mUnit.SetPresentPreset(aup);
}


OSStatus		CAAUProcessor::SetParameter (AudioUnitParameterID inID, AudioUnitScope scope, AudioUnitElement element,
											Float32 value, UInt32 bufferOffsetFrames)
{
	return mUnit.SetParameter(inID, scope, element, value, bufferOffsetFrames);
}


UInt32			CAAUProcessor::MaxFramesPerRender () const
{
	UInt32 maxFrames;
	UInt32 propSize = sizeof (maxFrames);
	if (mUnit.GetProperty (kAudioUnitProperty_MaximumFramesPerSlice,
							kAudioUnitScope_Global, 0, &maxFrames, &propSize))
	{
		return 0;
	}
	return maxFrames;
}

OSStatus		CAAUProcessor::SetMaxFramesPerRender (UInt32 inMaxFrames)
{
	return mUnit.SetProperty (kAudioUnitProperty_MaximumFramesPerSlice,
							kAudioUnitScope_Global, 0, &inMaxFrames, sizeof(inMaxFrames));
}

OSStatus		CAAUProcessor::Initialize (const CAStreamBasicDescription 	&inInputDesc,
											const CAStreamBasicDescription 	&inOutputDesc,
												UInt64 						inNumInputSamples)
{
	return DoInitialisation (inInputDesc, inOutputDesc, inNumInputSamples, MaxFramesPerRender());
}

OSStatus		CAAUProcessor::Reinitialize (UInt32 inNewMaxFrames)
{
	OSStatus result;
	CAStreamBasicDescription inputDesc, outputDesc;
	
	ca_require_noerr (result = mUnit.GetFormat (kAudioUnitScope_Input, 0, inputDesc), home);
	ca_require_noerr (result = mUnit.GetFormat (kAudioUnitScope_Output, 0, outputDesc), home);
	
	ca_require_noerr (result = DoInitialisation (inputDesc, outputDesc, mNumInputSamples, inNewMaxFrames), home);
	
home:
	return result;
}


OSStatus		CAAUProcessor::DoInitialisation (const CAStreamBasicDescription 	&inInputFormat,
												const CAStreamBasicDescription 		&inOutputFormat,
												UInt64								inNumInputSamples,
												UInt32 								inMaxFrames)
{
	OSStatus result;
	
	if (inNumInputSamples == 0 && IsOfflineAU())
		return kAudioUnitErr_InvalidOfflineRender;
		
	mNumInputSamples = inNumInputSamples;
	
		// first check that we can do this number of channels
	if (mUnit.CanDo (inInputFormat.NumberChannels(), inOutputFormat.NumberChannels()) == false)
		ca_require_noerr (result = kAudioUnitErr_FailedInitialization, home);
	
	// just uninitialise the AU as a matter of course
	ca_require_noerr (result = mUnit.Uninitialize(), home);

	ca_require_noerr (result = mUnit.SetFormat (kAudioUnitScope_Input, 0, inInputFormat), home); 
	ca_require_noerr (result = mUnit.SetFormat (kAudioUnitScope_Output, 0, inOutputFormat), home); 
	ca_require_noerr (result = SetMaxFramesPerRender (inMaxFrames), home);
	
#if !TARGET_OS_IPHONE
		// if we're any AU but an offline AU, we should tell it that we've processing offline
	if (!IsOfflineAU()) {
		UInt32 isOffline = (IsOfflineContext() ? 1 : 0);
			// don't care whether this succeeds of fails as many AU's don't care about this
			// but the ones that do its important that they are told their render context
		mUnit.SetProperty (kAudioUnitProperty_OfflineRender, kAudioUnitScope_Global, 0, &isOffline, sizeof(isOffline));
	} else {
			// tell the offline unit how many input samples we wish to process...
		mUnit.SetProperty (kAudioUnitOfflineProperty_InputSize,
												kAudioUnitScope_Global, 0,
												&mNumInputSamples, sizeof(mNumInputSamples));
	}
#endif
	
	ca_require_noerr (result = mUnit.Initialize(), home);

	ca_require_noerr (result = SetInputCallback (mUnit, mUserCallback), home);
	
	// finally reset our time stamp
	// the time stamp we use with the AU Render - only sample count is valid
	memset (&mRenderTimeStamp, 0, sizeof(mRenderTimeStamp));
	mRenderTimeStamp.mFlags = kAudioTimeStampSampleTimeValid;

	// now, if we're NOT an offline AU, preflighting is not required
	// if we are an offline AU, we should preflight.. an offline AU will tell us when its preflighting is done
	mPreflightDone = false;

	if (mPreflightABL) {
		delete mPreflightABL;
		mPreflightABL = NULL;
	}
	
	mPreflightABL = new AUOutputBL (inOutputFormat);

	mLastPercentReported = 0;
	
home:
	return result;
}

void		CAAUProcessor::CalculateRemainderSamples (Float64 inSampleRate)
{
	mLatencySamples = 0;
	mTailSamplesToProcess = 0;
	mTailSamples = 0;
	mTailSamplesRemaining = 0;
	return;
	
		// nothing to do because we're not processing offline
	if (IsOfflineContext() == false) return;
		
		// because an offline unit has some indeterminancy about what it does with the input samples
		// it is *required* to deal internally with both latency and tail
	if (!IsOfflineAU()) 
	{
			// when offline we need to deal with both latency and tail
			
		// if the AU has latency - how many samples at the start will be zero?
		// we'll end up chucking these away.
		Float64 renderTimeProps;
		UInt32 propSize = sizeof (renderTimeProps);
		OSStatus result = mUnit.GetProperty (kAudioUnitProperty_Latency, kAudioUnitScope_Global, 0,
													&renderTimeProps, &propSize);
		
		Float64 latencySamples = 0;
		if (result == noErr) // we have latency to deal with - its reported in seconds
			latencySamples = renderTimeProps * inSampleRate;
			
			// AU tail
			// if the AU has a tail - we'll pull that many zeroes through at the end to flush
			// out this tail - think of a decaying digital delay or reverb...
		result = mUnit.GetProperty (kAudioUnitProperty_TailTime, kAudioUnitScope_Global, 0,
													&renderTimeProps, &propSize);
		if (renderTimeProps > mMaxTailTime)
			renderTimeProps = mMaxTailTime;
		Float64 tailSamples = 0;
		if (result == noErr)
			tailSamples = renderTimeProps * inSampleRate;
		
		// this dictates how many samples at the end we need to pull through...
		// we add latency to tail because we throw the latency samples away from the start of the rendering
		// and we have to pull that many samples after the end of course to get the last of the original data
		// then to that is added the tail of the effect...
		mTailSamplesToProcess = UInt32(tailSamples + latencySamples);
		mTailSamples = UInt32(tailSamples);
		mLatencySamples = UInt32(latencySamples);
	}
}

#if !TARGET_OS_IPHONE
CFStringRef		CAAUProcessor::GetOLPreflightName () const
{
	if (OfflineAUNeedsPreflight()) 
	{
		CFStringRef str;
		UInt32 size = sizeof(str);
		OSStatus result = mUnit.GetProperty (kAudioUnitOfflineProperty_PreflightName,
												kAudioUnitScope_Global, 0,
												&str, &size);
		return result ? NULL : str;
	}
	return NULL; // says NO to preflighting
}

bool		CAAUProcessor::OfflineAUNeedsPreflight () const
{
	if (IsOfflineAU()) {
		UInt32 preflightRequirements;
		UInt32 size = sizeof(preflightRequirements);
		OSStatus result = mUnit.GetProperty (kAudioUnitOfflineProperty_PreflightRequirements,
												kAudioUnitScope_Global, 0,
												&preflightRequirements, &size);
		if (result)
			return false;
		return preflightRequirements;
	}
	return false;
}
#endif

OSStatus	CAAUProcessor::Preflight (bool inProcessPreceedingTail)
{
	printf(">>>>CAAUProcessor::Preflight\n");
		//we're preflighting again, so reset ourselves
	if (mPreflightDone) {
		mPreflightDone = false;
		// the time stamp we use with the AU Render - only sample count is valid
		memset (&mRenderTimeStamp, 0, sizeof(mRenderTimeStamp));
		mRenderTimeStamp.mFlags = kAudioTimeStampSampleTimeValid;
		mUnit.GlobalReset();
	}

	Float64 sampleRate;
	OSStatus result = mUnit.GetSampleRate (kAudioUnitScope_Output, 0, sampleRate);
	CalculateRemainderSamples (sampleRate);

	UInt32 numFrames = MaxFramesPerRender();
	if (numFrames == 0)
		return kAudioUnitErr_InvalidProperty;
	
	if (!IsOfflineAU()) 
	{
		if ((IsOfflineContext() == false && inProcessPreceedingTail) || IsOfflineContext())
		{
			// re-establish the user's input callback
			ca_require_noerr (result = SetInputCallback (mUnit, mUserCallback), home);

			// Consume the number of input samples indicated by the AU's latency or tail
			// based on whether the AU is being used in an offline context or not.
			
			UInt32 latSamps = IsOfflineContext() ? mLatencySamples : mTailSamples;	
			printf("latSamps %d\n", (int)latSamps);
			latSamps = 0;
			while (latSamps > 0)
			{
				if (latSamps < numFrames)
					numFrames = latSamps;
					
					// process the samples (the unit's input callback will read the samples
					// from the file and convert them to float for processing
				AudioUnitRenderActionFlags renderFlags = 0;
				mPreflightABL->Prepare();
				result = mUnit.Render (&renderFlags, &mRenderTimeStamp, 0, numFrames, mPreflightABL->ABL());
				if (result) { printf("B result %d\n", (int)result); goto home; }
		
				mRenderTimeStamp.mSampleTime += numFrames;
				latSamps -= numFrames;
			}
			if (IsOfflineContext())
				mRenderTimeStamp.mSampleTime = mLatencySamples;
		}
		else
		{
			// processing real-time but not processing preceeding tail, so we should preroll the AU
			ca_require_noerr (result = Preroll(mUnit, numFrames), home);
			
			// re-establish the user's input callback
			ca_require_noerr (result = SetInputCallback (mUnit, mUserCallback), home);
			
			mRenderTimeStamp.mSampleTime = 0;
		}
	}
#if !TARGET_OS_IPHONE
	else
	{
			// re-establish the user's input callback
		ca_require_noerr (result = SetInputCallback (mUnit, mUserCallback), home);
		
		UInt32 preflightRequirements;
		UInt32 size; size = sizeof(preflightRequirements);
		ca_require_noerr (result = mUnit.GetProperty (kAudioUnitOfflineProperty_PreflightRequirements,
												kAudioUnitScope_Global, 0,
												&preflightRequirements, &size), home);
												
			// 0 indicates none, otherwise optional or required -> we do it for either
		if (preflightRequirements) 
		{
			for (;;) {
				// here we need to do the preflight loop - we don't expect any data back, but have to 
				// give the offline unit all of its input data to allow it to prepare its processing
				AudioUnitRenderActionFlags renderFlags = kAudioOfflineUnitRenderAction_Preflight;
				mPreflightABL->Prepare();
				result = mUnit.Render (&renderFlags, &mRenderTimeStamp, 0, numFrames, mPreflightABL->ABL());
				if (result) { printf("C result %d\n", (int)result); goto home; }
				mRenderTimeStamp.mSampleTime += numFrames;
		
				if (renderFlags & kAudioOfflineUnitRenderAction_Complete)
					break;
			}
		}
		// the time stamp we use with the AU Render - only sample count is valid
		mRenderTimeStamp.mSampleTime = 0;
	}
#endif

	if (result == noErr) {
		mPreflightDone = true;
	}
	
home:
	printf("<<<<CAAUProcessor::Preflight\n");
	return result;
}

#if !TARGET_OS_IPHONE
OSStatus 	CAAUProcessor::OfflineAUPreflight (UInt32 inNumFrames, bool &outIsDone)
{
	if (!IsOfflineAU())
		return -50/*paramErr*/;
	if (mNumInputSamples == 0)
		return -50/*paramErr*/;

	UInt32 preflightRequirements;
	UInt32 size = sizeof(preflightRequirements);
	OSStatus result;
	ca_require_noerr (result = mUnit.GetProperty (kAudioUnitOfflineProperty_PreflightRequirements,
												kAudioUnitScope_Global, 0,
												&preflightRequirements, &size), home);
												
		// 0 indicates none, otherwise optional or required -> we do it for either
	if (preflightRequirements) 
	{
		AudioUnitRenderActionFlags renderFlags = kAudioOfflineUnitRenderAction_Preflight;
		mPreflightABL->Prepare();
		result = mUnit.Render (&renderFlags, &mRenderTimeStamp, 0, inNumFrames, mPreflightABL->ABL());
		if (result) { printf("D result %d\n", (int)result); goto home; }
		mRenderTimeStamp.mSampleTime += inNumFrames;
		
		if (renderFlags & kAudioOfflineUnitRenderAction_Complete) {
			outIsDone = true;
			mRenderTimeStamp.mSampleTime = 0;
			mPreflightDone = true;
			mLastPercentReported = 0;
		}
	}
	else
	{
		outIsDone = true;
		mRenderTimeStamp.mSampleTime = 0;
		mPreflightDone = true;
		mLastPercentReported = 0;
	}
	
home:
	return result;
}
#endif

void SetBufferListToNumFrames (AudioBufferList &list, UInt32 inNumFrames)
{
	for (unsigned int i = 0; i < list.mNumberBuffers; ++i) {
		AudioBuffer &buf = list.mBuffers[i];
		if (buf.mDataByteSize > 0)
			buf.mDataByteSize = inNumFrames * sizeof (Float32);
	}
}

OSStatus	CAAUProcessor::Render (AudioBufferList 		*ioData, 
									UInt32 				&ioNumFrames, 
									bool				&outIsSilence,
									bool 				*outOLCompleted, 
									bool 				*outOLRequiresPostProcess)
{
	if (IsOfflineContext())
	{
		if (!mPreflightDone)
			return kAudioUnitErr_InvalidOfflineRender;
	
			// YES - this is correct!!! you have to provide both if rendering in an offline Context
		*outOLCompleted = false;
		*outOLRequiresPostProcess = false;

		if (!IsOfflineAU() && !mUnit.Comp().Desc().IsFConv()) 
		{
				// have we processed the input we expect too?
				// in an offline case, we want to create output that matches the input
				// for an OfflineAU type, it manages this internally, so we don't have to do anything
				// for a FormatConverter AU, we don't know and can't tell, so we can't do anything here
				// for any other AU type (effect, instrument) the Prime assumption is that it will 
				// ask for the same number of frames of input as it is asked to output
				// so we can ask what it is doing, and get a sample accurate output (which is input + tail time)
			if (mRenderTimeStamp.mSampleTime + ioNumFrames >= InputSampleCount()) 
			{
					// if we fall into here, we have just a partial number of input samples left 
					// (less input less than what we've been asked to produce output for.
				*outOLCompleted = true;
					// we require post processing if we've got some tail (or latency) samples to flush through
				*outOLRequiresPostProcess = mTailSamplesToProcess > 0;
				if (InputSampleCount() > mRenderTimeStamp.mSampleTime) {
					ioNumFrames = (UInt32)(InputSampleCount() - mRenderTimeStamp.mSampleTime);
				} else {
					ioNumFrames = 0;
				}
				mTailSamplesRemaining = mTailSamplesToProcess;
					// we've got no input samples to process this time.
				SetBufferListToNumFrames (*ioData, ioNumFrames);
				if (ioNumFrames == 0) {
					if (*outOLRequiresPostProcess)
						SetInputCallback (mUnit, sSilentCallback);
					else
						mUnit.GlobalReset (); //flush this out, as we're done with this phase
					return noErr;
				}
			}
		}
		AudioUnitRenderActionFlags renderFlags = IsOfflineAU() ? kAudioOfflineUnitRenderAction_Render : 0;
		OSStatus result = mUnit.Render (&renderFlags, &mRenderTimeStamp, 0, ioNumFrames, ioData);
		if (result) { printf("E result %d\n", (int)result); }
		if (result) {
			if (mUnit.Comp().Desc().IsFConv()) { 
				// this is the only way we can tell we're done with a FormatConverter AU 
				// - ie. client returns an error from input
				result = noErr;
				*outOLCompleted = true;
				*outOLRequiresPostProcess = mTailSamplesToProcess > 0;
				ioNumFrames = 0;
				SetBufferListToNumFrames (*ioData, ioNumFrames);
			} else
				return result;
		}
//	for (UInt32 i = 0; i < ioNumFrames; ++i) {
//		union {
//			float f;
//			unsigned char c[4];
//		} u;
//		u.f = ((float*)(ioData->mBuffers[0].mData))[i];
//		printf("aup out %4d  %14.10f  %02X %02X %02X %02X\n", (int)i, u.f, u.c[0], u.c[1], u.c[2], u.c[3]);
//	}
		mRenderTimeStamp.mSampleTime += ioNumFrames;
		outIsSilence = (renderFlags & kAudioUnitRenderAction_OutputIsSilence);
		
			// if we're an Offline AU type, it will set this flag on completion of its processing
		if (renderFlags & kAudioOfflineUnitRenderAction_Complete) {
			// we now need to calculate how many frames we rendered.
				// as we're dealing with PCM non-interleaved buffers, we can calculate the numFrames simply
			ioNumFrames = ioData->mBuffers[0].mDataByteSize / sizeof(Float32);
			*outOLCompleted = true;
			*outOLRequiresPostProcess = false;
			mUnit.GlobalReset (); //flush this out, as we're done with this phase
		} else {
			if (*outOLCompleted) {
				if (*outOLRequiresPostProcess)
					result = SetInputCallback (mUnit, sSilentCallback);
				else
					mUnit.GlobalReset (); //flush this out, as we're done with this phase
			}
		}
	
		return result;
	}

// rendering in a RT context:
	AudioUnitRenderActionFlags renderFlags = 0;
	OSStatus result = mUnit.Render (&renderFlags, &mRenderTimeStamp, 0, ioNumFrames, ioData);
		if (result) { printf("F result %d\n", (int)result); }
	if (!result) {
		mRenderTimeStamp.mSampleTime += ioNumFrames;
		outIsSilence = (renderFlags & kAudioUnitRenderAction_OutputIsSilence);
	}
//	for (UInt32 i = 0; i < ioNumFrames; ++i) {
//		union {
//			float f;
//			unsigned char c[4];
//		} u;
//		u.f = ((float*)(ioData->mBuffers[0].mData))[i];
//		printf("aup out %4d  %14.10f  %02X %02X %02X %02X\n", (int)i, u.f, u.c[0], u.c[1], u.c[2], u.c[3]);
//	}
	
	return result;	
}
	
OSStatus	CAAUProcessor::PostProcess (AudioBufferList 	*ioData, 
										UInt32 				&ioNumFrames, 
										bool				&outIsSilence,
										bool 				&outDone)
{
	if (IsOfflineAU() || !IsOfflineContext()) 
		return kAudioUnitErr_CannotDoInCurrentContext;
	
	outDone = false;
	
		// we've got less samples to process than we've been asked to process
	if (mTailSamplesRemaining <= SInt32(ioNumFrames)) {
		outDone = true;
		ioNumFrames = mTailSamplesRemaining > 0 ? mTailSamplesRemaining : 0;
		SetBufferListToNumFrames (*ioData, ioNumFrames);
		if (ioNumFrames == 0)
			return noErr;
	}
	
	AudioUnitRenderActionFlags renderFlags = 0;
	OSStatus result;
	result = mUnit.Render (&renderFlags, &mRenderTimeStamp, 0, ioNumFrames, ioData);
		if (result) { printf("G result %d\n", (int)result); goto home; }
	mRenderTimeStamp.mSampleTime += ioNumFrames;
	mTailSamplesRemaining -= ioNumFrames;
	outIsSilence = (renderFlags & kAudioUnitRenderAction_OutputIsSilence);
			
	if (outDone) {
		ca_require_noerr (result = SetInputCallback (mUnit, mUserCallback), home);
		mUnit.GlobalReset (); //flush this out, as we're done with this phase
	}
home:
	return result;
}		

#if !TARGET_OS_IPHONE
Float32		CAAUProcessor::GetOLPercentComplete ()
{
	if (!IsOfflineContext())
		return 0;

	Float32 percentDone = mLastPercentReported;
		
	if (IsOfflineAU()) 
	{
		// we get the output size every time, as this can change as parameters are changed
		UInt64 numOutputSamples = mNumInputSamples;
		UInt32 propSize = sizeof(numOutputSamples);
		mUnit.GetProperty (kAudioUnitOfflineProperty_OutputSize,
							kAudioUnitScope_Global, 0, &numOutputSamples, &propSize);
		
		percentDone = (mRenderTimeStamp.mSampleTime / Float64(numOutputSamples)) * 100.;
	}
	else
	{
		percentDone = (mRenderTimeStamp.mSampleTime / Float64(mNumInputSamples + mTailSamples)) * 100.;
	}
	
	if (percentDone > mLastPercentReported)
		mLastPercentReported = percentDone;

	return mLastPercentReported;
}
#endif