ResidualVM logo ResidualVM website - Forums - Contact us BuildBot - Doxygen - Wiki curved edge

coremidi.cpp

Go to the documentation of this file.
00001 /* ScummVM - Graphic Adventure Engine
00002  *
00003  * ScummVM is the legal property of its developers, whose names
00004  * are too numerous to list here. Please refer to the COPYRIGHT
00005  * file distributed with this source distribution.
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation; either version 2
00010  * of the License, or (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00020  *
00021  */
00022 
00023 // Disable symbol overrides so that we can use system headers.
00024 #define FORBIDDEN_SYMBOL_ALLOW_ALL
00025 
00026 #include "common/scummsys.h"
00027 
00028 #ifdef MACOSX
00029 
00030 #include "common/config-manager.h"
00031 #include "common/error.h"
00032 #include "common/textconsole.h"
00033 #include "common/util.h"
00034 #include "audio/musicplugin.h"
00035 #include "audio/mpu401.h"
00036 
00037 #include <CoreMIDI/CoreMIDI.h>
00038 
00039 
00040 
00041 /*
00042 For information on how to unify the CoreMidi and MusicDevice code:
00043 
00044 http://lists.apple.com/archives/Coreaudio-api/2005/Jun/msg00194.html
00045 http://lists.apple.com/archives/coreaudio-api/2003/Mar/msg00248.html
00046 http://lists.apple.com/archives/coreaudio-api/2003/Jul/msg00137.html
00047 
00048 */
00049 
00050 
00051 /* CoreMIDI MIDI driver
00052  * By Max Horn
00053  */
00054 class MidiDriver_CoreMIDI : public MidiDriver_MPU401 {
00055 public:
00056     MidiDriver_CoreMIDI(ItemCount device);
00057     ~MidiDriver_CoreMIDI();
00058     int open();
00059     bool isOpen() const { return mOutPort != 0 && mDest != 0; }
00060     void close();
00061     void send(uint32 b);
00062     void sysEx(const byte *msg, uint16 length);
00063 
00064 private:
00065     ItemCount mDevice;
00066     MIDIClientRef   mClient;
00067     MIDIPortRef     mOutPort;
00068     MIDIEndpointRef mDest;
00069 };
00070 
00071 MidiDriver_CoreMIDI::MidiDriver_CoreMIDI(ItemCount device)
00072     : mDevice(device), mClient(0), mOutPort(0), mDest(0) {
00073 
00074     OSStatus err;
00075     err = MIDIClientCreate(CFSTR("ResidualVM MIDI Driver for OS X"), NULL, NULL, &mClient);
00076 }
00077 
00078 MidiDriver_CoreMIDI::~MidiDriver_CoreMIDI() {
00079     if (mClient)
00080         MIDIClientDispose(mClient);
00081     mClient = 0;
00082 }
00083 
00084 int MidiDriver_CoreMIDI::open() {
00085     if (isOpen())
00086         return MERR_ALREADY_OPEN;
00087 
00088     OSStatus err = noErr;
00089 
00090     mOutPort = 0;
00091 
00092     ItemCount dests = MIDIGetNumberOfDestinations();
00093     if (mDevice < dests && mClient) {
00094         mDest = MIDIGetDestination(mDevice);
00095         err = MIDIOutputPortCreate( mClient,
00096                                     CFSTR("residualvm_output_port"),
00097                                     &mOutPort);
00098     } else {
00099         return MERR_DEVICE_NOT_AVAILABLE;
00100     }
00101 
00102     if (err != noErr)
00103         return MERR_CANNOT_CONNECT;
00104 
00105     return 0;
00106 }
00107 
00108 void MidiDriver_CoreMIDI::close() {
00109     MidiDriver_MPU401::close();
00110 
00111     if (isOpen()) {
00112         MIDIPortDispose(mOutPort);
00113         mOutPort = 0;
00114         mDest = 0;
00115     }
00116 }
00117 
00118 void MidiDriver_CoreMIDI::send(uint32 b) {
00119     assert(isOpen());
00120 
00121     // Extract the MIDI data
00122     byte status_byte = (b & 0x000000FF);
00123     byte first_byte = (b & 0x0000FF00) >> 8;
00124     byte second_byte = (b & 0x00FF0000) >> 16;
00125 
00126     // Generate a single MIDI packet with that data
00127     MIDIPacketList packetList;
00128     MIDIPacket *packet = &packetList.packet[0];
00129 
00130     packetList.numPackets = 1;
00131 
00132     packet->timeStamp = 0;
00133     packet->data[0] = status_byte;
00134     packet->data[1] = first_byte;
00135     packet->data[2] = second_byte;
00136 
00137     // Compute the correct length of the MIDI command. This is important,
00138     // else things may screw up badly...
00139     switch (status_byte & 0xF0) {
00140     case 0x80:  // Note Off
00141     case 0x90:  // Note On
00142     case 0xA0:  // Polyphonic Aftertouch
00143     case 0xB0:  // Controller Change
00144     case 0xE0:  // Pitch Bending
00145         packet->length = 3;
00146         break;
00147     case 0xC0:  // Programm Change
00148     case 0xD0:  // Monophonic Aftertouch
00149         packet->length = 2;
00150         break;
00151     default:
00152         warning("CoreMIDI driver encountered unsupported status byte: 0x%02x", status_byte);
00153         packet->length = 3;
00154         break;
00155     }
00156 
00157     // Finally send it out to the synthesizer.
00158     MIDISend(mOutPort, mDest, &packetList);
00159 }
00160 
00161 void MidiDriver_CoreMIDI::sysEx(const byte *msg, uint16 length) {
00162     assert(isOpen());
00163 
00164     byte buf[384];
00165     MIDIPacketList *packetList = (MIDIPacketList *)buf;
00166     MIDIPacket *packet = packetList->packet;
00167 
00168     assert(sizeof(buf) >= sizeof(UInt32) + sizeof(MIDITimeStamp) + sizeof(UInt16) + length + 2);
00169 
00170     packetList->numPackets = 1;
00171 
00172     packet->timeStamp = 0;
00173 
00174     // Add SysEx frame
00175     packet->length = length + 2;
00176     packet->data[0] = 0xF0;
00177     memcpy(packet->data + 1, msg, length);
00178     packet->data[length + 1] = 0xF7;
00179 
00180     // Send it
00181     MIDISend(mOutPort, mDest, packetList);
00182 }
00183 
00184 
00185 // Plugin interface
00186 
00187 class CoreMIDIMusicPlugin : public MusicPluginObject {
00188 public:
00189     const char *getName() const {
00190         return "CoreMIDI";
00191     }
00192 
00193     const char *getId() const {
00194         return "coremidi";
00195     }
00196 
00197     MusicDevices getDevices() const;
00198     Common::Error createInstance(MidiDriver **mididriver, MidiDriver::DeviceHandle = 0) const;
00199 
00200 private:
00201     bool getDeviceName(ItemCount deviceIndex, Common::String &outName) const;
00202 };
00203 
00204 MusicDevices CoreMIDIMusicPlugin::getDevices() const {
00205     // TODO: Is it possible to get the music type for each device?
00206     // Maybe look at the kMIDIPropertyModel property?
00207 
00208     MusicDevices devices;
00209     ItemCount deviceCount = MIDIGetNumberOfDestinations();
00210     for (ItemCount i = 0 ; i < deviceCount ; ++i) {
00211         Common::String name;
00212         if (getDeviceName(i, name))
00213             devices.push_back(MusicDevice(this, name, MT_GM));
00214     }
00215     return devices;
00216 }
00217 
00218 Common::Error CoreMIDIMusicPlugin::createInstance(MidiDriver **mididriver, MidiDriver::DeviceHandle device) const {
00219     ItemCount deviceCount = MIDIGetNumberOfDestinations();
00220     for (ItemCount i = 0 ; i < deviceCount ; ++i) {
00221         Common::String name;
00222         if (getDeviceName(i, name)) {
00223             MusicDevice md(this, name, MT_GM);
00224             if (md.getHandle() == device) {
00225                 *mididriver = new MidiDriver_CoreMIDI(i);
00226                 return Common::kNoError;
00227             }
00228         }
00229     }
00230 
00231     return Common::kUnknownError;
00232 }
00233 
00234 bool CoreMIDIMusicPlugin::getDeviceName(ItemCount deviceIndex, Common::String &outName) const {
00235     MIDIEndpointRef dest = MIDIGetDestination(deviceIndex);
00236     if (!dest)
00237         return false;
00238     CFStringRef name = nil;
00239     if (MIDIObjectGetStringProperty(dest, kMIDIPropertyDisplayName, &name) == noErr) {
00240         char buffer[128];
00241         if (CFStringGetCString(name, buffer, sizeof(buffer), kCFStringEncodingASCII)) {
00242             outName = buffer;
00243             CFRelease(name);
00244             return true;
00245         }
00246         CFRelease(name);
00247     }
00248     // Rather than fail use a default name
00249     warning("Failed to get name for CoreMIDi device %lu", deviceIndex);
00250     outName = Common::String::format("Unknown Device %lu", deviceIndex);
00251     return true;
00252 }
00253 
00254 //#if PLUGIN_ENABLED_DYNAMIC(COREMIDI)
00255     //REGISTER_PLUGIN_DYNAMIC(COREMIDI, PLUGIN_TYPE_MUSIC, CoreMIDIMusicPlugin);
00256 //#else
00257     REGISTER_PLUGIN_STATIC(COREMIDI, PLUGIN_TYPE_MUSIC, CoreMIDIMusicPlugin);
00258 //#endif
00259 
00260 #endif // MACOSX


Generated on Sat Jun 15 2019 05:01:05 for ResidualVM by doxygen 1.7.1
curved edge   curved edge