/[svn]/linuxsampler/trunk/src/drivers/midi/MidiInputDeviceCoreMidi.cpp
ViewVC logotype

Contents of /linuxsampler/trunk/src/drivers/midi/MidiInputDeviceCoreMidi.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2431 - (show annotations) (download)
Mon Mar 4 15:12:48 2013 UTC (7 years, 1 month ago) by schoenebeck
File size: 11851 byte(s)
* CoreMIDI fix: a MIDIPacket can contain more than one event per packet.

1 /***************************************************************************
2 * *
3 * Copyright (C) 2004, 2005 Grame *
4 * Copyright (C) 2005 - 2012 Christian Schoenebeck *
5 * *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the Free Software *
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, *
19 * MA 02111-1307 USA *
20 ***************************************************************************/
21
22 #include "MidiInputDeviceCoreMidi.h"
23 #include "MidiInputDeviceFactory.h"
24
25 namespace LinuxSampler {
26
27 // *************** privat static functions ***************
28 // *
29
30 static void _bridgeInputEvent(const MIDIPacketList* packetList, void* /*device*/, void* port) {
31 //MidiInputDeviceCoreMidi* pDevice = (MidiInputDeviceCoreMidi*) device;
32 MidiInputDeviceCoreMidi::MidiInputPortCoreMidi* pPort = (MidiInputDeviceCoreMidi::MidiInputPortCoreMidi*) port;
33 pPort->ProcessMidiEvents(packetList);
34 }
35
36 static String _getDisplayName(MIDIObjectRef object) {
37 CFStringRef name = nil;
38 if (MIDIObjectGetStringProperty(object, kMIDIPropertyDisplayName, &name) != noErr) {
39 dmsg(1,("CoreMIDI: could not resolve display name of object\n"));
40 return "";
41 }
42 // convert NSString to C string
43 char* buf = new char[256];
44 if (!CFStringGetCString(name, buf, 256, kCFStringEncodingUTF8)) {
45 dmsg(1,("CoreMIDI: could not convert display name string\n"));
46 delete[] buf;
47 return "";
48 }
49 String result = buf;
50 delete[] buf;
51 return result;
52 }
53
54
55 // *************** ParameterName ***************
56 // *
57
58 MidiInputDeviceCoreMidi::MidiInputPortCoreMidi::ParameterName::ParameterName(MidiInputPort* pPort) throw (Exception) : MidiInputPort::ParameterName(pPort, "Port " + ToString(pPort->GetPortNumber())) {
59 OnSetValue(ValueAsString()); // initialize port name
60 }
61
62 void MidiInputDeviceCoreMidi::MidiInputPortCoreMidi::ParameterName::OnSetValue(String s) throw (Exception) {
63 //TODO: renaming of port to be implemented
64 }
65
66 // *************** ParameterCoreMidiBindings ***************
67 // *
68
69 MidiInputDeviceCoreMidi::MidiInputPortCoreMidi::ParameterCoreMidiBindings::ParameterCoreMidiBindings(MidiInputPortCoreMidi* pPort) : DeviceRuntimeParameterStrings( std::vector<String>() ) {
70 this->pPort = pPort;
71 }
72
73 String MidiInputDeviceCoreMidi::MidiInputPortCoreMidi::ParameterCoreMidiBindings::Description() {
74 return "Bindings to other CoreMidi clients";
75 }
76 bool MidiInputDeviceCoreMidi::MidiInputPortCoreMidi::ParameterCoreMidiBindings::Fix() {
77 return false;
78 }
79
80 std::vector<String> MidiInputDeviceCoreMidi::MidiInputPortCoreMidi::ParameterCoreMidiBindings::PossibilitiesAsString() {
81 std::vector<String> res;
82
83 const ItemCount sourceCount = MIDIGetNumberOfSources();
84 for (ItemCount i = 0; i < sourceCount; ++i) {
85 MIDIEndpointRef source = MIDIGetSource(i);
86 res.push_back(
87 _getDisplayName(source)
88 );
89 }
90
91 return res;
92 }
93
94 void MidiInputDeviceCoreMidi::MidiInputPortCoreMidi::ParameterCoreMidiBindings::OnSetValue(std::vector<String> vS) throw (Exception) {
95 for (int k = 0; k < vS.size(); ++k) {
96 const ItemCount sourceCount = MIDIGetNumberOfSources();
97 for (ItemCount i = 0; i < sourceCount; ++i) {
98 MIDIEndpointRef source = MIDIGetSource(i);
99 String name = _getDisplayName(source);
100 if (name == vS[k]) {
101 pPort->connectToSource(source);
102 goto matchFound;
103 }
104 }
105 throw MidiInputException("No CoreMIDI source '" + vS[k] + "' found to connect to");
106 matchFound:
107 ; // noop
108 }
109 }
110
111 // *************** ParameterAutoBind ***************
112 // *
113
114 MidiInputDeviceCoreMidi::MidiInputPortCoreMidi::ParameterAutoBind::ParameterAutoBind(MidiInputPortCoreMidi* pPort)
115 : DeviceRuntimeParameterBool(false)
116 {
117 this->pPort = pPort;
118 }
119
120 String MidiInputDeviceCoreMidi::MidiInputPortCoreMidi::ParameterAutoBind::Description() {
121 return "Whether port shall automatically be connected to all CoreMIDI source endpoints.";
122 }
123
124 bool MidiInputDeviceCoreMidi::MidiInputPortCoreMidi::ParameterAutoBind::Fix() {
125 return false;
126 }
127
128 void MidiInputDeviceCoreMidi::MidiInputPortCoreMidi::ParameterAutoBind::OnSetValue(bool b) throw (Exception) {
129 if (b) pPort->connectToAllSources();
130 }
131
132
133 // *************** MidiInputPortCoreMidi ***************
134 // *
135
136 int MidiInputDeviceCoreMidi::MidiInputPortCoreMidi::pPortID = 0;
137
138 MidiInputDeviceCoreMidi::MidiInputPortCoreMidi::MidiInputPortCoreMidi(MidiInputDeviceCoreMidi* pDevice) throw (MidiInputException) : MidiInputPort(pDevice, -1) {
139 this->pDevice = pDevice;
140
141 // create CoreMidi virtual destination
142
143 /* 20080105 Toshi Nagata */
144 char buf[32];
145 CFStringRef str;
146 snprintf(buf, sizeof buf, "LinuxSampler_in_%d", pPortID);
147 str = CFStringCreateWithCString(NULL, buf, kCFStringEncodingUTF8);
148 MIDIDestinationCreate(pDevice->hCoreMidiClient, str, ReadProc, this, &pDestination);
149 /* */
150
151 if (!pDestination) throw MidiInputException("Error creating CoreMidi virtual destination");
152 this->portNumber = pPortID++;
153
154 Parameters["NAME"] = new ParameterName(this);
155 Parameters["CORE_MIDI_BINDINGS"] = new ParameterCoreMidiBindings(this);
156 Parameters["AUTO_BIND"] = new ParameterAutoBind(this);
157 }
158
159 MidiInputDeviceCoreMidi::MidiInputPortCoreMidi::~MidiInputPortCoreMidi() {
160 MIDIEndpointDispose(pDestination);
161 }
162
163 void MidiInputDeviceCoreMidi::MidiInputPortCoreMidi::ReadProc(const MIDIPacketList* pktlist, void* refCon, void* connRefCon)
164 {
165 MidiInputPortCoreMidi* port = (MidiInputPortCoreMidi*)refCon;
166 port->ProcessMidiEvents(pktlist);
167 }
168
169 void MidiInputDeviceCoreMidi::MidiInputPortCoreMidi::ProcessMidiEvents(const MIDIPacketList *pktlist) {
170 MIDIPacket *packet = (MIDIPacket *)pktlist->packet;
171 for (unsigned int i = 0; i < pktlist->numPackets; ++i) {
172 char* pData = (char*) packet->data;
173 int k = 0;
174 // A MIDIPacket can have more than one (non SysEx) MIDI event in one
175 // packet. However SysEx messages are guaranteed to be alone in one
176 // MIDIPacket.
177 do {
178 int eventSize = expectedEventSize(pData[k]);
179 if (eventSize < 0) eventSize = packet->length - k;
180
181 if (k + eventSize > packet->length) goto next_packet;
182
183 DispatchRaw(packet->data);
184 k += eventSize;
185 } while (k < packet->length);
186
187 next_packet:
188 packet = MIDIPacketNext(packet);
189 }
190 }
191
192 void MidiInputDeviceCoreMidi::MidiInputPortCoreMidi::connectToSource(MIDIEndpointRef source) {
193 // check if we already have such a connection, if yes, disconnect it first
194 for (uint i = 0; i < bindings.size(); ++i) {
195 if (bindings[i] == source) {
196 MIDIPortDisconnectSource(pDevice->pBridge, source);
197 break;
198 }
199 }
200 // now establish the CoreMIDI connection
201 MIDIPortConnectSource(pDevice->pBridge, source, this);
202 // and remember it
203 bindings.push_back(source);
204 }
205
206 void MidiInputDeviceCoreMidi::MidiInputPortCoreMidi::connectToAllSources() {
207 const ItemCount sourceCount = MIDIGetNumberOfSources();
208 for (ItemCount i = 0; i < sourceCount; ++i) {
209 MIDIEndpointRef source = MIDIGetSource(i);
210 connectToSource(source);
211 dmsg(1,("Auto binded to CoreMIDI source '%s'\n", _getDisplayName(source).c_str()));
212 }
213 }
214
215 void MidiInputDeviceCoreMidi::MidiInputPortCoreMidi::onNewSourceAppeared(MIDIEndpointRef source) {
216 if (((ParameterAutoBind*)Parameters["AUTO_BIND"])->ValueAsBool()) {
217 connectToSource(source);
218 dmsg(1,("Auto binded to new CoreMIDI source '%s'\n", _getDisplayName(source).c_str()));
219 }
220 }
221
222 void MidiInputDeviceCoreMidi::MidiInputPortCoreMidi::onNewSourceDisappeared(MIDIEndpointRef source) {
223 std::vector<MIDIEndpointRef>::iterator iter = std::find(bindings.begin(), bindings.end(), source);
224 if (iter != bindings.end()) {
225 dmsg(1,("CoreMIDI source '%s' disappeared, disconnecting it.\n", _getDisplayName(source).c_str()));
226 bindings.erase(iter);
227 }
228 }
229
230
231 // *************** MidiInputDeviceCoreMidi ***************
232 // *
233
234 MidiInputDeviceCoreMidi::MidiInputDeviceCoreMidi(std::map<String,DeviceCreationParameter*> Parameters, void* pSampler) : MidiInputDevice(Parameters, pSampler)
235 {
236 MIDIClientCreate(CFSTR("LinuxSampler"), NotifyProc, this, &hCoreMidiClient);
237 if (!hCoreMidiClient) throw MidiInputException("Error opening CoreMidi client");
238
239 OSStatus status = MIDIInputPortCreate(hCoreMidiClient, CFSTR("bridge"), _bridgeInputEvent, this, &pBridge);
240 if (status != noErr) throw MidiInputException("Could not create bridge port for CoreMIDI client");
241
242 AcquirePorts(((DeviceCreationParameterInt*)Parameters["PORTS"])->ValueAsInt());
243 }
244
245 MidiInputDeviceCoreMidi::~MidiInputDeviceCoreMidi()
246 {
247 if (hCoreMidiClient) {
248 MIDIClientDispose(hCoreMidiClient);
249 }
250 }
251
252 MidiInputDeviceCoreMidi::MidiInputPortCoreMidi* MidiInputDeviceCoreMidi::CreateMidiPort() {
253 return new MidiInputPortCoreMidi(this);
254 }
255
256 String MidiInputDeviceCoreMidi::Name() {
257 return "COREMIDI";
258 }
259
260 String MidiInputDeviceCoreMidi::Driver() {
261 return Name();
262 }
263
264 String MidiInputDeviceCoreMidi::Description() {
265 return "Apple CoreMidi";
266 }
267
268 String MidiInputDeviceCoreMidi::Version() {
269 String s = "$Revision: 1.12 $";
270 return s.substr(11, s.size() - 13); // cut dollar signs, spaces and CVS macro keyword
271 }
272
273 void MidiInputDeviceCoreMidi::NotifyProc(const MIDINotification* message, void* refCon)
274 {
275 MidiInputDeviceCoreMidi* pDevice = (MidiInputDeviceCoreMidi*) refCon;
276
277 switch (message->messageID) {
278 case kMIDIMsgObjectAdded: {
279 MIDIObjectAddRemoveNotification* notification = (MIDIObjectAddRemoveNotification*) message;
280 if (notification->childType == kMIDIObjectType_Source) {
281 for (std::map<int,MidiInputPort*>::iterator iter = pDevice->Ports.begin();
282 iter != pDevice->Ports.end(); ++iter)
283 {
284 MidiInputPortCoreMidi* pPort = (MidiInputPortCoreMidi*) iter->second;
285 MIDIEndpointRef source = (MIDIEndpointRef) notification->child;
286 pPort->onNewSourceAppeared(source);
287 }
288 }
289 break;
290 }
291
292 case kMIDIMsgObjectRemoved: {
293 MIDIObjectAddRemoveNotification* notification = (MIDIObjectAddRemoveNotification*) message;
294 if (notification->childType == kMIDIObjectType_Source) {
295 for (std::map<int,MidiInputPort*>::iterator iter = pDevice->Ports.begin();
296 iter != pDevice->Ports.end(); ++iter)
297 {
298 MidiInputPortCoreMidi* pPort = (MidiInputPortCoreMidi*) iter->second;
299 MIDIEndpointRef source = (MIDIEndpointRef) notification->child;
300 pPort->onNewSourceDisappeared(source);
301 }
302 }
303 break;
304 }
305
306 case kMIDIMsgSetupChanged:
307 case kMIDIMsgPropertyChanged:
308 case kMIDIMsgThruConnectionsChanged:
309 case kMIDIMsgSerialPortOwnerChanged:
310 case kMIDIMsgIOError:
311 break;
312 }
313 }
314
315 } // namespace LinuxSampler

  ViewVC Help
Powered by ViewVC