/* CMP 0.90 by Pieter Suurmond, june 13, 2003. Supposed to be the platform-independant MIDI layer upon Mac-OMS, IRIX-midi, etc. Routines here never print to stdout or stderr but always to a text-buffer. To use MIDI in Comparser, first call 'initializeMidiIO()', it returns a pointer to ComParser's real time MIDI object. Then call 'processMidiEvents()' regularly to process incoming MIDI commands. At the end of this file, all MIDI commands can be patched. After use, free with 'releaseMidiIO()'. */ #include #include #include #include "CMP_midi.h" /* Include mac_oms/mac_midi_oms.c in project for OMS on Mac. */ /* Or sgi_libmd/sgi_midi.c for Silicon Graphics IRIX. */ /*-----------------------------------------------------------------------------------*/ CMP_MIDI_IO* initializeMidiIO(char* inName, /* Or NULL for default. */ char* outName, /* Or NULL for default. */ unsigned char recOmni, /* Pass your settings. */ unsigned char recChan, unsigned char recThru, unsigned char recSysexID, char* report255) /* 256 chars must be there! */ { const char* ff = "CMP.midi.c : initializeMidiIO()"; CMP_MIDI_IO* mio = (CMP_MIDI_IO*)malloc(sizeof(CMP_MIDI_IO)); if (!mio) { sprintf((char*)report255, "%s not enough memory.\n", ff); goto done; } if ((inName) &&(*inName)) mio->inName = strdup(inName); else mio->inName = NULL; if ((outName)&&(*outName)) mio->outName = strdup(outName); else mio->outName = NULL; mio->specific = (void*)NULL; /* So we can safely jump to "failed". */ strcpy(mio->login31, "ComParser"); mio->recOmni = recOmni; mio->recChan = recChan; mio->recThru = recThru; mio->recSysexID = recSysexID; mio->recOverflow = 0; /* OVFLOW not yet implemented for SGI. */ if (recChan > 15) /* On Mac: read before receiveMID(). */ { sprintf(report255, "%s illegal channel: %d.\n", ff, (int)recChan); goto failed; /* (mio->specific = NULL) */ } if (recSysexID > 127) { sprintf(report255, "%s sysex ID too high: %d.\n", ff, (int)recSysexID); goto failed; /* (mio->specific = NULL) */ } mio->specific = specificOpenMIDI(mio); /* In file mac_midi_oms.c or sgi_midi.c */ /* Both variants also write number of */ sprintf(report255, "%s", mio->rep255); /* devices found in (parent) mio-struc. */ if (mio->specific) goto done; failed: /* This ill make return value NULL. */ releaseMidiIO(&mio, report255 + strlen(report255)); done: /* Assume this still fits in 255 chars! */ return mio; } /*---------------------------------------------------*/ void releaseMidiIO(CMP_MIDI_IO** miop, char* report255) { CMP_MIDI_IO* mio; if (!miop) { sprintf(report255, "NULL argument supplied to releaseMidiIO().\n"); return; /* Arg may not be NULL itself. */ } /* It may however point to NULL */ mio = *miop; /* (then it is already released). */ if (mio) { /* Release dynamic memebers. */ if (mio->inName) free(mio->inName); if (mio->outName) free(mio->outName); if (mio->specific) /* (specificOpenMIDI() failed?) */ { /* Frees and clears(!) ->specific */ specificCloseMIDI(mio); /* and mio->numInterfaces fields. */ sprintf(report255, "%s", mio->rep255); } free(mio); *miop = (CMP_MIDI_IO*)NULL; /* Clear variable of caller. */ } else sprintf(report255, "Wrong handle supplied to releaseMidiIO().\n"); }