annotate data/midi/rtmidi/RtMidi.cpp @ 562:ecce042cc374

* Wire up MIDI input port (as far as printing a message when an event comes in, anyway)
author Chris Cannam
date Mon, 23 Feb 2009 17:50:17 +0000
parents 32d156c75df7
children a5e0d1068cae
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@562 72 RtMidiIn :: RtMidiIn(std::string name) : RtMidi()
Chris@559 73 {
Chris@562 74 this->initialize(name);
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@562 147 RtMidiOut :: RtMidiOut(std::string name) : RtMidi()
Chris@559 148 {
Chris@562 149 this->initialize(name);
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@562 314 void RtMidiIn :: initialize(std::string name)
Chris@559 315 {
Chris@559 316 // Set up our client.
Chris@559 317 MIDIClientRef client;
Chris@562 318 OSStatus result = MIDIClientCreate( CFSTR(name.c_str()), 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@562 482 void RtMidiOut :: initialize(std::string name)
Chris@559 483 {
Chris@559 484 // Set up our client.
Chris@559 485 MIDIClientRef client;
Chris@562 486 OSStatus result = MIDIClientCreate( CFSTR(name.c_str()), 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@562 816 void RtMidiIn :: initialize(std::string name)
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@562 827 snd_seq_set_client_name(seq, name.c_str());
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@562 888 snd_seq_port_info_t *pinfo;
Chris@562 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@562 898 char name[20];
Chris@562 899 sprintf(name, "Input %d", portNumber + 1);
Chris@559 900
Chris@559 901 snd_seq_addr_t sender, receiver;
Chris@559 902 sender.client = snd_seq_port_info_get_client( pinfo );
Chris@559 903 sender.port = snd_seq_port_info_get_port( pinfo );
Chris@559 904 receiver.client = snd_seq_client_id( data->seq );
Chris@559 905 if ( data->vport < 0 ) {
Chris@559 906 snd_seq_port_info_set_client( pinfo, 0 );
Chris@559 907 snd_seq_port_info_set_port( pinfo, 0 );
Chris@559 908 snd_seq_port_info_set_capability( pinfo,
Chris@559 909 SND_SEQ_PORT_CAP_WRITE |
Chris@559 910 SND_SEQ_PORT_CAP_SUBS_WRITE );
Chris@559 911 snd_seq_port_info_set_type( pinfo,
Chris@559 912 SND_SEQ_PORT_TYPE_MIDI_GENERIC |
Chris@559 913 SND_SEQ_PORT_TYPE_APPLICATION );
Chris@559 914 snd_seq_port_info_set_midi_channels(pinfo, 16);
Chris@559 915 snd_seq_port_info_set_timestamping(pinfo, 1);
Chris@559 916 snd_seq_port_info_set_timestamp_real(pinfo, 1);
Chris@559 917 snd_seq_port_info_set_timestamp_queue(pinfo, data->queue_id);
Chris@562 918 snd_seq_port_info_set_name(pinfo, name);
Chris@559 919 data->vport = snd_seq_create_port(data->seq, pinfo);
Chris@559 920
Chris@559 921 if ( data->vport < 0 ) {
Chris@559 922 errorString_ = "RtMidiIn::openPort: ALSA error creating input port.";
Chris@559 923 error( RtError::DRIVER_ERROR );
Chris@559 924 }
Chris@559 925 }
Chris@559 926
Chris@559 927 receiver.port = data->vport;
Chris@559 928
Chris@559 929 // Make subscription
Chris@559 930 snd_seq_port_subscribe_malloc( &data->subscription );
Chris@559 931 snd_seq_port_subscribe_set_sender(data->subscription, &sender);
Chris@559 932 snd_seq_port_subscribe_set_dest(data->subscription, &receiver);
Chris@559 933 if ( snd_seq_subscribe_port(data->seq, data->subscription) ) {
Chris@559 934 errorString_ = "RtMidiIn::openPort: ALSA error making port connection.";
Chris@559 935 error( RtError::DRIVER_ERROR );
Chris@559 936 }
Chris@559 937
Chris@559 938 if ( inputData_.doInput == false ) {
Chris@559 939 // Start the input queue
Chris@559 940 snd_seq_start_queue( data->seq, data->queue_id, NULL );
Chris@559 941 snd_seq_drain_output( data->seq );
Chris@559 942 // Start our MIDI input thread.
Chris@559 943 pthread_attr_t attr;
Chris@559 944 pthread_attr_init(&attr);
Chris@559 945 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
Chris@559 946 pthread_attr_setschedpolicy(&attr, SCHED_OTHER);
Chris@559 947
Chris@559 948 inputData_.doInput = true;
Chris@559 949 int err = pthread_create(&data->thread, &attr, alsaMidiHandler, &inputData_);
Chris@559 950 pthread_attr_destroy(&attr);
Chris@559 951 if (err) {
Chris@559 952 snd_seq_unsubscribe_port( data->seq, data->subscription );
Chris@559 953 snd_seq_port_subscribe_free( data->subscription );
Chris@559 954 inputData_.doInput = false;
Chris@559 955 errorString_ = "RtMidiIn::openPort: error starting MIDI input thread!";
Chris@559 956 error( RtError::THREAD_ERROR );
Chris@559 957 }
Chris@559 958 }
Chris@559 959
Chris@559 960 connected_ = true;
Chris@559 961 }
Chris@559 962
Chris@559 963 void RtMidiIn :: openVirtualPort( std::string portName )
Chris@559 964 {
Chris@559 965 AlsaMidiData *data = static_cast<AlsaMidiData *> (apiData_);
Chris@559 966 if ( data->vport < 0 ) {
Chris@559 967 snd_seq_port_info_t *pinfo;
Chris@559 968 snd_seq_port_info_alloca( &pinfo );
Chris@559 969 snd_seq_port_info_set_capability( pinfo,
Chris@559 970 SND_SEQ_PORT_CAP_WRITE |
Chris@559 971 SND_SEQ_PORT_CAP_SUBS_WRITE );
Chris@559 972 snd_seq_port_info_set_type( pinfo,
Chris@559 973 SND_SEQ_PORT_TYPE_MIDI_GENERIC |
Chris@559 974 SND_SEQ_PORT_TYPE_APPLICATION );
Chris@559 975 snd_seq_port_info_set_midi_channels(pinfo, 16);
Chris@559 976 snd_seq_port_info_set_timestamping(pinfo, 1);
Chris@559 977 snd_seq_port_info_set_timestamp_real(pinfo, 1);
Chris@559 978 snd_seq_port_info_set_timestamp_queue(pinfo, data->queue_id);
Chris@559 979 snd_seq_port_info_set_name(pinfo, portName.c_str());
Chris@559 980 data->vport = snd_seq_create_port(data->seq, pinfo);
Chris@559 981
Chris@559 982 if ( data->vport < 0 ) {
Chris@559 983 errorString_ = "RtMidiIn::openVirtualPort: ALSA error creating virtual port.";
Chris@559 984 error( RtError::DRIVER_ERROR );
Chris@559 985 }
Chris@559 986 }
Chris@559 987
Chris@559 988 if ( inputData_.doInput == false ) {
Chris@559 989 // Start the input queue
Chris@559 990 snd_seq_start_queue( data->seq, data->queue_id, NULL );
Chris@559 991 snd_seq_drain_output( data->seq );
Chris@559 992 // Start our MIDI input thread.
Chris@559 993 pthread_attr_t attr;
Chris@559 994 pthread_attr_init(&attr);
Chris@559 995 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
Chris@559 996 pthread_attr_setschedpolicy(&attr, SCHED_OTHER);
Chris@559 997
Chris@559 998 inputData_.doInput = true;
Chris@559 999 int err = pthread_create(&data->thread, &attr, alsaMidiHandler, &inputData_);
Chris@559 1000 pthread_attr_destroy(&attr);
Chris@559 1001 if (err) {
Chris@559 1002 snd_seq_unsubscribe_port( data->seq, data->subscription );
Chris@559 1003 snd_seq_port_subscribe_free( data->subscription );
Chris@559 1004 inputData_.doInput = false;
Chris@559 1005 errorString_ = "RtMidiIn::openPort: error starting MIDI input thread!";
Chris@559 1006 error( RtError::THREAD_ERROR );
Chris@559 1007 }
Chris@559 1008 }
Chris@559 1009 }
Chris@559 1010
Chris@559 1011 void RtMidiIn :: closePort( void )
Chris@559 1012 {
Chris@559 1013 if ( connected_ ) {
Chris@559 1014 AlsaMidiData *data = static_cast<AlsaMidiData *> (apiData_);
Chris@559 1015 snd_seq_unsubscribe_port( data->seq, data->subscription );
Chris@559 1016 snd_seq_port_subscribe_free( data->subscription );
Chris@559 1017 // Stop the input queue
Chris@559 1018 snd_seq_stop_queue( data->seq, data->queue_id, NULL );
Chris@559 1019 snd_seq_drain_output( data->seq );
Chris@559 1020 connected_ = false;
Chris@559 1021 }
Chris@559 1022 }
Chris@559 1023
Chris@559 1024 RtMidiIn :: ~RtMidiIn()
Chris@559 1025 {
Chris@559 1026 // Close a connection if it exists.
Chris@559 1027 closePort();
Chris@559 1028
Chris@559 1029 // Shutdown the input thread.
Chris@559 1030 AlsaMidiData *data = static_cast<AlsaMidiData *> (apiData_);
Chris@559 1031 if ( inputData_.doInput ) {
Chris@559 1032 inputData_.doInput = false;
Chris@559 1033 pthread_join( data->thread, NULL );
Chris@559 1034 }
Chris@559 1035
Chris@559 1036 // Cleanup.
Chris@559 1037 if ( data->vport >= 0 ) snd_seq_delete_port( data->seq, data->vport );
Chris@559 1038 snd_seq_free_queue( data->seq, data->queue_id );
Chris@559 1039 snd_seq_close( data->seq );
Chris@559 1040 delete data;
Chris@559 1041 }
Chris@559 1042
Chris@559 1043 unsigned int RtMidiIn :: getPortCount()
Chris@559 1044 {
Chris@559 1045 snd_seq_port_info_t *pinfo;
Chris@559 1046 snd_seq_port_info_alloca( &pinfo );
Chris@559 1047
Chris@559 1048 AlsaMidiData *data = static_cast<AlsaMidiData *> (apiData_);
Chris@559 1049 return portInfo( data->seq, pinfo, SND_SEQ_PORT_CAP_READ|SND_SEQ_PORT_CAP_SUBS_READ, -1 );
Chris@559 1050 }
Chris@559 1051
Chris@559 1052 std::string RtMidiIn :: getPortName( unsigned int portNumber )
Chris@559 1053 {
Chris@559 1054 snd_seq_port_info_t *pinfo;
Chris@559 1055 snd_seq_port_info_alloca( &pinfo );
Chris@559 1056
Chris@559 1057 AlsaMidiData *data = static_cast<AlsaMidiData *> (apiData_);
Chris@559 1058 if ( portInfo( data->seq, pinfo, SND_SEQ_PORT_CAP_READ|SND_SEQ_PORT_CAP_SUBS_READ, (int) portNumber ) ) {
Chris@559 1059 std::string stringName = std::string( snd_seq_port_info_get_name( pinfo ) );
Chris@559 1060 return stringName;
Chris@559 1061 }
Chris@559 1062
Chris@559 1063 // If we get here, we didn't find a match.
Chris@559 1064 errorString_ = "RtMidiIn::getPortName: error looking for port name!";
Chris@559 1065 error( RtError::INVALID_PARAMETER );
Chris@559 1066 return 0;
Chris@559 1067 }
Chris@559 1068
Chris@559 1069 //*********************************************************************//
Chris@559 1070 // API: LINUX ALSA
Chris@559 1071 // Class Definitions: RtMidiOut
Chris@559 1072 //*********************************************************************//
Chris@559 1073
Chris@559 1074 unsigned int RtMidiOut :: getPortCount()
Chris@559 1075 {
Chris@559 1076 snd_seq_port_info_t *pinfo;
Chris@559 1077 snd_seq_port_info_alloca( &pinfo );
Chris@559 1078
Chris@559 1079 AlsaMidiData *data = static_cast<AlsaMidiData *> (apiData_);
Chris@559 1080 return portInfo( data->seq, pinfo, SND_SEQ_PORT_CAP_WRITE|SND_SEQ_PORT_CAP_SUBS_WRITE, -1 );
Chris@559 1081 }
Chris@559 1082
Chris@559 1083 std::string RtMidiOut :: getPortName( unsigned int portNumber )
Chris@559 1084 {
Chris@559 1085 snd_seq_port_info_t *pinfo;
Chris@559 1086 snd_seq_port_info_alloca( &pinfo );
Chris@559 1087
Chris@559 1088 AlsaMidiData *data = static_cast<AlsaMidiData *> (apiData_);
Chris@559 1089 if ( portInfo( data->seq, pinfo, SND_SEQ_PORT_CAP_WRITE|SND_SEQ_PORT_CAP_SUBS_WRITE, (int) portNumber ) ) {
Chris@559 1090 std::string stringName = std::string( snd_seq_port_info_get_name( pinfo ) );
Chris@559 1091 return stringName;
Chris@559 1092 }
Chris@559 1093
Chris@559 1094 // If we get here, we didn't find a match.
Chris@559 1095 errorString_ = "RtMidiOut::getPortName: error looking for port name!";
Chris@559 1096 error( RtError::INVALID_PARAMETER );
Chris@559 1097 return 0;
Chris@559 1098 }
Chris@559 1099
Chris@562 1100 void RtMidiOut :: initialize(std::string name)
Chris@559 1101 {
Chris@559 1102 // Set up the ALSA sequencer client.
Chris@559 1103 snd_seq_t *seq;
Chris@559 1104 int result = snd_seq_open(&seq, "default", SND_SEQ_OPEN_OUTPUT, 0);
Chris@559 1105 if ( result < 0 ) {
Chris@559 1106 errorString_ = "RtMidiOut::initialize: error creating ALSA sequencer client object.";
Chris@559 1107 error( RtError::DRIVER_ERROR );
Chris@559 1108 }
Chris@559 1109
Chris@559 1110 // Set client name.
Chris@562 1111 snd_seq_set_client_name(seq, name.c_str());
Chris@559 1112
Chris@559 1113 // Save our api-specific connection information.
Chris@559 1114 AlsaMidiData *data = (AlsaMidiData *) new AlsaMidiData;
Chris@559 1115 data->seq = seq;
Chris@559 1116 data->vport = -1;
Chris@559 1117 data->bufferSize = 32;
Chris@559 1118 data->coder = 0;
Chris@559 1119 data->buffer = 0;
Chris@559 1120 result = snd_midi_event_new( data->bufferSize, &data->coder );
Chris@559 1121 if ( result < 0 ) {
Chris@559 1122 delete data;
Chris@559 1123 errorString_ = "RtMidiOut::initialize: error initializing MIDI event parser!\n\n";
Chris@559 1124 error( RtError::DRIVER_ERROR );
Chris@559 1125 }
Chris@559 1126 data->buffer = (unsigned char *) malloc( data->bufferSize );
Chris@559 1127 if ( data->buffer == NULL ) {
Chris@559 1128 delete data;
Chris@559 1129 errorString_ = "RtMidiOut::initialize: error allocating buffer memory!\n\n";
Chris@559 1130 error( RtError::MEMORY_ERROR );
Chris@559 1131 }
Chris@559 1132 snd_midi_event_init( data->coder );
Chris@559 1133 apiData_ = (void *) data;
Chris@559 1134 }
Chris@559 1135
Chris@559 1136 void RtMidiOut :: openPort( unsigned int portNumber )
Chris@559 1137 {
Chris@559 1138 if ( connected_ ) {
Chris@559 1139 errorString_ = "RtMidiOut::openPort: a valid connection already exists!";
Chris@559 1140 error( RtError::WARNING );
Chris@559 1141 return;
Chris@559 1142 }
Chris@559 1143
Chris@559 1144 unsigned int nSrc = this->getPortCount();
Chris@559 1145 if (nSrc < 1) {
Chris@559 1146 errorString_ = "RtMidiOut::openPort: no MIDI output sources found!";
Chris@559 1147 error( RtError::NO_DEVICES_FOUND );
Chris@559 1148 }
Chris@559 1149
Chris@562 1150 snd_seq_port_info_t *pinfo;
Chris@562 1151 snd_seq_port_info_alloca( &pinfo );
Chris@559 1152 std::ostringstream ost;
Chris@559 1153 AlsaMidiData *data = static_cast<AlsaMidiData *> (apiData_);
Chris@559 1154 if ( portInfo( data->seq, pinfo, SND_SEQ_PORT_CAP_WRITE|SND_SEQ_PORT_CAP_SUBS_WRITE, (int) portNumber ) == 0 ) {
Chris@559 1155 ost << "RtMidiOut::openPort: the 'portNumber' argument (" << portNumber << ") is invalid.";
Chris@559 1156 errorString_ = ost.str();
Chris@559 1157 error( RtError::INVALID_PARAMETER );
Chris@559 1158 }
Chris@559 1159
Chris@559 1160 snd_seq_addr_t sender, receiver;
Chris@559 1161 receiver.client = snd_seq_port_info_get_client( pinfo );
Chris@559 1162 receiver.port = snd_seq_port_info_get_port( pinfo );
Chris@559 1163 sender.client = snd_seq_client_id( data->seq );
Chris@559 1164
Chris@562 1165 char name[20];
Chris@562 1166 sprintf(name, "Output %d", portNumber + 1);
Chris@562 1167
Chris@559 1168 if ( data->vport < 0 ) {
Chris@562 1169 data->vport = snd_seq_create_simple_port( data->seq, name,
Chris@559 1170 SND_SEQ_PORT_CAP_READ|SND_SEQ_PORT_CAP_SUBS_READ,
Chris@559 1171 SND_SEQ_PORT_TYPE_MIDI_GENERIC );
Chris@559 1172 if ( data->vport < 0 ) {
Chris@559 1173 errorString_ = "RtMidiOut::openPort: ALSA error creating output port.";
Chris@559 1174 error( RtError::DRIVER_ERROR );
Chris@559 1175 }
Chris@559 1176 }
Chris@559 1177
Chris@559 1178 sender.port = data->vport;
Chris@559 1179
Chris@559 1180 // Make subscription
Chris@559 1181 snd_seq_port_subscribe_malloc( &data->subscription );
Chris@559 1182 snd_seq_port_subscribe_set_sender(data->subscription, &sender);
Chris@559 1183 snd_seq_port_subscribe_set_dest(data->subscription, &receiver);
Chris@559 1184 snd_seq_port_subscribe_set_time_update(data->subscription, 1);
Chris@559 1185 snd_seq_port_subscribe_set_time_real(data->subscription, 1);
Chris@559 1186 if ( snd_seq_subscribe_port(data->seq, data->subscription) ) {
Chris@559 1187 errorString_ = "RtMidiOut::openPort: ALSA error making port connection.";
Chris@559 1188 error( RtError::DRIVER_ERROR );
Chris@559 1189 }
Chris@559 1190
Chris@559 1191 connected_ = true;
Chris@559 1192 }
Chris@559 1193
Chris@559 1194 void RtMidiOut :: closePort( void )
Chris@559 1195 {
Chris@559 1196 if ( connected_ ) {
Chris@559 1197 AlsaMidiData *data = static_cast<AlsaMidiData *> (apiData_);
Chris@559 1198 snd_seq_unsubscribe_port( data->seq, data->subscription );
Chris@559 1199 snd_seq_port_subscribe_free( data->subscription );
Chris@559 1200 connected_ = false;
Chris@559 1201 }
Chris@559 1202 }
Chris@559 1203
Chris@559 1204 void RtMidiOut :: openVirtualPort( std::string portName )
Chris@559 1205 {
Chris@559 1206 AlsaMidiData *data = static_cast<AlsaMidiData *> (apiData_);
Chris@559 1207 if ( data->vport < 0 ) {
Chris@559 1208 data->vport = snd_seq_create_simple_port( data->seq, portName.c_str(),
Chris@559 1209 SND_SEQ_PORT_CAP_READ|SND_SEQ_PORT_CAP_SUBS_READ,
Chris@559 1210 SND_SEQ_PORT_TYPE_MIDI_GENERIC );
Chris@559 1211
Chris@559 1212 if ( data->vport < 0 ) {
Chris@559 1213 errorString_ = "RtMidiOut::openVirtualPort: ALSA error creating virtual port.";
Chris@559 1214 error( RtError::DRIVER_ERROR );
Chris@559 1215 }
Chris@559 1216 }
Chris@559 1217 }
Chris@559 1218
Chris@559 1219 RtMidiOut :: ~RtMidiOut()
Chris@559 1220 {
Chris@559 1221 // Close a connection if it exists.
Chris@559 1222 closePort();
Chris@559 1223
Chris@559 1224 // Cleanup.
Chris@559 1225 AlsaMidiData *data = static_cast<AlsaMidiData *> (apiData_);
Chris@559 1226 if ( data->vport >= 0 ) snd_seq_delete_port( data->seq, data->vport );
Chris@559 1227 if ( data->coder ) snd_midi_event_free( data->coder );
Chris@559 1228 if ( data->buffer ) free( data->buffer );
Chris@559 1229 snd_seq_close( data->seq );
Chris@559 1230 delete data;
Chris@559 1231 }
Chris@559 1232
Chris@559 1233 void RtMidiOut :: sendMessage( std::vector<unsigned char> *message )
Chris@559 1234 {
Chris@559 1235 int result;
Chris@559 1236 AlsaMidiData *data = static_cast<AlsaMidiData *> (apiData_);
Chris@559 1237 unsigned int nBytes = message->size();
Chris@559 1238 if ( nBytes > data->bufferSize ) {
Chris@559 1239 data->bufferSize = nBytes;
Chris@559 1240 result = snd_midi_event_resize_buffer ( data->coder, nBytes);
Chris@559 1241 if ( result != 0 ) {
Chris@559 1242 errorString_ = "RtMidiOut::sendMessage: ALSA error resizing MIDI event buffer.";
Chris@559 1243 error( RtError::DRIVER_ERROR );
Chris@559 1244 }
Chris@559 1245 free (data->buffer);
Chris@559 1246 data->buffer = (unsigned char *) malloc( data->bufferSize );
Chris@559 1247 if ( data->buffer == NULL ) {
Chris@559 1248 errorString_ = "RtMidiOut::initialize: error allocating buffer memory!\n\n";
Chris@559 1249 error( RtError::MEMORY_ERROR );
Chris@559 1250 }
Chris@559 1251 }
Chris@559 1252
Chris@559 1253 snd_seq_event_t ev;
Chris@559 1254 snd_seq_ev_clear(&ev);
Chris@559 1255 snd_seq_ev_set_source(&ev, data->vport);
Chris@559 1256 snd_seq_ev_set_subs(&ev);
Chris@559 1257 snd_seq_ev_set_direct(&ev);
Chris@559 1258 for ( unsigned int i=0; i<nBytes; i++ ) data->buffer[i] = message->at(i);
Chris@559 1259 result = snd_midi_event_encode( data->coder, data->buffer, (long)nBytes, &ev );
Chris@559 1260 if ( result < (int)nBytes ) {
Chris@559 1261 errorString_ = "RtMidiOut::sendMessage: event parsing error!";
Chris@559 1262 error( RtError::WARNING );
Chris@559 1263 return;
Chris@559 1264 }
Chris@559 1265
Chris@559 1266 // Send the event.
Chris@559 1267 result = snd_seq_event_output(data->seq, &ev);
Chris@559 1268 if ( result < 0 ) {
Chris@559 1269 errorString_ = "RtMidiOut::sendMessage: error sending MIDI message to port.";
Chris@559 1270 error( RtError::WARNING );
Chris@559 1271 }
Chris@559 1272 snd_seq_drain_output(data->seq);
Chris@559 1273 }
Chris@559 1274
Chris@559 1275 #endif // __LINUX_ALSA__
Chris@559 1276
Chris@559 1277
Chris@559 1278 //*********************************************************************//
Chris@559 1279 // API: IRIX MD
Chris@559 1280 //*********************************************************************//
Chris@559 1281
Chris@559 1282 // API information gleamed from:
Chris@559 1283 // http://techpubs.sgi.com/library/tpl/cgi-bin/getdoc.cgi?cmd=getdoc&coll=0650&db=man&fname=3%20mdIntro
Chris@559 1284
Chris@559 1285 // If the Makefile doesn't work, try the following:
Chris@559 1286 // CC -o midiinfo -LANG:std -D__IRIX_MD__ -I../ ../RtMidi.cpp midiinfo.cpp -lpthread -lmd
Chris@559 1287 // CC -o midiout -LANG:std -D__IRIX_MD__ -I../ ../RtMidi.cpp midiout.cpp -lpthread -lmd
Chris@559 1288 // CC -o qmidiin -LANG:std -D__IRIX_MD__ -I../ ../RtMidi.cpp qmidiin.cpp -lpthread -lmd
Chris@559 1289 // CC -o cmidiin -LANG:std -D__IRIX_MD__ -I../ ../RtMidi.cpp cmidiin.cpp -lpthread -lmd
Chris@559 1290
Chris@559 1291 #if defined(__IRIX_MD__)
Chris@559 1292
Chris@559 1293 #include <pthread.h>
Chris@559 1294 #include <sys/time.h>
Chris@559 1295 #include <unistd.h>
Chris@559 1296
Chris@559 1297 // Irix MIDI header file.
Chris@559 1298 #include <dmedia/midi.h>
Chris@559 1299
Chris@559 1300 // A structure to hold variables related to the IRIX API
Chris@559 1301 // implementation.
Chris@559 1302 struct IrixMidiData {
Chris@559 1303 MDport port;
Chris@559 1304 pthread_t thread;
Chris@559 1305 };
Chris@559 1306
Chris@559 1307 //*********************************************************************//
Chris@559 1308 // API: IRIX
Chris@559 1309 // Class Definitions: RtMidiIn
Chris@559 1310 //*********************************************************************//
Chris@559 1311
Chris@559 1312 extern "C" void *irixMidiHandler( void *ptr )
Chris@559 1313 {
Chris@559 1314 RtMidiIn::RtMidiInData *data = static_cast<RtMidiIn::RtMidiInData *> (ptr);
Chris@559 1315 IrixMidiData *apiData = static_cast<IrixMidiData *> (data->apiData);
Chris@559 1316
Chris@559 1317 bool continueSysex = false;
Chris@559 1318 unsigned char status;
Chris@559 1319 unsigned short size;
Chris@559 1320 MDevent event;
Chris@559 1321 int fd = mdGetFd( apiData->port );
Chris@559 1322 if ( fd < 0 ) {
Chris@559 1323 data->doInput = false;
Chris@559 1324 std::cerr << "\nRtMidiIn::irixMidiHandler: error getting port descriptor!\n\n";
Chris@559 1325 return 0;
Chris@559 1326 }
Chris@559 1327
Chris@559 1328 fd_set mask, rmask;
Chris@559 1329 FD_ZERO( &mask );
Chris@559 1330 FD_SET( fd, &mask );
Chris@559 1331 struct timeval timeout = {0, 0};
Chris@559 1332 RtMidiIn::MidiMessage message;
Chris@559 1333 int result;
Chris@559 1334
Chris@559 1335 while ( data->doInput ) {
Chris@559 1336
Chris@559 1337 rmask = mask;
Chris@559 1338 timeout.tv_sec = 0;
Chris@559 1339 timeout.tv_usec = 0;
Chris@559 1340 if ( select( fd+1, &rmask, NULL, NULL, &timeout ) <= 0 ) {
Chris@559 1341 // No data pending ... sleep a bit.
Chris@559 1342 usleep( 1000 );
Chris@559 1343 continue;
Chris@559 1344 }
Chris@559 1345
Chris@559 1346 // If here, there should be data.
Chris@559 1347 result = mdReceive( apiData->port, &event, 1);
Chris@559 1348 if ( result <= 0 ) {
Chris@559 1349 std::cerr << "\nRtMidiIn::irixMidiHandler: MIDI input read error!\n\n";
Chris@559 1350 continue;
Chris@559 1351 }
Chris@559 1352
Chris@559 1353 message.timeStamp = event.stamp * 0.000000001;
Chris@559 1354
Chris@559 1355 size = 0;
Chris@559 1356 status = event.msg[0];
Chris@559 1357 if ( !(status & 0x80) ) continue;
Chris@559 1358 if ( status == 0xF0 ) {
Chris@559 1359 // Sysex message ... can be segmented across multiple messages.
Chris@559 1360 if ( !(data->ignoreFlags & 0x01) ) {
Chris@559 1361 if ( continueSysex ) {
Chris@559 1362 // We have a continuing, segmented sysex message. Append
Chris@559 1363 // the new bytes to our existing message.
Chris@559 1364 for ( int i=0; i<event.msglen; i++ )
Chris@559 1365 message.bytes.push_back( event.sysexmsg[i] );
Chris@559 1366 if ( event.sysexmsg[event.msglen-1] == 0xF7 ) continueSysex = false;
Chris@559 1367 if ( !continueSysex ) {
Chris@559 1368 // If not a continuing sysex message, invoke the user callback function or queue the message.
Chris@559 1369 if ( data->usingCallback && message.bytes.size() > 0 ) {
Chris@559 1370 RtMidiIn::RtMidiCallback callback = (RtMidiIn::RtMidiCallback) data->userCallback;
Chris@559 1371 callback( message.timeStamp, &message.bytes, data->userData );
Chris@559 1372 }
Chris@559 1373 else {
Chris@559 1374 // As long as we haven't reached our queue size limit, push the message.
Chris@559 1375 if ( data->queueLimit > data->queue.size() )
Chris@559 1376 data->queue.push( message );
Chris@559 1377 else
Chris@559 1378 std::cerr << "\nRtMidiIn: message queue limit reached!!\n\n";
Chris@559 1379 }
Chris@559 1380 message.bytes.clear();
Chris@559 1381 }
Chris@559 1382 }
Chris@559 1383 }
Chris@559 1384 mdFree( NULL );
Chris@559 1385 continue;
Chris@559 1386 }
Chris@559 1387 else if ( status < 0xC0 ) size = 3;
Chris@559 1388 else if ( status < 0xE0 ) size = 2;
Chris@559 1389 else if ( status < 0xF0 ) size = 3;
Chris@559 1390 else if ( status < 0xF3 ) {
Chris@559 1391 if ( status == 0xF1 && !(data->ignoreFlags & 0x02) ) {
Chris@559 1392 // A MIDI time code message and we're not ignoring it.
Chris@559 1393 size = 3;
Chris@559 1394 }
Chris@559 1395 }
Chris@559 1396 else if ( status == 0xF3 ) size = 2;
Chris@559 1397 else if ( status == 0xF8 ) {
Chris@559 1398 if ( !(data->ignoreFlags & 0x02) ) {
Chris@559 1399 // A MIDI timing tick message and we're not ignoring it.
Chris@559 1400 size = 1;
Chris@559 1401 }
Chris@559 1402 }
Chris@559 1403 else if ( status == 0xFE ) { // MIDI active sensing
Chris@559 1404 if ( !(data->ignoreFlags & 0x04) )
Chris@559 1405 size = 1;
Chris@559 1406 }
Chris@559 1407 else size = 1;
Chris@559 1408
Chris@559 1409 // Copy the MIDI data to our vector.
Chris@559 1410 if ( size ) {
Chris@559 1411 message.bytes.assign( &event.msg[0], &event.msg[size] );
Chris@559 1412 // Invoke the user callback function or queue the message.
Chris@559 1413 if ( data->usingCallback ) {
Chris@559 1414 RtMidiIn::RtMidiCallback callback = (RtMidiIn::RtMidiCallback) data->userCallback;
Chris@559 1415 callback( message.timeStamp, &message.bytes, data->userData );
Chris@559 1416 }
Chris@559 1417 else {
Chris@559 1418 // As long as we haven't reached our queue size limit, push the message.
Chris@559 1419 if ( data->queueLimit > data->queue.size() )
Chris@559 1420 data->queue.push( message );
Chris@559 1421 else
Chris@559 1422 std::cerr << "\nRtMidiIn: message queue limit reached!!\n\n";
Chris@559 1423 }
Chris@559 1424 message.bytes.clear();
Chris@559 1425 }
Chris@559 1426 }
Chris@559 1427
Chris@559 1428 return 0;
Chris@559 1429 }
Chris@559 1430
Chris@562 1431 void RtMidiIn :: initialize(std::string name)
Chris@559 1432 {
Chris@559 1433 // Initialize the Irix MIDI system. At the moment, we will not
Chris@559 1434 // worry about a return value of zero (ports) because there is a
Chris@559 1435 // chance the user could plug something in after instantiation.
Chris@559 1436 int nPorts = mdInit();
Chris@559 1437
Chris@559 1438 // Create our api-specific connection information.
Chris@559 1439 IrixMidiData *data = (IrixMidiData *) new IrixMidiData;
Chris@559 1440 apiData_ = (void *) data;
Chris@559 1441 inputData_.apiData = (void *) data;
Chris@559 1442 }
Chris@559 1443
Chris@559 1444 void RtMidiIn :: openPort( unsigned int portNumber )
Chris@559 1445 {
Chris@559 1446 if ( connected_ ) {
Chris@559 1447 errorString_ = "RtMidiIn::openPort: a valid connection already exists!";
Chris@559 1448 error( RtError::WARNING );
Chris@559 1449 return;
Chris@559 1450 }
Chris@559 1451
Chris@559 1452 int nPorts = mdInit();
Chris@559 1453 if (nPorts < 1) {
Chris@559 1454 errorString_ = "RtMidiIn::openPort: no Irix MIDI input sources found!";
Chris@559 1455 error( RtError::NO_DEVICES_FOUND );
Chris@559 1456 }
Chris@559 1457
Chris@559 1458 std::ostringstream ost;
Chris@559 1459 if ( portNumber >= nPorts ) {
Chris@559 1460 ost << "RtMidiIn::openPort: the 'portNumber' argument (" << portNumber << ") is invalid.";
Chris@559 1461 errorString_ = ost.str();
Chris@559 1462 error( RtError::INVALID_PARAMETER );
Chris@559 1463 }
Chris@559 1464
Chris@559 1465 IrixMidiData *data = static_cast<IrixMidiData *> (apiData_);
Chris@559 1466 data->port = mdOpenInPort( mdGetName(portNumber) );
Chris@559 1467 if ( data->port == NULL ) {
Chris@559 1468 ost << "RtMidiIn::openPort: Irix error opening the port (" << portNumber << ").";
Chris@559 1469 errorString_ = ost.str();
Chris@559 1470 error( RtError::DRIVER_ERROR );
Chris@559 1471 }
Chris@559 1472 mdSetStampMode(data->port, MD_DELTASTAMP);
Chris@559 1473
Chris@559 1474 // Start our MIDI input thread.
Chris@559 1475 pthread_attr_t attr;
Chris@559 1476 pthread_attr_init(&attr);
Chris@559 1477 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
Chris@559 1478 pthread_attr_setschedpolicy(&attr, SCHED_RR);
Chris@559 1479
Chris@559 1480 inputData_.doInput = true;
Chris@559 1481 int err = pthread_create(&data->thread, &attr, irixMidiHandler, &inputData_);
Chris@559 1482 pthread_attr_destroy(&attr);
Chris@559 1483 if (err) {
Chris@559 1484 mdClosePort( data->port );
Chris@559 1485 inputData_.doInput = false;
Chris@559 1486 errorString_ = "RtMidiIn::openPort: error starting MIDI input thread!";
Chris@559 1487 error( RtError::THREAD_ERROR );
Chris@559 1488 }
Chris@559 1489
Chris@559 1490 connected_ = true;
Chris@559 1491 }
Chris@559 1492
Chris@559 1493 void RtMidiIn :: openVirtualPort( std::string portName )
Chris@559 1494 {
Chris@559 1495 // This function cannot be implemented for the Irix MIDI API.
Chris@559 1496 errorString_ = "RtMidiIn::openVirtualPort: cannot be implemented in Irix MIDI API!";
Chris@559 1497 error( RtError::WARNING );
Chris@559 1498 }
Chris@559 1499
Chris@559 1500 void RtMidiIn :: closePort( void )
Chris@559 1501 {
Chris@559 1502 if ( connected_ ) {
Chris@559 1503 IrixMidiData *data = static_cast<IrixMidiData *> (apiData_);
Chris@559 1504 mdClosePort( data->port );
Chris@559 1505 connected_ = false;
Chris@559 1506
Chris@559 1507 // Shutdown the input thread.
Chris@559 1508 inputData_.doInput = false;
Chris@559 1509 pthread_join( data->thread, NULL );
Chris@559 1510 }
Chris@559 1511 }
Chris@559 1512
Chris@559 1513 RtMidiIn :: ~RtMidiIn()
Chris@559 1514 {
Chris@559 1515 // Close a connection if it exists.
Chris@559 1516 closePort();
Chris@559 1517
Chris@559 1518 // Cleanup.
Chris@559 1519 IrixMidiData *data = static_cast<IrixMidiData *> (apiData_);
Chris@559 1520 delete data;
Chris@559 1521 }
Chris@559 1522
Chris@559 1523 unsigned int RtMidiIn :: getPortCount()
Chris@559 1524 {
Chris@559 1525 int nPorts = mdInit();
Chris@559 1526 if ( nPorts >= 0 ) return nPorts;
Chris@559 1527 else return 0;
Chris@559 1528 }
Chris@559 1529
Chris@559 1530 std::string RtMidiIn :: getPortName( unsigned int portNumber )
Chris@559 1531 {
Chris@559 1532 int nPorts = mdInit();
Chris@559 1533
Chris@559 1534 std::ostringstream ost;
Chris@559 1535 if ( portNumber >= nPorts ) {
Chris@559 1536 ost << "RtMidiIn::getPortName: the 'portNumber' argument (" << portNumber << ") is invalid.";
Chris@559 1537 errorString_ = ost.str();
Chris@559 1538 error( RtError::INVALID_PARAMETER );
Chris@559 1539 }
Chris@559 1540
Chris@559 1541 std::string stringName = std::string( mdGetName( portNumber ) );
Chris@559 1542 return stringName;
Chris@559 1543 }
Chris@559 1544
Chris@559 1545 //*********************************************************************//
Chris@559 1546 // API: IRIX MD
Chris@559 1547 // Class Definitions: RtMidiOut
Chris@559 1548 //*********************************************************************//
Chris@559 1549
Chris@559 1550 unsigned int RtMidiOut :: getPortCount()
Chris@559 1551 {
Chris@559 1552 int nPorts = mdInit();
Chris@559 1553 if ( nPorts >= 0 ) return nPorts;
Chris@559 1554 else return 0;
Chris@559 1555 }
Chris@559 1556
Chris@559 1557 std::string RtMidiOut :: getPortName( unsigned int portNumber )
Chris@559 1558 {
Chris@559 1559 int nPorts = mdInit();
Chris@559 1560
Chris@559 1561 std::ostringstream ost;
Chris@559 1562 if ( portNumber >= nPorts ) {
Chris@559 1563 ost << "RtMidiIn::getPortName: the 'portNumber' argument (" << portNumber << ") is invalid.";
Chris@559 1564 errorString_ = ost.str();
Chris@559 1565 error( RtError::INVALID_PARAMETER );
Chris@559 1566 }
Chris@559 1567
Chris@559 1568 std::string stringName = std::string( mdGetName( portNumber ) );
Chris@559 1569 return stringName;
Chris@559 1570 }
Chris@559 1571
Chris@562 1572 void RtMidiOut :: initialize(std::string name)
Chris@559 1573 {
Chris@559 1574 // Initialize the Irix MIDI system. At the moment, we will not
Chris@559 1575 // worry about a return value of zero (ports) because there is a
Chris@559 1576 // chance the user could plug something in after instantiation.
Chris@559 1577 int nPorts = mdInit();
Chris@559 1578
Chris@559 1579 // Create our api-specific connection information.
Chris@559 1580 IrixMidiData *data = (IrixMidiData *) new IrixMidiData;
Chris@559 1581 apiData_ = (void *) data;
Chris@559 1582 }
Chris@559 1583
Chris@559 1584 void RtMidiOut :: openPort( unsigned int portNumber )
Chris@559 1585 {
Chris@559 1586 if ( connected_ ) {
Chris@559 1587 errorString_ = "RtMidiOut::openPort: a valid connection already exists!";
Chris@559 1588 error( RtError::WARNING );
Chris@559 1589 return;
Chris@559 1590 }
Chris@559 1591
Chris@559 1592 int nPorts = mdInit();
Chris@559 1593 if (nPorts < 1) {
Chris@559 1594 errorString_ = "RtMidiOut::openPort: no Irix MIDI output sources found!";
Chris@559 1595 error( RtError::NO_DEVICES_FOUND );
Chris@559 1596 }
Chris@559 1597
Chris@559 1598 std::ostringstream ost;
Chris@559 1599 if ( portNumber >= nPorts ) {
Chris@559 1600 ost << "RtMidiOut::openPort: the 'portNumber' argument (" << portNumber << ") is invalid.";
Chris@559 1601 errorString_ = ost.str();
Chris@559 1602 error( RtError::INVALID_PARAMETER );
Chris@559 1603 }
Chris@559 1604
Chris@559 1605 IrixMidiData *data = static_cast<IrixMidiData *> (apiData_);
Chris@559 1606 data->port = mdOpenOutPort( mdGetName(portNumber) );
Chris@559 1607 if ( data->port == NULL ) {
Chris@559 1608 ost << "RtMidiOut::openPort: Irix error opening the port (" << portNumber << ").";
Chris@559 1609 errorString_ = ost.str();
Chris@559 1610 error( RtError::DRIVER_ERROR );
Chris@559 1611 }
Chris@559 1612 mdSetStampMode(data->port, MD_NOSTAMP);
Chris@559 1613
Chris@559 1614 connected_ = true;
Chris@559 1615 }
Chris@559 1616
Chris@559 1617 void RtMidiOut :: closePort( void )
Chris@559 1618 {
Chris@559 1619 if ( connected_ ) {
Chris@559 1620 IrixMidiData *data = static_cast<IrixMidiData *> (apiData_);
Chris@559 1621 mdClosePort( data->port );
Chris@559 1622 connected_ = false;
Chris@559 1623 }
Chris@559 1624 }
Chris@559 1625
Chris@559 1626 void RtMidiOut :: openVirtualPort( std::string portName )
Chris@559 1627 {
Chris@559 1628 // This function cannot be implemented for the Irix MIDI API.
Chris@559 1629 errorString_ = "RtMidiOut::openVirtualPort: cannot be implemented in Irix MIDI API!";
Chris@559 1630 error( RtError::WARNING );
Chris@559 1631 }
Chris@559 1632
Chris@559 1633 RtMidiOut :: ~RtMidiOut()
Chris@559 1634 {
Chris@559 1635 // Close a connection if it exists.
Chris@559 1636 closePort();
Chris@559 1637
Chris@559 1638 // Cleanup.
Chris@559 1639 IrixMidiData *data = static_cast<IrixMidiData *> (apiData_);
Chris@559 1640 delete data;
Chris@559 1641 }
Chris@559 1642
Chris@559 1643 void RtMidiOut :: sendMessage( std::vector<unsigned char> *message )
Chris@559 1644 {
Chris@559 1645 int result;
Chris@559 1646 MDevent event;
Chris@559 1647 IrixMidiData *data = static_cast<IrixMidiData *> (apiData_);
Chris@559 1648 char *buffer = 0;
Chris@559 1649
Chris@559 1650 unsigned int nBytes = message->size();
Chris@559 1651 if ( nBytes == 0 ) return;
Chris@559 1652 event.stamp = 0;
Chris@559 1653 if ( message->at(0) == 0xF0 ) {
Chris@559 1654 if ( nBytes < 3 ) return; // check for bogus sysex
Chris@559 1655 event.msg[0] = 0xF0;
Chris@559 1656 event.msglen = nBytes;
Chris@559 1657 buffer = (char *) malloc( nBytes );
Chris@559 1658 for ( int i=0; i<nBytes; i++ ) buffer[i] = message->at(i);
Chris@559 1659 event.sysexmsg = buffer;
Chris@559 1660 }
Chris@559 1661 else {
Chris@559 1662 for ( int i=0; i<nBytes; i++ )
Chris@559 1663 event.msg[i] = message->at(i);
Chris@559 1664 }
Chris@559 1665
Chris@559 1666 // Send the event.
Chris@559 1667 result = mdSend( data->port, &event, 1 );
Chris@559 1668 if ( buffer ) free( buffer );
Chris@559 1669 if ( result < 1 ) {
Chris@559 1670 errorString_ = "RtMidiOut::sendMessage: IRIX error sending MIDI message!";
Chris@559 1671 error( RtError::WARNING );
Chris@559 1672 return;
Chris@559 1673 }
Chris@559 1674 }
Chris@559 1675
Chris@559 1676 #endif // __IRIX_MD__
Chris@559 1677
Chris@559 1678 //*********************************************************************//
Chris@559 1679 // API: Windows Multimedia Library (MM)
Chris@559 1680 //*********************************************************************//
Chris@559 1681
Chris@559 1682 // API information deciphered from:
Chris@559 1683 // - http://msdn.microsoft.com/library/default.asp?url=/library/en-us/multimed/htm/_win32_midi_reference.asp
Chris@559 1684
Chris@559 1685 // Thanks to Jean-Baptiste Berruchon for the sysex code.
Chris@559 1686
Chris@559 1687 #if defined(__WINDOWS_MM__)
Chris@559 1688
Chris@559 1689 // The Windows MM API is based on the use of a callback function for
Chris@559 1690 // MIDI input. We convert the system specific time stamps to delta
Chris@559 1691 // time values.
Chris@559 1692
Chris@559 1693 // Windows MM MIDI header files.
Chris@559 1694 #include <windows.h>
Chris@559 1695 #include <mmsystem.h>
Chris@559 1696
Chris@559 1697 // A structure to hold variables related to the CoreMIDI API
Chris@559 1698 // implementation.
Chris@559 1699 struct WinMidiData {
Chris@559 1700 HMIDIIN inHandle; // Handle to Midi Input Device
Chris@559 1701 HMIDIOUT outHandle; // Handle to Midi Output Device
Chris@559 1702 DWORD lastTime;
Chris@559 1703 RtMidiIn::MidiMessage message;
Chris@559 1704 LPMIDIHDR sysexBuffer;
Chris@559 1705 };
Chris@559 1706
Chris@559 1707 #define RT_SYSEX_BUFFER_SIZE 1024
Chris@559 1708
Chris@559 1709 //*********************************************************************//
Chris@559 1710 // API: Windows MM
Chris@559 1711 // Class Definitions: RtMidiIn
Chris@559 1712 //*********************************************************************//
Chris@559 1713
Chris@559 1714 static void CALLBACK midiInputCallback( HMIDIOUT hmin,
Chris@559 1715 UINT inputStatus,
Chris@559 1716 DWORD instancePtr,
Chris@559 1717 DWORD midiMessage,
Chris@559 1718 DWORD timestamp )
Chris@559 1719 {
Chris@559 1720 if ( inputStatus != MIM_DATA && inputStatus != MIM_LONGDATA ) return;
Chris@559 1721
Chris@559 1722 //RtMidiIn::RtMidiInData *data = static_cast<RtMidiIn::RtMidiInData *> (instancePtr);
Chris@559 1723 RtMidiIn::RtMidiInData *data = (RtMidiIn::RtMidiInData *)instancePtr;
Chris@559 1724 WinMidiData *apiData = static_cast<WinMidiData *> (data->apiData);
Chris@559 1725
Chris@559 1726 // Calculate time stamp.
Chris@559 1727 apiData->message.timeStamp = 0.0;
Chris@559 1728 if ( data->firstMessage == true ) data->firstMessage = false;
Chris@559 1729 else apiData->message.timeStamp = (double) ( timestamp - apiData->lastTime ) * 0.001;
Chris@559 1730 apiData->lastTime = timestamp;
Chris@559 1731
Chris@559 1732 if ( inputStatus == MIM_DATA ) { // Channel or system message
Chris@559 1733
Chris@559 1734 // Make sure the first byte is a status byte.
Chris@559 1735 unsigned char status = (unsigned char) (midiMessage & 0x000000FF);
Chris@559 1736 if ( !(status & 0x80) ) return;
Chris@559 1737
Chris@559 1738 // Determine the number of bytes in the MIDI message.
Chris@559 1739 unsigned short nBytes = 1;
Chris@559 1740 if ( status < 0xC0 ) nBytes = 3;
Chris@559 1741 else if ( status < 0xE0 ) nBytes = 2;
Chris@559 1742 else if ( status < 0xF0 ) nBytes = 3;
Chris@559 1743 else if ( status < 0xF3 ) {
Chris@559 1744 // A MIDI time code message and we're ignoring it.
Chris@559 1745 if ( status == 0xF1 && (data->ignoreFlags & 0x02) ) return;
Chris@559 1746 nBytes = 3;
Chris@559 1747 }
Chris@559 1748 else if ( status == 0xF3 ) nBytes = 2;
Chris@559 1749 else if ( status == 0xF8 && (data->ignoreFlags & 0x02) ) {
Chris@559 1750 // A MIDI timing tick message and we're ignoring it.
Chris@559 1751 return;
Chris@559 1752 }
Chris@559 1753 else if ( status == 0xFE && (data->ignoreFlags & 0x04) ) {
Chris@559 1754 // A MIDI active sensing message and we're ignoring it.
Chris@559 1755 return;
Chris@559 1756 }
Chris@559 1757
Chris@559 1758 // Copy bytes to our MIDI message.
Chris@559 1759 unsigned char *ptr = (unsigned char *) &midiMessage;
Chris@559 1760 for ( int i=0; i<nBytes; i++ ) apiData->message.bytes.push_back( *ptr++ );
Chris@559 1761 }
Chris@559 1762 else if ( !(data->ignoreFlags & 0x01) ) {
Chris@559 1763 // Sysex message and we're not ignoring it
Chris@559 1764 MIDIHDR *sysex = ( MIDIHDR *) midiMessage;
Chris@559 1765 for ( int i=0; i<(int)sysex->dwBytesRecorded; i++ )
Chris@559 1766 apiData->message.bytes.push_back( sysex->lpData[i] );
Chris@559 1767
Chris@559 1768 // When the callback has to be unaffected (application closes),
Chris@559 1769 // it seems WinMM calls it with an empty sysex to de-queue the buffer
Chris@559 1770 // If the buffer is requeued afer that message, the PC suddenly reboots
Chris@559 1771 // after one or two minutes (JB).
Chris@559 1772 if ( apiData->sysexBuffer->dwBytesRecorded > 0 ) {
Chris@559 1773 MMRESULT result = midiInAddBuffer( apiData->inHandle, apiData->sysexBuffer, sizeof(MIDIHDR) );
Chris@559 1774 if ( result != MMSYSERR_NOERROR )
Chris@559 1775 std::cerr << "\nRtMidiIn::midiInputCallback: error sending sysex to Midi device!!\n\n";
Chris@559 1776 }
Chris@559 1777 }
Chris@559 1778
Chris@559 1779 if ( data->usingCallback ) {
Chris@559 1780 RtMidiIn::RtMidiCallback callback = (RtMidiIn::RtMidiCallback) data->userCallback;
Chris@559 1781 callback( apiData->message.timeStamp, &apiData->message.bytes, data->userData );
Chris@559 1782 }
Chris@559 1783 else {
Chris@559 1784 // As long as we haven't reached our queue size limit, push the message.
Chris@559 1785 if ( data->queueLimit > data->queue.size() )
Chris@559 1786 data->queue.push( apiData->message );
Chris@559 1787 else
Chris@559 1788 std::cerr << "\nRtMidiIn: message queue limit reached!!\n\n";
Chris@559 1789 }
Chris@559 1790
Chris@559 1791 // Clear the vector for the next input message. Note that doing
Chris@559 1792 // this here allows our code to work for sysex messages which are
Chris@559 1793 // segmented across multiple buffers.
Chris@559 1794 apiData->message.bytes.clear();
Chris@559 1795 }
Chris@559 1796
Chris@562 1797 void RtMidiIn :: initialize(std::string name)
Chris@559 1798 {
Chris@559 1799 // We'll issue a warning here if no devices are available but not
Chris@559 1800 // throw an error since the user can plugin something later.
Chris@559 1801 unsigned int nDevices = midiInGetNumDevs();
Chris@559 1802 if ( nDevices == 0 ) {
Chris@559 1803 errorString_ = "RtMidiIn::initialize: no MIDI input devices currently available.";
Chris@559 1804 error( RtError::WARNING );
Chris@559 1805 }
Chris@559 1806
Chris@559 1807 // Save our api-specific connection information.
Chris@559 1808 WinMidiData *data = (WinMidiData *) new WinMidiData;
Chris@559 1809 apiData_ = (void *) data;
Chris@559 1810 inputData_.apiData = (void *) data;
Chris@559 1811 data->message.bytes.clear(); // needs to be empty for first input message
Chris@559 1812 }
Chris@559 1813
Chris@559 1814 void RtMidiIn :: openPort( unsigned int portNumber )
Chris@559 1815 {
Chris@559 1816 if ( connected_ ) {
Chris@559 1817 errorString_ = "RtMidiIn::openPort: a valid connection already exists!";
Chris@559 1818 error( RtError::WARNING );
Chris@559 1819 return;
Chris@559 1820 }
Chris@559 1821
Chris@559 1822 unsigned int nDevices = midiInGetNumDevs();
Chris@559 1823 if (nDevices == 0) {
Chris@559 1824 errorString_ = "RtMidiIn::openPort: no MIDI input sources found!";
Chris@559 1825 error( RtError::NO_DEVICES_FOUND );
Chris@559 1826 }
Chris@559 1827
Chris@559 1828 std::ostringstream ost;
Chris@559 1829 if ( portNumber >= nDevices ) {
Chris@559 1830 ost << "RtMidiIn::openPort: the 'portNumber' argument (" << portNumber << ") is invalid.";
Chris@559 1831 errorString_ = ost.str();
Chris@559 1832 error( RtError::INVALID_PARAMETER );
Chris@559 1833 }
Chris@559 1834
Chris@559 1835 WinMidiData *data = static_cast<WinMidiData *> (apiData_);
Chris@559 1836 MMRESULT result = midiInOpen( &data->inHandle,
Chris@559 1837 portNumber,
Chris@559 1838 (DWORD)&midiInputCallback,
Chris@559 1839 (DWORD)&inputData_,
Chris@559 1840 CALLBACK_FUNCTION );
Chris@559 1841 if ( result != MMSYSERR_NOERROR ) {
Chris@559 1842 errorString_ = "RtMidiIn::openPort: error creating Windows MM MIDI input port.";
Chris@559 1843 error( RtError::DRIVER_ERROR );
Chris@559 1844 }
Chris@559 1845
Chris@559 1846 // Allocate and init the sysex buffer.
Chris@559 1847 data->sysexBuffer = (MIDIHDR*) new char[ sizeof(MIDIHDR) ];
Chris@559 1848 data->sysexBuffer->lpData = new char[1024];
Chris@559 1849 data->sysexBuffer->dwBufferLength = 1024;
Chris@559 1850 data->sysexBuffer->dwFlags = 0;
Chris@559 1851
Chris@559 1852 result = midiInPrepareHeader( data->inHandle, data->sysexBuffer, sizeof(MIDIHDR) );
Chris@559 1853 if ( result != MMSYSERR_NOERROR ) {
Chris@559 1854 midiInClose( data->inHandle );
Chris@559 1855 errorString_ = "RtMidiIn::openPort: error starting Windows MM MIDI input port (PrepareHeader).";
Chris@559 1856 error( RtError::DRIVER_ERROR );
Chris@559 1857 }
Chris@559 1858
Chris@559 1859 // Register the buffer.
Chris@559 1860 result = midiInAddBuffer( data->inHandle, data->sysexBuffer, sizeof(MIDIHDR) );
Chris@559 1861 if ( result != MMSYSERR_NOERROR ) {
Chris@559 1862 midiInClose( data->inHandle );
Chris@559 1863 errorString_ = "RtMidiIn::openPort: error starting Windows MM MIDI input port (AddBuffer).";
Chris@559 1864 error( RtError::DRIVER_ERROR );
Chris@559 1865 }
Chris@559 1866
Chris@559 1867 result = midiInStart( data->inHandle );
Chris@559 1868 if ( result != MMSYSERR_NOERROR ) {
Chris@559 1869 midiInClose( data->inHandle );
Chris@559 1870 errorString_ = "RtMidiIn::openPort: error starting Windows MM MIDI input port.";
Chris@559 1871 error( RtError::DRIVER_ERROR );
Chris@559 1872 }
Chris@559 1873
Chris@559 1874 connected_ = true;
Chris@559 1875 }
Chris@559 1876
Chris@559 1877 void RtMidiIn :: openVirtualPort( std::string portName )
Chris@559 1878 {
Chris@559 1879 // This function cannot be implemented for the Windows MM MIDI API.
Chris@559 1880 errorString_ = "RtMidiIn::openVirtualPort: cannot be implemented in Windows MM MIDI API!";
Chris@559 1881 error( RtError::WARNING );
Chris@559 1882 }
Chris@559 1883
Chris@559 1884 void RtMidiIn :: closePort( void )
Chris@559 1885 {
Chris@559 1886 if ( connected_ ) {
Chris@559 1887 WinMidiData *data = static_cast<WinMidiData *> (apiData_);
Chris@559 1888 midiInReset( data->inHandle );
Chris@559 1889 midiInStop( data->inHandle );
Chris@559 1890
Chris@559 1891 int result = midiInUnprepareHeader(data->inHandle, data->sysexBuffer, sizeof(MIDIHDR));
Chris@559 1892 delete [] data->sysexBuffer->lpData;
Chris@559 1893 delete [] data->sysexBuffer;
Chris@559 1894 if ( result != MMSYSERR_NOERROR ) {
Chris@559 1895 midiInClose( data->inHandle );
Chris@559 1896 errorString_ = "RtMidiIn::openPort: error closing Windows MM MIDI input port (midiInUnprepareHeader).";
Chris@559 1897 error( RtError::DRIVER_ERROR );
Chris@559 1898 }
Chris@559 1899
Chris@559 1900 midiInClose( data->inHandle );
Chris@559 1901 connected_ = false;
Chris@559 1902 }
Chris@559 1903 }
Chris@559 1904
Chris@559 1905 RtMidiIn :: ~RtMidiIn()
Chris@559 1906 {
Chris@559 1907 // Close a connection if it exists.
Chris@559 1908 closePort();
Chris@559 1909
Chris@559 1910 // Cleanup.
Chris@559 1911 WinMidiData *data = static_cast<WinMidiData *> (apiData_);
Chris@559 1912 delete data;
Chris@559 1913 }
Chris@559 1914
Chris@559 1915 unsigned int RtMidiIn :: getPortCount()
Chris@559 1916 {
Chris@559 1917 return midiInGetNumDevs();
Chris@559 1918 }
Chris@559 1919
Chris@559 1920 std::string RtMidiIn :: getPortName( unsigned int portNumber )
Chris@559 1921 {
Chris@559 1922 unsigned int nDevices = midiInGetNumDevs();
Chris@559 1923 if ( portNumber >= nDevices ) {
Chris@559 1924 std::ostringstream ost;
Chris@559 1925 ost << "RtMidiIn::getPortName: the 'portNumber' argument (" << portNumber << ") is invalid.";
Chris@559 1926 errorString_ = ost.str();
Chris@559 1927 error( RtError::INVALID_PARAMETER );
Chris@559 1928 }
Chris@559 1929
Chris@559 1930 MIDIINCAPS deviceCaps;
Chris@559 1931 midiInGetDevCaps( portNumber, &deviceCaps, sizeof(MIDIINCAPS));
Chris@559 1932
Chris@559 1933 // For some reason, we need to copy character by character with
Chris@559 1934 // UNICODE (thanks to Eduardo Coutinho!).
Chris@559 1935 //std::string stringName = std::string( deviceCaps.szPname );
Chris@559 1936 char nameString[MAXPNAMELEN];
Chris@559 1937 for( int i=0; i<MAXPNAMELEN; i++ )
Chris@559 1938 nameString[i] = (char)( deviceCaps.szPname[i] );
Chris@559 1939
Chris@559 1940 std::string stringName( nameString );
Chris@559 1941 return stringName;
Chris@559 1942 }
Chris@559 1943
Chris@559 1944 //*********************************************************************//
Chris@559 1945 // API: Windows MM
Chris@559 1946 // Class Definitions: RtMidiOut
Chris@559 1947 //*********************************************************************//
Chris@559 1948
Chris@559 1949 unsigned int RtMidiOut :: getPortCount()
Chris@559 1950 {
Chris@559 1951 return midiOutGetNumDevs();
Chris@559 1952 }
Chris@559 1953
Chris@559 1954 std::string RtMidiOut :: getPortName( unsigned int portNumber )
Chris@559 1955 {
Chris@559 1956 unsigned int nDevices = midiOutGetNumDevs();
Chris@559 1957 if ( portNumber >= nDevices ) {
Chris@559 1958 std::ostringstream ost;
Chris@559 1959 ost << "RtMidiOut::getPortName: the 'portNumber' argument (" << portNumber << ") is invalid.";
Chris@559 1960 errorString_ = ost.str();
Chris@559 1961 error( RtError::INVALID_PARAMETER );
Chris@559 1962 }
Chris@559 1963
Chris@559 1964 MIDIOUTCAPS deviceCaps;
Chris@559 1965 midiOutGetDevCaps( portNumber, &deviceCaps, sizeof(MIDIOUTCAPS));
Chris@559 1966
Chris@559 1967 // For some reason, we need to copy character by character with
Chris@559 1968 // UNICODE (thanks to Eduardo Coutinho!).
Chris@559 1969 //std::string stringName = std::string( deviceCaps.szPname );
Chris@559 1970 char nameString[MAXPNAMELEN];
Chris@559 1971 for( int i=0; i<MAXPNAMELEN; i++ )
Chris@559 1972 nameString[i] = (char)( deviceCaps.szPname[i] );
Chris@559 1973
Chris@559 1974 std::string stringName( nameString );
Chris@559 1975 return stringName;
Chris@559 1976 }
Chris@559 1977
Chris@562 1978 void RtMidiOut :: initialize(std::string name)
Chris@559 1979 {
Chris@559 1980 // We'll issue a warning here if no devices are available but not
Chris@559 1981 // throw an error since the user can plug something in later.
Chris@559 1982 unsigned int nDevices = midiOutGetNumDevs();
Chris@559 1983 if ( nDevices == 0 ) {
Chris@559 1984 errorString_ = "RtMidiOut::initialize: no MIDI output devices currently available.";
Chris@559 1985 error( RtError::WARNING );
Chris@559 1986 }
Chris@559 1987
Chris@559 1988 // Save our api-specific connection information.
Chris@559 1989 WinMidiData *data = (WinMidiData *) new WinMidiData;
Chris@559 1990 apiData_ = (void *) data;
Chris@559 1991 }
Chris@559 1992
Chris@559 1993 void RtMidiOut :: openPort( unsigned int portNumber )
Chris@559 1994 {
Chris@559 1995 if ( connected_ ) {
Chris@559 1996 errorString_ = "RtMidiOut::openPort: a valid connection already exists!";
Chris@559 1997 error( RtError::WARNING );
Chris@559 1998 return;
Chris@559 1999 }
Chris@559 2000
Chris@559 2001 unsigned int nDevices = midiOutGetNumDevs();
Chris@559 2002 if (nDevices < 1) {
Chris@559 2003 errorString_ = "RtMidiOut::openPort: no MIDI output destinations found!";
Chris@559 2004 error( RtError::NO_DEVICES_FOUND );
Chris@559 2005 }
Chris@559 2006
Chris@559 2007 std::ostringstream ost;
Chris@559 2008 if ( portNumber >= nDevices ) {
Chris@559 2009 ost << "RtMidiOut::openPort: the 'portNumber' argument (" << portNumber << ") is invalid.";
Chris@559 2010 errorString_ = ost.str();
Chris@559 2011 error( RtError::INVALID_PARAMETER );
Chris@559 2012 }
Chris@559 2013
Chris@559 2014 WinMidiData *data = static_cast<WinMidiData *> (apiData_);
Chris@559 2015 MMRESULT result = midiOutOpen( &data->outHandle,
Chris@559 2016 portNumber,
Chris@559 2017 (DWORD)NULL,
Chris@559 2018 (DWORD)NULL,
Chris@559 2019 CALLBACK_NULL );
Chris@559 2020 if ( result != MMSYSERR_NOERROR ) {
Chris@559 2021 errorString_ = "RtMidiOut::openPort: error creating Windows MM MIDI output port.";
Chris@559 2022 error( RtError::DRIVER_ERROR );
Chris@559 2023 }
Chris@559 2024
Chris@559 2025 connected_ = true;
Chris@559 2026 }
Chris@559 2027
Chris@559 2028 void RtMidiOut :: closePort( void )
Chris@559 2029 {
Chris@559 2030 if ( connected_ ) {
Chris@559 2031 WinMidiData *data = static_cast<WinMidiData *> (apiData_);
Chris@559 2032 midiOutReset( data->outHandle );
Chris@559 2033 midiOutClose( data->outHandle );
Chris@559 2034 connected_ = false;
Chris@559 2035 }
Chris@559 2036 }
Chris@559 2037
Chris@559 2038 void RtMidiOut :: openVirtualPort( std::string portName )
Chris@559 2039 {
Chris@559 2040 // This function cannot be implemented for the Windows MM MIDI API.
Chris@559 2041 errorString_ = "RtMidiOut::openVirtualPort: cannot be implemented in Windows MM MIDI API!";
Chris@559 2042 error( RtError::WARNING );
Chris@559 2043 }
Chris@559 2044
Chris@559 2045 RtMidiOut :: ~RtMidiOut()
Chris@559 2046 {
Chris@559 2047 // Close a connection if it exists.
Chris@559 2048 closePort();
Chris@559 2049
Chris@559 2050 // Cleanup.
Chris@559 2051 WinMidiData *data = static_cast<WinMidiData *> (apiData_);
Chris@559 2052 delete data;
Chris@559 2053 }
Chris@559 2054
Chris@559 2055 void RtMidiOut :: sendMessage( std::vector<unsigned char> *message )
Chris@559 2056 {
Chris@559 2057 unsigned int nBytes = message->size();
Chris@559 2058 if ( nBytes == 0 ) {
Chris@559 2059 errorString_ = "RtMidiOut::sendMessage: message argument is empty!";
Chris@559 2060 error( RtError::WARNING );
Chris@559 2061 return;
Chris@559 2062 }
Chris@559 2063
Chris@559 2064 MMRESULT result;
Chris@559 2065 WinMidiData *data = static_cast<WinMidiData *> (apiData_);
Chris@559 2066 if ( message->at(0) == 0xF0 ) { // Sysex message
Chris@559 2067
Chris@559 2068 // Allocate buffer for sysex data.
Chris@559 2069 char *buffer = (char *) malloc( nBytes );
Chris@559 2070 if ( buffer == NULL ) {
Chris@559 2071 errorString_ = "RtMidiOut::sendMessage: error allocating sysex message memory!";
Chris@559 2072 error( RtError::MEMORY_ERROR );
Chris@559 2073 }
Chris@559 2074
Chris@559 2075 // Copy data to buffer.
Chris@559 2076 for ( unsigned int i=0; i<nBytes; i++ ) buffer[i] = message->at(i);
Chris@559 2077
Chris@559 2078 // Create and prepare MIDIHDR structure.
Chris@559 2079 MIDIHDR sysex;
Chris@559 2080 sysex.lpData = (LPSTR) buffer;
Chris@559 2081 sysex.dwBufferLength = nBytes;
Chris@559 2082 sysex.dwFlags = 0;
Chris@559 2083 result = midiOutPrepareHeader( data->outHandle, &sysex, sizeof(MIDIHDR) );
Chris@559 2084 if ( result != MMSYSERR_NOERROR ) {
Chris@559 2085 free( buffer );
Chris@559 2086 errorString_ = "RtMidiOut::sendMessage: error preparing sysex header.";
Chris@559 2087 error( RtError::DRIVER_ERROR );
Chris@559 2088 }
Chris@559 2089
Chris@559 2090 // Send the message.
Chris@559 2091 result = midiOutLongMsg( data->outHandle, &sysex, sizeof(MIDIHDR) );
Chris@559 2092 if ( result != MMSYSERR_NOERROR ) {
Chris@559 2093 free( buffer );
Chris@559 2094 errorString_ = "RtMidiOut::sendMessage: error sending sysex message.";
Chris@559 2095 error( RtError::DRIVER_ERROR );
Chris@559 2096 }
Chris@559 2097
Chris@559 2098 // Unprepare the buffer and MIDIHDR.
Chris@559 2099 while ( MIDIERR_STILLPLAYING == midiOutUnprepareHeader( data->outHandle, &sysex, sizeof (MIDIHDR) ) ) Sleep( 1 );
Chris@559 2100 free( buffer );
Chris@559 2101
Chris@559 2102 }
Chris@559 2103 else { // Channel or system message.
Chris@559 2104
Chris@559 2105 // Make sure the message size isn't too big.
Chris@559 2106 if ( nBytes > 3 ) {
Chris@559 2107 errorString_ = "RtMidiOut::sendMessage: message size is greater than 3 bytes (and not sysex)!";
Chris@559 2108 error( RtError::WARNING );
Chris@559 2109 return;
Chris@559 2110 }
Chris@559 2111
Chris@559 2112 // Pack MIDI bytes into double word.
Chris@559 2113 DWORD packet;
Chris@559 2114 unsigned char *ptr = (unsigned char *) &packet;
Chris@559 2115 for ( unsigned int i=0; i<nBytes; i++ ) {
Chris@559 2116 *ptr = message->at(i);
Chris@559 2117 ptr++;
Chris@559 2118 }
Chris@559 2119
Chris@559 2120 // Send the message immediately.
Chris@559 2121 result = midiOutShortMsg( data->outHandle, packet );
Chris@559 2122 if ( result != MMSYSERR_NOERROR ) {
Chris@559 2123 errorString_ = "RtMidiOut::sendMessage: error sending MIDI message.";
Chris@559 2124 error( RtError::DRIVER_ERROR );
Chris@559 2125 }
Chris@559 2126 }
Chris@559 2127 }
Chris@559 2128
Chris@559 2129 #endif // __WINDOWS_MM__