annotate data/midi/rtmidi/RtMidi.cpp @ 560:2e50d95cf621

* restore compileability
author Chris Cannam
date Mon, 23 Feb 2009 11:43:55 +0000
parents 32d156c75df7
children ecce042cc374
rev   line source
Chris@559 1 /**********************************************************************/
Chris@559 2 /*! \class RtMidi
Chris@559 3 \brief An abstract base class for realtime MIDI input/output.
Chris@559 4
Chris@559 5 This class implements some common functionality for the realtime
Chris@559 6 MIDI input/output subclasses RtMidiIn and RtMidiOut.
Chris@559 7
Chris@559 8 RtMidi WWW site: http://music.mcgill.ca/~gary/rtmidi/
Chris@559 9
Chris@559 10 RtMidi: realtime MIDI i/o C++ classes
Chris@559 11 Copyright (c) 2003-2007 Gary P. Scavone
Chris@559 12
Chris@559 13 Permission is hereby granted, free of charge, to any person
Chris@559 14 obtaining a copy of this software and associated documentation files
Chris@559 15 (the "Software"), to deal in the Software without restriction,
Chris@559 16 including without limitation the rights to use, copy, modify, merge,
Chris@559 17 publish, distribute, sublicense, and/or sell copies of the Software,
Chris@559 18 and to permit persons to whom the Software is furnished to do so,
Chris@559 19 subject to the following conditions:
Chris@559 20
Chris@559 21 The above copyright notice and this permission notice shall be
Chris@559 22 included in all copies or substantial portions of the Software.
Chris@559 23
Chris@559 24 Any person wishing to distribute modifications to the Software is
Chris@559 25 requested to send the modifications to the original developer so that
Chris@559 26 they can be incorporated into the canonical version.
Chris@559 27
Chris@559 28 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
Chris@559 29 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
Chris@559 30 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
Chris@559 31 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
Chris@559 32 ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
Chris@559 33 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
Chris@559 34 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Chris@559 35 */
Chris@559 36 /**********************************************************************/
Chris@559 37
Chris@559 38 // RtMidi: Version 1.0.7
Chris@559 39
Chris@559 40 #include "RtMidi.h"
Chris@559 41 #include <sstream>
Chris@559 42
Chris@559 43 //*********************************************************************//
Chris@559 44 // Common RtMidi Definitions
Chris@559 45 //*********************************************************************//
Chris@559 46
Chris@559 47 RtMidi :: RtMidi()
Chris@559 48 : apiData_( 0 ), connected_( false )
Chris@559 49 {
Chris@559 50 }
Chris@559 51
Chris@559 52 void RtMidi :: error( RtError::Type type )
Chris@559 53 {
Chris@559 54 if (type == RtError::WARNING) {
Chris@559 55 std::cerr << '\n' << errorString_ << "\n\n";
Chris@559 56 }
Chris@559 57 else if (type == RtError::DEBUG_WARNING) {
Chris@559 58 #if defined(__RTMIDI_DEBUG__)
Chris@559 59 std::cerr << '\n' << errorString_ << "\n\n";
Chris@559 60 #endif
Chris@559 61 }
Chris@559 62 else {
Chris@559 63 std::cerr << '\n' << errorString_ << "\n\n";
Chris@559 64 throw RtError( errorString_, type );
Chris@559 65 }
Chris@559 66 }
Chris@559 67
Chris@559 68 //*********************************************************************//
Chris@559 69 // Common RtMidiIn Definitions
Chris@559 70 //*********************************************************************//
Chris@559 71
Chris@559 72 RtMidiIn :: RtMidiIn() : RtMidi()
Chris@559 73 {
Chris@559 74 this->initialize();
Chris@559 75 }
Chris@559 76
Chris@559 77 void RtMidiIn :: setCallback( RtMidiCallback callback, void *userData )
Chris@559 78 {
Chris@559 79 if ( inputData_.usingCallback ) {
Chris@559 80 errorString_ = "RtMidiIn::setCallback: a callback function is already set!";
Chris@559 81 error( RtError::WARNING );
Chris@559 82 return;
Chris@559 83 }
Chris@559 84
Chris@559 85 if ( !callback ) {
Chris@559 86 errorString_ = "RtMidiIn::setCallback: callback function value is invalid!";
Chris@559 87 error( RtError::WARNING );
Chris@559 88 return;
Chris@559 89 }
Chris@559 90
Chris@559 91 inputData_.userCallback = (void *) callback;
Chris@559 92 inputData_.userData = userData;
Chris@559 93 inputData_.usingCallback = true;
Chris@559 94 }
Chris@559 95
Chris@559 96 void RtMidiIn :: cancelCallback()
Chris@559 97 {
Chris@559 98 if ( !inputData_.usingCallback ) {
Chris@559 99 errorString_ = "RtMidiIn::cancelCallback: no callback function was set!";
Chris@559 100 error( RtError::WARNING );
Chris@559 101 return;
Chris@559 102 }
Chris@559 103
Chris@559 104 inputData_.userCallback = 0;
Chris@559 105 inputData_.userData = 0;
Chris@559 106 inputData_.usingCallback = false;
Chris@559 107 }
Chris@559 108
Chris@559 109 void RtMidiIn :: setQueueSizeLimit( unsigned int queueSize )
Chris@559 110 {
Chris@559 111 inputData_.queueLimit = queueSize;
Chris@559 112 }
Chris@559 113
Chris@559 114 void RtMidiIn :: ignoreTypes( bool midiSysex, bool midiTime, bool midiSense )
Chris@559 115 {
Chris@559 116 inputData_.ignoreFlags = 0;
Chris@559 117 if ( midiSysex ) inputData_.ignoreFlags = 0x01;
Chris@559 118 if ( midiTime ) inputData_.ignoreFlags |= 0x02;
Chris@559 119 if ( midiSense ) inputData_.ignoreFlags |= 0x04;
Chris@559 120 }
Chris@559 121
Chris@559 122 double RtMidiIn :: getMessage( std::vector<unsigned char> *message )
Chris@559 123 {
Chris@559 124 message->clear();
Chris@559 125
Chris@559 126 if ( inputData_.usingCallback ) {
Chris@559 127 errorString_ = "RtMidiIn::getNextMessage: a user callback is currently set for this port.";
Chris@559 128 error( RtError::WARNING );
Chris@559 129 return 0.0;
Chris@559 130 }
Chris@559 131
Chris@559 132 if ( inputData_.queue.size() == 0 ) return 0.0;
Chris@559 133
Chris@559 134 // Copy queued message to the vector pointer argument and then "pop" it.
Chris@559 135 std::vector<unsigned char> *bytes = &(inputData_.queue.front().bytes);
Chris@559 136 message->assign( bytes->begin(), bytes->end() );
Chris@559 137 double deltaTime = inputData_.queue.front().timeStamp;
Chris@559 138 inputData_.queue.pop();
Chris@559 139
Chris@559 140 return deltaTime;
Chris@559 141 }
Chris@559 142
Chris@559 143 //*********************************************************************//
Chris@559 144 // Common RtMidiOut Definitions
Chris@559 145 //*********************************************************************//
Chris@559 146
Chris@559 147 RtMidiOut :: RtMidiOut() : RtMidi()
Chris@559 148 {
Chris@559 149 this->initialize();
Chris@559 150 }
Chris@559 151
Chris@559 152
Chris@559 153 //*********************************************************************//
Chris@559 154 // API: Macintosh OS-X
Chris@559 155 //*********************************************************************//
Chris@559 156
Chris@559 157 // API information found at:
Chris@559 158 // - http://developer. apple .com/audio/pdf/coreaudio.pdf
Chris@559 159
Chris@559 160 #if defined(__MACOSX_CORE__)
Chris@559 161
Chris@559 162 // The CoreMIDI API is based on the use of a callback function for
Chris@559 163 // MIDI input. We convert the system specific time stamps to delta
Chris@559 164 // time values.
Chris@559 165
Chris@559 166 // OS-X CoreMIDI header files.
Chris@559 167 #include <CoreMIDI/CoreMIDI.h>
Chris@559 168 #include <CoreAudio/HostTime.h>
Chris@559 169
Chris@559 170 // A structure to hold variables related to the CoreMIDI API
Chris@559 171 // implementation.
Chris@559 172 struct CoreMidiData {
Chris@559 173 MIDIClientRef client;
Chris@559 174 MIDIPortRef port;
Chris@559 175 MIDIEndpointRef endpoint;
Chris@559 176 MIDIEndpointRef destinationId;
Chris@559 177 unsigned long long lastTime;
Chris@559 178 };
Chris@559 179
Chris@559 180 //*********************************************************************//
Chris@559 181 // API: OS-X
Chris@559 182 // Class Definitions: RtMidiIn
Chris@559 183 //*********************************************************************//
Chris@559 184
Chris@559 185 void midiInputCallback( const MIDIPacketList *list, void *procRef, void *srcRef )
Chris@559 186 {
Chris@559 187 RtMidiIn::RtMidiInData *data = static_cast<RtMidiIn::RtMidiInData *> (procRef);
Chris@559 188 CoreMidiData *apiData = static_cast<CoreMidiData *> (data->apiData);
Chris@559 189
Chris@559 190 bool continueSysex = false;
Chris@559 191 unsigned char status;
Chris@559 192 unsigned short nBytes, iByte, size;
Chris@559 193 unsigned long long time;
Chris@559 194 RtMidiIn::MidiMessage message;
Chris@559 195 const MIDIPacket *packet = &list->packet[0];
Chris@559 196 for ( unsigned int i=0; i<list->numPackets; ++i ) {
Chris@559 197
Chris@559 198 // My interpretation of the CoreMIDI documentation: all message
Chris@559 199 // types, except sysex, are complete within a packet and there may
Chris@559 200 // be several of them in a single packet. Sysex messages can be
Chris@559 201 // broken across multiple packets but are bundled alone within a
Chris@559 202 // packet. I'm assuming that sysex messages, if segmented, must
Chris@559 203 // be complete within the same MIDIPacketList.
Chris@559 204
Chris@559 205 nBytes = packet->length;
Chris@559 206 if ( nBytes == 0 ) continue;
Chris@559 207
Chris@559 208 // Calculate time stamp.
Chris@559 209 message.timeStamp = 0.0;
Chris@559 210 if ( data->firstMessage )
Chris@559 211 data->firstMessage = false;
Chris@559 212 else {
Chris@559 213 time = packet->timeStamp;
Chris@559 214 time -= apiData->lastTime;
Chris@559 215 time = AudioConvertHostTimeToNanos( time );
Chris@559 216 message.timeStamp = time * 0.000000001;
Chris@559 217 }
Chris@559 218 apiData->lastTime = packet->timeStamp;
Chris@559 219
Chris@559 220 iByte = 0;
Chris@559 221 if ( continueSysex ) {
Chris@559 222 // We have a continuing, segmented sysex message.
Chris@559 223 if ( !(data->ignoreFlags & 0x01) ) {
Chris@559 224 // If we're not ignoring sysex messages, copy the entire packet.
Chris@559 225 for ( unsigned int j=0; j<nBytes; j++ )
Chris@559 226 message.bytes.push_back( packet->data[j] );
Chris@559 227 }
Chris@559 228 if ( packet->data[nBytes] == 0xF7 ) continueSysex = false;
Chris@559 229 if ( !continueSysex ) {
Chris@559 230 // If not a continuing sysex message, invoke the user callback function or queue the message.
Chris@559 231 if ( data->usingCallback && message.bytes.size() > 0 ) {
Chris@559 232 RtMidiIn::RtMidiCallback callback = (RtMidiIn::RtMidiCallback) data->userCallback;
Chris@559 233 callback( message.timeStamp, &message.bytes, data->userData );
Chris@559 234 }
Chris@559 235 else {
Chris@559 236 // As long as we haven't reached our queue size limit, push the message.
Chris@559 237 if ( data->queueLimit > data->queue.size() )
Chris@559 238 data->queue.push( message );
Chris@559 239 else
Chris@559 240 std::cerr << "\nRtMidiIn: message queue limit reached!!\n\n";
Chris@559 241 }
Chris@559 242 message.bytes.clear();
Chris@559 243 }
Chris@559 244 }
Chris@559 245 else {
Chris@559 246 while ( iByte < nBytes ) {
Chris@559 247 size = 0;
Chris@559 248 // We are expecting that the next byte in the packet is a status byte.
Chris@559 249 status = packet->data[iByte];
Chris@559 250 if ( !(status & 0x80) ) break;
Chris@559 251 // Determine the number of bytes in the MIDI message.
Chris@559 252 if ( status < 0xC0 ) size = 3;
Chris@559 253 else if ( status < 0xE0 ) size = 2;
Chris@559 254 else if ( status < 0xF0 ) size = 3;
Chris@559 255 else if ( status == 0xF0 ) {
Chris@559 256 // A MIDI sysex
Chris@559 257 if ( data->ignoreFlags & 0x01 ) {
Chris@559 258 size = 0;
Chris@559 259 iByte = nBytes;
Chris@559 260 }
Chris@559 261 else size = nBytes - iByte;
Chris@559 262 if ( packet->data[nBytes] == 0xF7 ) continueSysex = false;
Chris@559 263 }
Chris@559 264 else if ( status < 0xF3 ) {
Chris@559 265 if ( status == 0xF1 && (data->ignoreFlags & 0x02) ) {
Chris@559 266 // A MIDI time code message and we're ignoring it.
Chris@559 267 size = 0;
Chris@559 268 iByte += 3;
Chris@559 269 }
Chris@559 270 else size = 3;
Chris@559 271 }
Chris@559 272 else if ( status == 0xF3 ) size = 2;
Chris@559 273 else if ( status == 0xF8 ) {
Chris@559 274 size = 1;
Chris@559 275 if ( data->ignoreFlags & 0x02 ) {
Chris@559 276 // A MIDI timing tick message and we're ignoring it.
Chris@559 277 size = 0;
Chris@559 278 iByte += 3;
Chris@559 279 }
Chris@559 280 }
Chris@559 281 else if ( status == 0xFE && (data->ignoreFlags & 0x04) ) {
Chris@559 282 // A MIDI active sensing message and we're ignoring it.
Chris@559 283 size = 0;
Chris@559 284 iByte += 1;
Chris@559 285 }
Chris@559 286 else size = 1;
Chris@559 287
Chris@559 288 // Copy the MIDI data to our vector.
Chris@559 289 if ( size ) {
Chris@559 290 message.bytes.assign( &packet->data[iByte], &packet->data[iByte+size] );
Chris@559 291 if ( !continueSysex ) {
Chris@559 292 // If not a continuing sysex message, invoke the user callback function or queue the message.
Chris@559 293 if ( data->usingCallback ) {
Chris@559 294 RtMidiIn::RtMidiCallback callback = (RtMidiIn::RtMidiCallback) data->userCallback;
Chris@559 295 callback( message.timeStamp, &message.bytes, data->userData );
Chris@559 296 }
Chris@559 297 else {
Chris@559 298 // As long as we haven't reached our queue size limit, push the message.
Chris@559 299 if ( data->queueLimit > data->queue.size() )
Chris@559 300 data->queue.push( message );
Chris@559 301 else
Chris@559 302 std::cerr << "\nRtMidiIn: message queue limit reached!!\n\n";
Chris@559 303 }
Chris@559 304 message.bytes.clear();
Chris@559 305 }
Chris@559 306 iByte += size;
Chris@559 307 }
Chris@559 308 }
Chris@559 309 }
Chris@559 310 packet = MIDIPacketNext(packet);
Chris@559 311 }
Chris@559 312 }
Chris@559 313
Chris@559 314 void RtMidiIn :: initialize( void )
Chris@559 315 {
Chris@559 316 // Set up our client.
Chris@559 317 MIDIClientRef client;
Chris@559 318 OSStatus result = MIDIClientCreate( CFSTR("RtMidi Input Client"), NULL, NULL, &client );
Chris@559 319 if ( result != noErr ) {
Chris@559 320 errorString_ = "RtMidiIn::initialize: error creating OS-X MIDI client object.";
Chris@559 321 error( RtError::DRIVER_ERROR );
Chris@559 322 }
Chris@559 323
Chris@559 324 // Save our api-specific connection information.
Chris@559 325 CoreMidiData *data = (CoreMidiData *) new CoreMidiData;
Chris@559 326 data->client = client;
Chris@559 327 data->endpoint = 0;
Chris@559 328 apiData_ = (void *) data;
Chris@559 329 inputData_.apiData = (void *) data;
Chris@559 330 }
Chris@559 331
Chris@559 332 void RtMidiIn :: openPort( unsigned int portNumber )
Chris@559 333 {
Chris@559 334 if ( connected_ ) {
Chris@559 335 errorString_ = "RtMidiIn::openPort: a valid connection already exists!";
Chris@559 336 error( RtError::WARNING );
Chris@559 337 return;
Chris@559 338 }
Chris@559 339
Chris@559 340 unsigned int nSrc = MIDIGetNumberOfSources();
Chris@559 341 if (nSrc < 1) {
Chris@559 342 errorString_ = "RtMidiIn::openPort: no MIDI input sources found!";
Chris@559 343 error( RtError::NO_DEVICES_FOUND );
Chris@559 344 }
Chris@559 345
Chris@559 346 std::ostringstream ost;
Chris@559 347 if ( portNumber >= nSrc ) {
Chris@559 348 ost << "RtMidiIn::openPort: the 'portNumber' argument (" << portNumber << ") is invalid.";
Chris@559 349 errorString_ = ost.str();
Chris@559 350 error( RtError::INVALID_PARAMETER );
Chris@559 351 }
Chris@559 352
Chris@559 353 MIDIPortRef port;
Chris@559 354 CoreMidiData *data = static_cast<CoreMidiData *> (apiData_);
Chris@559 355 OSStatus result = MIDIInputPortCreate( data->client, CFSTR("RtMidi MIDI Input Port"), midiInputCallback, (void *)&inputData_, &port );
Chris@559 356 if ( result != noErr ) {
Chris@559 357 MIDIClientDispose( data->client );
Chris@559 358 errorString_ = "RtMidiIn::openPort: error creating OS-X MIDI input port.";
Chris@559 359 error( RtError::DRIVER_ERROR );
Chris@559 360 }
Chris@559 361
Chris@559 362 // Get the desired input source identifier.
Chris@559 363 MIDIEndpointRef endpoint = MIDIGetSource( portNumber );
Chris@559 364 if ( endpoint == NULL ) {
Chris@559 365 MIDIPortDispose( port );
Chris@559 366 MIDIClientDispose( data->client );
Chris@559 367 errorString_ = "RtMidiIn::openPort: error getting MIDI input source reference.";
Chris@559 368 error( RtError::DRIVER_ERROR );
Chris@559 369 }
Chris@559 370
Chris@559 371 // Make the connection.
Chris@559 372 result = MIDIPortConnectSource( port, endpoint, NULL );
Chris@559 373 if ( result != noErr ) {
Chris@559 374 MIDIPortDispose( port );
Chris@559 375 MIDIClientDispose( data->client );
Chris@559 376 errorString_ = "RtMidiIn::openPort: error connecting OS-X MIDI input port.";
Chris@559 377 error( RtError::DRIVER_ERROR );
Chris@559 378 }
Chris@559 379
Chris@559 380 // Save our api-specific port information.
Chris@559 381 data->port = port;
Chris@559 382
Chris@559 383 connected_ = true;
Chris@559 384 }
Chris@559 385
Chris@559 386 void RtMidiIn :: openVirtualPort( const std::string portName )
Chris@559 387 {
Chris@559 388 CoreMidiData *data = static_cast<CoreMidiData *> (apiData_);
Chris@559 389
Chris@559 390 // Create a virtual MIDI input destination.
Chris@559 391 MIDIEndpointRef endpoint;
Chris@559 392 OSStatus result = MIDIDestinationCreate( data->client,
Chris@559 393 CFStringCreateWithCString( NULL, portName.c_str(), kCFStringEncodingASCII ),
Chris@559 394 midiInputCallback, (void *)&inputData_, &endpoint );
Chris@559 395 if ( result != noErr ) {
Chris@559 396 errorString_ = "RtMidiIn::openVirtualPort: error creating virtual OS-X MIDI destination.";
Chris@559 397 error( RtError::DRIVER_ERROR );
Chris@559 398 }
Chris@559 399
Chris@559 400 // Save our api-specific connection information.
Chris@559 401 data->endpoint = endpoint;
Chris@559 402 }
Chris@559 403
Chris@559 404 void RtMidiIn :: closePort( void )
Chris@559 405 {
Chris@559 406 if ( connected_ ) {
Chris@559 407 CoreMidiData *data = static_cast<CoreMidiData *> (apiData_);
Chris@559 408 MIDIPortDispose( data->port );
Chris@559 409 connected_ = false;
Chris@559 410 }
Chris@559 411 }
Chris@559 412
Chris@559 413 RtMidiIn :: ~RtMidiIn()
Chris@559 414 {
Chris@559 415 // Close a connection if it exists.
Chris@559 416 closePort();
Chris@559 417
Chris@559 418 // Cleanup.
Chris@559 419 CoreMidiData *data = static_cast<CoreMidiData *> (apiData_);
Chris@559 420 MIDIClientDispose( data->client );
Chris@559 421 if ( data->endpoint ) MIDIEndpointDispose( data->endpoint );
Chris@559 422 delete data;
Chris@559 423 }
Chris@559 424
Chris@559 425 unsigned int RtMidiIn :: getPortCount()
Chris@559 426 {
Chris@559 427 return MIDIGetNumberOfSources();
Chris@559 428 }
Chris@559 429
Chris@559 430 std::string RtMidiIn :: getPortName( unsigned int portNumber )
Chris@559 431 {
Chris@559 432 CFStringRef nameRef;
Chris@559 433 MIDIEndpointRef portRef;
Chris@559 434 std::ostringstream ost;
Chris@559 435 char name[128];
Chris@559 436
Chris@559 437 if ( portNumber >= MIDIGetNumberOfSources() ) {
Chris@559 438 ost << "RtMidiIn::getPortName: the 'portNumber' argument (" << portNumber << ") is invalid.";
Chris@559 439 errorString_ = ost.str();
Chris@559 440 error( RtError::INVALID_PARAMETER );
Chris@559 441 }
Chris@559 442 portRef = MIDIGetSource( portNumber );
Chris@559 443
Chris@559 444 MIDIObjectGetStringProperty( portRef, kMIDIPropertyName, &nameRef );
Chris@559 445 CFStringGetCString( nameRef, name, sizeof(name), 0);
Chris@559 446 CFRelease( nameRef );
Chris@559 447 std::string stringName = name;
Chris@559 448 return stringName;
Chris@559 449 }
Chris@559 450
Chris@559 451 //*********************************************************************//
Chris@559 452 // API: OS-X
Chris@559 453 // Class Definitions: RtMidiOut
Chris@559 454 //*********************************************************************//
Chris@559 455
Chris@559 456 unsigned int RtMidiOut :: getPortCount()
Chris@559 457 {
Chris@559 458 return MIDIGetNumberOfDestinations();
Chris@559 459 }
Chris@559 460
Chris@559 461 std::string RtMidiOut :: getPortName( unsigned int portNumber )
Chris@559 462 {
Chris@559 463 CFStringRef nameRef;
Chris@559 464 MIDIEndpointRef portRef;
Chris@559 465 std::ostringstream ost;
Chris@559 466 char name[128];
Chris@559 467
Chris@559 468 if ( portNumber >= MIDIGetNumberOfDestinations() ) {
Chris@559 469 ost << "RtMidiOut::getPortName: the 'portNumber' argument (" << portNumber << ") is invalid.";
Chris@559 470 errorString_ = ost.str();
Chris@559 471 error( RtError::INVALID_PARAMETER );
Chris@559 472 }
Chris@559 473 portRef = MIDIGetDestination( portNumber );
Chris@559 474
Chris@559 475 MIDIObjectGetStringProperty( portRef, kMIDIPropertyName, &nameRef );
Chris@559 476 CFStringGetCString( nameRef, name, sizeof(name), 0);
Chris@559 477 CFRelease( nameRef );
Chris@559 478 std::string stringName = name;
Chris@559 479 return stringName;
Chris@559 480 }
Chris@559 481
Chris@559 482 void RtMidiOut :: initialize( void )
Chris@559 483 {
Chris@559 484 // Set up our client.
Chris@559 485 MIDIClientRef client;
Chris@559 486 OSStatus result = MIDIClientCreate( CFSTR("RtMidi Output Client"), NULL, NULL, &client );
Chris@559 487 if ( result != noErr ) {
Chris@559 488 errorString_ = "RtMidiOut::initialize: error creating OS-X MIDI client object.";
Chris@559 489 error( RtError::DRIVER_ERROR );
Chris@559 490 }
Chris@559 491
Chris@559 492 // Save our api-specific connection information.
Chris@559 493 CoreMidiData *data = (CoreMidiData *) new CoreMidiData;
Chris@559 494 data->client = client;
Chris@559 495 data->endpoint = 0;
Chris@559 496 apiData_ = (void *) data;
Chris@559 497 }
Chris@559 498
Chris@559 499 void RtMidiOut :: openPort( unsigned int portNumber )
Chris@559 500 {
Chris@559 501 if ( connected_ ) {
Chris@559 502 errorString_ = "RtMidiOut::openPort: a valid connection already exists!";
Chris@559 503 error( RtError::WARNING );
Chris@559 504 return;
Chris@559 505 }
Chris@559 506
Chris@559 507 unsigned int nDest = MIDIGetNumberOfDestinations();
Chris@559 508 if (nDest < 1) {
Chris@559 509 errorString_ = "RtMidiOut::openPort: no MIDI output destinations found!";
Chris@559 510 error( RtError::NO_DEVICES_FOUND );
Chris@559 511 }
Chris@559 512
Chris@559 513 std::ostringstream ost;
Chris@559 514 if ( portNumber >= nDest ) {
Chris@559 515 ost << "RtMidiOut::openPort: the 'portNumber' argument (" << portNumber << ") is invalid.";
Chris@559 516 errorString_ = ost.str();
Chris@559 517 error( RtError::INVALID_PARAMETER );
Chris@559 518 }
Chris@559 519
Chris@559 520 MIDIPortRef port;
Chris@559 521 CoreMidiData *data = static_cast<CoreMidiData *> (apiData_);
Chris@559 522 OSStatus result = MIDIOutputPortCreate( data->client, CFSTR("RtMidi Virtual MIDI Output Port"), &port );
Chris@559 523 if ( result != noErr ) {
Chris@559 524 MIDIClientDispose( data->client );
Chris@559 525 errorString_ = "RtMidiOut::openPort: error creating OS-X MIDI output port.";
Chris@559 526 error( RtError::DRIVER_ERROR );
Chris@559 527 }
Chris@559 528
Chris@559 529 // Get the desired output port identifier.
Chris@559 530 MIDIEndpointRef destination = MIDIGetDestination( portNumber );
Chris@559 531 if ( destination == NULL ) {
Chris@559 532 MIDIPortDispose( port );
Chris@559 533 MIDIClientDispose( data->client );
Chris@559 534 errorString_ = "RtMidiOut::openPort: error getting MIDI output destination reference.";
Chris@559 535 error( RtError::DRIVER_ERROR );
Chris@559 536 }
Chris@559 537
Chris@559 538 // Save our api-specific connection information.
Chris@559 539 data->port = port;
Chris@559 540 data->destinationId = destination;
Chris@559 541 connected_ = true;
Chris@559 542 }
Chris@559 543
Chris@559 544 void RtMidiOut :: closePort( void )
Chris@559 545 {
Chris@559 546 if ( connected_ ) {
Chris@559 547 CoreMidiData *data = static_cast<CoreMidiData *> (apiData_);
Chris@559 548 MIDIPortDispose( data->port );
Chris@559 549 connected_ = false;
Chris@559 550 }
Chris@559 551 }
Chris@559 552
Chris@559 553 void RtMidiOut :: openVirtualPort( std::string portName )
Chris@559 554 {
Chris@559 555 CoreMidiData *data = static_cast<CoreMidiData *> (apiData_);
Chris@559 556
Chris@559 557 if ( data->endpoint ) {
Chris@559 558 errorString_ = "RtMidiOut::openVirtualPort: a virtual output port already exists!";
Chris@559 559 error( RtError::WARNING );
Chris@559 560 return;
Chris@559 561 }
Chris@559 562
Chris@559 563 // Create a virtual MIDI output source.
Chris@559 564 MIDIEndpointRef endpoint;
Chris@559 565 OSStatus result = MIDISourceCreate( data->client,
Chris@559 566 CFStringCreateWithCString( NULL, portName.c_str(), kCFStringEncodingASCII ),
Chris@559 567 &endpoint );
Chris@559 568 if ( result != noErr ) {
Chris@559 569 errorString_ = "RtMidiOut::initialize: error creating OS-X virtual MIDI source.";
Chris@559 570 error( RtError::DRIVER_ERROR );
Chris@559 571 }
Chris@559 572
Chris@559 573 // Save our api-specific connection information.
Chris@559 574 data->endpoint = endpoint;
Chris@559 575 }
Chris@559 576
Chris@559 577 RtMidiOut :: ~RtMidiOut()
Chris@559 578 {
Chris@559 579 // Close a connection if it exists.
Chris@559 580 closePort();
Chris@559 581
Chris@559 582 // Cleanup.
Chris@559 583 CoreMidiData *data = static_cast<CoreMidiData *> (apiData_);
Chris@559 584 MIDIClientDispose( data->client );
Chris@559 585 if ( data->endpoint ) MIDIEndpointDispose( data->endpoint );
Chris@559 586 delete data;
Chris@559 587 }
Chris@559 588
Chris@559 589 void RtMidiOut :: sendMessage( std::vector<unsigned char> *message )
Chris@559 590 {
Chris@559 591 unsigned int nBytes = message->size();
Chris@559 592 // Pad the buffer for extra (unknown) structure data.
Chris@559 593 Byte buffer[nBytes+32];
Chris@559 594 MIDIPacketList *pktlist = (MIDIPacketList *) buffer;
Chris@559 595 MIDIPacket *curPacket = MIDIPacketListInit( pktlist );
Chris@559 596
Chris@559 597 MIDITimeStamp timeStamp = 0;
Chris@559 598 curPacket = MIDIPacketListAdd( pktlist, sizeof(buffer), curPacket, timeStamp, nBytes, &message->at(0) );
Chris@559 599
Chris@559 600 CoreMidiData *data = static_cast<CoreMidiData *> (apiData_);
Chris@559 601
Chris@559 602 // Send to any destinations that may have connected to us.
Chris@559 603 OSStatus result;
Chris@559 604 if ( data->endpoint ) {
Chris@559 605 result = MIDIReceived( data->endpoint, pktlist );
Chris@559 606 if ( result != noErr ) {
Chris@559 607 errorString_ = "RtMidiOut::sendMessage: error sending MIDI to virtual destinations.";
Chris@559 608 error( RtError::WARNING );
Chris@559 609 }
Chris@559 610 }
Chris@559 611
Chris@559 612 // And send to an explicit destination port if we're connected.
Chris@559 613 if ( connected_ ) {
Chris@559 614 result = MIDISend( data->port, data->destinationId, pktlist );
Chris@559 615 if ( result != noErr ) {
Chris@559 616 errorString_ = "RtMidiOut::sendMessage: error sending MIDI message to port.";
Chris@559 617 error( RtError::WARNING );
Chris@559 618 }
Chris@559 619 }
Chris@559 620 }
Chris@559 621
Chris@559 622 #endif // __MACOSX_CORE__
Chris@559 623
Chris@559 624
Chris@559 625 //*********************************************************************//
Chris@559 626 // API: LINUX ALSA SEQUENCER
Chris@559 627 //*********************************************************************//
Chris@559 628
Chris@559 629 // API information found at:
Chris@559 630 // - http://www.alsa-project.org/documentation.php#Library
Chris@559 631
Chris@559 632 #if defined(__LINUX_ALSASEQ__)
Chris@559 633
Chris@559 634 // The ALSA Sequencer API is based on the use of a callback function for
Chris@559 635 // MIDI input.
Chris@559 636 //
Chris@559 637 // Thanks to Pedro Lopez-Cabanillas for help with the ALSA sequencer
Chris@559 638 // time stamps and other assorted fixes!!!
Chris@559 639
Chris@559 640 #include <pthread.h>
Chris@559 641 #include <sys/time.h>
Chris@559 642
Chris@559 643 // ALSA header file.
Chris@559 644 #include <alsa/asoundlib.h>
Chris@559 645
Chris@559 646 // A structure to hold variables related to the ALSA API
Chris@559 647 // implementation.
Chris@559 648 struct AlsaMidiData {
Chris@559 649 snd_seq_t *seq;
Chris@559 650 int vport;
Chris@559 651 snd_seq_port_subscribe_t *subscription;
Chris@559 652 snd_midi_event_t *coder;
Chris@559 653 unsigned int bufferSize;
Chris@559 654 unsigned char *buffer;
Chris@559 655 pthread_t thread;
Chris@559 656 unsigned long long lastTime;
Chris@559 657 int queue_id; // an input queue is needed to get timestamped events
Chris@559 658 };
Chris@559 659
Chris@559 660 #define PORT_TYPE( pinfo, bits ) ((snd_seq_port_info_get_capability(pinfo) & (bits)) == (bits))
Chris@559 661
Chris@559 662 //*********************************************************************//
Chris@559 663 // API: LINUX ALSA
Chris@559 664 // Class Definitions: RtMidiIn
Chris@559 665 //*********************************************************************//
Chris@559 666
Chris@559 667 extern "C" void *alsaMidiHandler( void *ptr )
Chris@559 668 {
Chris@559 669 RtMidiIn::RtMidiInData *data = static_cast<RtMidiIn::RtMidiInData *> (ptr);
Chris@559 670 AlsaMidiData *apiData = static_cast<AlsaMidiData *> (data->apiData);
Chris@559 671
Chris@559 672 long nBytes;
Chris@559 673 unsigned long long time, lastTime;
Chris@559 674 bool continueSysex = false;
Chris@559 675 RtMidiIn::MidiMessage message;
Chris@559 676
Chris@559 677 snd_seq_event_t *ev;
Chris@559 678 int result;
Chris@559 679 apiData->bufferSize = 32;
Chris@559 680 result = snd_midi_event_new( 0, &apiData->coder );
Chris@559 681 if ( result < 0 ) {
Chris@559 682 data->doInput = false;
Chris@559 683 std::cerr << "\nRtMidiIn::alsaMidiHandler: error initializing MIDI event parser!\n\n";
Chris@559 684 return 0;
Chris@559 685 }
Chris@559 686 unsigned char *buffer = (unsigned char *) malloc( apiData->bufferSize );
Chris@559 687 if ( buffer == NULL ) {
Chris@559 688 data->doInput = false;
Chris@559 689 std::cerr << "\nRtMidiIn::alsaMidiHandler: error initializing buffer memory!\n\n";
Chris@559 690 return 0;
Chris@559 691 }
Chris@559 692 snd_midi_event_init( apiData->coder );
Chris@559 693 snd_midi_event_no_status( apiData->coder, 1 ); // suppress running status messages
Chris@559 694
Chris@559 695 while ( data->doInput ) {
Chris@559 696
Chris@559 697 if ( snd_seq_event_input_pending( apiData->seq, 1 ) == 0 ) {
Chris@559 698 // No data pending ... sleep a bit.
Chris@559 699 usleep( 1000 );
Chris@559 700 continue;
Chris@559 701 }
Chris@559 702
Chris@559 703 // If here, there should be data.
Chris@559 704 result = snd_seq_event_input( apiData->seq, &ev );
Chris@559 705 if ( result == -ENOSPC ) {
Chris@559 706 std::cerr << "\nRtMidiIn::alsaMidiHandler: MIDI input buffer overrun!\n\n";
Chris@559 707 continue;
Chris@559 708 }
Chris@559 709 else if ( result <= 0 ) {
Chris@559 710 std::cerr << "RtMidiIn::alsaMidiHandler: unknown MIDI input error!\n";
Chris@559 711 continue;
Chris@559 712 }
Chris@559 713
Chris@559 714 // This is a bit weird, but we now have to decode an ALSA MIDI
Chris@559 715 // event (back) into MIDI bytes. We'll ignore non-MIDI types.
Chris@559 716 message.bytes.clear();
Chris@559 717 switch ( ev->type ) {
Chris@559 718
Chris@559 719 case SND_SEQ_EVENT_PORT_SUBSCRIBED:
Chris@559 720 #if defined(__RTMIDI_DEBUG__)
Chris@559 721 std::cout << "RtMidiIn::alsaMidiHandler: port connection made!\n";
Chris@559 722 #endif
Chris@559 723 break;
Chris@559 724
Chris@559 725 case SND_SEQ_EVENT_PORT_UNSUBSCRIBED:
Chris@559 726 std::cerr << "RtMidiIn::alsaMidiHandler: port connection has closed!\n";
Chris@559 727 data->doInput = false;
Chris@559 728 break;
Chris@559 729
Chris@559 730 case SND_SEQ_EVENT_QFRAME: // MIDI time code
Chris@559 731 if ( data->ignoreFlags & 0x02 ) break;
Chris@559 732
Chris@559 733 case SND_SEQ_EVENT_TICK: // MIDI timing tick
Chris@559 734 if ( data->ignoreFlags & 0x02 ) break;
Chris@559 735
Chris@559 736 case SND_SEQ_EVENT_SENSING: // Active sensing
Chris@559 737 if ( data->ignoreFlags & 0x04 ) break;
Chris@559 738
Chris@559 739 case SND_SEQ_EVENT_SYSEX:
Chris@559 740 if ( (data->ignoreFlags & 0x01) ) break;
Chris@559 741 if ( ev->data.ext.len > apiData->bufferSize ) {
Chris@559 742 apiData->bufferSize = ev->data.ext.len;
Chris@559 743 free( buffer );
Chris@559 744 buffer = (unsigned char *) malloc( apiData->bufferSize );
Chris@559 745 if ( buffer == NULL ) {
Chris@559 746 data->doInput = false;
Chris@559 747 std::cerr << "\nRtMidiIn::alsaMidiHandler: error resizing buffer memory!\n\n";
Chris@559 748 break;
Chris@559 749 }
Chris@559 750 }
Chris@559 751
Chris@559 752 default:
Chris@559 753 nBytes = snd_midi_event_decode( apiData->coder, buffer, apiData->bufferSize, ev );
Chris@559 754 if ( nBytes <= 0 ) {
Chris@559 755 #if defined(__RTMIDI_DEBUG__)
Chris@559 756 std::cerr << "\nRtMidiIn::alsaMidiHandler: event parsing error or not a MIDI event!\n\n";
Chris@559 757 #endif
Chris@559 758 break;
Chris@559 759 }
Chris@559 760
Chris@559 761 // The ALSA sequencer has a maximum buffer size for MIDI sysex
Chris@559 762 // events of 256 bytes. If a device sends sysex messages larger
Chris@559 763 // than this, they are segmented into 256 byte chunks. So,
Chris@559 764 // we'll watch for this and concatenate sysex chunks into a
Chris@559 765 // single sysex message if necessary.
Chris@559 766 if ( !continueSysex )
Chris@559 767 message.bytes.assign( buffer, &buffer[nBytes] );
Chris@559 768 else
Chris@559 769 message.bytes.insert( message.bytes.end(), buffer, &buffer[nBytes] );
Chris@559 770
Chris@559 771 continueSysex = ( ev->type == SND_SEQ_EVENT_SYSEX && message.bytes.back() != 0xF7 );
Chris@559 772 if ( continueSysex )
Chris@559 773 break;
Chris@559 774
Chris@559 775 // Calculate the time stamp:
Chris@559 776 message.timeStamp = 0.0;
Chris@559 777
Chris@559 778 // Method 1: Use the system time.
Chris@559 779 //(void)gettimeofday(&tv, (struct timezone *)NULL);
Chris@559 780 //time = (tv.tv_sec * 1000000) + tv.tv_usec;
Chris@559 781
Chris@559 782 // Method 2: Use the ALSA sequencer event time data.
Chris@559 783 // (thanks to Pedro Lopez-Cabanillas!).
Chris@559 784 time = ( ev->time.time.tv_sec * 1000000 ) + ( ev->time.time.tv_nsec/1000 );
Chris@559 785 lastTime = time;
Chris@559 786 time -= apiData->lastTime;
Chris@559 787 apiData->lastTime = lastTime;
Chris@559 788 if ( data->firstMessage == true )
Chris@559 789 data->firstMessage = false;
Chris@559 790 else
Chris@559 791 message.timeStamp = time * 0.000001;
Chris@559 792 }
Chris@559 793
Chris@559 794 snd_seq_free_event(ev);
Chris@559 795 if ( message.bytes.size() == 0 ) continue;
Chris@559 796
Chris@559 797 if ( data->usingCallback ) {
Chris@559 798 RtMidiIn::RtMidiCallback callback = (RtMidiIn::RtMidiCallback) data->userCallback;
Chris@559 799 callback( message.timeStamp, &message.bytes, data->userData );
Chris@559 800 }
Chris@559 801 else {
Chris@559 802 // As long as we haven't reached our queue size limit, push the message.
Chris@559 803 if ( data->queueLimit > data->queue.size() )
Chris@559 804 data->queue.push( message );
Chris@559 805 else
Chris@559 806 std::cerr << "\nRtMidiIn: message queue limit reached!!\n\n";
Chris@559 807 }
Chris@559 808 }
Chris@559 809
Chris@559 810 if ( buffer ) free( buffer );
Chris@559 811 snd_midi_event_free( apiData->coder );
Chris@559 812 apiData->coder = 0;
Chris@559 813 return 0;
Chris@559 814 }
Chris@559 815
Chris@559 816 void RtMidiIn :: initialize( void )
Chris@559 817 {
Chris@559 818 // Set up the ALSA sequencer client.
Chris@559 819 snd_seq_t *seq;
Chris@559 820 int result = snd_seq_open(&seq, "default", SND_SEQ_OPEN_DUPLEX, SND_SEQ_NONBLOCK);
Chris@559 821 if ( result < 0 ) {
Chris@559 822 errorString_ = "RtMidiIn::initialize: error creating ALSA sequencer input client object.";
Chris@559 823 error( RtError::DRIVER_ERROR );
Chris@559 824 }
Chris@559 825
Chris@559 826 // Set client name.
Chris@559 827 snd_seq_set_client_name(seq, "RtMidi Input Client");
Chris@559 828
Chris@559 829 // Save our api-specific connection information.
Chris@559 830 AlsaMidiData *data = (AlsaMidiData *) new AlsaMidiData;
Chris@559 831 data->seq = seq;
Chris@559 832 data->vport = -1;
Chris@559 833 apiData_ = (void *) data;
Chris@559 834 inputData_.apiData = (void *) data;
Chris@559 835
Chris@559 836 // Create the input queue
Chris@559 837 data->queue_id = snd_seq_alloc_named_queue(seq, "RtMidi Queue");
Chris@559 838 // Set arbitrary tempo (mm=100) and resolution (240)
Chris@559 839 snd_seq_queue_tempo_t *qtempo;
Chris@559 840 snd_seq_queue_tempo_alloca(&qtempo);
Chris@559 841 snd_seq_queue_tempo_set_tempo(qtempo, 600000);
Chris@559 842 snd_seq_queue_tempo_set_ppq(qtempo, 240);
Chris@559 843 snd_seq_set_queue_tempo(data->seq, data->queue_id, qtempo);
Chris@559 844 snd_seq_drain_output(data->seq);
Chris@559 845 }
Chris@559 846
Chris@559 847 // This function is used to count or get the pinfo structure for a given port number.
Chris@559 848 unsigned int portInfo( snd_seq_t *seq, snd_seq_port_info_t *pinfo, unsigned int type, int portNumber )
Chris@559 849 {
Chris@559 850 snd_seq_client_info_t *cinfo;
Chris@559 851 int client;
Chris@559 852 int count = 0;
Chris@559 853 snd_seq_client_info_alloca( &cinfo );
Chris@559 854
Chris@559 855 snd_seq_client_info_set_client( cinfo, -1 );
Chris@559 856 while ( snd_seq_query_next_client( seq, cinfo ) >= 0 ) {
Chris@559 857 client = snd_seq_client_info_get_client( cinfo );
Chris@559 858 if ( client == 0 ) continue;
Chris@559 859 // Reset query info
Chris@559 860 snd_seq_port_info_set_client( pinfo, client );
Chris@559 861 snd_seq_port_info_set_port( pinfo, -1 );
Chris@559 862 while ( snd_seq_query_next_port( seq, pinfo ) >= 0 ) {
Chris@559 863 if ( !PORT_TYPE( pinfo, type ) ) continue;
Chris@559 864 if ( count == portNumber ) return 1;
Chris@559 865 count++;
Chris@559 866 }
Chris@559 867 }
Chris@559 868
Chris@559 869 // If a negative portNumber was used, return the port count.
Chris@559 870 if ( portNumber < 0 ) return count;
Chris@559 871 return 0;
Chris@559 872 }
Chris@559 873
Chris@559 874 void RtMidiIn :: openPort( unsigned int portNumber )
Chris@559 875 {
Chris@559 876 if ( connected_ ) {
Chris@559 877 errorString_ = "RtMidiIn::openPort: a valid connection already exists!";
Chris@559 878 error( RtError::WARNING );
Chris@559 879 return;
Chris@559 880 }
Chris@559 881
Chris@559 882 unsigned int nSrc = this->getPortCount();
Chris@559 883 if (nSrc < 1) {
Chris@559 884 errorString_ = "RtMidiIn::openPort: no MIDI input sources found!";
Chris@559 885 error( RtError::NO_DEVICES_FOUND );
Chris@559 886 }
Chris@559 887
Chris@559 888 snd_seq_port_info_t *pinfo;
Chris@559 889 snd_seq_port_info_alloca( &pinfo );
Chris@559 890 std::ostringstream ost;
Chris@559 891 AlsaMidiData *data = static_cast<AlsaMidiData *> (apiData_);
Chris@559 892 if ( portInfo( data->seq, pinfo, SND_SEQ_PORT_CAP_READ|SND_SEQ_PORT_CAP_SUBS_READ, (int) portNumber ) == 0 ) {
Chris@559 893 ost << "RtMidiIn::openPort: the 'portNumber' argument (" << portNumber << ") is invalid.";
Chris@559 894 errorString_ = ost.str();
Chris@559 895 error( RtError::INVALID_PARAMETER );
Chris@559 896 }
Chris@559 897
Chris@559 898
Chris@559 899 snd_seq_addr_t sender, receiver;
Chris@559 900 sender.client = snd_seq_port_info_get_client( pinfo );
Chris@559 901 sender.port = snd_seq_port_info_get_port( pinfo );
Chris@559 902 receiver.client = snd_seq_client_id( data->seq );
Chris@559 903 if ( data->vport < 0 ) {
Chris@559 904 snd_seq_port_info_set_client( pinfo, 0 );
Chris@559 905 snd_seq_port_info_set_port( pinfo, 0 );
Chris@559 906 snd_seq_port_info_set_capability( pinfo,
Chris@559 907 SND_SEQ_PORT_CAP_WRITE |
Chris@559 908 SND_SEQ_PORT_CAP_SUBS_WRITE );
Chris@559 909 snd_seq_port_info_set_type( pinfo,
Chris@559 910 SND_SEQ_PORT_TYPE_MIDI_GENERIC |
Chris@559 911 SND_SEQ_PORT_TYPE_APPLICATION );
Chris@559 912 snd_seq_port_info_set_midi_channels(pinfo, 16);
Chris@559 913 snd_seq_port_info_set_timestamping(pinfo, 1);
Chris@559 914 snd_seq_port_info_set_timestamp_real(pinfo, 1);
Chris@559 915 snd_seq_port_info_set_timestamp_queue(pinfo, data->queue_id);
Chris@559 916 snd_seq_port_info_set_name(pinfo, "RtMidi Input");
Chris@559 917 data->vport = snd_seq_create_port(data->seq, pinfo);
Chris@559 918
Chris@559 919 if ( data->vport < 0 ) {
Chris@559 920 errorString_ = "RtMidiIn::openPort: ALSA error creating input port.";
Chris@559 921 error( RtError::DRIVER_ERROR );
Chris@559 922 }
Chris@559 923 }
Chris@559 924
Chris@559 925 receiver.port = data->vport;
Chris@559 926
Chris@559 927 // Make subscription
Chris@559 928 snd_seq_port_subscribe_malloc( &data->subscription );
Chris@559 929 snd_seq_port_subscribe_set_sender(data->subscription, &sender);
Chris@559 930 snd_seq_port_subscribe_set_dest(data->subscription, &receiver);
Chris@559 931 if ( snd_seq_subscribe_port(data->seq, data->subscription) ) {
Chris@559 932 errorString_ = "RtMidiIn::openPort: ALSA error making port connection.";
Chris@559 933 error( RtError::DRIVER_ERROR );
Chris@559 934 }
Chris@559 935
Chris@559 936 if ( inputData_.doInput == false ) {
Chris@559 937 // Start the input queue
Chris@559 938 snd_seq_start_queue( data->seq, data->queue_id, NULL );
Chris@559 939 snd_seq_drain_output( data->seq );
Chris@559 940 // Start our MIDI input thread.
Chris@559 941 pthread_attr_t attr;
Chris@559 942 pthread_attr_init(&attr);
Chris@559 943 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
Chris@559 944 pthread_attr_setschedpolicy(&attr, SCHED_OTHER);
Chris@559 945
Chris@559 946 inputData_.doInput = true;
Chris@559 947 int err = pthread_create(&data->thread, &attr, alsaMidiHandler, &inputData_);
Chris@559 948 pthread_attr_destroy(&attr);
Chris@559 949 if (err) {
Chris@559 950 snd_seq_unsubscribe_port( data->seq, data->subscription );
Chris@559 951 snd_seq_port_subscribe_free( data->subscription );
Chris@559 952 inputData_.doInput = false;
Chris@559 953 errorString_ = "RtMidiIn::openPort: error starting MIDI input thread!";
Chris@559 954 error( RtError::THREAD_ERROR );
Chris@559 955 }
Chris@559 956 }
Chris@559 957
Chris@559 958 connected_ = true;
Chris@559 959 }
Chris@559 960
Chris@559 961 void RtMidiIn :: openVirtualPort( std::string portName )
Chris@559 962 {
Chris@559 963 AlsaMidiData *data = static_cast<AlsaMidiData *> (apiData_);
Chris@559 964 if ( data->vport < 0 ) {
Chris@559 965 snd_seq_port_info_t *pinfo;
Chris@559 966 snd_seq_port_info_alloca( &pinfo );
Chris@559 967 snd_seq_port_info_set_capability( pinfo,
Chris@559 968 SND_SEQ_PORT_CAP_WRITE |
Chris@559 969 SND_SEQ_PORT_CAP_SUBS_WRITE );
Chris@559 970 snd_seq_port_info_set_type( pinfo,
Chris@559 971 SND_SEQ_PORT_TYPE_MIDI_GENERIC |
Chris@559 972 SND_SEQ_PORT_TYPE_APPLICATION );
Chris@559 973 snd_seq_port_info_set_midi_channels(pinfo, 16);
Chris@559 974 snd_seq_port_info_set_timestamping(pinfo, 1);
Chris@559 975 snd_seq_port_info_set_timestamp_real(pinfo, 1);
Chris@559 976 snd_seq_port_info_set_timestamp_queue(pinfo, data->queue_id);
Chris@559 977 snd_seq_port_info_set_name(pinfo, portName.c_str());
Chris@559 978 data->vport = snd_seq_create_port(data->seq, pinfo);
Chris@559 979
Chris@559 980 if ( data->vport < 0 ) {
Chris@559 981 errorString_ = "RtMidiIn::openVirtualPort: ALSA error creating virtual port.";
Chris@559 982 error( RtError::DRIVER_ERROR );
Chris@559 983 }
Chris@559 984 }
Chris@559 985
Chris@559 986 if ( inputData_.doInput == false ) {
Chris@559 987 // Start the input queue
Chris@559 988 snd_seq_start_queue( data->seq, data->queue_id, NULL );
Chris@559 989 snd_seq_drain_output( data->seq );
Chris@559 990 // Start our MIDI input thread.
Chris@559 991 pthread_attr_t attr;
Chris@559 992 pthread_attr_init(&attr);
Chris@559 993 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
Chris@559 994 pthread_attr_setschedpolicy(&attr, SCHED_OTHER);
Chris@559 995
Chris@559 996 inputData_.doInput = true;
Chris@559 997 int err = pthread_create(&data->thread, &attr, alsaMidiHandler, &inputData_);
Chris@559 998 pthread_attr_destroy(&attr);
Chris@559 999 if (err) {
Chris@559 1000 snd_seq_unsubscribe_port( data->seq, data->subscription );
Chris@559 1001 snd_seq_port_subscribe_free( data->subscription );
Chris@559 1002 inputData_.doInput = false;
Chris@559 1003 errorString_ = "RtMidiIn::openPort: error starting MIDI input thread!";
Chris@559 1004 error( RtError::THREAD_ERROR );
Chris@559 1005 }
Chris@559 1006 }
Chris@559 1007 }
Chris@559 1008
Chris@559 1009 void RtMidiIn :: closePort( void )
Chris@559 1010 {
Chris@559 1011 if ( connected_ ) {
Chris@559 1012 AlsaMidiData *data = static_cast<AlsaMidiData *> (apiData_);
Chris@559 1013 snd_seq_unsubscribe_port( data->seq, data->subscription );
Chris@559 1014 snd_seq_port_subscribe_free( data->subscription );
Chris@559 1015 // Stop the input queue
Chris@559 1016 snd_seq_stop_queue( data->seq, data->queue_id, NULL );
Chris@559 1017 snd_seq_drain_output( data->seq );
Chris@559 1018 connected_ = false;
Chris@559 1019 }
Chris@559 1020 }
Chris@559 1021
Chris@559 1022 RtMidiIn :: ~RtMidiIn()
Chris@559 1023 {
Chris@559 1024 // Close a connection if it exists.
Chris@559 1025 closePort();
Chris@559 1026
Chris@559 1027 // Shutdown the input thread.
Chris@559 1028 AlsaMidiData *data = static_cast<AlsaMidiData *> (apiData_);
Chris@559 1029 if ( inputData_.doInput ) {
Chris@559 1030 inputData_.doInput = false;
Chris@559 1031 pthread_join( data->thread, NULL );
Chris@559 1032 }
Chris@559 1033
Chris@559 1034 // Cleanup.
Chris@559 1035 if ( data->vport >= 0 ) snd_seq_delete_port( data->seq, data->vport );
Chris@559 1036 snd_seq_free_queue( data->seq, data->queue_id );
Chris@559 1037 snd_seq_close( data->seq );
Chris@559 1038 delete data;
Chris@559 1039 }
Chris@559 1040
Chris@559 1041 unsigned int RtMidiIn :: getPortCount()
Chris@559 1042 {
Chris@559 1043 snd_seq_port_info_t *pinfo;
Chris@559 1044 snd_seq_port_info_alloca( &pinfo );
Chris@559 1045
Chris@559 1046 AlsaMidiData *data = static_cast<AlsaMidiData *> (apiData_);
Chris@559 1047 return portInfo( data->seq, pinfo, SND_SEQ_PORT_CAP_READ|SND_SEQ_PORT_CAP_SUBS_READ, -1 );
Chris@559 1048 }
Chris@559 1049
Chris@559 1050 std::string RtMidiIn :: getPortName( unsigned int portNumber )
Chris@559 1051 {
Chris@559 1052 snd_seq_port_info_t *pinfo;
Chris@559 1053 snd_seq_port_info_alloca( &pinfo );
Chris@559 1054
Chris@559 1055 AlsaMidiData *data = static_cast<AlsaMidiData *> (apiData_);
Chris@559 1056 if ( portInfo( data->seq, pinfo, SND_SEQ_PORT_CAP_READ|SND_SEQ_PORT_CAP_SUBS_READ, (int) portNumber ) ) {
Chris@559 1057 std::string stringName = std::string( snd_seq_port_info_get_name( pinfo ) );
Chris@559 1058 return stringName;
Chris@559 1059 }
Chris@559 1060
Chris@559 1061 // If we get here, we didn't find a match.
Chris@559 1062 errorString_ = "RtMidiIn::getPortName: error looking for port name!";
Chris@559 1063 error( RtError::INVALID_PARAMETER );
Chris@559 1064 return 0;
Chris@559 1065 }
Chris@559 1066
Chris@559 1067 //*********************************************************************//
Chris@559 1068 // API: LINUX ALSA
Chris@559 1069 // Class Definitions: RtMidiOut
Chris@559 1070 //*********************************************************************//
Chris@559 1071
Chris@559 1072 unsigned int RtMidiOut :: getPortCount()
Chris@559 1073 {
Chris@559 1074 snd_seq_port_info_t *pinfo;
Chris@559 1075 snd_seq_port_info_alloca( &pinfo );
Chris@559 1076
Chris@559 1077 AlsaMidiData *data = static_cast<AlsaMidiData *> (apiData_);
Chris@559 1078 return portInfo( data->seq, pinfo, SND_SEQ_PORT_CAP_WRITE|SND_SEQ_PORT_CAP_SUBS_WRITE, -1 );
Chris@559 1079 }
Chris@559 1080
Chris@559 1081 std::string RtMidiOut :: getPortName( unsigned int portNumber )
Chris@559 1082 {
Chris@559 1083 snd_seq_port_info_t *pinfo;
Chris@559 1084 snd_seq_port_info_alloca( &pinfo );
Chris@559 1085
Chris@559 1086 AlsaMidiData *data = static_cast<AlsaMidiData *> (apiData_);
Chris@559 1087 if ( portInfo( data->seq, pinfo, SND_SEQ_PORT_CAP_WRITE|SND_SEQ_PORT_CAP_SUBS_WRITE, (int) portNumber ) ) {
Chris@559 1088 std::string stringName = std::string( snd_seq_port_info_get_name( pinfo ) );
Chris@559 1089 return stringName;
Chris@559 1090 }
Chris@559 1091
Chris@559 1092 // If we get here, we didn't find a match.
Chris@559 1093 errorString_ = "RtMidiOut::getPortName: error looking for port name!";
Chris@559 1094 error( RtError::INVALID_PARAMETER );
Chris@559 1095 return 0;
Chris@559 1096 }
Chris@559 1097
Chris@559 1098 void RtMidiOut :: initialize( void )
Chris@559 1099 {
Chris@559 1100 // Set up the ALSA sequencer client.
Chris@559 1101 snd_seq_t *seq;
Chris@559 1102 int result = snd_seq_open(&seq, "default", SND_SEQ_OPEN_OUTPUT, 0);
Chris@559 1103 if ( result < 0 ) {
Chris@559 1104 errorString_ = "RtMidiOut::initialize: error creating ALSA sequencer client object.";
Chris@559 1105 error( RtError::DRIVER_ERROR );
Chris@559 1106 }
Chris@559 1107
Chris@559 1108 // Set client name.
Chris@559 1109 snd_seq_set_client_name(seq, "RtMidi Output Client");
Chris@559 1110
Chris@559 1111 // Save our api-specific connection information.
Chris@559 1112 AlsaMidiData *data = (AlsaMidiData *) new AlsaMidiData;
Chris@559 1113 data->seq = seq;
Chris@559 1114 data->vport = -1;
Chris@559 1115 data->bufferSize = 32;
Chris@559 1116 data->coder = 0;
Chris@559 1117 data->buffer = 0;
Chris@559 1118 result = snd_midi_event_new( data->bufferSize, &data->coder );
Chris@559 1119 if ( result < 0 ) {
Chris@559 1120 delete data;
Chris@559 1121 errorString_ = "RtMidiOut::initialize: error initializing MIDI event parser!\n\n";
Chris@559 1122 error( RtError::DRIVER_ERROR );
Chris@559 1123 }
Chris@559 1124 data->buffer = (unsigned char *) malloc( data->bufferSize );
Chris@559 1125 if ( data->buffer == NULL ) {
Chris@559 1126 delete data;
Chris@559 1127 errorString_ = "RtMidiOut::initialize: error allocating buffer memory!\n\n";
Chris@559 1128 error( RtError::MEMORY_ERROR );
Chris@559 1129 }
Chris@559 1130 snd_midi_event_init( data->coder );
Chris@559 1131 apiData_ = (void *) data;
Chris@559 1132 }
Chris@559 1133
Chris@559 1134 void RtMidiOut :: openPort( unsigned int portNumber )
Chris@559 1135 {
Chris@559 1136 if ( connected_ ) {
Chris@559 1137 errorString_ = "RtMidiOut::openPort: a valid connection already exists!";
Chris@559 1138 error( RtError::WARNING );
Chris@559 1139 return;
Chris@559 1140 }
Chris@559 1141
Chris@559 1142 unsigned int nSrc = this->getPortCount();
Chris@559 1143 if (nSrc < 1) {
Chris@559 1144 errorString_ = "RtMidiOut::openPort: no MIDI output sources found!";
Chris@559 1145 error( RtError::NO_DEVICES_FOUND );
Chris@559 1146 }
Chris@559 1147
Chris@559 1148 snd_seq_port_info_t *pinfo;
Chris@559 1149 snd_seq_port_info_alloca( &pinfo );
Chris@559 1150 std::ostringstream ost;
Chris@559 1151 AlsaMidiData *data = static_cast<AlsaMidiData *> (apiData_);
Chris@559 1152 if ( portInfo( data->seq, pinfo, SND_SEQ_PORT_CAP_WRITE|SND_SEQ_PORT_CAP_SUBS_WRITE, (int) portNumber ) == 0 ) {
Chris@559 1153 ost << "RtMidiOut::openPort: the 'portNumber' argument (" << portNumber << ") is invalid.";
Chris@559 1154 errorString_ = ost.str();
Chris@559 1155 error( RtError::INVALID_PARAMETER );
Chris@559 1156 }
Chris@559 1157
Chris@559 1158 snd_seq_addr_t sender, receiver;
Chris@559 1159 receiver.client = snd_seq_port_info_get_client( pinfo );
Chris@559 1160 receiver.port = snd_seq_port_info_get_port( pinfo );
Chris@559 1161 sender.client = snd_seq_client_id( data->seq );
Chris@559 1162
Chris@559 1163 if ( data->vport < 0 ) {
Chris@559 1164 data->vport = snd_seq_create_simple_port( data->seq, "RtMidi Output",
Chris@559 1165 SND_SEQ_PORT_CAP_READ|SND_SEQ_PORT_CAP_SUBS_READ,
Chris@559 1166 SND_SEQ_PORT_TYPE_MIDI_GENERIC );
Chris@559 1167 if ( data->vport < 0 ) {
Chris@559 1168 errorString_ = "RtMidiOut::openPort: ALSA error creating output port.";
Chris@559 1169 error( RtError::DRIVER_ERROR );
Chris@559 1170 }
Chris@559 1171 }
Chris@559 1172
Chris@559 1173 sender.port = data->vport;
Chris@559 1174
Chris@559 1175 // Make subscription
Chris@559 1176 snd_seq_port_subscribe_malloc( &data->subscription );
Chris@559 1177 snd_seq_port_subscribe_set_sender(data->subscription, &sender);
Chris@559 1178 snd_seq_port_subscribe_set_dest(data->subscription, &receiver);
Chris@559 1179 snd_seq_port_subscribe_set_time_update(data->subscription, 1);
Chris@559 1180 snd_seq_port_subscribe_set_time_real(data->subscription, 1);
Chris@559 1181 if ( snd_seq_subscribe_port(data->seq, data->subscription) ) {
Chris@559 1182 errorString_ = "RtMidiOut::openPort: ALSA error making port connection.";
Chris@559 1183 error( RtError::DRIVER_ERROR );
Chris@559 1184 }
Chris@559 1185
Chris@559 1186 connected_ = true;
Chris@559 1187 }
Chris@559 1188
Chris@559 1189 void RtMidiOut :: closePort( void )
Chris@559 1190 {
Chris@559 1191 if ( connected_ ) {
Chris@559 1192 AlsaMidiData *data = static_cast<AlsaMidiData *> (apiData_);
Chris@559 1193 snd_seq_unsubscribe_port( data->seq, data->subscription );
Chris@559 1194 snd_seq_port_subscribe_free( data->subscription );
Chris@559 1195 connected_ = false;
Chris@559 1196 }
Chris@559 1197 }
Chris@559 1198
Chris@559 1199 void RtMidiOut :: openVirtualPort( std::string portName )
Chris@559 1200 {
Chris@559 1201 AlsaMidiData *data = static_cast<AlsaMidiData *> (apiData_);
Chris@559 1202 if ( data->vport < 0 ) {
Chris@559 1203 data->vport = snd_seq_create_simple_port( data->seq, portName.c_str(),
Chris@559 1204 SND_SEQ_PORT_CAP_READ|SND_SEQ_PORT_CAP_SUBS_READ,
Chris@559 1205 SND_SEQ_PORT_TYPE_MIDI_GENERIC );
Chris@559 1206
Chris@559 1207 if ( data->vport < 0 ) {
Chris@559 1208 errorString_ = "RtMidiOut::openVirtualPort: ALSA error creating virtual port.";
Chris@559 1209 error( RtError::DRIVER_ERROR );
Chris@559 1210 }
Chris@559 1211 }
Chris@559 1212 }
Chris@559 1213
Chris@559 1214 RtMidiOut :: ~RtMidiOut()
Chris@559 1215 {
Chris@559 1216 // Close a connection if it exists.
Chris@559 1217 closePort();
Chris@559 1218
Chris@559 1219 // Cleanup.
Chris@559 1220 AlsaMidiData *data = static_cast<AlsaMidiData *> (apiData_);
Chris@559 1221 if ( data->vport >= 0 ) snd_seq_delete_port( data->seq, data->vport );
Chris@559 1222 if ( data->coder ) snd_midi_event_free( data->coder );
Chris@559 1223 if ( data->buffer ) free( data->buffer );
Chris@559 1224 snd_seq_close( data->seq );
Chris@559 1225 delete data;
Chris@559 1226 }
Chris@559 1227
Chris@559 1228 void RtMidiOut :: sendMessage( std::vector<unsigned char> *message )
Chris@559 1229 {
Chris@559 1230 int result;
Chris@559 1231 AlsaMidiData *data = static_cast<AlsaMidiData *> (apiData_);
Chris@559 1232 unsigned int nBytes = message->size();
Chris@559 1233 if ( nBytes > data->bufferSize ) {
Chris@559 1234 data->bufferSize = nBytes;
Chris@559 1235 result = snd_midi_event_resize_buffer ( data->coder, nBytes);
Chris@559 1236 if ( result != 0 ) {
Chris@559 1237 errorString_ = "RtMidiOut::sendMessage: ALSA error resizing MIDI event buffer.";
Chris@559 1238 error( RtError::DRIVER_ERROR );
Chris@559 1239 }
Chris@559 1240 free (data->buffer);
Chris@559 1241 data->buffer = (unsigned char *) malloc( data->bufferSize );
Chris@559 1242 if ( data->buffer == NULL ) {
Chris@559 1243 errorString_ = "RtMidiOut::initialize: error allocating buffer memory!\n\n";
Chris@559 1244 error( RtError::MEMORY_ERROR );
Chris@559 1245 }
Chris@559 1246 }
Chris@559 1247
Chris@559 1248 snd_seq_event_t ev;
Chris@559 1249 snd_seq_ev_clear(&ev);
Chris@559 1250 snd_seq_ev_set_source(&ev, data->vport);
Chris@559 1251 snd_seq_ev_set_subs(&ev);
Chris@559 1252 snd_seq_ev_set_direct(&ev);
Chris@559 1253 for ( unsigned int i=0; i<nBytes; i++ ) data->buffer[i] = message->at(i);
Chris@559 1254 result = snd_midi_event_encode( data->coder, data->buffer, (long)nBytes, &ev );
Chris@559 1255 if ( result < (int)nBytes ) {
Chris@559 1256 errorString_ = "RtMidiOut::sendMessage: event parsing error!";
Chris@559 1257 error( RtError::WARNING );
Chris@559 1258 return;
Chris@559 1259 }
Chris@559 1260
Chris@559 1261 // Send the event.
Chris@559 1262 result = snd_seq_event_output(data->seq, &ev);
Chris@559 1263 if ( result < 0 ) {
Chris@559 1264 errorString_ = "RtMidiOut::sendMessage: error sending MIDI message to port.";
Chris@559 1265 error( RtError::WARNING );
Chris@559 1266 }
Chris@559 1267 snd_seq_drain_output(data->seq);
Chris@559 1268 }
Chris@559 1269
Chris@559 1270 #endif // __LINUX_ALSA__
Chris@559 1271
Chris@559 1272
Chris@559 1273 //*********************************************************************//
Chris@559 1274 // API: IRIX MD
Chris@559 1275 //*********************************************************************//
Chris@559 1276
Chris@559 1277 // API information gleamed from:
Chris@559 1278 // http://techpubs.sgi.com/library/tpl/cgi-bin/getdoc.cgi?cmd=getdoc&coll=0650&db=man&fname=3%20mdIntro
Chris@559 1279
Chris@559 1280 // If the Makefile doesn't work, try the following:
Chris@559 1281 // CC -o midiinfo -LANG:std -D__IRIX_MD__ -I../ ../RtMidi.cpp midiinfo.cpp -lpthread -lmd
Chris@559 1282 // CC -o midiout -LANG:std -D__IRIX_MD__ -I../ ../RtMidi.cpp midiout.cpp -lpthread -lmd
Chris@559 1283 // CC -o qmidiin -LANG:std -D__IRIX_MD__ -I../ ../RtMidi.cpp qmidiin.cpp -lpthread -lmd
Chris@559 1284 // CC -o cmidiin -LANG:std -D__IRIX_MD__ -I../ ../RtMidi.cpp cmidiin.cpp -lpthread -lmd
Chris@559 1285
Chris@559 1286 #if defined(__IRIX_MD__)
Chris@559 1287
Chris@559 1288 #include <pthread.h>
Chris@559 1289 #include <sys/time.h>
Chris@559 1290 #include <unistd.h>
Chris@559 1291
Chris@559 1292 // Irix MIDI header file.
Chris@559 1293 #include <dmedia/midi.h>
Chris@559 1294
Chris@559 1295 // A structure to hold variables related to the IRIX API
Chris@559 1296 // implementation.
Chris@559 1297 struct IrixMidiData {
Chris@559 1298 MDport port;
Chris@559 1299 pthread_t thread;
Chris@559 1300 };
Chris@559 1301
Chris@559 1302 //*********************************************************************//
Chris@559 1303 // API: IRIX
Chris@559 1304 // Class Definitions: RtMidiIn
Chris@559 1305 //*********************************************************************//
Chris@559 1306
Chris@559 1307 extern "C" void *irixMidiHandler( void *ptr )
Chris@559 1308 {
Chris@559 1309 RtMidiIn::RtMidiInData *data = static_cast<RtMidiIn::RtMidiInData *> (ptr);
Chris@559 1310 IrixMidiData *apiData = static_cast<IrixMidiData *> (data->apiData);
Chris@559 1311
Chris@559 1312 bool continueSysex = false;
Chris@559 1313 unsigned char status;
Chris@559 1314 unsigned short size;
Chris@559 1315 MDevent event;
Chris@559 1316 int fd = mdGetFd( apiData->port );
Chris@559 1317 if ( fd < 0 ) {
Chris@559 1318 data->doInput = false;
Chris@559 1319 std::cerr << "\nRtMidiIn::irixMidiHandler: error getting port descriptor!\n\n";
Chris@559 1320 return 0;
Chris@559 1321 }
Chris@559 1322
Chris@559 1323 fd_set mask, rmask;
Chris@559 1324 FD_ZERO( &mask );
Chris@559 1325 FD_SET( fd, &mask );
Chris@559 1326 struct timeval timeout = {0, 0};
Chris@559 1327 RtMidiIn::MidiMessage message;
Chris@559 1328 int result;
Chris@559 1329
Chris@559 1330 while ( data->doInput ) {
Chris@559 1331
Chris@559 1332 rmask = mask;
Chris@559 1333 timeout.tv_sec = 0;
Chris@559 1334 timeout.tv_usec = 0;
Chris@559 1335 if ( select( fd+1, &rmask, NULL, NULL, &timeout ) <= 0 ) {
Chris@559 1336 // No data pending ... sleep a bit.
Chris@559 1337 usleep( 1000 );
Chris@559 1338 continue;
Chris@559 1339 }
Chris@559 1340
Chris@559 1341 // If here, there should be data.
Chris@559 1342 result = mdReceive( apiData->port, &event, 1);
Chris@559 1343 if ( result <= 0 ) {
Chris@559 1344 std::cerr << "\nRtMidiIn::irixMidiHandler: MIDI input read error!\n\n";
Chris@559 1345 continue;
Chris@559 1346 }
Chris@559 1347
Chris@559 1348 message.timeStamp = event.stamp * 0.000000001;
Chris@559 1349
Chris@559 1350 size = 0;
Chris@559 1351 status = event.msg[0];
Chris@559 1352 if ( !(status & 0x80) ) continue;
Chris@559 1353 if ( status == 0xF0 ) {
Chris@559 1354 // Sysex message ... can be segmented across multiple messages.
Chris@559 1355 if ( !(data->ignoreFlags & 0x01) ) {
Chris@559 1356 if ( continueSysex ) {
Chris@559 1357 // We have a continuing, segmented sysex message. Append
Chris@559 1358 // the new bytes to our existing message.
Chris@559 1359 for ( int i=0; i<event.msglen; i++ )
Chris@559 1360 message.bytes.push_back( event.sysexmsg[i] );
Chris@559 1361 if ( event.sysexmsg[event.msglen-1] == 0xF7 ) continueSysex = false;
Chris@559 1362 if ( !continueSysex ) {
Chris@559 1363 // If not a continuing sysex message, invoke the user callback function or queue the message.
Chris@559 1364 if ( data->usingCallback && message.bytes.size() > 0 ) {
Chris@559 1365 RtMidiIn::RtMidiCallback callback = (RtMidiIn::RtMidiCallback) data->userCallback;
Chris@559 1366 callback( message.timeStamp, &message.bytes, data->userData );
Chris@559 1367 }
Chris@559 1368 else {
Chris@559 1369 // As long as we haven't reached our queue size limit, push the message.
Chris@559 1370 if ( data->queueLimit > data->queue.size() )
Chris@559 1371 data->queue.push( message );
Chris@559 1372 else
Chris@559 1373 std::cerr << "\nRtMidiIn: message queue limit reached!!\n\n";
Chris@559 1374 }
Chris@559 1375 message.bytes.clear();
Chris@559 1376 }
Chris@559 1377 }
Chris@559 1378 }
Chris@559 1379 mdFree( NULL );
Chris@559 1380 continue;
Chris@559 1381 }
Chris@559 1382 else if ( status < 0xC0 ) size = 3;
Chris@559 1383 else if ( status < 0xE0 ) size = 2;
Chris@559 1384 else if ( status < 0xF0 ) size = 3;
Chris@559 1385 else if ( status < 0xF3 ) {
Chris@559 1386 if ( status == 0xF1 && !(data->ignoreFlags & 0x02) ) {
Chris@559 1387 // A MIDI time code message and we're not ignoring it.
Chris@559 1388 size = 3;
Chris@559 1389 }
Chris@559 1390 }
Chris@559 1391 else if ( status == 0xF3 ) size = 2;
Chris@559 1392 else if ( status == 0xF8 ) {
Chris@559 1393 if ( !(data->ignoreFlags & 0x02) ) {
Chris@559 1394 // A MIDI timing tick message and we're not ignoring it.
Chris@559 1395 size = 1;
Chris@559 1396 }
Chris@559 1397 }
Chris@559 1398 else if ( status == 0xFE ) { // MIDI active sensing
Chris@559 1399 if ( !(data->ignoreFlags & 0x04) )
Chris@559 1400 size = 1;
Chris@559 1401 }
Chris@559 1402 else size = 1;
Chris@559 1403
Chris@559 1404 // Copy the MIDI data to our vector.
Chris@559 1405 if ( size ) {
Chris@559 1406 message.bytes.assign( &event.msg[0], &event.msg[size] );
Chris@559 1407 // Invoke the user callback function or queue the message.
Chris@559 1408 if ( data->usingCallback ) {
Chris@559 1409 RtMidiIn::RtMidiCallback callback = (RtMidiIn::RtMidiCallback) data->userCallback;
Chris@559 1410 callback( message.timeStamp, &message.bytes, data->userData );
Chris@559 1411 }
Chris@559 1412 else {
Chris@559 1413 // As long as we haven't reached our queue size limit, push the message.
Chris@559 1414 if ( data->queueLimit > data->queue.size() )
Chris@559 1415 data->queue.push( message );
Chris@559 1416 else
Chris@559 1417 std::cerr << "\nRtMidiIn: message queue limit reached!!\n\n";
Chris@559 1418 }
Chris@559 1419 message.bytes.clear();
Chris@559 1420 }
Chris@559 1421 }
Chris@559 1422
Chris@559 1423 return 0;
Chris@559 1424 }
Chris@559 1425
Chris@559 1426 void RtMidiIn :: initialize( void )
Chris@559 1427 {
Chris@559 1428 // Initialize the Irix MIDI system. At the moment, we will not
Chris@559 1429 // worry about a return value of zero (ports) because there is a
Chris@559 1430 // chance the user could plug something in after instantiation.
Chris@559 1431 int nPorts = mdInit();
Chris@559 1432
Chris@559 1433 // Create our api-specific connection information.
Chris@559 1434 IrixMidiData *data = (IrixMidiData *) new IrixMidiData;
Chris@559 1435 apiData_ = (void *) data;
Chris@559 1436 inputData_.apiData = (void *) data;
Chris@559 1437 }
Chris@559 1438
Chris@559 1439 void RtMidiIn :: openPort( unsigned int portNumber )
Chris@559 1440 {
Chris@559 1441 if ( connected_ ) {
Chris@559 1442 errorString_ = "RtMidiIn::openPort: a valid connection already exists!";
Chris@559 1443 error( RtError::WARNING );
Chris@559 1444 return;
Chris@559 1445 }
Chris@559 1446
Chris@559 1447 int nPorts = mdInit();
Chris@559 1448 if (nPorts < 1) {
Chris@559 1449 errorString_ = "RtMidiIn::openPort: no Irix MIDI input sources found!";
Chris@559 1450 error( RtError::NO_DEVICES_FOUND );
Chris@559 1451 }
Chris@559 1452
Chris@559 1453 std::ostringstream ost;
Chris@559 1454 if ( portNumber >= nPorts ) {
Chris@559 1455 ost << "RtMidiIn::openPort: the 'portNumber' argument (" << portNumber << ") is invalid.";
Chris@559 1456 errorString_ = ost.str();
Chris@559 1457 error( RtError::INVALID_PARAMETER );
Chris@559 1458 }
Chris@559 1459
Chris@559 1460 IrixMidiData *data = static_cast<IrixMidiData *> (apiData_);
Chris@559 1461 data->port = mdOpenInPort( mdGetName(portNumber) );
Chris@559 1462 if ( data->port == NULL ) {
Chris@559 1463 ost << "RtMidiIn::openPort: Irix error opening the port (" << portNumber << ").";
Chris@559 1464 errorString_ = ost.str();
Chris@559 1465 error( RtError::DRIVER_ERROR );
Chris@559 1466 }
Chris@559 1467 mdSetStampMode(data->port, MD_DELTASTAMP);
Chris@559 1468
Chris@559 1469 // Start our MIDI input thread.
Chris@559 1470 pthread_attr_t attr;
Chris@559 1471 pthread_attr_init(&attr);
Chris@559 1472 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
Chris@559 1473 pthread_attr_setschedpolicy(&attr, SCHED_RR);
Chris@559 1474
Chris@559 1475 inputData_.doInput = true;
Chris@559 1476 int err = pthread_create(&data->thread, &attr, irixMidiHandler, &inputData_);
Chris@559 1477 pthread_attr_destroy(&attr);
Chris@559 1478 if (err) {
Chris@559 1479 mdClosePort( data->port );
Chris@559 1480 inputData_.doInput = false;
Chris@559 1481 errorString_ = "RtMidiIn::openPort: error starting MIDI input thread!";
Chris@559 1482 error( RtError::THREAD_ERROR );
Chris@559 1483 }
Chris@559 1484
Chris@559 1485 connected_ = true;
Chris@559 1486 }
Chris@559 1487
Chris@559 1488 void RtMidiIn :: openVirtualPort( std::string portName )
Chris@559 1489 {
Chris@559 1490 // This function cannot be implemented for the Irix MIDI API.
Chris@559 1491 errorString_ = "RtMidiIn::openVirtualPort: cannot be implemented in Irix MIDI API!";
Chris@559 1492 error( RtError::WARNING );
Chris@559 1493 }
Chris@559 1494
Chris@559 1495 void RtMidiIn :: closePort( void )
Chris@559 1496 {
Chris@559 1497 if ( connected_ ) {
Chris@559 1498 IrixMidiData *data = static_cast<IrixMidiData *> (apiData_);
Chris@559 1499 mdClosePort( data->port );
Chris@559 1500 connected_ = false;
Chris@559 1501
Chris@559 1502 // Shutdown the input thread.
Chris@559 1503 inputData_.doInput = false;
Chris@559 1504 pthread_join( data->thread, NULL );
Chris@559 1505 }
Chris@559 1506 }
Chris@559 1507
Chris@559 1508 RtMidiIn :: ~RtMidiIn()
Chris@559 1509 {
Chris@559 1510 // Close a connection if it exists.
Chris@559 1511 closePort();
Chris@559 1512
Chris@559 1513 // Cleanup.
Chris@559 1514 IrixMidiData *data = static_cast<IrixMidiData *> (apiData_);
Chris@559 1515 delete data;
Chris@559 1516 }
Chris@559 1517
Chris@559 1518 unsigned int RtMidiIn :: getPortCount()
Chris@559 1519 {
Chris@559 1520 int nPorts = mdInit();
Chris@559 1521 if ( nPorts >= 0 ) return nPorts;
Chris@559 1522 else return 0;
Chris@559 1523 }
Chris@559 1524
Chris@559 1525 std::string RtMidiIn :: getPortName( unsigned int portNumber )
Chris@559 1526 {
Chris@559 1527 int nPorts = mdInit();
Chris@559 1528
Chris@559 1529 std::ostringstream ost;
Chris@559 1530 if ( portNumber >= nPorts ) {
Chris@559 1531 ost << "RtMidiIn::getPortName: the 'portNumber' argument (" << portNumber << ") is invalid.";
Chris@559 1532 errorString_ = ost.str();
Chris@559 1533 error( RtError::INVALID_PARAMETER );
Chris@559 1534 }
Chris@559 1535
Chris@559 1536 std::string stringName = std::string( mdGetName( portNumber ) );
Chris@559 1537 return stringName;
Chris@559 1538 }
Chris@559 1539
Chris@559 1540 //*********************************************************************//
Chris@559 1541 // API: IRIX MD
Chris@559 1542 // Class Definitions: RtMidiOut
Chris@559 1543 //*********************************************************************//
Chris@559 1544
Chris@559 1545 unsigned int RtMidiOut :: getPortCount()
Chris@559 1546 {
Chris@559 1547 int nPorts = mdInit();
Chris@559 1548 if ( nPorts >= 0 ) return nPorts;
Chris@559 1549 else return 0;
Chris@559 1550 }
Chris@559 1551
Chris@559 1552 std::string RtMidiOut :: getPortName( unsigned int portNumber )
Chris@559 1553 {
Chris@559 1554 int nPorts = mdInit();
Chris@559 1555
Chris@559 1556 std::ostringstream ost;
Chris@559 1557 if ( portNumber >= nPorts ) {
Chris@559 1558 ost << "RtMidiIn::getPortName: the 'portNumber' argument (" << portNumber << ") is invalid.";
Chris@559 1559 errorString_ = ost.str();
Chris@559 1560 error( RtError::INVALID_PARAMETER );
Chris@559 1561 }
Chris@559 1562
Chris@559 1563 std::string stringName = std::string( mdGetName( portNumber ) );
Chris@559 1564 return stringName;
Chris@559 1565 }
Chris@559 1566
Chris@559 1567 void RtMidiOut :: initialize( void )
Chris@559 1568 {
Chris@559 1569 // Initialize the Irix MIDI system. At the moment, we will not
Chris@559 1570 // worry about a return value of zero (ports) because there is a
Chris@559 1571 // chance the user could plug something in after instantiation.
Chris@559 1572 int nPorts = mdInit();
Chris@559 1573
Chris@559 1574 // Create our api-specific connection information.
Chris@559 1575 IrixMidiData *data = (IrixMidiData *) new IrixMidiData;
Chris@559 1576 apiData_ = (void *) data;
Chris@559 1577 }
Chris@559 1578
Chris@559 1579 void RtMidiOut :: openPort( unsigned int portNumber )
Chris@559 1580 {
Chris@559 1581 if ( connected_ ) {
Chris@559 1582 errorString_ = "RtMidiOut::openPort: a valid connection already exists!";
Chris@559 1583 error( RtError::WARNING );
Chris@559 1584 return;
Chris@559 1585 }
Chris@559 1586
Chris@559 1587 int nPorts = mdInit();
Chris@559 1588 if (nPorts < 1) {
Chris@559 1589 errorString_ = "RtMidiOut::openPort: no Irix MIDI output sources found!";
Chris@559 1590 error( RtError::NO_DEVICES_FOUND );
Chris@559 1591 }
Chris@559 1592
Chris@559 1593 std::ostringstream ost;
Chris@559 1594 if ( portNumber >= nPorts ) {
Chris@559 1595 ost << "RtMidiOut::openPort: the 'portNumber' argument (" << portNumber << ") is invalid.";
Chris@559 1596 errorString_ = ost.str();
Chris@559 1597 error( RtError::INVALID_PARAMETER );
Chris@559 1598 }
Chris@559 1599
Chris@559 1600 IrixMidiData *data = static_cast<IrixMidiData *> (apiData_);
Chris@559 1601 data->port = mdOpenOutPort( mdGetName(portNumber) );
Chris@559 1602 if ( data->port == NULL ) {
Chris@559 1603 ost << "RtMidiOut::openPort: Irix error opening the port (" << portNumber << ").";
Chris@559 1604 errorString_ = ost.str();
Chris@559 1605 error( RtError::DRIVER_ERROR );
Chris@559 1606 }
Chris@559 1607 mdSetStampMode(data->port, MD_NOSTAMP);
Chris@559 1608
Chris@559 1609 connected_ = true;
Chris@559 1610 }
Chris@559 1611
Chris@559 1612 void RtMidiOut :: closePort( void )
Chris@559 1613 {
Chris@559 1614 if ( connected_ ) {
Chris@559 1615 IrixMidiData *data = static_cast<IrixMidiData *> (apiData_);
Chris@559 1616 mdClosePort( data->port );
Chris@559 1617 connected_ = false;
Chris@559 1618 }
Chris@559 1619 }
Chris@559 1620
Chris@559 1621 void RtMidiOut :: openVirtualPort( std::string portName )
Chris@559 1622 {
Chris@559 1623 // This function cannot be implemented for the Irix MIDI API.
Chris@559 1624 errorString_ = "RtMidiOut::openVirtualPort: cannot be implemented in Irix MIDI API!";
Chris@559 1625 error( RtError::WARNING );
Chris@559 1626 }
Chris@559 1627
Chris@559 1628 RtMidiOut :: ~RtMidiOut()
Chris@559 1629 {
Chris@559 1630 // Close a connection if it exists.
Chris@559 1631 closePort();
Chris@559 1632
Chris@559 1633 // Cleanup.
Chris@559 1634 IrixMidiData *data = static_cast<IrixMidiData *> (apiData_);
Chris@559 1635 delete data;
Chris@559 1636 }
Chris@559 1637
Chris@559 1638 void RtMidiOut :: sendMessage( std::vector<unsigned char> *message )
Chris@559 1639 {
Chris@559 1640 int result;
Chris@559 1641 MDevent event;
Chris@559 1642 IrixMidiData *data = static_cast<IrixMidiData *> (apiData_);
Chris@559 1643 char *buffer = 0;
Chris@559 1644
Chris@559 1645 unsigned int nBytes = message->size();
Chris@559 1646 if ( nBytes == 0 ) return;
Chris@559 1647 event.stamp = 0;
Chris@559 1648 if ( message->at(0) == 0xF0 ) {
Chris@559 1649 if ( nBytes < 3 ) return; // check for bogus sysex
Chris@559 1650 event.msg[0] = 0xF0;
Chris@559 1651 event.msglen = nBytes;
Chris@559 1652 buffer = (char *) malloc( nBytes );
Chris@559 1653 for ( int i=0; i<nBytes; i++ ) buffer[i] = message->at(i);
Chris@559 1654 event.sysexmsg = buffer;
Chris@559 1655 }
Chris@559 1656 else {
Chris@559 1657 for ( int i=0; i<nBytes; i++ )
Chris@559 1658 event.msg[i] = message->at(i);
Chris@559 1659 }
Chris@559 1660
Chris@559 1661 // Send the event.
Chris@559 1662 result = mdSend( data->port, &event, 1 );
Chris@559 1663 if ( buffer ) free( buffer );
Chris@559 1664 if ( result < 1 ) {
Chris@559 1665 errorString_ = "RtMidiOut::sendMessage: IRIX error sending MIDI message!";
Chris@559 1666 error( RtError::WARNING );
Chris@559 1667 return;
Chris@559 1668 }
Chris@559 1669 }
Chris@559 1670
Chris@559 1671 #endif // __IRIX_MD__
Chris@559 1672
Chris@559 1673 //*********************************************************************//
Chris@559 1674 // API: Windows Multimedia Library (MM)
Chris@559 1675 //*********************************************************************//
Chris@559 1676
Chris@559 1677 // API information deciphered from:
Chris@559 1678 // - http://msdn.microsoft.com/library/default.asp?url=/library/en-us/multimed/htm/_win32_midi_reference.asp
Chris@559 1679
Chris@559 1680 // Thanks to Jean-Baptiste Berruchon for the sysex code.
Chris@559 1681
Chris@559 1682 #if defined(__WINDOWS_MM__)
Chris@559 1683
Chris@559 1684 // The Windows MM API is based on the use of a callback function for
Chris@559 1685 // MIDI input. We convert the system specific time stamps to delta
Chris@559 1686 // time values.
Chris@559 1687
Chris@559 1688 // Windows MM MIDI header files.
Chris@559 1689 #include <windows.h>
Chris@559 1690 #include <mmsystem.h>
Chris@559 1691
Chris@559 1692 // A structure to hold variables related to the CoreMIDI API
Chris@559 1693 // implementation.
Chris@559 1694 struct WinMidiData {
Chris@559 1695 HMIDIIN inHandle; // Handle to Midi Input Device
Chris@559 1696 HMIDIOUT outHandle; // Handle to Midi Output Device
Chris@559 1697 DWORD lastTime;
Chris@559 1698 RtMidiIn::MidiMessage message;
Chris@559 1699 LPMIDIHDR sysexBuffer;
Chris@559 1700 };
Chris@559 1701
Chris@559 1702 #define RT_SYSEX_BUFFER_SIZE 1024
Chris@559 1703
Chris@559 1704 //*********************************************************************//
Chris@559 1705 // API: Windows MM
Chris@559 1706 // Class Definitions: RtMidiIn
Chris@559 1707 //*********************************************************************//
Chris@559 1708
Chris@559 1709 static void CALLBACK midiInputCallback( HMIDIOUT hmin,
Chris@559 1710 UINT inputStatus,
Chris@559 1711 DWORD instancePtr,
Chris@559 1712 DWORD midiMessage,
Chris@559 1713 DWORD timestamp )
Chris@559 1714 {
Chris@559 1715 if ( inputStatus != MIM_DATA && inputStatus != MIM_LONGDATA ) return;
Chris@559 1716
Chris@559 1717 //RtMidiIn::RtMidiInData *data = static_cast<RtMidiIn::RtMidiInData *> (instancePtr);
Chris@559 1718 RtMidiIn::RtMidiInData *data = (RtMidiIn::RtMidiInData *)instancePtr;
Chris@559 1719 WinMidiData *apiData = static_cast<WinMidiData *> (data->apiData);
Chris@559 1720
Chris@559 1721 // Calculate time stamp.
Chris@559 1722 apiData->message.timeStamp = 0.0;
Chris@559 1723 if ( data->firstMessage == true ) data->firstMessage = false;
Chris@559 1724 else apiData->message.timeStamp = (double) ( timestamp - apiData->lastTime ) * 0.001;
Chris@559 1725 apiData->lastTime = timestamp;
Chris@559 1726
Chris@559 1727 if ( inputStatus == MIM_DATA ) { // Channel or system message
Chris@559 1728
Chris@559 1729 // Make sure the first byte is a status byte.
Chris@559 1730 unsigned char status = (unsigned char) (midiMessage & 0x000000FF);
Chris@559 1731 if ( !(status & 0x80) ) return;
Chris@559 1732
Chris@559 1733 // Determine the number of bytes in the MIDI message.
Chris@559 1734 unsigned short nBytes = 1;
Chris@559 1735 if ( status < 0xC0 ) nBytes = 3;
Chris@559 1736 else if ( status < 0xE0 ) nBytes = 2;
Chris@559 1737 else if ( status < 0xF0 ) nBytes = 3;
Chris@559 1738 else if ( status < 0xF3 ) {
Chris@559 1739 // A MIDI time code message and we're ignoring it.
Chris@559 1740 if ( status == 0xF1 && (data->ignoreFlags & 0x02) ) return;
Chris@559 1741 nBytes = 3;
Chris@559 1742 }
Chris@559 1743 else if ( status == 0xF3 ) nBytes = 2;
Chris@559 1744 else if ( status == 0xF8 && (data->ignoreFlags & 0x02) ) {
Chris@559 1745 // A MIDI timing tick message and we're ignoring it.
Chris@559 1746 return;
Chris@559 1747 }
Chris@559 1748 else if ( status == 0xFE && (data->ignoreFlags & 0x04) ) {
Chris@559 1749 // A MIDI active sensing message and we're ignoring it.
Chris@559 1750 return;
Chris@559 1751 }
Chris@559 1752
Chris@559 1753 // Copy bytes to our MIDI message.
Chris@559 1754 unsigned char *ptr = (unsigned char *) &midiMessage;
Chris@559 1755 for ( int i=0; i<nBytes; i++ ) apiData->message.bytes.push_back( *ptr++ );
Chris@559 1756 }
Chris@559 1757 else if ( !(data->ignoreFlags & 0x01) ) {
Chris@559 1758 // Sysex message and we're not ignoring it
Chris@559 1759 MIDIHDR *sysex = ( MIDIHDR *) midiMessage;
Chris@559 1760 for ( int i=0; i<(int)sysex->dwBytesRecorded; i++ )
Chris@559 1761 apiData->message.bytes.push_back( sysex->lpData[i] );
Chris@559 1762
Chris@559 1763 // When the callback has to be unaffected (application closes),
Chris@559 1764 // it seems WinMM calls it with an empty sysex to de-queue the buffer
Chris@559 1765 // If the buffer is requeued afer that message, the PC suddenly reboots
Chris@559 1766 // after one or two minutes (JB).
Chris@559 1767 if ( apiData->sysexBuffer->dwBytesRecorded > 0 ) {
Chris@559 1768 MMRESULT result = midiInAddBuffer( apiData->inHandle, apiData->sysexBuffer, sizeof(MIDIHDR) );
Chris@559 1769 if ( result != MMSYSERR_NOERROR )
Chris@559 1770 std::cerr << "\nRtMidiIn::midiInputCallback: error sending sysex to Midi device!!\n\n";
Chris@559 1771 }
Chris@559 1772 }
Chris@559 1773
Chris@559 1774 if ( data->usingCallback ) {
Chris@559 1775 RtMidiIn::RtMidiCallback callback = (RtMidiIn::RtMidiCallback) data->userCallback;
Chris@559 1776 callback( apiData->message.timeStamp, &apiData->message.bytes, data->userData );
Chris@559 1777 }
Chris@559 1778 else {
Chris@559 1779 // As long as we haven't reached our queue size limit, push the message.
Chris@559 1780 if ( data->queueLimit > data->queue.size() )
Chris@559 1781 data->queue.push( apiData->message );
Chris@559 1782 else
Chris@559 1783 std::cerr << "\nRtMidiIn: message queue limit reached!!\n\n";
Chris@559 1784 }
Chris@559 1785
Chris@559 1786 // Clear the vector for the next input message. Note that doing
Chris@559 1787 // this here allows our code to work for sysex messages which are
Chris@559 1788 // segmented across multiple buffers.
Chris@559 1789 apiData->message.bytes.clear();
Chris@559 1790 }
Chris@559 1791
Chris@559 1792 void RtMidiIn :: initialize( void )
Chris@559 1793 {
Chris@559 1794 // We'll issue a warning here if no devices are available but not
Chris@559 1795 // throw an error since the user can plugin something later.
Chris@559 1796 unsigned int nDevices = midiInGetNumDevs();
Chris@559 1797 if ( nDevices == 0 ) {
Chris@559 1798 errorString_ = "RtMidiIn::initialize: no MIDI input devices currently available.";
Chris@559 1799 error( RtError::WARNING );
Chris@559 1800 }
Chris@559 1801
Chris@559 1802 // Save our api-specific connection information.
Chris@559 1803 WinMidiData *data = (WinMidiData *) new WinMidiData;
Chris@559 1804 apiData_ = (void *) data;
Chris@559 1805 inputData_.apiData = (void *) data;
Chris@559 1806 data->message.bytes.clear(); // needs to be empty for first input message
Chris@559 1807 }
Chris@559 1808
Chris@559 1809 void RtMidiIn :: openPort( unsigned int portNumber )
Chris@559 1810 {
Chris@559 1811 if ( connected_ ) {
Chris@559 1812 errorString_ = "RtMidiIn::openPort: a valid connection already exists!";
Chris@559 1813 error( RtError::WARNING );
Chris@559 1814 return;
Chris@559 1815 }
Chris@559 1816
Chris@559 1817 unsigned int nDevices = midiInGetNumDevs();
Chris@559 1818 if (nDevices == 0) {
Chris@559 1819 errorString_ = "RtMidiIn::openPort: no MIDI input sources found!";
Chris@559 1820 error( RtError::NO_DEVICES_FOUND );
Chris@559 1821 }
Chris@559 1822
Chris@559 1823 std::ostringstream ost;
Chris@559 1824 if ( portNumber >= nDevices ) {
Chris@559 1825 ost << "RtMidiIn::openPort: the 'portNumber' argument (" << portNumber << ") is invalid.";
Chris@559 1826 errorString_ = ost.str();
Chris@559 1827 error( RtError::INVALID_PARAMETER );
Chris@559 1828 }
Chris@559 1829
Chris@559 1830 WinMidiData *data = static_cast<WinMidiData *> (apiData_);
Chris@559 1831 MMRESULT result = midiInOpen( &data->inHandle,
Chris@559 1832 portNumber,
Chris@559 1833 (DWORD)&midiInputCallback,
Chris@559 1834 (DWORD)&inputData_,
Chris@559 1835 CALLBACK_FUNCTION );
Chris@559 1836 if ( result != MMSYSERR_NOERROR ) {
Chris@559 1837 errorString_ = "RtMidiIn::openPort: error creating Windows MM MIDI input port.";
Chris@559 1838 error( RtError::DRIVER_ERROR );
Chris@559 1839 }
Chris@559 1840
Chris@559 1841 // Allocate and init the sysex buffer.
Chris@559 1842 data->sysexBuffer = (MIDIHDR*) new char[ sizeof(MIDIHDR) ];
Chris@559 1843 data->sysexBuffer->lpData = new char[1024];
Chris@559 1844 data->sysexBuffer->dwBufferLength = 1024;
Chris@559 1845 data->sysexBuffer->dwFlags = 0;
Chris@559 1846
Chris@559 1847 result = midiInPrepareHeader( data->inHandle, data->sysexBuffer, sizeof(MIDIHDR) );
Chris@559 1848 if ( result != MMSYSERR_NOERROR ) {
Chris@559 1849 midiInClose( data->inHandle );
Chris@559 1850 errorString_ = "RtMidiIn::openPort: error starting Windows MM MIDI input port (PrepareHeader).";
Chris@559 1851 error( RtError::DRIVER_ERROR );
Chris@559 1852 }
Chris@559 1853
Chris@559 1854 // Register the buffer.
Chris@559 1855 result = midiInAddBuffer( data->inHandle, data->sysexBuffer, sizeof(MIDIHDR) );
Chris@559 1856 if ( result != MMSYSERR_NOERROR ) {
Chris@559 1857 midiInClose( data->inHandle );
Chris@559 1858 errorString_ = "RtMidiIn::openPort: error starting Windows MM MIDI input port (AddBuffer).";
Chris@559 1859 error( RtError::DRIVER_ERROR );
Chris@559 1860 }
Chris@559 1861
Chris@559 1862 result = midiInStart( data->inHandle );
Chris@559 1863 if ( result != MMSYSERR_NOERROR ) {
Chris@559 1864 midiInClose( data->inHandle );
Chris@559 1865 errorString_ = "RtMidiIn::openPort: error starting Windows MM MIDI input port.";
Chris@559 1866 error( RtError::DRIVER_ERROR );
Chris@559 1867 }
Chris@559 1868
Chris@559 1869 connected_ = true;
Chris@559 1870 }
Chris@559 1871
Chris@559 1872 void RtMidiIn :: openVirtualPort( std::string portName )
Chris@559 1873 {
Chris@559 1874 // This function cannot be implemented for the Windows MM MIDI API.
Chris@559 1875 errorString_ = "RtMidiIn::openVirtualPort: cannot be implemented in Windows MM MIDI API!";
Chris@559 1876 error( RtError::WARNING );
Chris@559 1877 }
Chris@559 1878
Chris@559 1879 void RtMidiIn :: closePort( void )
Chris@559 1880 {
Chris@559 1881 if ( connected_ ) {
Chris@559 1882 WinMidiData *data = static_cast<WinMidiData *> (apiData_);
Chris@559 1883 midiInReset( data->inHandle );
Chris@559 1884 midiInStop( data->inHandle );
Chris@559 1885
Chris@559 1886 int result = midiInUnprepareHeader(data->inHandle, data->sysexBuffer, sizeof(MIDIHDR));
Chris@559 1887 delete [] data->sysexBuffer->lpData;
Chris@559 1888 delete [] data->sysexBuffer;
Chris@559 1889 if ( result != MMSYSERR_NOERROR ) {
Chris@559 1890 midiInClose( data->inHandle );
Chris@559 1891 errorString_ = "RtMidiIn::openPort: error closing Windows MM MIDI input port (midiInUnprepareHeader).";
Chris@559 1892 error( RtError::DRIVER_ERROR );
Chris@559 1893 }
Chris@559 1894
Chris@559 1895 midiInClose( data->inHandle );
Chris@559 1896 connected_ = false;
Chris@559 1897 }
Chris@559 1898 }
Chris@559 1899
Chris@559 1900 RtMidiIn :: ~RtMidiIn()
Chris@559 1901 {
Chris@559 1902 // Close a connection if it exists.
Chris@559 1903 closePort();
Chris@559 1904
Chris@559 1905 // Cleanup.
Chris@559 1906 WinMidiData *data = static_cast<WinMidiData *> (apiData_);
Chris@559 1907 delete data;
Chris@559 1908 }
Chris@559 1909
Chris@559 1910 unsigned int RtMidiIn :: getPortCount()
Chris@559 1911 {
Chris@559 1912 return midiInGetNumDevs();
Chris@559 1913 }
Chris@559 1914
Chris@559 1915 std::string RtMidiIn :: getPortName( unsigned int portNumber )
Chris@559 1916 {
Chris@559 1917 unsigned int nDevices = midiInGetNumDevs();
Chris@559 1918 if ( portNumber >= nDevices ) {
Chris@559 1919 std::ostringstream ost;
Chris@559 1920 ost << "RtMidiIn::getPortName: the 'portNumber' argument (" << portNumber << ") is invalid.";
Chris@559 1921 errorString_ = ost.str();
Chris@559 1922 error( RtError::INVALID_PARAMETER );
Chris@559 1923 }
Chris@559 1924
Chris@559 1925 MIDIINCAPS deviceCaps;
Chris@559 1926 midiInGetDevCaps( portNumber, &deviceCaps, sizeof(MIDIINCAPS));
Chris@559 1927
Chris@559 1928 // For some reason, we need to copy character by character with
Chris@559 1929 // UNICODE (thanks to Eduardo Coutinho!).
Chris@559 1930 //std::string stringName = std::string( deviceCaps.szPname );
Chris@559 1931 char nameString[MAXPNAMELEN];
Chris@559 1932 for( int i=0; i<MAXPNAMELEN; i++ )
Chris@559 1933 nameString[i] = (char)( deviceCaps.szPname[i] );
Chris@559 1934
Chris@559 1935 std::string stringName( nameString );
Chris@559 1936 return stringName;
Chris@559 1937 }
Chris@559 1938
Chris@559 1939 //*********************************************************************//
Chris@559 1940 // API: Windows MM
Chris@559 1941 // Class Definitions: RtMidiOut
Chris@559 1942 //*********************************************************************//
Chris@559 1943
Chris@559 1944 unsigned int RtMidiOut :: getPortCount()
Chris@559 1945 {
Chris@559 1946 return midiOutGetNumDevs();
Chris@559 1947 }
Chris@559 1948
Chris@559 1949 std::string RtMidiOut :: getPortName( unsigned int portNumber )
Chris@559 1950 {
Chris@559 1951 unsigned int nDevices = midiOutGetNumDevs();
Chris@559 1952 if ( portNumber >= nDevices ) {
Chris@559 1953 std::ostringstream ost;
Chris@559 1954 ost << "RtMidiOut::getPortName: the 'portNumber' argument (" << portNumber << ") is invalid.";
Chris@559 1955 errorString_ = ost.str();
Chris@559 1956 error( RtError::INVALID_PARAMETER );
Chris@559 1957 }
Chris@559 1958
Chris@559 1959 MIDIOUTCAPS deviceCaps;
Chris@559 1960 midiOutGetDevCaps( portNumber, &deviceCaps, sizeof(MIDIOUTCAPS));
Chris@559 1961
Chris@559 1962 // For some reason, we need to copy character by character with
Chris@559 1963 // UNICODE (thanks to Eduardo Coutinho!).
Chris@559 1964 //std::string stringName = std::string( deviceCaps.szPname );
Chris@559 1965 char nameString[MAXPNAMELEN];
Chris@559 1966 for( int i=0; i<MAXPNAMELEN; i++ )
Chris@559 1967 nameString[i] = (char)( deviceCaps.szPname[i] );
Chris@559 1968
Chris@559 1969 std::string stringName( nameString );
Chris@559 1970 return stringName;
Chris@559 1971 }
Chris@559 1972
Chris@559 1973 void RtMidiOut :: initialize( void )
Chris@559 1974 {
Chris@559 1975 // We'll issue a warning here if no devices are available but not
Chris@559 1976 // throw an error since the user can plug something in later.
Chris@559 1977 unsigned int nDevices = midiOutGetNumDevs();
Chris@559 1978 if ( nDevices == 0 ) {
Chris@559 1979 errorString_ = "RtMidiOut::initialize: no MIDI output devices currently available.";
Chris@559 1980 error( RtError::WARNING );
Chris@559 1981 }
Chris@559 1982
Chris@559 1983 // Save our api-specific connection information.
Chris@559 1984 WinMidiData *data = (WinMidiData *) new WinMidiData;
Chris@559 1985 apiData_ = (void *) data;
Chris@559 1986 }
Chris@559 1987
Chris@559 1988 void RtMidiOut :: openPort( unsigned int portNumber )
Chris@559 1989 {
Chris@559 1990 if ( connected_ ) {
Chris@559 1991 errorString_ = "RtMidiOut::openPort: a valid connection already exists!";
Chris@559 1992 error( RtError::WARNING );
Chris@559 1993 return;
Chris@559 1994 }
Chris@559 1995
Chris@559 1996 unsigned int nDevices = midiOutGetNumDevs();
Chris@559 1997 if (nDevices < 1) {
Chris@559 1998 errorString_ = "RtMidiOut::openPort: no MIDI output destinations found!";
Chris@559 1999 error( RtError::NO_DEVICES_FOUND );
Chris@559 2000 }
Chris@559 2001
Chris@559 2002 std::ostringstream ost;
Chris@559 2003 if ( portNumber >= nDevices ) {
Chris@559 2004 ost << "RtMidiOut::openPort: the 'portNumber' argument (" << portNumber << ") is invalid.";
Chris@559 2005 errorString_ = ost.str();
Chris@559 2006 error( RtError::INVALID_PARAMETER );
Chris@559 2007 }
Chris@559 2008
Chris@559 2009 WinMidiData *data = static_cast<WinMidiData *> (apiData_);
Chris@559 2010 MMRESULT result = midiOutOpen( &data->outHandle,
Chris@559 2011 portNumber,
Chris@559 2012 (DWORD)NULL,
Chris@559 2013 (DWORD)NULL,
Chris@559 2014 CALLBACK_NULL );
Chris@559 2015 if ( result != MMSYSERR_NOERROR ) {
Chris@559 2016 errorString_ = "RtMidiOut::openPort: error creating Windows MM MIDI output port.";
Chris@559 2017 error( RtError::DRIVER_ERROR );
Chris@559 2018 }
Chris@559 2019
Chris@559 2020 connected_ = true;
Chris@559 2021 }
Chris@559 2022
Chris@559 2023 void RtMidiOut :: closePort( void )
Chris@559 2024 {
Chris@559 2025 if ( connected_ ) {
Chris@559 2026 WinMidiData *data = static_cast<WinMidiData *> (apiData_);
Chris@559 2027 midiOutReset( data->outHandle );
Chris@559 2028 midiOutClose( data->outHandle );
Chris@559 2029 connected_ = false;
Chris@559 2030 }
Chris@559 2031 }
Chris@559 2032
Chris@559 2033 void RtMidiOut :: openVirtualPort( std::string portName )
Chris@559 2034 {
Chris@559 2035 // This function cannot be implemented for the Windows MM MIDI API.
Chris@559 2036 errorString_ = "RtMidiOut::openVirtualPort: cannot be implemented in Windows MM MIDI API!";
Chris@559 2037 error( RtError::WARNING );
Chris@559 2038 }
Chris@559 2039
Chris@559 2040 RtMidiOut :: ~RtMidiOut()
Chris@559 2041 {
Chris@559 2042 // Close a connection if it exists.
Chris@559 2043 closePort();
Chris@559 2044
Chris@559 2045 // Cleanup.
Chris@559 2046 WinMidiData *data = static_cast<WinMidiData *> (apiData_);
Chris@559 2047 delete data;
Chris@559 2048 }
Chris@559 2049
Chris@559 2050 void RtMidiOut :: sendMessage( std::vector<unsigned char> *message )
Chris@559 2051 {
Chris@559 2052 unsigned int nBytes = message->size();
Chris@559 2053 if ( nBytes == 0 ) {
Chris@559 2054 errorString_ = "RtMidiOut::sendMessage: message argument is empty!";
Chris@559 2055 error( RtError::WARNING );
Chris@559 2056 return;
Chris@559 2057 }
Chris@559 2058
Chris@559 2059 MMRESULT result;
Chris@559 2060 WinMidiData *data = static_cast<WinMidiData *> (apiData_);
Chris@559 2061 if ( message->at(0) == 0xF0 ) { // Sysex message
Chris@559 2062
Chris@559 2063 // Allocate buffer for sysex data.
Chris@559 2064 char *buffer = (char *) malloc( nBytes );
Chris@559 2065 if ( buffer == NULL ) {
Chris@559 2066 errorString_ = "RtMidiOut::sendMessage: error allocating sysex message memory!";
Chris@559 2067 error( RtError::MEMORY_ERROR );
Chris@559 2068 }
Chris@559 2069
Chris@559 2070 // Copy data to buffer.
Chris@559 2071 for ( unsigned int i=0; i<nBytes; i++ ) buffer[i] = message->at(i);
Chris@559 2072
Chris@559 2073 // Create and prepare MIDIHDR structure.
Chris@559 2074 MIDIHDR sysex;
Chris@559 2075 sysex.lpData = (LPSTR) buffer;
Chris@559 2076 sysex.dwBufferLength = nBytes;
Chris@559 2077 sysex.dwFlags = 0;
Chris@559 2078 result = midiOutPrepareHeader( data->outHandle, &sysex, sizeof(MIDIHDR) );
Chris@559 2079 if ( result != MMSYSERR_NOERROR ) {
Chris@559 2080 free( buffer );
Chris@559 2081 errorString_ = "RtMidiOut::sendMessage: error preparing sysex header.";
Chris@559 2082 error( RtError::DRIVER_ERROR );
Chris@559 2083 }
Chris@559 2084
Chris@559 2085 // Send the message.
Chris@559 2086 result = midiOutLongMsg( data->outHandle, &sysex, sizeof(MIDIHDR) );
Chris@559 2087 if ( result != MMSYSERR_NOERROR ) {
Chris@559 2088 free( buffer );
Chris@559 2089 errorString_ = "RtMidiOut::sendMessage: error sending sysex message.";
Chris@559 2090 error( RtError::DRIVER_ERROR );
Chris@559 2091 }
Chris@559 2092
Chris@559 2093 // Unprepare the buffer and MIDIHDR.
Chris@559 2094 while ( MIDIERR_STILLPLAYING == midiOutUnprepareHeader( data->outHandle, &sysex, sizeof (MIDIHDR) ) ) Sleep( 1 );
Chris@559 2095 free( buffer );
Chris@559 2096
Chris@559 2097 }
Chris@559 2098 else { // Channel or system message.
Chris@559 2099
Chris@559 2100 // Make sure the message size isn't too big.
Chris@559 2101 if ( nBytes > 3 ) {
Chris@559 2102 errorString_ = "RtMidiOut::sendMessage: message size is greater than 3 bytes (and not sysex)!";
Chris@559 2103 error( RtError::WARNING );
Chris@559 2104 return;
Chris@559 2105 }
Chris@559 2106
Chris@559 2107 // Pack MIDI bytes into double word.
Chris@559 2108 DWORD packet;
Chris@559 2109 unsigned char *ptr = (unsigned char *) &packet;
Chris@559 2110 for ( unsigned int i=0; i<nBytes; i++ ) {
Chris@559 2111 *ptr = message->at(i);
Chris@559 2112 ptr++;
Chris@559 2113 }
Chris@559 2114
Chris@559 2115 // Send the message immediately.
Chris@559 2116 result = midiOutShortMsg( data->outHandle, packet );
Chris@559 2117 if ( result != MMSYSERR_NOERROR ) {
Chris@559 2118 errorString_ = "RtMidiOut::sendMessage: error sending MIDI message.";
Chris@559 2119 error( RtError::DRIVER_ERROR );
Chris@559 2120 }
Chris@559 2121 }
Chris@559 2122 }
Chris@559 2123
Chris@559 2124 #endif // __WINDOWS_MM__