NOOP, remove trailing tabs/whitespace.
[ardour.git] / libs / appleutility / CAAUParameter.cpp
1 /*      Copyright:      � Copyright 2005 Apple Computer, Inc. All rights reserved.
2
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.
8
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.
23
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.
29
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.
37 */
38 /*=============================================================================
39         CAAUParameter.cpp
40
41 =============================================================================*/
42
43 #include "CAAUParameter.h"
44
45 CAAUParameter::CAAUParameter()
46 {
47         memset(this, 0, sizeof(CAAUParameter));
48 }
49
50 CAAUParameter::CAAUParameter(AudioUnit au, AudioUnitParameterID param, AudioUnitScope scope, AudioUnitElement element)
51 {
52         memset(this, 0, sizeof(CAAUParameter));
53         Init (au, param, scope, element);
54 }
55
56 CAAUParameter::CAAUParameter (AudioUnitParameter &inParam)
57 {
58         memset(this, 0, sizeof(CAAUParameter));
59         Init (inParam.mAudioUnit, inParam.mParameterID, inParam.mScope, inParam.mElement);
60 }
61
62 CAAUParameter::CAAUParameter(const CAAUParameter &a)
63 {
64         memset(this, 0, sizeof(CAAUParameter));
65         *this = a;
66 }
67
68 CAAUParameter & CAAUParameter::operator = (const CAAUParameter &a)
69 {
70         if (mParamName) CFRelease(mParamName);
71         if (mParamTag) CFRelease(mParamTag);
72         if (mNamedParams) CFRelease(mNamedParams);
73
74         memcpy(this, &a, sizeof(CAAUParameter));
75
76         if (mParamName) CFRetain(mParamName);
77         if (mParamTag) CFRetain(mParamTag);
78         if (mNamedParams) CFRetain(mNamedParams);
79
80         return *this;
81 }
82
83 CAAUParameter::~CAAUParameter()
84 {
85         if (mParamName) CFRelease(mParamName);
86         if (mParamTag) CFRelease(mParamTag);
87         if (mNamedParams) CFRelease (mNamedParams);
88 }
89
90 void            CAAUParameter::Init (AudioUnit au, AudioUnitParameterID param, AudioUnitScope scope, AudioUnitElement element)
91 {
92         mAudioUnit = au;
93         mParameterID = param;
94         mScope = scope;
95         mElement = element;
96
97         UInt32 propertySize = sizeof(mParamInfo);
98         OSStatus err = AudioUnitGetProperty(au, kAudioUnitProperty_ParameterInfo,
99                         scope, param, &mParamInfo, &propertySize);
100         if (err)
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);
106         } else
107                 mParamName = CFStringCreateWithCString(NULL, mParamInfo.name, kCFStringEncodingUTF8);
108
109         char* str = 0;
110         switch (mParamInfo.unit)
111         {
112                 case kAudioUnitParameterUnit_Boolean:
113                         str = "T/F";
114                         break;
115                 case kAudioUnitParameterUnit_Percent:
116                 case kAudioUnitParameterUnit_EqualPowerCrossfade:
117                         str = "%";
118                         break;
119                 case kAudioUnitParameterUnit_Seconds:
120                         str = "Secs";
121                         break;
122                 case kAudioUnitParameterUnit_SampleFrames:
123                         str = "Samps";
124                         break;
125                 case kAudioUnitParameterUnit_Phase:
126                 case kAudioUnitParameterUnit_Degrees:
127                         str = "Degr.";
128                         break;
129                 case kAudioUnitParameterUnit_Hertz:
130                         str = "Hz";
131                         break;
132                 case kAudioUnitParameterUnit_Cents:
133                 case kAudioUnitParameterUnit_AbsoluteCents:
134                         str = "Cents";
135                         break;
136                 case kAudioUnitParameterUnit_RelativeSemiTones:
137                         str = "S-T";
138                         break;
139                 case kAudioUnitParameterUnit_MIDINoteNumber:
140                 case kAudioUnitParameterUnit_MIDIController:
141                         str = "MIDI";
142                                 //these are inclusive, so add one value here
143                         mNumIndexedParams = short(mParamInfo.maxValue+1 - mParamInfo.minValue);
144                         break;
145                 case kAudioUnitParameterUnit_Decibels:
146                         str = "dB";
147                         break;
148                 case kAudioUnitParameterUnit_MixerFaderCurve1:
149                 case kAudioUnitParameterUnit_LinearGain:
150                         str = "Gain";
151                         break;
152                 case kAudioUnitParameterUnit_Pan:
153                         str = "L/R";
154                         break;
155                 case kAudioUnitParameterUnit_Meters:
156                         str = "Mtrs";
157                         break;
158                 case kAudioUnitParameterUnit_Octaves:
159                         str = "8ve";
160                         break;
161                 case kAudioUnitParameterUnit_BPM:
162                         str = "BPM";
163                         break;
164                 case kAudioUnitParameterUnit_Beats:
165                         str = "Beats";
166                         break;
167                 case kAudioUnitParameterUnit_Milliseconds:
168                         str = "msecs";
169                         break;
170                 case kAudioUnitParameterUnit_Ratio:
171                         str = "ratio";
172                         break;
173                 case kAudioUnitParameterUnit_Indexed:
174                         {
175                                 propertySize = sizeof(mNamedParams);
176                                 err = AudioUnitGetProperty (au,
177                                                                         kAudioUnitProperty_ParameterValueStrings,
178                                                                         scope,
179                                                                         param,
180                                                                         &mNamedParams,
181                                                                         &propertySize);
182                                 if (!err && mNamedParams) {
183                                         mNumIndexedParams = CFArrayGetCount(mNamedParams);
184                                 } else {
185                                                 //these are inclusive, so add one value here
186                                         mNumIndexedParams = short(mParamInfo.maxValue+1 - mParamInfo.minValue);
187                                 }
188                                 str = NULL;
189                         }
190                         break;
191                 case kAudioUnitParameterUnit_CustomUnit:
192                 {
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);
198                         str = paramStr;
199                         break;
200                 }
201                 case kAudioUnitParameterUnit_Generic:
202                 case kAudioUnitParameterUnit_Rate:
203                 default:
204                         str = NULL;
205                         break;
206         }
207
208         if (str)
209                 mParamTag = CFStringCreateWithCString(NULL, str, kCFStringEncodingUTF8);
210         else
211                 mParamTag = NULL;
212 }
213
214
215 Float32         CAAUParameter::GetValue() const
216 {
217         Float32 value = 0.;
218         //OSStatus err =
219         AudioUnitGetParameter(mAudioUnit, mParameterID, mScope, mElement, &value);
220         return value;
221 }
222
223 CFStringRef CAAUParameter::GetStringFromValueCopy(const Float32 *value) const
224 {
225         if (HasNamedParams())
226         {
227                 Float32 val = (value == NULL ? GetValue() : *value);
228                 int index = int(mParamInfo.minValue) + int(val);
229                 CFStringRef str = GetParamName (index);
230                 if (str) {
231                         CFRetain (str);
232                         return str;
233                 }
234         }
235         else if (ValuesHaveStrings())
236         {
237                 AudioUnitParameterStringFromValue stringValue;
238                 stringValue.inParamID = mParameterID;
239                 stringValue.inValue = value;
240                 stringValue.outString = NULL;
241                 UInt32 propertySize = sizeof(stringValue);
242
243                 OSStatus err = AudioUnitGetProperty (mAudioUnit,
244                                                                                         kAudioUnitProperty_ParameterStringFromValue,
245                                                                                         mScope,
246                                                                                         mParameterID,
247                                                                                         &stringValue,
248                                                                                         &propertySize);
249
250                 if (err == noErr && stringValue.outString != NULL)
251                         return stringValue.outString;
252         }
253
254         Float32 val = (value == NULL ? GetValue() : *value);
255         char valstr[32];
256         AUParameterFormatValue (val, this, valstr, 4);
257         return CFStringCreateWithCString(NULL, valstr, kCFStringEncodingUTF8);
258 }
259
260 Float32 CAAUParameter::GetValueFromString(CFStringRef str) const
261 {
262         if (ValuesHaveStrings())
263         {
264                 AudioUnitParameterValueFromString valueString;
265                 valueString.inParamID = mParameterID;
266                 valueString.inString = str;
267                 UInt32 propertySize = sizeof(valueString);
268
269                 OSStatus err = AudioUnitGetProperty (mAudioUnit,
270                                                                                 kAudioUnitProperty_ParameterValueFromString,
271                                                                                 mScope,
272                                                                                 mParameterID,
273                                                                                 &valueString,
274                                                                                 &propertySize);
275
276                 if (err == noErr) {
277                         return valueString.outValue;
278                 }
279         }
280
281         Float32 paramValue = mParamInfo.defaultValue;
282         char valstr[32];
283         CFStringGetCString(str, valstr, sizeof(valstr), kCFStringEncodingUTF8);
284         sscanf(valstr, "%f", &paramValue);
285         return paramValue;
286 }
287
288 void            CAAUParameter::SetValue(        AUParameterListenerRef          inListener,
289                                                                         void *                                                  inObject,
290                                                                         Float32                                                 inValue) const
291 {
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;
298
299         AUParameterSet(inListener, inObject, this, valueToSet, 0);
300 }
301
302 #if DEBUG
303 void    CAAUParameter::Print() const
304 {
305         UInt32 clump = 0;
306         GetClumpID (clump);
307
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))
311                 chars[0] = 0;
312
313         printf ("ID: %ld, Clump: %ld, Name: %s\n", mParameterID, clump, chars);
314         free (chars);
315 }
316 #endif