Jack2 1.9.8

JackCoreMidiVirtualInputPort.cpp

00001 /*
00002 Copyright (C) 2011 Devin Anderson
00003 
00004 This program is free software; you can redistribute it and/or modify
00005 it under the terms of the GNU General Public License as published by
00006 the Free Software Foundation; either version 2 of the License, or
00007 (at your option) any later version.
00008 
00009 This program is distributed in the hope that it will be useful,
00010 but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 GNU General Public License for more details.
00013 
00014 You should have received a copy of the GNU General Public License
00015 along with this program; if not, write to the Free Software
00016 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00017 
00018 */
00019 
00020 #include <sstream>
00021 #include <stdexcept>
00022 
00023 #include "JackCoreMidiUtil.h"
00024 #include "JackCoreMidiVirtualInputPort.h"
00025 
00026 using Jack::JackCoreMidiVirtualInputPort;
00027 
00029 // Static callbacks
00031 
00032 void
00033 JackCoreMidiVirtualInputPort::
00034 HandleInputEvent(const MIDIPacketList *packet_list, void *port,
00035                  void */*src_ref*/)
00036 {
00037     ((JackCoreMidiVirtualInputPort *) port)->ProcessCoreMidi(packet_list);
00038 }
00039 
00041 // Class
00043 
00044 JackCoreMidiVirtualInputPort::
00045 JackCoreMidiVirtualInputPort(const char *alias_name, const char *client_name,
00046                              const char *driver_name, int index,
00047                              MIDIClientRef client, double time_ratio,
00048                              size_t max_bytes, size_t max_messages):
00049     JackCoreMidiInputPort(time_ratio, max_bytes, max_messages)
00050 {
00051     std::stringstream stream;
00052     stream << "virtual" << (index + 1);
00053     CFStringRef name = CFStringCreateWithCString(0, stream.str().c_str(),
00054                                                 CFStringGetSystemEncoding());
00055     if (! name) {
00056         throw std::bad_alloc();
00057     }
00058     MIDIEndpointRef destination;
00059     OSStatus status = MIDIDestinationCreate(client, name, HandleInputEvent,
00060                                             this, &destination);
00061     CFRelease(name);
00062     if (status != noErr) {
00063         throw std::runtime_error(GetMacOSErrorString(status));
00064     }
00065     Initialize(alias_name, client_name, driver_name, index, destination);
00066 }
00067 
00068 JackCoreMidiVirtualInputPort::~JackCoreMidiVirtualInputPort()
00069 {
00070     OSStatus status = MIDIEndpointDispose(GetEndpoint());
00071     if (status != noErr) {
00072         WriteMacOSError("JackCoreMidiVirtualInputPort [destructor]",
00073                         "MIDIEndpointDispose", status);
00074     }
00075 }