| 1 | /* $Id: IRTMidi.cpp,v 1.5 1999-08-19 18:46:03 phaller Exp $ */ | 
|---|
| 2 |  | 
|---|
| 3 | /******************************************************************************* | 
|---|
| 4 | * FILE NAME: IRTMidi.cpp                                                       * | 
|---|
| 5 | *                                                                              * | 
|---|
| 6 | * DESCRIPTION:                                                                 * | 
|---|
| 7 | *   Class implementation of the class:                                         * | 
|---|
| 8 | *     IRTMidi- Real Time Midi Base Class                                       * | 
|---|
| 9 | *                                                                              * | 
|---|
| 10 | * Copyright Joel Troster                                                       * | 
|---|
| 11 | * | 
|---|
| 12 | * Project Odin Software License can be found in LICENSE.TXT | 
|---|
| 13 | * | 
|---|
| 14 | *******************************************************************************/ | 
|---|
| 15 |  | 
|---|
| 16 |  | 
|---|
| 17 | /**************************************************************************** | 
|---|
| 18 | * Includes                                                                 * | 
|---|
| 19 | ****************************************************************************/ | 
|---|
| 20 |  | 
|---|
| 21 | #define INCL_DOS | 
|---|
| 22 | #include <os2wrap.h>     //Odin32 OS/2 api wrappers | 
|---|
| 23 | #include <win32type.h> | 
|---|
| 24 | #include "IRTMidi.hpp" | 
|---|
| 25 | #include <meerror.h> | 
|---|
| 26 | #include <string.h> | 
|---|
| 27 | #include <stdio.h> | 
|---|
| 28 | #include <malloc.h> | 
|---|
| 29 |  | 
|---|
| 30 | IRTMidi * IRTMidi::iTheIRTMidiSingleton = NULL; | 
|---|
| 31 |  | 
|---|
| 32 | //------------------------------------------------------------------------------ | 
|---|
| 33 | // IRTMidi::IRTMidi | 
|---|
| 34 | //------------------------------------------------------------------------------ | 
|---|
| 35 | IRTMidi::IRTMidi() | 
|---|
| 36 | : iHardwareClass( 0 ) | 
|---|
| 37 | , iApplicationClass( 1 ) | 
|---|
| 38 | , iRTMidiModule( NULL ) | 
|---|
| 39 | , iNumInInstances( 0 ) | 
|---|
| 40 | , iNumOutInstances( 0 ) | 
|---|
| 41 | { | 
|---|
| 42 | unsigned long rc = 0; | 
|---|
| 43 |  | 
|---|
| 44 | // Remember my address | 
|---|
| 45 | iTheIRTMidiSingleton = this; | 
|---|
| 46 |  | 
|---|
| 47 | // Get RTMIDI.DLL Module Handle procedures | 
|---|
| 48 | rc = getRTMidiProcs(); | 
|---|
| 49 | //fprintf( stderr, "Get Procs rc=%d\n", rc ); | 
|---|
| 50 |  | 
|---|
| 51 | // Initialize MIDI Setup structure and call MIDISetup | 
|---|
| 52 | iSetup.ulStructLength         = sizeof( MIDISETUP ); | 
|---|
| 53 | iSetup.pulMaxRTSysexLength    = &maxRTSysexLen; | 
|---|
| 54 | iSetup.ppulMIDICurrentTime    = &iCurrentTime; | 
|---|
| 55 | iSetup.ulFlags                = 0; | 
|---|
| 56 | iSetup.pfnMIDI_NotifyCallback = NULL; | 
|---|
| 57 | iSetup.hwndCallback           = 0; | 
|---|
| 58 | iSetup.hqueueCallback         = 0; | 
|---|
| 59 | if ( rc == 0 ) | 
|---|
| 60 | rc = (*MidiSetup)( &iSetup, 0 ); | 
|---|
| 61 | //fprintf( stderr, "MidiSetup rc=%d\n", rc ); | 
|---|
| 62 |  | 
|---|
| 63 | // Clear all Instance slots | 
|---|
| 64 | unsigned int i; | 
|---|
| 65 | for ( i = 0; i < MAX_INSTANCES; i++ ) | 
|---|
| 66 | { | 
|---|
| 67 | iInstances[ i ] = NULL; | 
|---|
| 68 | iInInstances[ i ] = NULL; | 
|---|
| 69 | iOutInstances[ i ] = NULL; | 
|---|
| 70 | } | 
|---|
| 71 |  | 
|---|
| 72 | // Query Classes & Instances, filling in the slots | 
|---|
| 73 | if ( rc == 0 ) rc = getClasses(); | 
|---|
| 74 | if ( rc == 0 ) rc = getInstances(); | 
|---|
| 75 | setLastRC( rc ); | 
|---|
| 76 |  | 
|---|
| 77 | // Start up the MIDI timer | 
|---|
| 78 | if ( rc == 0 ) | 
|---|
| 79 | startTimer(); | 
|---|
| 80 | } | 
|---|
| 81 |  | 
|---|
| 82 | unsigned long IRTMidi::getRTMidiProcs() | 
|---|
| 83 | { | 
|---|
| 84 | char loadError[256] = ""; | 
|---|
| 85 | unsigned long rc; | 
|---|
| 86 | PFN modAddr; | 
|---|
| 87 | rc = DosLoadModule( loadError, sizeof(loadError), "RTMIDI", &iRTMidiModule ); | 
|---|
| 88 | if ( rc ) return rc; | 
|---|
| 89 |  | 
|---|
| 90 | rc = DosQueryProcAddr( iRTMidiModule, 0L, "MIDICreateInstance", &modAddr ); | 
|---|
| 91 | if ( rc ) return rc; | 
|---|
| 92 | MidiCreateInstance =  (ULONG(*APIENTRY )( ULONG, MINSTANCE*, PSZ, ULONG)) modAddr; | 
|---|
| 93 |  | 
|---|
| 94 | rc = DosQueryProcAddr( iRTMidiModule, 0L, "MIDIDeleteInstance", &modAddr ); | 
|---|
| 95 | if ( rc ) return rc; | 
|---|
| 96 | MidiDeleteInstance =  (ULONG(*APIENTRY )(MINSTANCE, ULONG)) modAddr; | 
|---|
| 97 |  | 
|---|
| 98 | rc = DosQueryProcAddr( iRTMidiModule, 0L, "MIDIEnableInstance", &modAddr ); | 
|---|
| 99 | if ( rc ) return rc; | 
|---|
| 100 | MidiEnableInstance =  (ULONG(*APIENTRY )(MINSTANCE, ULONG)) modAddr; | 
|---|
| 101 |  | 
|---|
| 102 | rc = DosQueryProcAddr( iRTMidiModule, 0L, "MIDIDisableInstance", &modAddr ); | 
|---|
| 103 | if ( rc ) return rc; | 
|---|
| 104 | MidiDisableInstance = (ULONG(*APIENTRY )(MINSTANCE, ULONG)) modAddr; | 
|---|
| 105 |  | 
|---|
| 106 | rc = DosQueryProcAddr( iRTMidiModule, 0L, "MIDIAddLink", &modAddr ); | 
|---|
| 107 | if ( rc ) return rc; | 
|---|
| 108 | MidiAddLink =         (ULONG(*APIENTRY )(MINSTANCE, MINSTANCE, ULONG, ULONG)) modAddr; | 
|---|
| 109 |  | 
|---|
| 110 | rc = DosQueryProcAddr( iRTMidiModule, 0L, "MIDIRemoveLink", &modAddr ); | 
|---|
| 111 | if ( rc ) return rc; | 
|---|
| 112 | MidiRemoveLink =      (ULONG(*APIENTRY )(MINSTANCE, MINSTANCE, ULONG, ULONG)) modAddr; | 
|---|
| 113 |  | 
|---|
| 114 | rc = DosQueryProcAddr( iRTMidiModule, 0L, "MIDIQueryClassList", &modAddr ); | 
|---|
| 115 | if ( rc ) return rc; | 
|---|
| 116 | MidiQueryClassList    = (ULONG(*APIENTRY )(ULONG, PMIDICLASSINFO, ULONG)) modAddr; | 
|---|
| 117 |  | 
|---|
| 118 | rc = DosQueryProcAddr( iRTMidiModule, 0L, "MIDIQueryInstanceList", &modAddr ); | 
|---|
| 119 | if ( rc ) return rc; | 
|---|
| 120 | MidiQueryInstanceList = (ULONG(*APIENTRY )(ULONG, PMIDIINSTANCEINFO, ULONG)) modAddr; | 
|---|
| 121 |  | 
|---|
| 122 | rc = DosQueryProcAddr( iRTMidiModule, 0L, "MIDIQueryNumClasses", &modAddr ); | 
|---|
| 123 | if ( rc ) return rc; | 
|---|
| 124 | MidiQueryNumClasses   = (ULONG(*APIENTRY )(PULONG, ULONG)) modAddr; | 
|---|
| 125 |  | 
|---|
| 126 | rc = DosQueryProcAddr( iRTMidiModule, 0L, "MIDIQueryNumInstances", &modAddr ); | 
|---|
| 127 | if ( rc ) return rc; | 
|---|
| 128 | MidiQueryNumInstances = (ULONG(*APIENTRY )(PULONG, ULONG)) modAddr; | 
|---|
| 129 |  | 
|---|
| 130 | rc = DosQueryProcAddr( iRTMidiModule, 0L, "MIDISendMessages", &modAddr ); | 
|---|
| 131 | if ( rc ) return rc; | 
|---|
| 132 | MidiSendMessages      = (ULONG(*APIENTRY )(PMESSAGE, ULONG, ULONG)) modAddr; | 
|---|
| 133 |  | 
|---|
| 134 | rc = DosQueryProcAddr( iRTMidiModule, 0L, "MIDISendSysexMessage", &modAddr ); | 
|---|
| 135 | if ( rc ) return rc; | 
|---|
| 136 | MidiSendSysexMessage  = (ULONG(*APIENTRY )(PMESSAGE, ULONG, ULONG)) modAddr; | 
|---|
| 137 |  | 
|---|
| 138 | rc = DosQueryProcAddr( iRTMidiModule, 0L, "MIDIRetrieveMessages", &modAddr ); | 
|---|
| 139 | if ( rc ) return rc; | 
|---|
| 140 | MidiRetrieveMessages  = (ULONG(*APIENTRY )(MINSTANCE, PVOID, PULONG, ULONG)) modAddr; | 
|---|
| 141 |  | 
|---|
| 142 | rc = DosQueryProcAddr( iRTMidiModule, 0L, "MIDISetup", &modAddr ); | 
|---|
| 143 | if ( rc ) return rc; | 
|---|
| 144 | MidiSetup             = (ULONG(*APIENTRY)(PMIDISETUP, ULONG)) modAddr; | 
|---|
| 145 |  | 
|---|
| 146 | rc = DosQueryProcAddr( iRTMidiModule, 0L, "MIDITimer", &modAddr ); | 
|---|
| 147 | if ( rc ) return rc; | 
|---|
| 148 | MidiTimer             = (ULONG(*APIENTRY )(ULONG, ULONG)) modAddr; | 
|---|
| 149 |  | 
|---|
| 150 | return 0; | 
|---|
| 151 | } | 
|---|
| 152 |  | 
|---|
| 153 | //------------------------------------------------------------------------------ | 
|---|
| 154 | // IRTMidi::~IRTMidi | 
|---|
| 155 | //------------------------------------------------------------------------------ | 
|---|
| 156 | IRTMidi::~IRTMidi() | 
|---|
| 157 | { | 
|---|
| 158 | stopTimer(); | 
|---|
| 159 | delInstances(); | 
|---|
| 160 | iTheIRTMidiSingleton = NULL; | 
|---|
| 161 | } | 
|---|
| 162 |  | 
|---|
| 163 | void IRTMidi::setLastRC(unsigned long theRC) | 
|---|
| 164 | { | 
|---|
| 165 | iLastRC = theRC; | 
|---|
| 166 | } | 
|---|
| 167 |  | 
|---|
| 168 | void IRTMidi::resetLastRC() | 
|---|
| 169 | { | 
|---|
| 170 | setLastRC( 0 ); | 
|---|
| 171 | } | 
|---|
| 172 |  | 
|---|
| 173 | //------------------------------------------------------------------------------ | 
|---|
| 174 | // IRTMidi :: RCExplanation | 
|---|
| 175 | //------------------------------------------------------------------------------ | 
|---|
| 176 | char * IRTMidi::RCExplanation() const | 
|---|
| 177 | { | 
|---|
| 178 | char * DebugString; | 
|---|
| 179 | switch( iLastRC ) | 
|---|
| 180 | { | 
|---|
| 181 | case 0: | 
|---|
| 182 | DebugString = "OK"; | 
|---|
| 183 | break; | 
|---|
| 184 |  | 
|---|
| 185 | // Timer error messages | 
|---|
| 186 | case TIMERERR_INVALID_PARAMETER: | 
|---|
| 187 | DebugString = "MIDI Timer: Invalid Parameter"; | 
|---|
| 188 | break; | 
|---|
| 189 | case TIMERERR_INTERNAL_SYSTEM: | 
|---|
| 190 | DebugString = "MIDI Timer: Internal System Error"; | 
|---|
| 191 | break; | 
|---|
| 192 |  | 
|---|
| 193 | // RTMIDI messages | 
|---|
| 194 | case MIDIERR_DUPLICATE_INSTANCE_NAME: | 
|---|
| 195 | DebugString = "MIDIERR_DUPLICATE_INSTANCE_NAME"; | 
|---|
| 196 | break; | 
|---|
| 197 | case MIDIERR_HARDWARE_FAILED: | 
|---|
| 198 | DebugString    = "MIDIERR_HARDWARE_FAILED"; | 
|---|
| 199 | break; | 
|---|
| 200 | case MIDIERR_INTERNAL_SYSTEM: | 
|---|
| 201 | DebugString = "MIDIERR_INTERNAL_SYSTEM"; | 
|---|
| 202 | break; | 
|---|
| 203 | case MIDIERR_INVALID_BUFFER_LENGTH: | 
|---|
| 204 | DebugString = "MIDIERR_INVALID_BUFFER_LENGTH"; | 
|---|
| 205 | break; | 
|---|
| 206 | case MIDIERR_INVALID_CLASS_NUMBER: | 
|---|
| 207 | DebugString    = "MIDIERR_INVALID_CLASS_NUMBER"; | 
|---|
| 208 | break; | 
|---|
| 209 | case MIDIERR_INVALID_CONFIG_DATA: | 
|---|
| 210 | DebugString    = "MIDIERR_INVALID_CONFIG_DATA"; | 
|---|
| 211 | break; | 
|---|
| 212 | case MIDIERR_INVALID_FLAG: | 
|---|
| 213 | DebugString    = "MIDIERR_INVALID_FLAG"; | 
|---|
| 214 | break; | 
|---|
| 215 | case MIDIERR_INVALID_INSTANCE_NAME: | 
|---|
| 216 | DebugString    = "MIDIERR_INVALID_INSTANCE_NAME"; | 
|---|
| 217 | break; | 
|---|
| 218 | case MIDIERR_INVALID_INSTANCE_NUMBER: | 
|---|
| 219 | DebugString    = "MIDIERR_INVALID_INSTANCE_NUMBER"; | 
|---|
| 220 | break; | 
|---|
| 221 | case MIDIERR_INVALID_PARAMETER: | 
|---|
| 222 | DebugString = "MIDIERR_INVALID_PARAMETER"; | 
|---|
| 223 | break; | 
|---|
| 224 | case MIDIERR_INVALID_SETUP: | 
|---|
| 225 | DebugString = "MIDIERR_INVALID_SETUP"; | 
|---|
| 226 | break; | 
|---|
| 227 | case MIDIERR_NO_DRIVER: | 
|---|
| 228 | DebugString    = "MIDIERR_NO_DRIVER"; | 
|---|
| 229 | break; | 
|---|
| 230 | case MIDIERR_NO_DEFAULT_HW_NODE: | 
|---|
| 231 | DebugString    = "MIDIERR_NO_DEFAULT_HW_NODE"; | 
|---|
| 232 | break; | 
|---|
| 233 | case MIDIERR_NOT_ALLOWED: | 
|---|
| 234 | DebugString    = "MIDIERR_NOT_ALLOWED"; | 
|---|
| 235 | break; | 
|---|
| 236 | case MIDIERR_NOTIFY_MISSED: | 
|---|
| 237 | DebugString    = "MIDIERR_NOTIFY_MISSED"; | 
|---|
| 238 | break; | 
|---|
| 239 | case MIDIERR_RESOURCE_NOT_AVAILABLE: | 
|---|
| 240 | DebugString    = "MIDIERR_RESOURCE_NOT_AVAILABLE"; | 
|---|
| 241 | break; | 
|---|
| 242 | case MIDIERR_SENDONLY: | 
|---|
| 243 | DebugString    = "MIDIERR_SENDONLY"; | 
|---|
| 244 | break; | 
|---|
| 245 | case MIDIERR_RECEIVEONLY: | 
|---|
| 246 | DebugString    = "MIDIERR_RECEIVEONLY"; | 
|---|
| 247 | break; | 
|---|
| 248 |  | 
|---|
| 249 | default: | 
|---|
| 250 | DebugString    = "Beats Me!"; | 
|---|
| 251 | break; | 
|---|
| 252 | } | 
|---|
| 253 | return DebugString; | 
|---|
| 254 | } | 
|---|
| 255 |  | 
|---|
| 256 | //------------------------------------------------------------------------------ | 
|---|
| 257 | // IRTMidi :: getClasses (private) | 
|---|
| 258 | //------------------------------------------------------------------------------ | 
|---|
| 259 | unsigned long IRTMidi::getClasses() | 
|---|
| 260 | { | 
|---|
| 261 | unsigned long rc; | 
|---|
| 262 | unsigned long nc = 0; | 
|---|
| 263 | PMIDICLASSINFO ci = NULL; | 
|---|
| 264 |  | 
|---|
| 265 | // Query classes and get the Hardware & Application class numbers | 
|---|
| 266 | rc = (*MidiQueryNumClasses)( &nc, 0 ); | 
|---|
| 267 | if ( rc == 0 && nc > 0 ) | 
|---|
| 268 | { | 
|---|
| 269 | ci = new MIDICLASSINFO[ nc ]; | 
|---|
| 270 | rc = (*MidiQueryClassList)( nc, ci, 0 ); | 
|---|
| 271 | } | 
|---|
| 272 | if ( rc == 0 && ci ) | 
|---|
| 273 | { | 
|---|
| 274 | for ( int i = 0; i < nc; i++ ) | 
|---|
| 275 | { | 
|---|
| 276 | if ( strcmp( ci[i].szmClassName, "Application" ) == 0 ) | 
|---|
| 277 | iApplicationClass = ci[i].ulClassNumber; | 
|---|
| 278 | if ( strcmp( ci[i].szmClassName, "Hardware" )    == 0 ) | 
|---|
| 279 | iHardwareClass    = ci[i].ulClassNumber; | 
|---|
| 280 | } | 
|---|
| 281 | } | 
|---|
| 282 | if ( ci ) | 
|---|
| 283 | { | 
|---|
| 284 | delete[] ci; | 
|---|
| 285 | } | 
|---|
| 286 | return rc; | 
|---|
| 287 | } | 
|---|
| 288 |  | 
|---|
| 289 | unsigned long IRTMidi::findFreeInstance() const | 
|---|
| 290 | { | 
|---|
| 291 | unsigned long i; | 
|---|
| 292 | for ( i = 0; i < MAX_INSTANCES; i++ ) | 
|---|
| 293 | { | 
|---|
| 294 | if ( iInstances[ i ] == NULL ) | 
|---|
| 295 | return i; | 
|---|
| 296 | } | 
|---|
| 297 | return MAX_INSTANCES; // should not happen to a dog | 
|---|
| 298 | } | 
|---|
| 299 |  | 
|---|
| 300 | //------------------------------------------------------------------------------ | 
|---|
| 301 | // IRTMidi :: addInstance | 
|---|
| 302 | //------------------------------------------------------------------------------ | 
|---|
| 303 | MINSTANCE IRTMidi::addInstance( IMidiInstance * theInstance, | 
|---|
| 304 | ULONG classNum, char * instanceName ) | 
|---|
| 305 | { | 
|---|
| 306 | unsigned long rc; | 
|---|
| 307 | MINSTANCE newInstance; | 
|---|
| 308 | unsigned long ni = 0; | 
|---|
| 309 | PMIDIINSTANCEINFO ci = NULL; | 
|---|
| 310 |  | 
|---|
| 311 | // Add new instance | 
|---|
| 312 | rc = (*MidiCreateInstance)( classNum, | 
|---|
| 313 | &newInstance, | 
|---|
| 314 | instanceName, | 
|---|
| 315 | 0 ); | 
|---|
| 316 | // Now find it to initialize the Instance Object | 
|---|
| 317 | if ( rc == 0 ) | 
|---|
| 318 | { | 
|---|
| 319 | rc = (*MidiQueryNumInstances)( &ni, 0 ); | 
|---|
| 320 | if ( rc == 0 && ni > 0 ) | 
|---|
| 321 | { | 
|---|
| 322 | ci = new MIDIINSTANCEINFO[ ni ]; | 
|---|
| 323 | rc = (*MidiQueryInstanceList)( ni, ci, 0 ); | 
|---|
| 324 | } | 
|---|
| 325 | } | 
|---|
| 326 | if ( rc == 0 && ci ) | 
|---|
| 327 | { | 
|---|
| 328 | for ( int i = 0; i < ni; i++ ) | 
|---|
| 329 | { | 
|---|
| 330 | if ( ci[i].minstance == newInstance ) | 
|---|
| 331 | { | 
|---|
| 332 | theInstance->setInstanceInfo( &ci[i] ); | 
|---|
| 333 | iInstances[ findFreeInstance() ] = theInstance; | 
|---|
| 334 | } | 
|---|
| 335 | } | 
|---|
| 336 | } | 
|---|
| 337 | if ( ci ) | 
|---|
| 338 | { | 
|---|
| 339 | delete[] ci; | 
|---|
| 340 | } | 
|---|
| 341 | setLastRC( rc ); | 
|---|
| 342 | return newInstance; | 
|---|
| 343 | } | 
|---|
| 344 |  | 
|---|
| 345 | //------------------------------------------------------------------------------ | 
|---|
| 346 | // IRTMidi :: delInstance | 
|---|
| 347 | //------------------------------------------------------------------------------ | 
|---|
| 348 | void IRTMidi::delInstance( IMidiInstance * theInstance ) | 
|---|
| 349 | { | 
|---|
| 350 | unsigned long rc; | 
|---|
| 351 |  | 
|---|
| 352 | // Find instance in array and null it | 
|---|
| 353 | for( unsigned int i = 0; i < MAX_INSTANCES; i++ ) | 
|---|
| 354 | { | 
|---|
| 355 | if ( iInstances[ i ] == theInstance ) | 
|---|
| 356 | { | 
|---|
| 357 | iInstances[ i ] = NULL; | 
|---|
| 358 | } | 
|---|
| 359 | } | 
|---|
| 360 |  | 
|---|
| 361 | // Delete MIDI Instance | 
|---|
| 362 | rc = (*MidiDeleteInstance)( theInstance->instance(), 0 ); | 
|---|
| 363 | setLastRC( rc ); | 
|---|
| 364 | } | 
|---|
| 365 |  | 
|---|
| 366 | //------------------------------------------------------------------------------ | 
|---|
| 367 | // IRTMidi :: delInstances (private) | 
|---|
| 368 | //------------------------------------------------------------------------------ | 
|---|
| 369 | void IRTMidi::delInstances() | 
|---|
| 370 | { | 
|---|
| 371 | // Delete all instances this way as delete of an instance removes | 
|---|
| 372 | // it from the instance list through the miracle of double | 
|---|
| 373 | // dispatching.  See above. | 
|---|
| 374 | for( unsigned int i = 0; i < MAX_INSTANCES; i++ ) | 
|---|
| 375 | { | 
|---|
| 376 | IMidiInstance * ins = iInstances[ i ]; | 
|---|
| 377 | if ( ins ) | 
|---|
| 378 | delete ins; | 
|---|
| 379 | } | 
|---|
| 380 | } | 
|---|
| 381 |  | 
|---|
| 382 | //------------------------------------------------------------------------------ | 
|---|
| 383 | // IRTMidi :: getInstances (hardware only) | 
|---|
| 384 | //------------------------------------------------------------------------------ | 
|---|
| 385 | unsigned long IRTMidi::getInstances() | 
|---|
| 386 | { | 
|---|
| 387 | // Query all Instances | 
|---|
| 388 | unsigned long rc; | 
|---|
| 389 | unsigned long ni = 0; | 
|---|
| 390 | PMIDIINSTANCEINFO ci = NULL; | 
|---|
| 391 |  | 
|---|
| 392 | iNumInInstances  = 0; | 
|---|
| 393 | iNumOutInstances = 0; | 
|---|
| 394 |  | 
|---|
| 395 | // Query instances and post them | 
|---|
| 396 | rc = (*MidiQueryNumInstances)( &ni, 0 ); | 
|---|
| 397 | if ( rc == 0 && ni > 0 ) | 
|---|
| 398 | { | 
|---|
| 399 | ci = new MIDIINSTANCEINFO[ ni ]; | 
|---|
| 400 | rc = (*MidiQueryInstanceList)( ni, ci, 0 ); | 
|---|
| 401 | } | 
|---|
| 402 | if ( rc == 0 && ci ) | 
|---|
| 403 | { | 
|---|
| 404 | for ( int i = 0; i < ni; i++ ) | 
|---|
| 405 | { | 
|---|
| 406 | if ( ci[i].ulClassNumber == hardwareClass() ) | 
|---|
| 407 | { | 
|---|
| 408 | IMidiInstance * ins = new IMidiInstance(); | 
|---|
| 409 | ins ->setInstanceInfo( &ci[i] ); | 
|---|
| 410 | iInstances[ i ] = ins; | 
|---|
| 411 | if ( ins->canSend() ) | 
|---|
| 412 | { | 
|---|
| 413 | iInInstances[ iNumInInstances ] = ins; | 
|---|
| 414 | iNumInInstances++; | 
|---|
| 415 | } | 
|---|
| 416 | if ( ins->canReceive() ) | 
|---|
| 417 | { | 
|---|
| 418 | iOutInstances[ iNumOutInstances ] = ins; | 
|---|
| 419 | iNumOutInstances++; | 
|---|
| 420 | } | 
|---|
| 421 | } | 
|---|
| 422 | } | 
|---|
| 423 | } | 
|---|
| 424 | if ( ci ) | 
|---|
| 425 | { | 
|---|
| 426 | delete[] ci; | 
|---|
| 427 | } | 
|---|
| 428 | return rc; | 
|---|
| 429 | } | 
|---|
| 430 |  | 
|---|
| 431 | void IRTMidi::startTimer() const | 
|---|
| 432 | { | 
|---|
| 433 | ((IRTMidi*)this)->setLastRC( (*MidiTimer)( MIDI_START_TIMER, 0 ) ); | 
|---|
| 434 | } | 
|---|
| 435 |  | 
|---|
| 436 | void IRTMidi::stopTimer() const | 
|---|
| 437 | { | 
|---|
| 438 | ((IRTMidi*)this)->setLastRC( (*MidiTimer)( MIDI_STOP_TIMER, 0 ) ); | 
|---|
| 439 | } | 
|---|
| 440 |  | 
|---|
| 441 | IRTMidi* IRTMidi::instance() | 
|---|
| 442 | { | 
|---|
| 443 | if ( iTheIRTMidiSingleton == NULL ) | 
|---|
| 444 | iTheIRTMidiSingleton = new IRTMidi(); | 
|---|
| 445 | return iTheIRTMidiSingleton; | 
|---|
| 446 | } | 
|---|
| 447 |  | 
|---|
| 448 | void IRTMidiShutdown() | 
|---|
| 449 | { | 
|---|
| 450 | IRTMidi::shutdown(); | 
|---|
| 451 | } | 
|---|
| 452 |  | 
|---|
| 453 | void IRTMidi::shutdown() | 
|---|
| 454 | { | 
|---|
| 455 | if ( iTheIRTMidiSingleton != NULL ) | 
|---|
| 456 | delete iTheIRTMidiSingleton; | 
|---|
| 457 | } | 
|---|
| 458 |  | 
|---|
| 459 | //------------------------------------------------------------------------------ | 
|---|
| 460 | // IMidiInstance::IMidiInstance | 
|---|
| 461 | //------------------------------------------------------------------------------ | 
|---|
| 462 | IMidiInstance::IMidiInstance() | 
|---|
| 463 | { | 
|---|
| 464 | } | 
|---|
| 465 |  | 
|---|
| 466 | //------------------------------------------------------------------------------ | 
|---|
| 467 | // IMidiInstance::~IMidiInstance | 
|---|
| 468 | //------------------------------------------------------------------------------ | 
|---|
| 469 | IMidiInstance::~IMidiInstance() | 
|---|
| 470 | { | 
|---|
| 471 | // Disable send and receive if set | 
|---|
| 472 | if ( isReceive() ) enableReceive( FALSE ); | 
|---|
| 473 | if ( isSend() )     enableSend( FALSE ); | 
|---|
| 474 | // Remove from RTMIDI | 
|---|
| 475 | IRTMIDI->delInstance( this ); | 
|---|
| 476 | } | 
|---|
| 477 |  | 
|---|
| 478 | //------------------------------------------------------------------------------ | 
|---|
| 479 | // IMidiInstance :: setInfo | 
|---|
| 480 | //------------------------------------------------------------------------------ | 
|---|
| 481 | void IMidiInstance::setInstanceInfo( MIDIINSTANCEINFO* i ) | 
|---|
| 482 | { | 
|---|
| 483 | memcpy( &iInfo, i, sizeof( MIDIINSTANCEINFO ) ); | 
|---|
| 484 | } | 
|---|
| 485 |  | 
|---|
| 486 | MINSTANCE IMidiInstance::instance() const | 
|---|
| 487 | { | 
|---|
| 488 | return iInfo.minstance; | 
|---|
| 489 | } | 
|---|
| 490 |  | 
|---|
| 491 | unsigned long IMidiInstance::classNum() const | 
|---|
| 492 | { | 
|---|
| 493 | return iInfo.ulClassNumber; | 
|---|
| 494 | } | 
|---|
| 495 |  | 
|---|
| 496 | char * IMidiInstance::name() | 
|---|
| 497 | { | 
|---|
| 498 | return iInfo.szmInstanceName; | 
|---|
| 499 | } | 
|---|
| 500 |  | 
|---|
| 501 | unsigned long IMidiInstance::numLinks() const | 
|---|
| 502 | { | 
|---|
| 503 | return iInfo.ulNumLinks; | 
|---|
| 504 | } | 
|---|
| 505 |  | 
|---|
| 506 | unsigned long IMidiInstance::attributes() const | 
|---|
| 507 | { | 
|---|
| 508 | return iInfo.ulAttributes; | 
|---|
| 509 | } | 
|---|
| 510 |  | 
|---|
| 511 | BOOL IMidiInstance::isSend() const | 
|---|
| 512 | { | 
|---|
| 513 | return ( iInfo.ulAttributes & MIDI_INST_ATTR_ENABLE_S ); | 
|---|
| 514 | } | 
|---|
| 515 |  | 
|---|
| 516 | IMidiInstance& IMidiInstance::enableSend(BOOL enable) | 
|---|
| 517 | { | 
|---|
| 518 | if (!(isSend() == enable)) | 
|---|
| 519 | { | 
|---|
| 520 | unsigned long rc; | 
|---|
| 521 | if ( enable ) | 
|---|
| 522 | { | 
|---|
| 523 | // Enable send | 
|---|
| 524 | rc = (*IRTMIDI->MidiEnableInstance)( instance(), MIDI_ENABLE_SEND ); | 
|---|
| 525 | if ( rc == 0 ) | 
|---|
| 526 | iInfo.ulAttributes |= MIDI_INST_ATTR_ENABLE_S; | 
|---|
| 527 | } | 
|---|
| 528 | else | 
|---|
| 529 | { | 
|---|
| 530 | // Disable send | 
|---|
| 531 | rc = (*IRTMIDI->MidiDisableInstance)( instance(), MIDI_DISABLE_SEND ); | 
|---|
| 532 | if ( rc == 0 ) | 
|---|
| 533 | iInfo.ulAttributes &= ~MIDI_INST_ATTR_ENABLE_S; | 
|---|
| 534 | } | 
|---|
| 535 | IRTMIDI->setLastRC( rc ); | 
|---|
| 536 | } | 
|---|
| 537 | return *this; | 
|---|
| 538 | } | 
|---|
| 539 |  | 
|---|
| 540 | BOOL IMidiInstance::isReceive() const | 
|---|
| 541 | { | 
|---|
| 542 | return ( iInfo.ulAttributes & MIDI_INST_ATTR_ENABLE_R ); | 
|---|
| 543 | } | 
|---|
| 544 |  | 
|---|
| 545 | IMidiInstance& IMidiInstance::enableReceive(BOOL enable) | 
|---|
| 546 | { | 
|---|
| 547 | if (!(isReceive() == enable)) | 
|---|
| 548 | { | 
|---|
| 549 | unsigned long rc; | 
|---|
| 550 | if ( enable ) | 
|---|
| 551 | { | 
|---|
| 552 | // Enable receive | 
|---|
| 553 | rc = (*IRTMIDI->MidiEnableInstance)( instance(), MIDI_ENABLE_RECEIVE ); | 
|---|
| 554 | if ( rc == 0 ) | 
|---|
| 555 | iInfo.ulAttributes |= MIDI_INST_ATTR_ENABLE_R; | 
|---|
| 556 | } | 
|---|
| 557 | else | 
|---|
| 558 | { | 
|---|
| 559 | // Disable receive | 
|---|
| 560 | rc = (*IRTMIDI->MidiDisableInstance)( instance(), MIDI_DISABLE_RECEIVE ); | 
|---|
| 561 | if ( rc == 0 ) | 
|---|
| 562 | iInfo.ulAttributes &= ~MIDI_INST_ATTR_ENABLE_R; | 
|---|
| 563 | } | 
|---|
| 564 | IRTMIDI->setLastRC( rc ); | 
|---|
| 565 | } | 
|---|
| 566 | return *this; | 
|---|
| 567 | } | 
|---|
| 568 |  | 
|---|
| 569 | BOOL IMidiInstance::canReceive() const | 
|---|
| 570 | { | 
|---|
| 571 | return ( iInfo.ulAttributes & MIDI_INST_ATTR_CAN_RECV ); | 
|---|
| 572 | } | 
|---|
| 573 |  | 
|---|
| 574 | BOOL IMidiInstance::canSend() const | 
|---|
| 575 | { | 
|---|
| 576 | return ( iInfo.ulAttributes & MIDI_INST_ATTR_CAN_SEND ); | 
|---|
| 577 | } | 
|---|
| 578 |  | 
|---|
| 579 | IMidiInstance& IMidiInstance::removeLink(IMidiInstance* toLink) | 
|---|
| 580 | { | 
|---|
| 581 | if ( canSend() && toLink->canReceive() ) | 
|---|
| 582 | { | 
|---|
| 583 | IRTMIDI->setLastRC( | 
|---|
| 584 | (*IRTMIDI->MidiRemoveLink)( instance(), toLink->instance(), | 
|---|
| 585 | 0,       // slot? | 
|---|
| 586 | 0 ) ); | 
|---|
| 587 | } | 
|---|
| 588 | return *this; | 
|---|
| 589 | } | 
|---|
| 590 |  | 
|---|
| 591 | IMidiInstance& IMidiInstance::addLink(IMidiInstance* toLink) | 
|---|
| 592 | { | 
|---|
| 593 | if ( canSend() && toLink->canReceive() ) | 
|---|
| 594 | { | 
|---|
| 595 | if ( !isSend() ) | 
|---|
| 596 | { | 
|---|
| 597 | enableSend(); | 
|---|
| 598 | } | 
|---|
| 599 | if ( !toLink->isReceive() ) | 
|---|
| 600 | { | 
|---|
| 601 | toLink->enableReceive(); | 
|---|
| 602 | } | 
|---|
| 603 | IRTMIDI->setLastRC( | 
|---|
| 604 | (*IRTMIDI->MidiAddLink)( instance(), toLink->instance(), | 
|---|
| 605 | 0,       // slot ? | 
|---|
| 606 | 0 ) ); | 
|---|
| 607 | } | 
|---|
| 608 | return *this; | 
|---|
| 609 | } | 
|---|
| 610 |  | 
|---|
| 611 | //------------------------------------------------------------------------------ | 
|---|
| 612 | // IMidiInstance :: sendMessage | 
|---|
| 613 | //------------------------------------------------------------------------------ | 
|---|
| 614 | void IMidiInstance::sendMessage( UCHAR b1, UCHAR b2, UCHAR b3, UCHAR b4 ) const | 
|---|
| 615 | { | 
|---|
| 616 | // Build message | 
|---|
| 617 | MESSAGE msg; | 
|---|
| 618 | msg.ulSourceInstance = instance(); | 
|---|
| 619 | msg.ulTime           = 0; | 
|---|
| 620 | msg.ulTrack          = 0; | 
|---|
| 621 | msg.msg.abData[0]    = b1; | 
|---|
| 622 | msg.msg.abData[1]    = b2; | 
|---|
| 623 | msg.msg.abData[2]    = b3; | 
|---|
| 624 | msg.msg.abData[3]    = b4; | 
|---|
| 625 | IRTMIDI->setLastRC( (*IRTMIDI->MidiSendMessages)( &msg, 1, 0 ) ); | 
|---|
| 626 | } | 
|---|
| 627 |  | 
|---|
| 628 | //------------------------------------------------------------------------------ | 
|---|
| 629 | // IMidiInstance :: sendMessage | 
|---|
| 630 | //------------------------------------------------------------------------------ | 
|---|
| 631 | void IMidiInstance::sendMessage( ULONG inMsg ) const | 
|---|
| 632 | { | 
|---|
| 633 | // Build message | 
|---|
| 634 | MESSAGE msg; | 
|---|
| 635 | msg.ulSourceInstance = instance(); | 
|---|
| 636 | msg.ulTime           = 0; | 
|---|
| 637 | msg.ulTrack          = 0; | 
|---|
| 638 | msg.msg.ulMessage    = inMsg; | 
|---|
| 639 | IRTMIDI->setLastRC( (*IRTMIDI->MidiSendMessages)( &msg, 1, 0 ) ); | 
|---|
| 640 | } | 
|---|
| 641 |  | 
|---|
| 642 | //------------------------------------------------------------------------------ | 
|---|
| 643 | // IMidiInstance :: sendSysexMessage | 
|---|
| 644 | //------------------------------------------------------------------------------ | 
|---|
| 645 | void IMidiInstance::sendSysexMessage( UCHAR* theMsg, ULONG msgLen ) const | 
|---|
| 646 | { | 
|---|
| 647 | // Build message | 
|---|
| 648 | MESSAGE* msg; | 
|---|
| 649 | ULONG outLen = sizeof(MESSAGE) - 4 + msgLen; | 
|---|
| 650 | msg = (MESSAGE*)malloc( outLen ); | 
|---|
| 651 |  | 
|---|
| 652 | msg->ulSourceInstance = instance(); | 
|---|
| 653 | msg->ulTime           = 0; | 
|---|
| 654 | msg->ulTrack          = 0; | 
|---|
| 655 | memcpy( msg->msg.abData, theMsg, outLen ); | 
|---|
| 656 | IRTMIDI->setLastRC( (*IRTMIDI->MidiSendSysexMessage)( msg, outLen, 0 ) ); | 
|---|
| 657 | free(msg); | 
|---|
| 658 | } | 
|---|
| 659 |  | 
|---|
| 660 | unsigned long IMidiInstance::getMessage( ULONG * pTime, ULONG * pMsg ) | 
|---|
| 661 | { | 
|---|
| 662 | MESSAGE theMessage; | 
|---|
| 663 | unsigned long numUCHARs; | 
|---|
| 664 | unsigned long rc = 0; | 
|---|
| 665 | numUCHARs = sizeof( theMessage ); | 
|---|
| 666 | rc = (*IRTMIDI->MidiRetrieveMessages)( instance(), &theMessage, &numUCHARs, 0 ); | 
|---|
| 667 | if ( rc == 0 ) | 
|---|
| 668 | { | 
|---|
| 669 | *pTime = theMessage.ulTime; | 
|---|
| 670 | *pMsg  = theMessage.msg.ulMessage; | 
|---|
| 671 | } | 
|---|
| 672 | IRTMIDI->setLastRC( rc ); | 
|---|
| 673 | return rc; | 
|---|
| 674 | } | 
|---|
| 675 |  | 
|---|
| 676 | //------------------------------------------------------------------------------ | 
|---|
| 677 | // IAppMidiInstance::IAppMidiInstance | 
|---|
| 678 | //------------------------------------------------------------------------------ | 
|---|
| 679 | unsigned long IAppMidiInstance::appNum = 0; | 
|---|
| 680 |  | 
|---|
| 681 | IAppMidiInstance::IAppMidiInstance( unsigned long attrs ) | 
|---|
| 682 | { | 
|---|
| 683 | iInfo.ulAttributes = attrs; | 
|---|
| 684 | char name[32]; | 
|---|
| 685 | appNum++; | 
|---|
| 686 | sprintf( name, "MIDI App(%d)", appNum ); | 
|---|
| 687 |  | 
|---|
| 688 | // Create application instance | 
|---|
| 689 | MINSTANCE AppInstance = IRTMIDI->addInstance( | 
|---|
| 690 | this, | 
|---|
| 691 | IRTMIDI->applicationClass(), | 
|---|
| 692 | name ); | 
|---|
| 693 | } | 
|---|
| 694 |  | 
|---|
| 695 | IAppMidiInstance::~IAppMidiInstance() | 
|---|
| 696 | { | 
|---|
| 697 | } | 
|---|
| 698 |  | 
|---|
| 699 |  | 
|---|