Jamoma API  0.6.0.a19
CAAUMIDIMapManager.cpp
1 /* Copyright © 2007 Apple Inc. All Rights Reserved.
2 
3  Disclaimer: IMPORTANT: This Apple software is supplied to you by
4  Apple Inc. ("Apple") in consideration of your agreement to the
5  following terms, and your use, installation, modification or
6  redistribution of this Apple software constitutes acceptance of these
7  terms. If you do not agree with these terms, please do not use,
8  install, modify or redistribute this Apple software.
9 
10  In consideration of your agreement to abide by the following terms, and
11  subject to these terms, Apple grants you a personal, non-exclusive
12  license, under Apple's copyrights in this original Apple software (the
13  "Apple Software"), to use, reproduce, modify and redistribute the Apple
14  Software, with or without modifications, in source and/or binary forms;
15  provided that if you redistribute the Apple Software in its entirety and
16  without modifications, you must retain this notice and the following
17  text and disclaimers in all such redistributions of the Apple Software.
18  Neither the name, trademarks, service marks or logos of Apple Inc.
19  may be used to endorse or promote products derived from the Apple
20  Software without specific prior written permission from Apple. Except
21  as expressly stated in this notice, no other rights or licenses, express
22  or implied, are granted by Apple herein, including but not limited to
23  any patent rights that may be infringed by your derivative works or by
24  other works in which the Apple Software may be incorporated.
25 
26  The Apple Software is provided by Apple on an "AS IS" basis. APPLE
27  MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
28  THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
29  FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
30  OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
31 
32  IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
33  OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35  INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
36  MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
37  AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
38  STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
39  POSSIBILITY OF SUCH DAMAGE.
40 */
41 #include "CAAUMIDIMapManager.h"
42 #include <AudioToolbox/AudioUnitUtilities.h>
43 
44 CAAUMIDIMapManager::CAAUMIDIMapManager()
45 {
46  hotMapping = false;
47 }
48 
49 static void FillInMap (CAAUMIDIMap &map, AUBase &That)
50 {
51  AudioUnitParameterInfo info;
52  That.GetParameterInfo (map.mScope, map.mParameterID, info);
53 
54  if (map.IsSubRange()) {
55  map.mMinValue = map.mSubRangeMin;
56  map.mMaxValue = map.mSubRangeMax;
57  } else {
58  map.mMinValue = info.minValue;
59  map.mMaxValue = info.maxValue;
60  }
61 
62  map.mTransType = CAAUMIDIMap::GetTransformer(info.flags);
63 }
64 
65 OSStatus CAAUMIDIMapManager::SortedInsertToParamaterMaps (AUParameterMIDIMapping *maps, UInt32 inNumMaps, AUBase &That)
66 {
67  for (unsigned int i = 0; i < inNumMaps; ++i)
68  {
69  CAAUMIDIMap map(maps[i]);
70 
71  FillInMap (map, That);
72 
73  int idx = FindParameterIndex (maps[i]);
74  if (idx > -1)
75  mParameterMaps.erase(mParameterMaps.begin() + idx);
76 
77  // least disruptive place to put this is at the end
78  mParameterMaps.push_back(map);
79  }
80 
81  std::sort(mParameterMaps.begin(), mParameterMaps.end(), CompareMIDIMap());
82 
83  return noErr;
84 }
85 
86 void CAAUMIDIMapManager::GetHotParameterMap(AUParameterMIDIMapping &outMap )
87 {
88  outMap = mHotMap;
89 }
90 
91 void CAAUMIDIMapManager::SortedRemoveFromParameterMaps(AUParameterMIDIMapping *maps, UInt32 inNumMaps, bool &outMapDidChange)
92 {
93  if (hotMapping) {
94  hotMapping = false;
95  }
96 
97  outMapDidChange = false;
98  for (unsigned int i = 0; i < inNumMaps; ++i) {
99  int idx = FindParameterIndex (maps[i]);
100  if (idx > -1) {
101  //mParameterMaps[idx].Print();
102  mParameterMaps.erase(mParameterMaps.begin() + idx);
103  outMapDidChange = true;
104  }
105  }
106 }
107 
108 void CAAUMIDIMapManager::ReplaceAllMaps (AUParameterMIDIMapping* inMappings, UInt32 inNumMaps, AUBase &That)
109 {
110  mParameterMaps.clear();
111 
112  for (unsigned int i = 0; i < inNumMaps; ++i) {
113  CAAUMIDIMap mapping(inMappings[i]);
114 
115  FillInMap (mapping, That);
116  mParameterMaps.push_back (mapping);
117  }
118 
119  std::sort(mParameterMaps.begin(),mParameterMaps.end(), CompareMIDIMap());
120 }
121 
122 bool CAAUMIDIMapManager::HandleHotMapping(UInt8 inStatus,
123  UInt8 inChannel,
124  UInt8 inData1,
125  AUBase &That)
126 { //used to set the hot map info
127 
128  if (inStatus == 0xf0) return false;
129 
130  if (!hotMapping) return false;
131  hotMapping = false;
132 
133  mHotMap.mStatus = inStatus | inChannel;
134  mHotMap.mData1 = inData1;
135 
136  SortedInsertToParamaterMaps (&mHotMap, 1, That);
137  return true;
138 }
139 
140 #if DEBUG
141 
142 void CAAUMIDIMapManager::Print()
143 {
144  for ( ParameterMaps::iterator i = mParameterMaps.begin(); i < mParameterMaps.end(); ++i) {
145  CAAUMIDIMap* listmap = &(*i);
146  listmap->Print();
147  }
148 }
149 
150 #endif // DEBUG
151 
152 void CAAUMIDIMapManager::GetMaps(AUParameterMIDIMapping* maps)
153 {
154  int i = 0;
155  for ( ParameterMaps::iterator iter = mParameterMaps.begin(); iter < mParameterMaps.end(); ++iter, ++i) {
156  AUParameterMIDIMapping &listmap = (*iter);
157  maps[i] = listmap;
158  }
159 }
160 
161 int CAAUMIDIMapManager::FindParameterIndex (AUParameterMIDIMapping &inMap)
162 {
163  //used to get back hot mapping and one at a time maps, for ui
164 
165  int idx = 0;
166  for ( ParameterMaps::iterator i = mParameterMaps.begin(); i < mParameterMaps.end(); ++i) {
167  CAAUMIDIMap & listmap = (*i);
168  if ( (listmap.mParameterID == inMap.mParameterID) &&
169  (listmap.mScope == inMap.mScope) &&
170  (listmap.mElement == inMap.mElement) )
171  {
172  return idx;
173  }
174  idx++;
175  }
176  return -1;
177 }
178 
179 bool CAAUMIDIMapManager::FindParameterMapEventMatch( UInt8 inStatus,
180  UInt8 inChannel,
181  UInt8 inData1,
182  UInt8 inData2,
183  UInt32 inBufferOffset,
184  AUBase& inAUBase)
185 {
186  bool ret_value = false;
187 
188  if (inStatus == 0x90 && !inData2)
189  inStatus = 0x80 | inChannel;
190 
191  //used to test for midi matches once map is made
192  CAAUMIDIMap tempMap;
193  tempMap.mStatus = inStatus | inChannel;
194  tempMap.mData1 = inData1;
195 
196  CompareMIDIMap compareObj;
197 
198  AudioUnitEvent event;
199  event.mEventType = kAudioUnitEvent_ParameterValueChange;
200  event.mArgument.mParameter.mAudioUnit = inAUBase.GetComponentInstance();
201 
202  ParameterMaps::iterator lower_iter =
203  std::lower_bound(mParameterMaps.begin(), mParameterMaps.end(), tempMap, compareObj);
204 
205  while (lower_iter < mParameterMaps.end())
206  {
207  CAAUMIDIMap & map = (*lower_iter);
208  if (compareObj.Finish(map, tempMap))
209  break;
210 
211  Float32 value;
212  if (map.MIDI_Matches(inChannel, inData1, inData2, value))
213  {
214  inAUBase.SetParameter ( map.mParameterID, map.mScope, map.mElement,
215  map.ParamValueFromMIDILinear(value), inBufferOffset);
216 
217  event.mArgument.mParameter.mParameterID = map.mParameterID;
218  event.mArgument.mParameter.mScope = map.mScope;
219  event.mArgument.mParameter.mElement = map.mElement;
220 
221  AUEventListenerNotify(NULL, NULL, &event);
222  ret_value = true;
223  }
224  ++lower_iter;
225  }
226  return ret_value;
227 }