DOSBox-X
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines
src/gui/midi_coremidi.h
00001 /*
00002  *  This program is free software; you can redistribute it and/or modify
00003  *  it under the terms of the GNU General Public License as published by
00004  *  the Free Software Foundation; either version 2 of the License, or
00005  *  (at your option) any later version.
00006  *
00007  *  This program is distributed in the hope that it will be useful,
00008  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00009  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00010  *  GNU General Public License for more details.
00011  *
00012  *  You should have received a copy of the GNU General Public License
00013  *  along with this program; if not, write to the Free Software
00014  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00015  */
00016 
00017 #include <CoreMIDI/MIDIServices.h>
00018 
00019 class MidiHandler_coremidi : public MidiHandler {
00020 private:
00021         MIDIPortRef m_port;
00022         MIDIClientRef m_client;
00023         MIDIEndpointRef m_endpoint;
00024         MIDIPacket* m_pCurPacket;
00025 public:
00026         MidiHandler_coremidi()  {m_pCurPacket = 0;}
00027         const char * GetName(void) { return "coremidi"; }
00028         bool Open(const char * conf) {
00029         
00030                 // Get the MIDIEndPoint
00031                 m_endpoint = 0;
00032 //              OSStatus result;
00033                 Bitu numDests = MIDIGetNumberOfDestinations();
00034                 Bitu destId = 0;
00035                 if(conf && conf[0]) destId = atoi(conf);
00036                 if (destId < numDests)
00037                 {
00038                         m_endpoint = MIDIGetDestination(destId);
00039                 }
00040 
00041                 // Create a MIDI client and port
00042                 MIDIClientCreate(CFSTR("MyClient"), 0, 0, &m_client);
00043 
00044                 if (!m_client)
00045                 {
00046                         LOG_MSG("MIDI:coremidi: No client created.");
00047                         return false;
00048                 }
00049 
00050                 MIDIOutputPortCreate(m_client, CFSTR("MyOutPort"), &m_port);
00051 
00052                 if (!m_port)
00053                 {
00054                         LOG_MSG("MIDI:coremidi: No port created.");
00055                         return false;
00056                 }
00057 
00058                 
00059                 return true;
00060         }
00061         
00062         void Close(void) {
00063                 // Dispose the port
00064                 MIDIPortDispose(m_port);
00065 
00066                 // Dispose the client
00067                 MIDIClientDispose(m_client);
00068 
00069                 // Dispose the endpoint
00070                 MIDIEndpointDispose(m_endpoint);
00071         }
00072         
00073         void PlayMsg(Bit8u * msg) {
00074                 // Acquire a MIDIPacketList
00075                 Byte packetBuf[128];
00076                 MIDIPacketList *packetList = (MIDIPacketList *)packetBuf;
00077                 m_pCurPacket = MIDIPacketListInit(packetList);
00078                 
00079                 // Determine the length of msg
00080                 Bitu len=MIDI_evt_len[*msg];
00081                 
00082                 // Add msg to the MIDIPacketList
00083                 MIDIPacketListAdd(packetList, (ByteCount)sizeof(packetBuf), m_pCurPacket, (MIDITimeStamp)0, len, msg);
00084                 
00085                 // Send the MIDIPacketList
00086                 MIDISend(m_port,m_endpoint,packetList);
00087         }
00088         
00089         void PlaySysex(Bit8u * sysex, Bitu len) {
00090                 // Acquire a MIDIPacketList
00091                 Byte packetBuf[SYSEX_SIZE*4];
00092 //              Bitu pos=0;
00093                 MIDIPacketList *packetList = (MIDIPacketList *)packetBuf;
00094                 m_pCurPacket = MIDIPacketListInit(packetList);
00095                 
00096                 // Add msg to the MIDIPacketList
00097                 MIDIPacketListAdd(packetList, (ByteCount)sizeof(packetBuf), m_pCurPacket, (MIDITimeStamp)0, len, sysex);
00098                 
00099                 // Send the MIDIPacketList
00100                 MIDISend(m_port,m_endpoint,packetList);
00101         }
00102 };
00103 
00104 MidiHandler_coremidi Midi_coremidi;
00105