Jamoma API  0.6.0.a19
CAAUMIDIMap.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 "CAAUMIDIMap.h"
42 
43 
44 static MIDILinearTransformer linearTrans;
45 static MIDILogTransformer logTrans;
46 static MIDIExpTransformer expTrans;
47 static MIDISqrtTransformer sqrtTrans;
48 static MIDISquareTransformer squareTrans;
49 static MIDICubeRtTransformer cubeRtTrans;
50 static MIDICubeTransformer cubeTrans;
51 
52 MIDIValueTransformer * CAAUMIDIMap::GetTransformer (UInt32 inFlags)
53 {
54  if (AudioUnitDisplayTypeIsLogarithmic(inFlags))
55  return &logTrans;
56  else if (AudioUnitDisplayTypeIsExponential(inFlags))
57  return &expTrans;
58  else if (AudioUnitDisplayTypeIsSquareRoot(inFlags))
59  return &sqrtTrans;
60  else if (AudioUnitDisplayTypeIsSquared(inFlags))
61  return &squareTrans;
62  else if (AudioUnitDisplayTypeIsCubed(inFlags))
63  return &cubeTrans;
64  else if (AudioUnitDisplayTypeIsCubeRoot(inFlags))
65  return &cubeRtTrans;
66  else
67  return &linearTrans;
68 }
69 
70 // The CALLER of this method must ensure that the status byte's MIDI Command matches!!!
71 bool CAAUMIDIMap::MIDI_Matches (UInt8 inChannel, UInt8 inData1, UInt8 inData2, Float32 &outLinear) const
72 {
73  // see if the channels match first
74  SInt8 chan = Channel();
75  // channel matches (if chan is less than zero, "Any Channel" flag is set)
76  if (chan >= 0 && chan != inChannel)
77  return false;
78 
79  // match the special cases first
80  if (IsKeyEvent()) {
81  // we're using this key event as an on/off type switch
82  if (IsBipolar()) {
83  if (IsKeyPressure()){
84  if (IsBipolar_OnValue()) {
85  if (inData2 > 63) {
86  outLinear = 1;
87  return true;
88  }
89  } else {
90  if (inData2 < 64) {
91  outLinear = 0;
92  return true;
93  }
94  }
95  return false;
96  }
97  else {
98  if (IsBipolar_OnValue()) {
99  if (inData1 > 63) {
100  outLinear = 1;
101  return true;
102  }
103  } else {
104  if (inData1 < 64) {
105  outLinear = 0;
106  return true;
107  }
108  }
109  return false;
110  }
111  }
112  if (IsAnyNote()) {
113 // not quite sure how to interpret this...
114  if (IsKeyPressure())
115  outLinear = inData2 / 127.0;
116  else
117  outLinear = inData1 / 127.0;
118  return true;
119  }
120  if (mData1 == inData1) {
121  if (IsKeyPressure())
122  outLinear = inData2 / 127.0;
123  else
124  outLinear = 1;
125  return true;
126  }
127  return false;
128  }
129  else if (IsControlChange()) {
130  // controller ID matches
131  if (mData1 == inData1) {
132  if (IsBipolar()) {
133  if (IsBipolar_OnValue()) {
134  if (inData2 > 63) {
135  outLinear = 1;
136  return true;
137  }
138  } else {
139  if (inData2 < 64) {
140  outLinear = 0;
141  return true;
142  }
143  }
144  return false;
145  }
146  //printf("this in midi matches %X with ", this);
147  outLinear = inData2 / 127.;
148  return true;
149  }
150  return false;
151  }
152 
153  // this just matches on the patch change value itself...
154  if (IsPatchChange()) {
155  if (mData1 == inData1) {
156  outLinear = 1;
157  return true;
158  }
159  return false;
160  }
161 
162  // finally, for the other two, just check the bi-polar matching conditions
163  // pitch bend and after touch
164  if (IsBipolar()) {
165  if (IsBipolar_OnValue()) {
166  if (inData1 > 63) {
167  outLinear = 1;
168  return true;
169  }
170  } else {
171  if (inData1 < 64) {
172  outLinear = 0;
173  return true;
174  }
175  }
176  return false;
177  }
178 
179  if (IsPitchBend()) {
180  UInt16 value = (inData2 << 7) | inData1;
181  outLinear = value / 16383.;
182  }
183  else if (IsChannelPressure()) {
184  outLinear = inData1 / 127.0;
185  }
186 
187  return true;
188 }
189 
190 
191 void CAAUMIDIMap::Print () const
192 {
193  printf ("CAAUMIDIMap:%p, (%u/%u), mParamID %d, IsValid:%c, Status:0x%X, mData1 %d, Flags:0x%X\n", this, (unsigned int)mScope, (unsigned int)mElement, (int)mParameterID, (IsValid() ? 'T' : 'F'), mStatus, mData1, (int)mFlags);
194 }