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