41 #include "AUScopeElement.h"
52 void AUElement::UseIndexedParameters(
int inNumberOfParameters)
54 mIndexedParameters.resize (inNumberOfParameters);
55 mUseIndexedParameters =
true;
63 inline ParameterMapEvent& AUElement::GetParamEvent(AudioUnitParameterID paramID)
65 ParameterMapEvent *event;
67 if(mUseIndexedParameters)
69 if(paramID >= mIndexedParameters.size() )
70 COMPONENT_THROW(kAudioUnitErr_InvalidParameter);
72 event = &mIndexedParameters[paramID];
76 ParameterMap::iterator i = mParameters.find(paramID);
77 if (i == mParameters.end())
78 COMPONENT_THROW(kAudioUnitErr_InvalidParameter);
90 AudioUnitParameterValue AUElement::GetParameter(AudioUnitParameterID paramID)
92 ParameterMapEvent &
event = GetParamEvent(paramID);
94 return event.GetValue();
100 void AUElement::GetRampSliceStartEnd( AudioUnitParameterID paramID,
101 AudioUnitParameterValue & outStartValue,
102 AudioUnitParameterValue & outEndValue,
103 AudioUnitParameterValue & outValuePerFrameDelta )
106 ParameterMapEvent &
event = GetParamEvent(paramID);
109 event.GetRampSliceStartEnd(outStartValue, outEndValue, outValuePerFrameDelta );
114 AudioUnitParameterValue AUElement::GetEndValue( AudioUnitParameterID paramID)
117 ParameterMapEvent &
event = GetParamEvent(paramID);
120 return event.GetEndValue();
125 void AUElement::SetParameter(AudioUnitParameterID paramID, AudioUnitParameterValue inValue,
bool okWhenInitialized)
127 if(mUseIndexedParameters)
129 ParameterMapEvent &
event = GetParamEvent(paramID);
130 event.SetValue(inValue);
134 ParameterMap::iterator i = mParameters.find(paramID);
136 if (i == mParameters.end())
138 if (mAudioUnit->IsInitialized() && !okWhenInitialized) {
144 fprintf(stderr,
"WARNING: %s SetParameter for undefined param ID %d while initialized. Ignoring..\n",
145 mAudioUnit->GetLoggingString(), (int)paramID);
149 ParameterMapEvent event(inValue);
150 mParameters[paramID] = event;
156 ParameterMapEvent &
event = (*i).second;
157 event.SetValue(inValue);
164 void AUElement::SetScheduledEvent( AudioUnitParameterID paramID,
165 const AudioUnitParameterEvent &inEvent,
166 UInt32 inSliceOffsetInBuffer,
167 UInt32 inSliceDurationFrames,
168 bool okWhenInitialized )
170 if(mUseIndexedParameters)
172 ParameterMapEvent &
event = GetParamEvent(paramID);
173 event.SetScheduledEvent(inEvent, inSliceOffsetInBuffer, inSliceDurationFrames );
177 ParameterMap::iterator i = mParameters.find(paramID);
179 if (i == mParameters.end())
181 if (mAudioUnit->IsInitialized() && !okWhenInitialized) {
187 fprintf(stderr,
"WARNING: %s SetScheduledEvent for undefined param ID %d while initialized. Ignoring..\n",
188 mAudioUnit->GetLoggingString(), (int)paramID);
192 ParameterMapEvent event(inEvent, inSliceOffsetInBuffer, inSliceDurationFrames);
193 mParameters[paramID] = event;
199 ParameterMapEvent &
event = (*i).second;
201 event.SetScheduledEvent(inEvent, inSliceOffsetInBuffer, inSliceDurationFrames );
210 void AUElement::GetParameterList(AudioUnitParameterID *outList)
212 if(mUseIndexedParameters)
214 UInt32 nparams = mIndexedParameters.size();
215 for (UInt32 i = 0; i < nparams; i++ )
216 *outList++ = (AudioUnitParameterID)i;
220 for (ParameterMap::iterator i = mParameters.begin(); i != mParameters.end(); ++i)
221 *outList++ = (*i).first;
227 void AUElement::SaveState(CFMutableDataRef data)
229 if(mUseIndexedParameters)
231 UInt32 nparams = mIndexedParameters.size();
232 UInt32 theData = CFSwapInt32HostToBig(nparams);
233 CFDataAppendBytes(data, (UInt8 *)&theData,
sizeof(nparams));
235 for (UInt32 i = 0; i < nparams; i++)
243 entry.paramID = CFSwapInt32HostToBig(i);
245 AudioUnitParameterValue v = mIndexedParameters[i].GetValue();
246 entry.value = CFSwapInt32HostToBig(*(UInt32 *)&v );
248 CFDataAppendBytes(data, (UInt8 *)&entry,
sizeof(entry));
253 UInt32 nparams = CFSwapInt32HostToBig(mParameters.size());
254 CFDataAppendBytes(data, (UInt8 *)&nparams,
sizeof(nparams));
256 for (ParameterMap::iterator i = mParameters.begin(); i != mParameters.end(); ++i) {
263 entry.paramID = CFSwapInt32HostToBig((*i).first);
265 AudioUnitParameterValue v = (*i).second.GetValue();
266 entry.value = CFSwapInt32HostToBig(*(UInt32 *)&v );
268 CFDataAppendBytes(data, (UInt8 *)&entry,
sizeof(entry));
275 const UInt8 * AUElement::RestoreState(
const UInt8 *state)
277 union FloatInt32 { UInt32 i; AudioUnitParameterValue f; };
278 const UInt8 *p = state;
279 UInt32 nparams = CFSwapInt32BigToHost(*(UInt32 *)p);
282 for (UInt32 i = 0; i < nparams; ++i) {
284 AudioUnitParameterID paramID;
285 AudioUnitParameterValue value;
288 entry.paramID = CFSwapInt32BigToHost(*(UInt32 *)p);
291 temp.i = CFSwapInt32BigToHost(*(UInt32 *)p);
292 entry.value = temp.f;
293 p +=
sizeof(AudioUnitParameterValue);
295 SetParameter(entry.paramID, entry.value);
302 void AUElement::SetName (CFStringRef inName)
304 if (mElementName) CFRelease (mElementName);
305 mElementName = inName;
306 if (mElementName) CFRetain (mElementName);
312 AUIOElement::AUIOElement(AUBase *audioUnit) :
313 AUElement(audioUnit),
316 mStreamFormat.SetAUCanonical(2,
317 audioUnit->AudioUnitAPIVersion() == 1);
319 mStreamFormat.mSampleRate = kAUDefaultSampleRate;
324 OSStatus AUIOElement::SetStreamFormat(
const CAStreamBasicDescription &desc)
326 mStreamFormat = desc;
327 return AUBase::noErr;
332 void AUIOElement::AllocateBuffer(UInt32 inFramesToAllocate)
334 if (GetAudioUnit()->HasBegunInitializing())
336 UInt32 framesToAllocate = inFramesToAllocate > 0 ? inFramesToAllocate : GetAudioUnit()->GetMaxFramesPerSlice();
340 mIOBuffer.Allocate(mStreamFormat, (mWillAllocate && NeedsBufferSpace()) ? framesToAllocate : 0);
346 void AUIOElement::DeallocateBuffer()
348 mIOBuffer.Deallocate();
358 UInt32 AUIOElement::GetChannelLayoutTags (AudioChannelLayoutTag *outLayoutTagsPtr)
370 UInt32 AUIOElement::GetAudioChannelLayout (AudioChannelLayout *outMapPtr,
371 Boolean &outWritable)
379 OSStatus AUIOElement::SetAudioChannelLayout (
const AudioChannelLayout &inData)
381 return kAudioUnitErr_InvalidProperty;
389 OSStatus AUIOElement::RemoveAudioChannelLayout ()
391 return kAudioUnitErr_InvalidPropertyValue;
399 for (ElementVector::iterator it = mElements.begin(); it != mElements.end(); ++it)
405 void AUScope::SetNumberOfElements(UInt32 numElements)
408 return mDelegate->SetNumberOfElements(numElements);
410 if (numElements > mElements.size()) {
411 mElements.reserve(numElements);
412 while (numElements > mElements.size()) {
413 AUElement *elem = GetCreator()->CreateElement(GetScope(), mElements.size());
414 mElements.push_back(elem);
417 while (numElements < mElements.size()) {
418 AUElement *elem = mElements.back();
419 mElements.pop_back();
426 bool AUScope::HasElementWithName ()
const
428 for (UInt32 i = 0; i < GetNumberOfElements(); ++i) {
429 AUElement * el =
const_cast<AUScope*
>(
this)->GetElement (i);
430 if (el && el->HasName()) {
440 void AUScope::AddElementNamesToDict (CFMutableDictionaryRef & inNameDict)
442 if (HasElementWithName())
444 static char string[32];
445 CFMutableDictionaryRef elementDict = CFDictionaryCreateMutable (NULL, 0,
446 &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
448 for (UInt32 i = 0; i < GetNumberOfElements(); ++i) {
449 AUElement * el = GetElement (i);
450 if (el && el->HasName()) {
451 snprintf (
string,
sizeof(
string),
"%d",
int(i));
452 str = CFStringCreateWithCString (NULL,
string, kCFStringEncodingASCII);
453 CFDictionarySetValue (elementDict, str, el->GetName());
458 snprintf (
string,
sizeof(
string),
"%d",
int(mScope));
459 str = CFStringCreateWithCString (NULL,
string, kCFStringEncodingASCII);
460 CFDictionarySetValue (inNameDict, str, elementDict);
462 CFRelease (elementDict);
468 bool AUScope::RestoreElementNames (CFDictionaryRef& inNameDict)
470 static char string[32];
473 bool didAddElements =
false;
474 unsigned int maxElNum = 0;
476 int dictSize = CFDictionaryGetCount(inNameDict);
477 CFStringRef * keys = (CFStringRef*)CA_malloc (dictSize *
sizeof (CFStringRef));
478 CFDictionaryGetKeysAndValues (inNameDict, reinterpret_cast<const void**>(keys), NULL);
479 for (
int i = 0; i < dictSize; i++)
482 CFStringGetCString (keys[i],
string, 32, kCFStringEncodingASCII);
483 sscanf (
string,
"%u", &intKey);
484 if (UInt32(intKey) > maxElNum)
488 if (maxElNum >= GetNumberOfElements()) {
489 SetNumberOfElements (maxElNum+1);
490 didAddElements =
true;
494 for (
int i = 0; i < dictSize; i++)
496 CFStringRef elName =
reinterpret_cast<CFStringRef
>(CFDictionaryGetValue (inNameDict, keys[i]));
498 CFStringGetCString (keys[i],
string, 32, kCFStringEncodingASCII);
499 sscanf (
string,
"%d", &intKey);
500 GetElement (intKey)->SetName (elName);
504 return didAddElements;