Jack2  1.9.10
JackCoreMidiVirtualInputPort.cpp
1 /*
2 Copyright (C) 2011 Devin Anderson
3 
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8 
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13 
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 
18 */
19 
20 #include <sstream>
21 #include <stdexcept>
22 
23 #include "JackError.h"
24 #include "JackCoreMidiUtil.h"
25 #include "JackCoreMidiVirtualInputPort.h"
26 
28 
30 // Static callbacks
32 
33 void
34 JackCoreMidiVirtualInputPort::
35 HandleInputEvent(const MIDIPacketList *packet_list, void *port,
36  void */*src_ref*/)
37 {
38  ((JackCoreMidiVirtualInputPort *) port)->ProcessCoreMidi(packet_list);
39 }
40 
42 // Class
44 
45 JackCoreMidiVirtualInputPort::
46 JackCoreMidiVirtualInputPort(const char *alias_name, const char *client_name,
47  const char *driver_name, int base_index, int index,
48  MIDIClientRef client, double time_ratio,
49  size_t max_bytes, size_t max_messages):
50  JackCoreMidiInputPort(time_ratio, max_bytes, max_messages)
51 {
52  std::stringstream stream;
53  stream << "virtual" << (base_index + 1);
54  CFStringRef name = CFStringCreateWithCString(0, stream.str().c_str(),
55  CFStringGetSystemEncoding());
56  if (! name) {
57  throw std::bad_alloc();
58  }
59  MIDIEndpointRef destination;
60  OSStatus status = MIDIDestinationCreate(client, name, HandleInputEvent,
61  this, &destination);
62 
63  /*
64  SInt32 value;
65  status = MIDIObjectGetIntegerProperty(destination, kMIDIPropertyUniqueID, &value);
66  if (status == noErr) {
67  jack_info("kMIDIPropertyUniqueID %d", value);
68  }
69  */
70 
71  CFRelease(name);
72  if (status != noErr) {
73  throw std::runtime_error(GetMacOSErrorString(status));
74  }
75  Initialize(alias_name, client_name, driver_name, index, destination);
76 
77  // Keep in global list (that keeps growing during the whole session...)
78  endpoint_list.insert(endpoint);
79 }
80 
81 JackCoreMidiVirtualInputPort::~JackCoreMidiVirtualInputPort()
82 {
83  OSStatus status = MIDIEndpointDispose(GetEndpoint());
84  if (status != noErr) {
85  WriteMacOSError("JackCoreMidiVirtualInputPort [destructor]",
86  "MIDIEndpointDispose", status);
87  }
88 }