1 /* Copyright: � Copyright 2005 Apple Computer, Inc. All rights reserved.
3 Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple Computer, Inc.
4 ("Apple") in consideration of your agreement to the following terms, and your
5 use, installation, modification or redistribution of this Apple software
6 constitutes acceptance of these terms. If you do not agree with these terms,
7 please do not use, install, modify or redistribute this Apple software.
9 In consideration of your agreement to abide by the following terms, and subject
10 to these terms, Apple grants you a personal, non-exclusive license, under Apple�s
11 copyrights in this original Apple software (the "Apple Software"), to use,
12 reproduce, modify and redistribute the Apple Software, with or without
13 modifications, in source and/or binary forms; provided that if you redistribute
14 the Apple Software in its entirety and without modifications, you must retain
15 this notice and the following text and disclaimers in all such redistributions of
16 the Apple Software. Neither the name, trademarks, service marks or logos of
17 Apple Computer, Inc. may be used to endorse or promote products derived from the
18 Apple Software without specific prior written permission from Apple. Except as
19 expressly stated in this notice, no other rights or licenses, express or implied,
20 are granted by Apple herein, including but not limited to any patent rights that
21 may be infringed by your derivative works or by other works in which the Apple
22 Software may be incorporated.
24 The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO
25 WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
26 WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
27 PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
28 COMBINATION WITH YOUR PRODUCTS.
30 IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
31 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
32 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
34 OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
35 (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
36 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 /*=============================================================================
41 =============================================================================*/
43 #include "CAAUParameter.h"
45 CAAUParameter::CAAUParameter()
47 memset(this, 0, sizeof(CAAUParameter));
50 CAAUParameter::CAAUParameter(AudioUnit au, AudioUnitParameterID param, AudioUnitScope scope, AudioUnitElement element)
52 memset(this, 0, sizeof(CAAUParameter));
53 Init (au, param, scope, element);
56 CAAUParameter::CAAUParameter (AudioUnitParameter &inParam)
58 memset(this, 0, sizeof(CAAUParameter));
59 Init (inParam.mAudioUnit, inParam.mParameterID, inParam.mScope, inParam.mElement);
62 CAAUParameter::CAAUParameter(const CAAUParameter &a)
64 memset(this, 0, sizeof(CAAUParameter));
68 CAAUParameter & CAAUParameter::operator = (const CAAUParameter &a)
70 if (mParamName) CFRelease(mParamName);
71 if (mParamTag) CFRelease(mParamTag);
72 if (mNamedParams) CFRelease(mNamedParams);
74 memcpy(this, &a, sizeof(CAAUParameter));
76 if (mParamName) CFRetain(mParamName);
77 if (mParamTag) CFRetain(mParamTag);
78 if (mNamedParams) CFRetain(mNamedParams);
83 CAAUParameter::~CAAUParameter()
85 if (mParamName) CFRelease(mParamName);
86 if (mParamTag) CFRelease(mParamTag);
87 if (mNamedParams) CFRelease (mNamedParams);
90 void CAAUParameter::Init (AudioUnit au, AudioUnitParameterID param, AudioUnitScope scope, AudioUnitElement element)
97 UInt32 propertySize = sizeof(mParamInfo);
98 OSStatus err = AudioUnitGetProperty(au, kAudioUnitProperty_ParameterInfo,
99 scope, param, &mParamInfo, &propertySize);
101 memset(&mParamInfo, 0, sizeof(mParamInfo));
102 if (mParamInfo.flags & kAudioUnitParameterFlag_HasCFNameString) {
103 mParamName = mParamInfo.cfNameString;
104 if (!(mParamInfo.flags & kAudioUnitParameterFlag_CFNameRelease))
105 CFRetain (mParamName);
107 mParamName = CFStringCreateWithCString(NULL, mParamInfo.name, kCFStringEncodingUTF8);
110 switch (mParamInfo.unit)
112 case kAudioUnitParameterUnit_Boolean:
115 case kAudioUnitParameterUnit_Percent:
116 case kAudioUnitParameterUnit_EqualPowerCrossfade:
119 case kAudioUnitParameterUnit_Seconds:
122 case kAudioUnitParameterUnit_SampleFrames:
125 case kAudioUnitParameterUnit_Phase:
126 case kAudioUnitParameterUnit_Degrees:
129 case kAudioUnitParameterUnit_Hertz:
132 case kAudioUnitParameterUnit_Cents:
133 case kAudioUnitParameterUnit_AbsoluteCents:
136 case kAudioUnitParameterUnit_RelativeSemiTones:
139 case kAudioUnitParameterUnit_MIDINoteNumber:
140 case kAudioUnitParameterUnit_MIDIController:
142 //these are inclusive, so add one value here
143 mNumIndexedParams = short(mParamInfo.maxValue+1 - mParamInfo.minValue);
145 case kAudioUnitParameterUnit_Decibels:
148 case kAudioUnitParameterUnit_MixerFaderCurve1:
149 case kAudioUnitParameterUnit_LinearGain:
152 case kAudioUnitParameterUnit_Pan:
155 case kAudioUnitParameterUnit_Meters:
158 case kAudioUnitParameterUnit_Octaves:
161 case kAudioUnitParameterUnit_BPM:
164 case kAudioUnitParameterUnit_Beats:
167 case kAudioUnitParameterUnit_Milliseconds:
170 case kAudioUnitParameterUnit_Ratio:
173 case kAudioUnitParameterUnit_Indexed:
175 propertySize = sizeof(mNamedParams);
176 err = AudioUnitGetProperty (au,
177 kAudioUnitProperty_ParameterValueStrings,
182 if (!err && mNamedParams) {
183 mNumIndexedParams = CFArrayGetCount(mNamedParams);
185 //these are inclusive, so add one value here
186 mNumIndexedParams = short(mParamInfo.maxValue+1 - mParamInfo.minValue);
191 case kAudioUnitParameterUnit_CustomUnit:
193 CFStringRef unitName = mParamInfo.unitName;
194 static char paramStr[256];
195 CFStringGetCString (unitName, paramStr, 256, kCFStringEncodingUTF8);
196 if (mParamInfo.flags & kAudioUnitParameterFlag_CFNameRelease)
197 CFRelease (unitName);
201 case kAudioUnitParameterUnit_Generic:
202 case kAudioUnitParameterUnit_Rate:
209 mParamTag = CFStringCreateWithCString(NULL, str, kCFStringEncodingUTF8);
215 Float32 CAAUParameter::GetValue() const
219 AudioUnitGetParameter(mAudioUnit, mParameterID, mScope, mElement, &value);
223 CFStringRef CAAUParameter::GetStringFromValueCopy(const Float32 *value) const
225 if (HasNamedParams())
227 Float32 val = (value == NULL ? GetValue() : *value);
228 int index = int(mParamInfo.minValue) + int(val);
229 CFStringRef str = GetParamName (index);
235 else if (ValuesHaveStrings())
237 AudioUnitParameterStringFromValue stringValue;
238 stringValue.inParamID = mParameterID;
239 stringValue.inValue = value;
240 stringValue.outString = NULL;
241 UInt32 propertySize = sizeof(stringValue);
243 OSStatus err = AudioUnitGetProperty (mAudioUnit,
244 kAudioUnitProperty_ParameterStringFromValue,
250 if (err == noErr && stringValue.outString != NULL)
251 return stringValue.outString;
254 Float32 val = (value == NULL ? GetValue() : *value);
256 AUParameterFormatValue (val, this, valstr, 4);
257 return CFStringCreateWithCString(NULL, valstr, kCFStringEncodingUTF8);
260 Float32 CAAUParameter::GetValueFromString(CFStringRef str) const
262 if (ValuesHaveStrings())
264 AudioUnitParameterValueFromString valueString;
265 valueString.inParamID = mParameterID;
266 valueString.inString = str;
267 UInt32 propertySize = sizeof(valueString);
269 OSStatus err = AudioUnitGetProperty (mAudioUnit,
270 kAudioUnitProperty_ParameterValueFromString,
277 return valueString.outValue;
281 Float32 paramValue = mParamInfo.defaultValue;
283 CFStringGetCString(str, valstr, sizeof(valstr), kCFStringEncodingUTF8);
284 sscanf(valstr, "%f", ¶mValue);
288 void CAAUParameter::SetValue( AUParameterListenerRef inListener,
290 Float32 inValue) const
292 // clip inValue as: maxValue >= inValue >= minValue before setting
293 Float32 valueToSet = inValue;
294 if (valueToSet > mParamInfo.maxValue)
295 valueToSet = mParamInfo.maxValue;
296 if (valueToSet < mParamInfo.minValue)
297 valueToSet = mParamInfo.minValue;
299 AUParameterSet(inListener, inObject, this, valueToSet, 0);
303 void CAAUParameter::Print() const
308 UInt32 len = CFStringGetLength(mParamName);
309 char* chars = (char*)malloc (len * 2); // give us plenty of room for unichar chars
310 if (!CFStringGetCString (mParamName, chars, len * 2, kCFStringEncodingUTF8))
313 printf ("ID: %ld, Clump: %ld, Name: %s\n", mParameterID, clump, chars);