summaryrefslogtreecommitdiff
path: root/libs/appleutility/CoreAudio/AudioUnits/AUPublic/AUBase/AUScopeElement.h
blob: ae493f217b59c06cc38ed1c5f1df8963e0359f3b (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
/*
     File: AUScopeElement.h
 Abstract: Part of CoreAudio Utility Classes
  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.

*/
#ifndef __AUScopeElement_h__
#define __AUScopeElement_h__

#include <map>
#include <vector>

#if !defined(__COREAUDIO_USE_FLAT_INCLUDES__)
	#include <AudioUnit/AudioUnit.h>
#else
	#include <AudioUnit.h>
#endif
#include "ComponentBase.h"
#include "AUBuffer.h"


class AUBase;

// ____________________________________________________________________________
//
// represents a parameter's value (either constant or ramped)
/*! @class ParameterMapEvent */
class ParameterMapEvent
{
public:
/*! @ctor ParameterMapEvent */
	ParameterMapEvent()
		: mEventType(kParameterEvent_Immediate), mBufferOffset(0), mDurationInFrames(0), mValue1(0.0f), mValue2(0.0f), mSliceDurationFrames(0)
		{}

/*! @ctor ParameterMapEvent */
	ParameterMapEvent(AudioUnitParameterValue inValue)
		: mEventType(kParameterEvent_Immediate), mBufferOffset(0), mDurationInFrames(0), mValue1(inValue), mValue2(inValue), mSliceDurationFrames(0)
		{}

	// constructor for scheduled event
/*! @ctor ParameterMapEvent */
	ParameterMapEvent(	const AudioUnitParameterEvent 	&inEvent,
						UInt32 							inSliceOffsetInBuffer,
						UInt32							inSliceDurationFrames )
	{
		SetScheduledEvent(inEvent, inSliceOffsetInBuffer, inSliceDurationFrames );
	};

/*! @method SetScheduledEvent */
	void SetScheduledEvent(	const AudioUnitParameterEvent 	&inEvent,
							UInt32 							inSliceOffsetInBuffer,
							UInt32							inSliceDurationFrames )
	{
		mEventType = inEvent.eventType;
		mSliceDurationFrames = inSliceDurationFrames;

		if(mEventType == kParameterEvent_Immediate )
		{
			// constant immediate value for the whole slice
			mValue1 = inEvent.eventValues.immediate.value;
			mValue2 = mValue1;
			mDurationInFrames = inSliceDurationFrames;
			mBufferOffset = 0;
		}
		else
		{
			mDurationInFrames 	= 	inEvent.eventValues.ramp.durationInFrames;
			mBufferOffset 		= 	inEvent.eventValues.ramp.startBufferOffset - inSliceOffsetInBuffer;	// shift over for this slice
			mValue1 			= 	inEvent.eventValues.ramp.startValue;
			mValue2 			= 	inEvent.eventValues.ramp.endValue;
		}
	};



/*! @method GetEventType */
	AUParameterEventType		GetEventType() const {return mEventType;};

/*! @method GetValue */
	AudioUnitParameterValue		GetValue() const {return mValue1;};	// only valid if immediate event type
/*! @method GetEndValue */
	AudioUnitParameterValue		GetEndValue() const {return mValue2;};	// only valid if immediate event type
/*! @method SetValue */
	void						SetValue(AudioUnitParameterValue inValue)
								{
									mEventType = kParameterEvent_Immediate;
									mValue1 = inValue;
									mValue2 = inValue;
								}

	// interpolates the start and end values corresponding to the current processing slice
	// most ramp parameter implementations will want to use this method
	// the start value will correspond to the start of the slice
	// the end value will correspond to the end of the slice
/*! @method GetRampSliceStartEnd */
	void					GetRampSliceStartEnd(	AudioUnitParameterValue &	outStartValue,
													AudioUnitParameterValue &	outEndValue,
													AudioUnitParameterValue &	outValuePerFrameDelta )
	{
		if (mEventType == kParameterEvent_Ramped) {
			outValuePerFrameDelta = (mValue2 - mValue1) / mDurationInFrames;

			outStartValue = mValue1 + outValuePerFrameDelta * (-mBufferOffset);	// corresponds to frame 0 of this slice
			outEndValue = outStartValue +  outValuePerFrameDelta * mSliceDurationFrames;
		} else {
			outValuePerFrameDelta = 0;
			outStartValue = outEndValue = mValue1;
		}
	};

	// Some ramp parameter implementations will want to interpret the ramp using their
	// own interpolation method (perhaps non-linear)
	// This method gives the raw ramp information, relative to this processing slice
	// for the client to interpret as desired
/*! @method GetRampInfo */
	void					GetRampInfo(	SInt32 	&					outBufferOffset,
											UInt32 	&					outDurationInFrames,
											AudioUnitParameterValue &	outStartValue,
											AudioUnitParameterValue &	outEndValue )
	{
		outBufferOffset = mBufferOffset;
		outDurationInFrames = mDurationInFrames;
		outStartValue = mValue1;
		outEndValue = mValue2;
	};

#if DEBUG
	void					Print()
	{
		printf("ParameterEvent @ %p\n", this);
		printf("	mEventType = %d\n", (int)mEventType);
		printf("	mBufferOffset = %d\n", (int)mBufferOffset);
		printf("	mDurationInFrames = %d\n", (int)mDurationInFrames);
		printf("	mSliceDurationFrames = %d\n", (int)mSliceDurationFrames);
		printf("	mValue1 = %.5f\n", mValue1);
		printf("	mValue2 = %.5f\n", mValue2);
	}
#endif

private:
	AUParameterEventType		mEventType;

	SInt32						mBufferOffset;		// ramp start offset relative to start of this slice (may be negative)
	UInt32						mDurationInFrames;	// total duration of ramp parameter
	AudioUnitParameterValue     mValue1;				// value if immediate : startValue if ramp
	AudioUnitParameterValue		mValue2;				// endValue (only used for ramp)

	UInt32					mSliceDurationFrames;	// duration of this processing slice
};



// ____________________________________________________________________________
//
class AUIOElement;

/*! @class AUElement */
class AUElement {
public:
/*! @ctor AUElement */
								AUElement(AUBase *audioUnit) : mAudioUnit(audioUnit),
									mUseIndexedParameters(false), mElementName(0) { }

/*! @dtor ~AUElement */
	virtual						~AUElement() { if (mElementName) CFRelease (mElementName); }

/*! @method GetNumberOfParameters */
	virtual UInt32				GetNumberOfParameters()
	{
		if(mUseIndexedParameters) return static_cast<UInt32>(mIndexedParameters.size()); else return static_cast<UInt32>(mParameters.size());
	}
/*! @method GetParameterList */
	virtual void				GetParameterList(AudioUnitParameterID *outList);
/*! @method HasParameterID */
	bool						HasParameterID (AudioUnitParameterID paramID) const;

/*! @method GetParameter */
	AudioUnitParameterValue		GetParameter(AudioUnitParameterID paramID);
/*! @method SetParameter */
	void						SetParameter(AudioUnitParameterID paramID, AudioUnitParameterValue value, bool okWhenInitialized = false);
	// Only set okWhenInitialized to true when you know the outside world cannot access this element. Otherwise the parameter map could get corrupted.

	// interpolates the start and end values corresponding to the current processing slice
	// most ramp parameter implementations will want to use this method
/*! @method GetRampSliceStartEnd */
	void						GetRampSliceStartEnd(	AudioUnitParameterID	paramID,
													AudioUnitParameterValue &	outStartValue,
													AudioUnitParameterValue &	outEndValue,
													AudioUnitParameterValue &	outValuePerFrameDelta );

/*! @method GetEndValue */
	AudioUnitParameterValue		GetEndValue(	AudioUnitParameterID		paramID);

/*! @method SetRampParameter */
	void						SetScheduledEvent(	AudioUnitParameterID 			paramID,
													const AudioUnitParameterEvent 	&inEvent,
													UInt32 							inSliceOffsetInBuffer,
													UInt32							inSliceDurationFrames,
													bool							okWhenInitialized = false );
	// Only set okWhenInitialized to true when you know the outside world cannot access this element. Otherwise the parameter map could get corrupted.


/*! @method GetAudioUnit */
	AUBase *					GetAudioUnit() const { return mAudioUnit; };

/*! @method SaveState */
	void						SaveState(CFMutableDataRef data);
/*! @method RestoreState */
	const UInt8 *				RestoreState(const UInt8 *state);
/*! @method GetName */
	CFStringRef					GetName () const { return mElementName; }
/*! @method SetName */
	void						SetName (CFStringRef inName);
/*! @method HasName */
	bool						HasName () const { return mElementName != 0; }
/*! @method UseIndexedParameters */
	virtual void				UseIndexedParameters(int inNumberOfParameters);

/*! @method AsIOElement*/
	virtual AUIOElement*		AsIOElement () { return NULL; }

protected:
	inline ParameterMapEvent&	GetParamEvent(AudioUnitParameterID paramID);

private:
	typedef std::map<AudioUnitParameterID, ParameterMapEvent, std::less<AudioUnitParameterID> > ParameterMap;

/*! @var mAudioUnit */
	AUBase *						mAudioUnit;
/*! @var mParameters */
	ParameterMap					mParameters;

/*! @var mUseIndexedParameters */
	bool							mUseIndexedParameters;
/*! @var mIndexedParameters */
	std::vector<ParameterMapEvent>	mIndexedParameters;

/*! @var mElementName */
	CFStringRef						mElementName;
};



// ____________________________________________________________________________
//
/*! @class AUIOElement */
class AUIOElement : public AUElement {
public:
/*! @ctor AUIOElement */
								AUIOElement(AUBase *audioUnit);

/*! @method GetStreamFormat */
	const CAStreamBasicDescription &GetStreamFormat() const { return mStreamFormat; }

/*! @method SetStreamFormat */
	virtual OSStatus			SetStreamFormat(const CAStreamBasicDescription &desc);

/*! @method AllocateBuffer */
	virtual void				AllocateBuffer(UInt32 inFramesToAllocate = 0);
/*! @method DeallocateBuffer */
	void						DeallocateBuffer();
/*! @method NeedsBufferSpace */
	virtual bool				NeedsBufferSpace() const = 0;

/*! @method SetWillAllocateBuffer */
	void						SetWillAllocateBuffer(bool inFlag) {
									mWillAllocate = inFlag;
								}
/*! @method WillAllocateBuffer */
	bool						WillAllocateBuffer() const {
									return mWillAllocate;
								}

/*! @method UseExternalBuffer */
	void						UseExternalBuffer(const AudioUnitExternalBuffer &buf) {
									mIOBuffer.UseExternalBuffer(mStreamFormat, buf);
								}
/*! @method PrepareBuffer */
	AudioBufferList &			PrepareBuffer(UInt32 nFrames) {
									if (mWillAllocate)
										return mIOBuffer.PrepareBuffer(mStreamFormat, nFrames);
									throw OSStatus(kAudioUnitErr_InvalidPropertyValue);
								}
/*! @method PrepareNullBuffer */
	AudioBufferList &			PrepareNullBuffer(UInt32 nFrames) {
									return mIOBuffer.PrepareNullBuffer(mStreamFormat, nFrames);
								}
/*! @method SetBufferList */
	AudioBufferList &			SetBufferList(AudioBufferList &abl) { return mIOBuffer.SetBufferList(abl); }
/*! @method SetBuffer */
	void						SetBuffer(UInt32 index, AudioBuffer &ab) { mIOBuffer.SetBuffer(index, ab); }
/*! @method InvalidateBufferList */
	void						InvalidateBufferList() { mIOBuffer.InvalidateBufferList(); }

/*! @method GetBufferList */
	AudioBufferList &			GetBufferList() const { return mIOBuffer.GetBufferList(); }

/*! @method GetChannelData */
	AudioUnitSampleType *		GetChannelData(int ch) const {
									if (mStreamFormat.IsInterleaved())
										return static_cast<AudioUnitSampleType *>(mIOBuffer.GetBufferList().mBuffers[0].mData) + ch;
									else
										return static_cast<AudioUnitSampleType *>(mIOBuffer.GetBufferList().mBuffers[ch].mData);
								}
	Float32 *					GetFloat32ChannelData(int ch) const {
									if (mStreamFormat.IsInterleaved())
										return static_cast<Float32 *>(mIOBuffer.GetBufferList().mBuffers[0].mData) + ch;
									else
										return static_cast<Float32 *>(mIOBuffer.GetBufferList().mBuffers[ch].mData);
								}
	SInt32 *					GetSInt32ChannelData(int ch) const {
									if (mStreamFormat.IsInterleaved())
										return static_cast<SInt32 *>(mIOBuffer.GetBufferList().mBuffers[0].mData) + ch;
									else
										return static_cast<SInt32 *>(mIOBuffer.GetBufferList().mBuffers[ch].mData);
								}
	SInt16 *					GetInt16ChannelData(int ch) const {
									if (mStreamFormat.IsInterleaved())
										return static_cast<SInt16 *>(mIOBuffer.GetBufferList().mBuffers[0].mData) + ch;
									else
										return static_cast<SInt16 *>(mIOBuffer.GetBufferList().mBuffers[ch].mData);
								}

/*! @method CopyBufferListTo */
	void						CopyBufferListTo(AudioBufferList &abl) const {
									mIOBuffer.CopyBufferListTo(abl);
								}
/*! @method CopyBufferContentsTo */
	void						CopyBufferContentsTo(AudioBufferList &abl) const {
									mIOBuffer.CopyBufferContentsTo(abl);
								}

/*	UInt32						BytesToFrames(UInt32 nBytes) { return nBytes / mStreamFormat.mBytesPerFrame; }
	UInt32						BytesToFrames(AudioBufferList &abl) {
									return BytesToFrames(abl.mBuffers[0].mDataByteSize);
								}
	UInt32						FramesToBytes(UInt32 nFrames) { return nFrames * mStreamFormat.mBytesPerFrame; }*/

/*! @method IsInterleaved */
	bool						IsInterleaved() const { return mStreamFormat.IsInterleaved(); }
/*! @method NumberChannels */
	UInt32						NumberChannels() const { return mStreamFormat.NumberChannels(); }
/*! @method NumberInterleavedChannels */
	UInt32						NumberInterleavedChannels() const { return mStreamFormat.NumberInterleavedChannels(); }

/*! @method GetChannelMapTags */
	virtual UInt32				GetChannelLayoutTags (AudioChannelLayoutTag	*outLayoutTagsPtr);

/*! @method GetAudioChannelLayout */
	virtual UInt32				GetAudioChannelLayout (AudioChannelLayout	*outMapPtr, Boolean	&outWritable);

/*! @method SetAudioChannelLayout */
	virtual OSStatus			SetAudioChannelLayout (const AudioChannelLayout &inData);

/*! @method RemoveAudioChannelLayout */
	virtual OSStatus			RemoveAudioChannelLayout ();

/*! @method AsIOElement*/
	virtual AUIOElement*		AsIOElement () { return this; }

protected:
/*! @var mStreamFormat */
	CAStreamBasicDescription	mStreamFormat;
/*! @var mIOBuffer */
	AUBufferList				mIOBuffer;	// for input: input proc buffer, only allocated when needed
											// for output: output cache, usually allocated early on
/*! @var mWillAllocate */
	bool						mWillAllocate;
};

// ____________________________________________________________________________
//
// AUScopeDelegates are a way to get virtual scopes.
/*! @class AUScopeDelegate */
class AUScopeDelegate {
public:
/*! @ctor AUScopeDelegate */
					AUScopeDelegate() : mCreator(NULL), mScope(0) { }
/*! @dtor ~AUScopeDelegate */
					virtual ~AUScopeDelegate() {}

/*! @method Initialize */
	void					Initialize(	AUBase *creator,
										AudioUnitScope scope,
										UInt32 numElements)
	{
		mCreator = creator;
		mScope = scope;
		SetNumberOfElements(numElements);
	}

/*! @method SetNumberOfElements */
	virtual void			SetNumberOfElements(UInt32 numElements) = 0;

/*! @method GetNumberOfElements */
	virtual UInt32			GetNumberOfElements()	 = 0;

/*! @method GetElement */
	virtual AUElement *		GetElement(UInt32 elementIndex) = 0;

	AUBase *			GetCreator() const { return mCreator; }
	AudioUnitScope		GetScope() const { return mScope; }


private:
/*! @var mCreator */
	AUBase *					mCreator;
/*! @var mScope */
	AudioUnitScope				mScope;
};



// ____________________________________________________________________________
//
/*! @class AUScope */
class AUScope {
public:
/*! @ctor AUScope */
					AUScope() : mCreator(NULL), mScope(0), mDelegate(0) { }
/*! @dtor ~AUScope */
					~AUScope();

/*! @method Initialize */
	void			Initialize(AUBase *creator,
								AudioUnitScope scope,
								UInt32 numElements)
	{
		mCreator = creator;
		mScope = scope;

		if (mDelegate)
			return mDelegate->Initialize(creator, scope, numElements);

		SetNumberOfElements(numElements);
	}

/*! @method SetNumberOfElements */
	void			SetNumberOfElements(UInt32 numElements);

/*! @method GetNumberOfElements */
	UInt32			GetNumberOfElements()	const
	{
		if (mDelegate)
			return mDelegate->GetNumberOfElements();

		return static_cast<UInt32>(mElements.size());
	}

/*! @method GetElement */
	AUElement *		GetElement(UInt32 elementIndex) const
	{
		if (mDelegate)
			return mDelegate->GetElement(elementIndex);

		ElementVector::const_iterator i = mElements.begin() + elementIndex;
			// catch passing -1 in as the elementIndex - causes a wrap around
		return (i >= mElements.end() || i < mElements.begin()) ? NULL : *i;
	}

/*! @method SafeGetElement */
	AUElement *		SafeGetElement(UInt32 elementIndex)
	{
		AUElement *element = GetElement(elementIndex);
		if (element == NULL)
			COMPONENT_THROW(kAudioUnitErr_InvalidElement);
		return element;
	}

/*! @method GetIOElement */
	AUIOElement *	GetIOElement(UInt32 elementIndex) const
	{
		AUElement *element = GetElement(elementIndex);
		AUIOElement *ioel = element ? element->AsIOElement () : NULL;
		if (!ioel)
			COMPONENT_THROW (kAudioUnitErr_InvalidElement);
		return ioel;
	}

/*! @method HasElementWithName */
	bool			HasElementWithName () const;

/*! @method AddElementNamesToDict */
	void			AddElementNamesToDict (CFMutableDictionaryRef & inNameDict);

	bool			RestoreElementNames (CFDictionaryRef& inNameDict);

	AudioUnitScope		GetScope() const { return mScope; }

	void SetDelegate(AUScopeDelegate* inDelegate) { mDelegate = inDelegate; }

/*! @method SaveState */
    void            SaveState(CFMutableDataRef data);

/*! @method RestoreState */
    const UInt8 *	RestoreState(const UInt8 *state);

private:
	typedef std::vector<AUElement *> ElementVector;
/*! @var mCreator */
	AUBase *					mCreator;
/*! @var mScope */
	AudioUnitScope				mScope;
/*! @var mElements */
	ElementVector				mElements;
/*! @var mDelegate */
	AUScopeDelegate *			mDelegate;
};



#endif // __AUScopeElement_h__