annotate data/midi/rtmidi/RtMidi.cpp @ 563:a5e0d1068cae

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