1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409
|
Index: pianobooster-src-0.6.7~svn156/src/rtmidi/RtError.h
===================================================================
--- pianobooster-src-0.6.7~svn156.orig/src/rtmidi/RtError.h
+++ pianobooster-src-0.6.7~svn156/src/rtmidi/RtError.h
@@ -12,12 +12,13 @@
#ifndef RTERROR_H
#define RTERROR_H
+#include <exception>
#include <iostream>
#include <string>
-class RtError
+class RtError : public std::exception
{
-public:
+ public:
//! Defined RtError types.
enum Type {
WARNING, /*!< A non-critical error. */
@@ -25,36 +26,35 @@ public:
UNSPECIFIED, /*!< The default, unspecified error type. */
NO_DEVICES_FOUND, /*!< No devices found on system. */
INVALID_DEVICE, /*!< An invalid device ID was specified. */
- INVALID_STREAM, /*!< An invalid stream ID was specified. */
MEMORY_ERROR, /*!< An error occured during memory allocation. */
INVALID_PARAMETER, /*!< An invalid parameter was specified to a function. */
+ INVALID_USE, /*!< The function was called incorrectly. */
DRIVER_ERROR, /*!< A system driver error occured. */
SYSTEM_ERROR, /*!< A system error occured. */
THREAD_ERROR /*!< A thread error occured. */
};
-protected:
- std::string message_;
- Type type_;
-
-public:
//! The constructor.
- RtError(const std::string& message, Type type = RtError::UNSPECIFIED) : message_(message), type_(type) {}
-
+ RtError( const std::string& message, Type type = RtError::UNSPECIFIED ) throw() : message_(message), type_(type) {}
+
//! The destructor.
- virtual ~RtError(void) {};
+ virtual ~RtError( void ) throw() {}
//! Prints thrown error message to stderr.
- virtual void printMessage(void) { std::cerr << '\n' << message_ << "\n\n"; }
+ virtual void printMessage( void ) const throw() { std::cerr << '\n' << message_ << "\n\n"; }
//! Returns the thrown error message type.
- virtual const Type& getType(void) { return type_; }
+ virtual const Type& getType(void) const throw() { return type_; }
//! Returns the thrown error message string.
- virtual const std::string& getMessage(void) { return message_; }
+ virtual const std::string& getMessage(void) const throw() { return message_; }
- //! Returns the thrown error message as a C string.
- virtual const char *getMessageString(void) { return message_.c_str(); }
+ //! Returns the thrown error message as a c-style string.
+ virtual const char* what( void ) const throw() { return message_.c_str(); }
+
+ protected:
+ std::string message_;
+ Type type_;
};
#endif
Index: pianobooster-src-0.6.7~svn156/src/rtmidi/RtMidi.cpp
===================================================================
--- pianobooster-src-0.6.7~svn156.orig/src/rtmidi/RtMidi.cpp
+++ pianobooster-src-0.6.7~svn156/src/rtmidi/RtMidi.cpp
@@ -8,7 +8,7 @@
RtMidi WWW site: http://music.mcgill.ca/~gary/rtmidi/
RtMidi: realtime MIDI i/o C++ classes
- Copyright (c) 2003-2010 Gary P. Scavone
+ Copyright (c) 2003-2011 Gary P. Scavone
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation files
@@ -35,7 +35,7 @@
*/
/**********************************************************************/
-// RtMidi: Version 1.0.11
+// RtMidi: Version 1.0.15
#include "RtMidi.h"
#include <sstream>
@@ -69,9 +69,14 @@ void RtMidi :: error( RtError::Type type
// Common RtMidiIn Definitions
//*********************************************************************//
-RtMidiIn :: RtMidiIn( const std::string clientName ) : RtMidi()
+RtMidiIn :: RtMidiIn( const std::string clientName, unsigned int queueSizeLimit ) : RtMidi()
{
this->initialize( clientName );
+
+ // Allocate the MIDI queue.
+ inputData_.queue.ringSize = queueSizeLimit;
+ if ( inputData_.queue.ringSize > 0 )
+ inputData_.queue.ring = new MidiMessage[ inputData_.queue.ringSize ];
}
void RtMidiIn :: setCallback( RtMidiCallback callback, void *userData )
@@ -106,11 +111,6 @@ void RtMidiIn :: cancelCallback()
inputData_.usingCallback = false;
}
-void RtMidiIn :: setQueueSizeLimit( unsigned int queueSize )
-{
- inputData_.queueLimit = queueSize;
-}
-
void RtMidiIn :: ignoreTypes( bool midiSysex, bool midiTime, bool midiSense )
{
inputData_.ignoreFlags = 0;
@@ -129,13 +129,16 @@ double RtMidiIn :: getMessage( std::vect
return 0.0;
}
- if ( inputData_.queue.size() == 0 ) return 0.0;
+ if ( inputData_.queue.size == 0 ) return 0.0;
// Copy queued message to the vector pointer argument and then "pop" it.
- std::vector<unsigned char> *bytes = &(inputData_.queue.front().bytes);
+ std::vector<unsigned char> *bytes = &(inputData_.queue.ring[inputData_.queue.front].bytes);
message->assign( bytes->begin(), bytes->end() );
- double deltaTime = inputData_.queue.front().timeStamp;
- inputData_.queue.pop();
+ double deltaTime = inputData_.queue.ring[inputData_.queue.front].timeStamp;
+ inputData_.queue.size--;
+ inputData_.queue.front++;
+ if ( inputData_.queue.front == inputData_.queue.ringSize )
+ inputData_.queue.front = 0;
return deltaTime;
}
@@ -176,6 +179,7 @@ struct CoreMidiData {
MIDIEndpointRef endpoint;
MIDIEndpointRef destinationId;
unsigned long long lastTime;
+ MIDISysexSendRequest sysexreq;
};
//*********************************************************************//
@@ -216,11 +220,18 @@ void midiInputCallback( const MIDIPacket
data->firstMessage = false;
else {
time = packet->timeStamp;
+ if ( time == 0 ) { // this happens when receiving asynchronous sysex messages
+ time = AudioGetCurrentHostTime();
+ }
time -= apiData->lastTime;
time = AudioConvertHostTimeToNanos( time );
message.timeStamp = time * 0.000000001;
}
apiData->lastTime = packet->timeStamp;
+ if ( apiData->lastTime == 0 ) { // this happens when receiving asynchronous sysex messages
+ apiData->lastTime = AudioGetCurrentHostTime();
+ }
+ //std::cout << "TimeStamp = " << packet->timeStamp << std::endl;
iByte = 0;
if ( continueSysex ) {
@@ -240,8 +251,12 @@ void midiInputCallback( const MIDIPacket
}
else {
// As long as we haven't reached our queue size limit, push the message.
- if ( data->queueLimit > data->queue.size() )
- data->queue.push( message );
+ if ( data->queue.size < data->queue.ringSize ) {
+ data->queue.ring[data->queue.back++] = message;
+ if ( data->queue.back == data->queue.ringSize )
+ data->queue.back = 0;
+ data->queue.size++;
+ }
else
std::cerr << "\nRtMidiIn: message queue limit reached!!\n\n";
}
@@ -265,26 +280,24 @@ void midiInputCallback( const MIDIPacket
iByte = nBytes;
}
else size = nBytes - iByte;
- continueSysex = packet->data[nBytes-1] != 0xF7;
+ continueSysex = packet->data[nBytes-1] != 0xF7;
}
- else if ( status < 0xF3 ) {
- if ( status == 0xF1 && (data->ignoreFlags & 0x02) ) {
- // A MIDI time code message and we're ignoring it.
+ else if ( status == 0xF1 ) {
+ // A MIDI time code message
+ if ( data->ignoreFlags & 0x02 ) {
size = 0;
- iByte += 3;
- }
- else size = 3;
+ iByte += 2;
+ }
+ else size = 2;
}
+ else if ( status == 0xF2 ) size = 3;
else if ( status == 0xF3 ) size = 2;
- else if ( status == 0xF8 ) {
- size = 1;
- if ( data->ignoreFlags & 0x02 ) {
- // A MIDI timing tick message and we're ignoring it.
- size = 0;
- iByte += 3;
- }
+ else if ( status == 0xF8 && ( data->ignoreFlags & 0x02 ) ) {
+ // A MIDI timing tick message and we're ignoring it.
+ size = 0;
+ iByte += 1;
}
- else if ( status == 0xFE && (data->ignoreFlags & 0x04) ) {
+ else if ( status == 0xFE && ( data->ignoreFlags & 0x04 ) ) {
// A MIDI active sensing message and we're ignoring it.
size = 0;
iByte += 1;
@@ -302,8 +315,12 @@ void midiInputCallback( const MIDIPacket
}
else {
// As long as we haven't reached our queue size limit, push the message.
- if ( data->queueLimit > data->queue.size() )
- data->queue.push( message );
+ if ( data->queue.size < data->queue.ringSize ) {
+ data->queue.ring[data->queue.back++] = message;
+ if ( data->queue.back == data->queue.ringSize )
+ data->queue.back = 0;
+ data->queue.size++;
+ }
else
std::cerr << "\nRtMidiIn: message queue limit reached!!\n\n";
}
@@ -428,6 +445,9 @@ RtMidiIn :: ~RtMidiIn()
MIDIClientDispose( data->client );
if ( data->endpoint ) MIDIEndpointDispose( data->endpoint );
delete data;
+
+ // Delete the MIDI queue.
+ if ( inputData_.queue.ringSize > 0 ) delete [] inputData_.queue.ring;
}
unsigned int RtMidiIn :: getPortCount()
@@ -566,21 +586,21 @@ std::string RtMidiIn :: getPortName( uns
std::ostringstream ost;
char name[128];
+ std::string stringName;
if ( portNumber >= MIDIGetNumberOfSources() ) {
ost << "RtMidiIn::getPortName: the 'portNumber' argument (" << portNumber << ") is invalid.";
errorString_ = ost.str();
- error( RtError::INVALID_PARAMETER );
+ error( RtError::WARNING );
+ //error( RtError::INVALID_PARAMETER );
+ return stringName;
}
- portRef = MIDIGetSource( portNumber );
+ portRef = MIDIGetSource( portNumber );
nameRef = ConnectedEndpointName(portRef);
- //MIDIObjectGetStringProperty( portRef, kMIDIPropertyName, &nameRef );
- // modified by D. Casey Tucker 2009-03-10
-
CFStringGetCString( nameRef, name, sizeof(name), 0);
CFRelease( nameRef );
- std::string stringName = name;
- return stringName;
+
+ return stringName = name;
}
//*********************************************************************//
@@ -600,19 +620,21 @@ std::string RtMidiOut :: getPortName( un
std::ostringstream ost;
char name[128];
+ std::string stringName;
if ( portNumber >= MIDIGetNumberOfDestinations() ) {
ost << "RtMidiOut::getPortName: the 'portNumber' argument (" << portNumber << ") is invalid.";
errorString_ = ost.str();
- error( RtError::INVALID_PARAMETER );
+ error( RtError::WARNING );
+ return stringName;
+ //error( RtError::INVALID_PARAMETER );
}
- portRef = MIDIGetDestination( portNumber );
+ portRef = MIDIGetDestination( portNumber );
nameRef = ConnectedEndpointName(portRef);
- //MIDIObjectGetStringProperty( portRef, kMIDIPropertyName, &nameRef );
CFStringGetCString( nameRef, name, sizeof(name), 0);
CFRelease( nameRef );
- std::string stringName = name;
- return stringName;
+
+ return stringName = name;
}
void RtMidiOut :: initialize( const std::string& clientName )
@@ -724,11 +746,19 @@ RtMidiOut :: ~RtMidiOut()
delete data;
}
+char *sysexBuffer = 0;
+
+void sysexCompletionProc( MIDISysexSendRequest * sreq )
+{
+ //std::cout << "Completed SysEx send\n";
+ delete sysexBuffer;
+ sysexBuffer = 0;
+}
+
void RtMidiOut :: sendMessage( std::vector<unsigned char> *message )
{
- // The CoreMidi documentation indicates a maximum PackList size of
- // 64K, so we may need to break long sysex messages into pieces and
- // send via separate lists.
+ // We use the MIDISendSysex() function to asynchronously send sysex
+ // messages. Otherwise, we use a single CoreMidi MIDIPacket.
unsigned int nBytes = message->size();
if ( nBytes == 0 ) {
errorString_ = "RtMidiOut::sendMessage: no data in message argument!";
@@ -736,51 +766,71 @@ void RtMidiOut :: sendMessage( std::vect
return;
}
- if ( nBytes > 3 && ( message->at(0) != 0xF0 ) ) {
- errorString_ = "RtMidiOut::sendMessage: message format problem ... not sysex but > 3 bytes?";
- error( RtError::WARNING );
- return;
- }
-
- unsigned int packetBytes, bytesLeft = nBytes;
- unsigned int messageIndex = 0;
+ // unsigned int packetBytes, bytesLeft = nBytes;
+ // unsigned int messageIndex = 0;
MIDITimeStamp timeStamp = AudioGetCurrentHostTime();
CoreMidiData *data = static_cast<CoreMidiData *> (apiData_);
+ OSStatus result;
- while ( bytesLeft > 0 ) {
+ if ( message->at(0) == 0xF0 ) {
- packetBytes = ( bytesLeft > 32736 ) ? 32736 : bytesLeft;
- Byte buffer[packetBytes + 32]; // extra memory for other structure variables
- MIDIPacketList *packetList = (MIDIPacketList *) buffer;
- MIDIPacket *curPacket = MIDIPacketListInit( packetList );
-
- curPacket = MIDIPacketListAdd( packetList, packetBytes+32, curPacket, timeStamp, packetBytes, (const Byte *) &message->at( messageIndex ) );
- if ( !curPacket ) {
- errorString_ = "RtMidiOut::sendMessage: could not allocate packet list";
- error( RtError::DRIVER_ERROR );
- }
- messageIndex += packetBytes;
- bytesLeft -= packetBytes;
+ while ( sysexBuffer != 0 ) usleep( 1000 ); // sleep 1 ms
- // Send to any destinations that may have connected to us.
- OSStatus result;
- if ( data->endpoint ) {
- result = MIDIReceived( data->endpoint, packetList );
- if ( result != noErr ) {
- errorString_ = "RtMidiOut::sendMessage: error sending MIDI to virtual destinations.";
- error( RtError::WARNING );
- }
+ sysexBuffer = new char[nBytes];
+ if ( sysexBuffer == NULL ) {
+ errorString_ = "RtMidiOut::sendMessage: error allocating sysex message memory!";
+ error( RtError::MEMORY_ERROR );
+ }
+
+ // Copy data to buffer.
+ for ( unsigned int i=0; i<nBytes; ++i ) sysexBuffer[i] = message->at(i);
+
+ data->sysexreq.destination = data->destinationId;
+ data->sysexreq.data = (Byte *)sysexBuffer;
+ data->sysexreq.bytesToSend = nBytes;
+ data->sysexreq.complete = 0;
+ data->sysexreq.completionProc = sysexCompletionProc;
+ data->sysexreq.completionRefCon = &(data->sysexreq);
+
+ result = MIDISendSysex( &(data->sysexreq) );
+ if ( result != noErr ) {
+ errorString_ = "RtMidiOut::sendMessage: error sending MIDI to virtual destinations.";
+ error( RtError::WARNING );
+ }
+ return;
+ }
+ else if ( nBytes > 3 ) {
+ errorString_ = "RtMidiOut::sendMessage: message format problem ... not sysex but > 3 bytes?";
+ error( RtError::WARNING );
+ return;
+ }
+
+ MIDIPacketList packetList;
+ MIDIPacket *packet = MIDIPacketListInit( &packetList );
+ packet = MIDIPacketListAdd( &packetList, sizeof(packetList), packet, timeStamp, nBytes, (const Byte *) &message->at( 0 ) );
+ if ( !packet ) {
+ errorString_ = "RtMidiOut::sendMessage: could not allocate packet list";
+ error( RtError::DRIVER_ERROR );
+ }
+
+ // Send to any destinations that may have connected to us.
+ if ( data->endpoint ) {
+ result = MIDIReceived( data->endpoint, &packetList );
+ if ( result != noErr ) {
+ errorString_ = "RtMidiOut::sendMessage: error sending MIDI to virtual destinations.";
+ error( RtError::WARNING );
}
+ }
- // And send to an explicit destination port if we're connected.
- if ( connected_ ) {
- result = MIDISend( data->port, data->destinationId, packetList );
- if ( result != noErr ) {
- errorString_ = "RtMidiOut::sendMessage: error sending MIDI message to port.";
- error( RtError::WARNING );
- }
+ // And send to an explicit destination port if we're connected.
+ if ( connected_ ) {
+ result = MIDISend( data->port, data->destinationId, &packetList );
+ if ( result != noErr ) {
+ errorString_ = "RtMidiOut::sendMessage: error sending MIDI message to port.";
+ error( RtError::WARNING );
}
}
+
}
#endif // __MACOSX_CORE__
@@ -840,6 +890,7 @@ extern "C" void *alsaMidiHandler( void *
long nBytes;
unsigned long long time, lastTime;
bool continueSysex = false;
+ bool doDecode = false;
RtMidiIn::MidiMessage message;
snd_seq_event_t *ev;
@@ -881,9 +932,9 @@ extern "C" void *alsaMidiHandler( void *
// This is a bit weird, but we now have to decode an ALSA MIDI
// event (back) into MIDI bytes. We'll ignore non-MIDI types.
- if ( !continueSysex )
- message.bytes.clear();
+ if ( !continueSysex ) message.bytes.clear();
+ doDecode = false;
switch ( ev->type ) {
case SND_SEQ_EVENT_PORT_SUBSCRIBED:
@@ -896,21 +947,24 @@ extern "C" void *alsaMidiHandler( void *
#if defined(__RTMIDI_DEBUG__)
std::cerr << "RtMidiIn::alsaMidiHandler: port connection has closed!\n";
std::cout << "sender = " << (int) ev->data.connect.sender.client << ":"
- << (int) ev->data.connect.sender.port
+ << (int) ev->data.connect.sender.port
<< ", dest = " << (int) ev->data.connect.dest.client << ":"
- << (int) ev->data.connect.dest.port
+ << (int) ev->data.connect.dest.port
<< std::endl;
#endif
break;
case SND_SEQ_EVENT_QFRAME: // MIDI time code
- if ( data->ignoreFlags & 0x02 ) break;
+ if ( !( data->ignoreFlags & 0x02 ) ) doDecode = true;
+ break;
case SND_SEQ_EVENT_TICK: // MIDI timing tick
- if ( data->ignoreFlags & 0x02 ) break;
+ if ( !( data->ignoreFlags & 0x02 ) ) doDecode = true;
+ break;
case SND_SEQ_EVENT_SENSING: // Active sensing
- if ( data->ignoreFlags & 0x04 ) break;
+ if ( !( data->ignoreFlags & 0x04 ) ) doDecode = true;
+ break;
case SND_SEQ_EVENT_SYSEX:
if ( (data->ignoreFlags & 0x01) ) break;
@@ -926,48 +980,53 @@ extern "C" void *alsaMidiHandler( void *
}
default:
+ doDecode = true;
+ }
+
+ if ( doDecode ) {
+
nBytes = snd_midi_event_decode( apiData->coder, buffer, apiData->bufferSize, ev );
- if ( nBytes <= 0 ) {
+ if ( nBytes > 0 ) {
+ // The ALSA sequencer has a maximum buffer size for MIDI sysex
+ // events of 256 bytes. If a device sends sysex messages larger
+ // than this, they are segmented into 256 byte chunks. So,
+ // we'll watch for this and concatenate sysex chunks into a
+ // single sysex message if necessary.
+ if ( !continueSysex )
+ message.bytes.assign( buffer, &buffer[nBytes] );
+ else
+ message.bytes.insert( message.bytes.end(), buffer, &buffer[nBytes] );
+
+ continueSysex = ( ( ev->type == SND_SEQ_EVENT_SYSEX ) && ( message.bytes.back() != 0xF7 ) );
+ if ( !continueSysex ) {
+
+ // Calculate the time stamp:
+ message.timeStamp = 0.0;
+
+ // Method 1: Use the system time.
+ //(void)gettimeofday(&tv, (struct timezone *)NULL);
+ //time = (tv.tv_sec * 1000000) + tv.tv_usec;
+
+ // Method 2: Use the ALSA sequencer event time data.
+ // (thanks to Pedro Lopez-Cabanillas!).
+ time = ( ev->time.time.tv_sec * 1000000 ) + ( ev->time.time.tv_nsec/1000 );
+ lastTime = time;
+ time -= apiData->lastTime;
+ apiData->lastTime = lastTime;
+ if ( data->firstMessage == true )
+ data->firstMessage = false;
+ else
+ message.timeStamp = time * 0.000001;
+ }
+ else {
#if defined(__RTMIDI_DEBUG__)
- std::cerr << "\nRtMidiIn::alsaMidiHandler: event parsing error or not a MIDI event!\n\n";
+ std::cerr << "\nRtMidiIn::alsaMidiHandler: event parsing error or not a MIDI event!\n\n";
#endif
- break;
+ }
}
-
- // The ALSA sequencer has a maximum buffer size for MIDI sysex
- // events of 256 bytes. If a device sends sysex messages larger
- // than this, they are segmented into 256 byte chunks. So,
- // we'll watch for this and concatenate sysex chunks into a
- // single sysex message if necessary.
- if ( !continueSysex )
- message.bytes.assign( buffer, &buffer[nBytes] );
- else
- message.bytes.insert( message.bytes.end(), buffer, &buffer[nBytes] );
-
- continueSysex = ( ( ev->type == SND_SEQ_EVENT_SYSEX ) && ( message.bytes.back() != 0xF7 ) );
- if ( continueSysex )
- break;
-
- // Calculate the time stamp:
- message.timeStamp = 0.0;
-
- // Method 1: Use the system time.
- //(void)gettimeofday(&tv, (struct timezone *)NULL);
- //time = (tv.tv_sec * 1000000) + tv.tv_usec;
-
- // Method 2: Use the ALSA sequencer event time data.
- // (thanks to Pedro Lopez-Cabanillas!).
- time = ( ev->time.time.tv_sec * 1000000 ) + ( ev->time.time.tv_nsec/1000 );
- lastTime = time;
- time -= apiData->lastTime;
- apiData->lastTime = lastTime;
- if ( data->firstMessage == true )
- data->firstMessage = false;
- else
- message.timeStamp = time * 0.000001;
}
- snd_seq_free_event(ev);
+ snd_seq_free_event( ev );
if ( message.bytes.size() == 0 ) continue;
if ( data->usingCallback && !continueSysex ) {
@@ -976,8 +1035,12 @@ extern "C" void *alsaMidiHandler( void *
}
else {
// As long as we haven't reached our queue size limit, push the message.
- if ( data->queueLimit > data->queue.size() )
- data->queue.push( message );
+ if ( data->queue.size < data->queue.ringSize ) {
+ data->queue.ring[data->queue.back++] = message;
+ if ( data->queue.back == data->queue.ringSize )
+ data->queue.back = 0;
+ data->queue.size++;
+ }
else
std::cerr << "\nRtMidiIn: message queue limit reached!!\n\n";
}
@@ -1229,6 +1292,9 @@ RtMidiIn :: ~RtMidiIn()
#endif
snd_seq_close( data->seq );
delete data;
+
+ // Delete the MIDI queue.
+ if ( inputData_.queue.ringSize > 0 ) delete [] inputData_.queue.ring;
}
unsigned int RtMidiIn :: getPortCount()
@@ -1247,6 +1313,7 @@ std::string RtMidiIn :: getPortName( uns
snd_seq_client_info_alloca( &cinfo );
snd_seq_port_info_alloca( &pinfo );
+ std::string stringName;
AlsaMidiData *data = static_cast<AlsaMidiData *> (apiData_);
if ( portInfo( data->seq, pinfo, SND_SEQ_PORT_CAP_READ|SND_SEQ_PORT_CAP_SUBS_READ, (int) portNumber ) ) {
int cnum = snd_seq_port_info_get_client( pinfo );
@@ -1255,14 +1322,15 @@ std::string RtMidiIn :: getPortName( uns
os << snd_seq_client_info_get_name( cinfo );
os << ":";
os << snd_seq_port_info_get_port( pinfo );
- std::string stringName = os.str();
+ stringName = os.str();
return stringName;
}
// If we get here, we didn't find a match.
errorString_ = "RtMidiIn::getPortName: error looking for port name!";
- error( RtError::INVALID_PARAMETER );
- return 0;
+ error( RtError::WARNING );
+ return stringName;
+ //error( RtError::INVALID_PARAMETER );
}
//*********************************************************************//
@@ -1286,6 +1354,7 @@ std::string RtMidiOut :: getPortName( un
snd_seq_client_info_alloca( &cinfo );
snd_seq_port_info_alloca( &pinfo );
+ std::string stringName;
AlsaMidiData *data = static_cast<AlsaMidiData *> (apiData_);
if ( portInfo( data->seq, pinfo, SND_SEQ_PORT_CAP_WRITE|SND_SEQ_PORT_CAP_SUBS_WRITE, (int) portNumber ) ) {
int cnum = snd_seq_port_info_get_client(pinfo);
@@ -1294,14 +1363,15 @@ std::string RtMidiOut :: getPortName( un
os << snd_seq_client_info_get_name(cinfo);
os << ":";
os << snd_seq_port_info_get_port(pinfo);
- std::string stringName = os.str();
+ stringName = os.str();
return stringName;
}
// If we get here, we didn't find a match.
errorString_ = "RtMidiOut::getPortName: error looking for port name!";
- error( RtError::INVALID_PARAMETER );
- return 0;
+ //error( RtError::INVALID_PARAMETER );
+ error( RtError::WARNING );
+ return stringName;
}
void RtMidiOut :: initialize( const std::string& clientName )
@@ -1372,7 +1442,7 @@ void RtMidiOut :: openPort( unsigned int
if ( data->vport < 0 ) {
data->vport = snd_seq_create_simple_port( data->seq, portName.c_str(),
SND_SEQ_PORT_CAP_READ|SND_SEQ_PORT_CAP_SUBS_READ,
- SND_SEQ_PORT_TYPE_MIDI_GENERIC );
+ SND_SEQ_PORT_TYPE_MIDI_GENERIC|SND_SEQ_PORT_TYPE_APPLICATION );
if ( data->vport < 0 ) {
errorString_ = "RtMidiOut::openPort: ALSA error creating output port.";
error( RtError::DRIVER_ERROR );
@@ -1411,7 +1481,7 @@ void RtMidiOut :: openVirtualPort( std::
if ( data->vport < 0 ) {
data->vport = snd_seq_create_simple_port( data->seq, portName.c_str(),
SND_SEQ_PORT_CAP_READ|SND_SEQ_PORT_CAP_SUBS_READ,
- SND_SEQ_PORT_TYPE_MIDI_GENERIC );
+ SND_SEQ_PORT_TYPE_MIDI_GENERIC|SND_SEQ_PORT_TYPE_APPLICATION );
if ( data->vport < 0 ) {
errorString_ = "RtMidiOut::openVirtualPort: ALSA error creating virtual port.";
@@ -1576,8 +1646,12 @@ extern "C" void *irixMidiHandler( void *
}
else {
// As long as we haven't reached our queue size limit, push the message.
- if ( data->queueLimit > data->queue.size() )
- data->queue.push( message );
+ if ( data->queue.size < data->queue.ringSize ) {
+ data->queue.ring[data->queue.back++] = message;
+ if ( data->queue.back == data->queue.ringSize )
+ data->queue.back = 0;
+ data->queue.size++;
+ }
else
std::cerr << "\nRtMidiIn: message queue limit reached!!\n\n";
}
@@ -1591,12 +1665,11 @@ extern "C" void *irixMidiHandler( void *
else if ( status < 0xC0 ) size = 3;
else if ( status < 0xE0 ) size = 2;
else if ( status < 0xF0 ) size = 3;
- else if ( status < 0xF3 ) {
- if ( status == 0xF1 && !(data->ignoreFlags & 0x02) ) {
+ else if ( status == 0xF1 && !(data->ignoreFlags & 0x02) ) {
// A MIDI time code message and we're not ignoring it.
- size = 3;
- }
+ size = 2;
}
+ else if ( status == 0xF2 ) size = 3;
else if ( status == 0xF3 ) size = 2;
else if ( status == 0xF8 ) {
if ( !(data->ignoreFlags & 0x02) ) {
@@ -1620,8 +1693,12 @@ extern "C" void *irixMidiHandler( void *
}
else {
// As long as we haven't reached our queue size limit, push the message.
- if ( data->queueLimit > data->queue.size() )
- data->queue.push( message );
+ if ( data->queue.size < data->queue.ringSize ) {
+ data->queue.ring[data->queue.back++] = message;
+ if ( data->queue.back == data->queue.ringSize )
+ data->queue.back = 0;
+ data->queue.size++;
+ }
else
std::cerr << "\nRtMidiIn: message queue limit reached!!\n\n";
}
@@ -1722,6 +1799,9 @@ RtMidiIn :: ~RtMidiIn()
// Cleanup.
IrixMidiData *data = static_cast<IrixMidiData *> (apiData_);
delete data;
+
+ // Delete the MIDI queue.
+ if ( inputData_.queue.ringSize > 0 ) delete [] inputData_.queue.ring;
}
unsigned int RtMidiIn :: getPortCount()
@@ -1735,14 +1815,17 @@ std::string RtMidiIn :: getPortName( uns
{
int nPorts = mdInit();
+ std::string stringName;
std::ostringstream ost;
if ( portNumber >= nPorts ) {
ost << "RtMidiIn::getPortName: the 'portNumber' argument (" << portNumber << ") is invalid.";
errorString_ = ost.str();
- error( RtError::INVALID_PARAMETER );
+ //error( RtError::INVALID_PARAMETER );
+ error( RtError::WARNING );
}
+ else
+ std::string stringName = std::string( mdGetName( portNumber ) );
- std::string stringName = std::string( mdGetName( portNumber ) );
return stringName;
}
@@ -1762,14 +1845,17 @@ std::string RtMidiOut :: getPortName( un
{
int nPorts = mdInit();
+ std::string stringName;
std::ostringstream ost;
if ( portNumber >= nPorts ) {
ost << "RtMidiIn::getPortName: the 'portNumber' argument (" << portNumber << ") is invalid.";
errorString_ = ost.str();
- error( RtError::INVALID_PARAMETER );
+ //error( RtError::INVALID_PARAMETER );
+ error( RtError::WARNING );
}
+ else
+ std::string stringName = std::string( mdGetName( portNumber ) );
- std::string stringName = std::string( mdGetName( portNumber ) );
return stringName;
}
@@ -1916,10 +2002,10 @@ struct WinMidiData {
// Class Definitions: RtMidiIn
//*********************************************************************//
-static void CALLBACK midiInputCallback( HMIDIOUT hmin,
+static void CALLBACK midiInputCallback( HMIDIIN hmin,
UINT inputStatus,
- DWORD instancePtr,
- DWORD midiMessage,
+ DWORD_PTR instancePtr,
+ DWORD_PTR midiMessage,
DWORD timestamp )
{
if ( inputStatus != MIM_DATA && inputStatus != MIM_LONGDATA && inputStatus != MIM_LONGERROR ) return;
@@ -1945,11 +2031,11 @@ static void CALLBACK midiInputCallback(
if ( status < 0xC0 ) nBytes = 3;
else if ( status < 0xE0 ) nBytes = 2;
else if ( status < 0xF0 ) nBytes = 3;
- else if ( status < 0xF3 ) {
- // A MIDI time code message and we're ignoring it.
- if ( status == 0xF1 && (data->ignoreFlags & 0x02) ) return;
- nBytes = 3;
+ else if ( status == 0xF1 ) {
+ if ( data->ignoreFlags & 0x02 ) return;
+ else nBytes = 2;
}
+ else if ( status == 0xF2 ) nBytes = 3;
else if ( status == 0xF3 ) nBytes = 2;
else if ( status == 0xF8 && (data->ignoreFlags & 0x02) ) {
// A MIDI timing tick message and we're ignoring it.
@@ -1997,8 +2083,12 @@ static void CALLBACK midiInputCallback(
}
else {
// As long as we haven't reached our queue size limit, push the message.
- if ( data->queueLimit > data->queue.size() )
- data->queue.push( apiData->message );
+ if ( data->queue.size < data->queue.ringSize ) {
+ data->queue.ring[data->queue.back++] = apiData->message;
+ if ( data->queue.back == data->queue.ringSize )
+ data->queue.back = 0;
+ data->queue.size++;
+ }
else
std::cerr << "\nRtMidiIn: message queue limit reached!!\n\n";
}
@@ -2048,8 +2138,8 @@ void RtMidiIn :: openPort( unsigned int
WinMidiData *data = static_cast<WinMidiData *> (apiData_);
MMRESULT result = midiInOpen( &data->inHandle,
portNumber,
- (DWORD)&midiInputCallback,
- (DWORD)&inputData_,
+ (DWORD_PTR)&midiInputCallback,
+ (DWORD_PTR)&inputData_,
CALLBACK_FUNCTION );
if ( result != MMSYSERR_NOERROR ) {
errorString_ = "RtMidiIn::openPort: error creating Windows MM MIDI input port.";
@@ -2128,6 +2218,9 @@ RtMidiIn :: ~RtMidiIn()
// Cleanup.
WinMidiData *data = static_cast<WinMidiData *> (apiData_);
delete data;
+
+ // Delete the MIDI queue.
+ if ( inputData_.queue.ringSize > 0 ) delete [] inputData_.queue.ring;
}
unsigned int RtMidiIn :: getPortCount()
@@ -2137,25 +2230,28 @@ unsigned int RtMidiIn :: getPortCount()
std::string RtMidiIn :: getPortName( unsigned int portNumber )
{
+ std::string stringName;
unsigned int nDevices = midiInGetNumDevs();
if ( portNumber >= nDevices ) {
std::ostringstream ost;
ost << "RtMidiIn::getPortName: the 'portNumber' argument (" << portNumber << ") is invalid.";
errorString_ = ost.str();
- error( RtError::INVALID_PARAMETER );
+ //error( RtError::INVALID_PARAMETER );
+ error( RtError::WARNING );
+ return stringName;
}
MIDIINCAPS deviceCaps;
midiInGetDevCaps( portNumber, &deviceCaps, sizeof(MIDIINCAPS));
- // For some reason, we need to copy character by character with
- // UNICODE (thanks to Eduardo Coutinho!).
- //std::string stringName = std::string( deviceCaps.szPname );
- char nameString[MAXPNAMELEN];
- for( int i=0; i<MAXPNAMELEN; ++i )
- nameString[i] = (char)( deviceCaps.szPname[i] );
+#if defined( UNICODE ) || defined( _UNICODE )
+ int length = WideCharToMultiByte(CP_UTF8, 0, deviceCaps.szPname, -1, NULL, 0, NULL, NULL);
+ stringName.assign( length, 0 );
+ length = WideCharToMultiByte(CP_UTF8, 0, deviceCaps.szPname, wcslen(deviceCaps.szPname), &stringName[0], length, NULL, NULL);
+#else
+ stringName = std::string( deviceCaps.szPname );
+#endif
- std::string stringName( nameString );
return stringName;
}
@@ -2171,25 +2267,28 @@ unsigned int RtMidiOut :: getPortCount()
std::string RtMidiOut :: getPortName( unsigned int portNumber )
{
+ std::string stringName;
unsigned int nDevices = midiOutGetNumDevs();
if ( portNumber >= nDevices ) {
std::ostringstream ost;
ost << "RtMidiOut::getPortName: the 'portNumber' argument (" << portNumber << ") is invalid.";
errorString_ = ost.str();
- error( RtError::INVALID_PARAMETER );
+ //error( RtError::INVALID_PARAMETER );
+ error( RtError::WARNING );
+ return stringName;
}
MIDIOUTCAPS deviceCaps;
midiOutGetDevCaps( portNumber, &deviceCaps, sizeof(MIDIOUTCAPS));
- // For some reason, we need to copy character by character with
- // UNICODE (thanks to Eduardo Coutinho!).
- //std::string stringName = std::string( deviceCaps.szPname );
- char nameString[MAXPNAMELEN];
- for( int i=0; i<MAXPNAMELEN; ++i )
- nameString[i] = (char)( deviceCaps.szPname[i] );
+#if defined( UNICODE ) || defined( _UNICODE )
+ int length = WideCharToMultiByte(CP_UTF8, 0, deviceCaps.szPname, -1, NULL, 0, NULL, NULL);
+ stringName.assign( length, 0 );
+ length = WideCharToMultiByte(CP_UTF8, 0, deviceCaps.szPname, wcslen(deviceCaps.szPname), &stringName[0], length, NULL, NULL);
+#else
+ stringName = std::string( deviceCaps.szPname );
+#endif
- std::string stringName( nameString );
return stringName;
}
@@ -2272,7 +2371,7 @@ RtMidiOut :: ~RtMidiOut()
void RtMidiOut :: sendMessage( std::vector<unsigned char> *message )
{
- unsigned int nBytes = message->size();
+ unsigned int nBytes = static_cast<unsigned int>(message->size());
if ( nBytes == 0 ) {
errorString_ = "RtMidiOut::sendMessage: message argument is empty!";
error( RtError::WARNING );
@@ -2345,3 +2444,371 @@ void RtMidiOut :: sendMessage( std::vect
}
#endif // __WINDOWS_MM__
+
+//*********************************************************************//
+// API: LINUX JACK
+//
+// Written primarily by Alexander Svetalkin, with updates for delta
+// time by Gary Scavone, April 2011.
+//
+// *********************************************************************//
+
+#if defined(__LINUX_JACK__)
+
+// JACK header files
+#include <jack/jack.h>
+#include <jack/midiport.h>
+#include <jack/ringbuffer.h>
+
+#define JACK_RINGBUFFER_SIZE 16384 // Default size for ringbuffer
+
+struct JackMidiData {
+ jack_client_t *client;
+ jack_port_t *port;
+ jack_ringbuffer_t *buffSize;
+ jack_ringbuffer_t *buffMessage;
+ jack_time_t lastTime;
+ };
+
+struct Arguments {
+ JackMidiData *jackData;
+ RtMidiIn :: RtMidiInData *rtMidiIn;
+ };
+
+//*********************************************************************//
+// API: JACK
+// Class Definitions: RtMidiIn
+//*********************************************************************//
+
+int jackProcessIn( jack_nframes_t nframes, void *arg )
+{
+ JackMidiData *jData = ( (Arguments *) arg )->jackData;
+ RtMidiIn :: RtMidiInData *rtData = ( (Arguments *) arg )->rtMidiIn;
+ jack_midi_event_t event;
+ jack_time_t long long time;
+
+ // Is port created?
+ if ( jData->port == NULL ) return 0;
+ void *buff = jack_port_get_buffer( jData->port, nframes );
+
+ // We have midi events in buffer
+ int evCount = jack_midi_get_event_count( buff );
+ if ( evCount > 0 ) {
+ RtMidiIn::MidiMessage message;
+ message.bytes.clear();
+
+ jack_midi_event_get( &event, buff, 0 );
+
+ for (unsigned int i = 0; i < event.size; i++ )
+ message.bytes.push_back( event.buffer[i] );
+
+ // Compute the delta time.
+ time = jack_get_time();
+ if ( rtData->firstMessage == true )
+ rtData->firstMessage = false;
+ else
+ message.timeStamp = ( time - jData->lastTime ) * 0.000001;
+
+ jData->lastTime = time;
+
+ if ( rtData->usingCallback && !rtData->continueSysex ) {
+ RtMidiIn::RtMidiCallback callback = (RtMidiIn::RtMidiCallback) rtData->userCallback;
+ callback( message.timeStamp, &message.bytes, rtData->userData );
+ }
+ else {
+ // As long as we haven't reached our queue size limit, push the message.
+ if ( rtData->queue.size < rtData->queue.ringSize ) {
+ rtData->queue.ring[rtData->queue.back++] = message;
+ if ( rtData->queue.back == rtData->queue.ringSize )
+ rtData->queue.back = 0;
+ rtData->queue.size++;
+ }
+ else
+ std::cerr << "\nRtMidiIn: message queue limit reached!!\n\n";
+ }
+ }
+
+ return 0;
+}
+
+void RtMidiIn :: initialize( const std::string& clientName )
+{
+ JackMidiData *data = new JackMidiData;
+
+ // Initialize JACK client
+ if (( data->client = jack_client_open( clientName.c_str(), JackNullOption, NULL )) == 0)
+ {
+ errorString_ = "RtMidiOut::initialize: JACK server not running?";
+ error( RtError::DRIVER_ERROR );
+ return;
+ }
+
+ Arguments *arg = new Arguments;
+ arg->jackData = data;
+
+ arg->rtMidiIn = &inputData_;
+ jack_set_process_callback( data->client, jackProcessIn, arg );
+ data->port = NULL;
+ jack_activate( data->client );
+
+ apiData_ = (void *) data;
+}
+
+RtMidiIn :: ~RtMidiIn()
+{
+ JackMidiData *data = static_cast<JackMidiData *> (apiData_);
+ jack_client_close( data->client );
+
+ // Delete the MIDI queue.
+ if ( inputData_.queue.ringSize > 0 ) delete [] inputData_.queue.ring;
+}
+
+void RtMidiIn :: openPort( unsigned int portNumber, const std::string portName )
+{
+ JackMidiData *data = static_cast<JackMidiData *> (apiData_);
+
+ // Creating new port
+ if ( data->port == NULL)
+ data->port = jack_port_register( data->client, portName.c_str(),
+ JACK_DEFAULT_MIDI_TYPE, JackPortIsInput, 0 );
+
+ if ( data->port == NULL) {
+ errorString_ = "RtMidiOut::openVirtualPort: JACK error creating virtual port";
+ error( RtError::DRIVER_ERROR );
+ }
+
+ // Connecting to the output
+ std::string name = getPortName( portNumber );
+ jack_connect( data->client, name.c_str(), jack_port_name( data->port ) );
+}
+
+void RtMidiIn :: openVirtualPort( const std::string portName )
+{
+ JackMidiData *data = static_cast<JackMidiData *> (apiData_);
+
+ if ( data->port == NULL )
+ data->port = jack_port_register( data->client, portName.c_str(),
+ JACK_DEFAULT_MIDI_TYPE, JackPortIsInput, 0 );
+
+ if ( data->port == NULL ) {
+ errorString_ = "RtMidiOut::openVirtualPort: JACK error creating virtual port";
+ error( RtError::DRIVER_ERROR );
+ }
+}
+
+unsigned int RtMidiIn :: getPortCount()
+{
+ int count = 0;
+ JackMidiData *data = static_cast<JackMidiData *> (apiData_);
+
+ // List of available ports
+ const char **ports = jack_get_ports( data->client, NULL,
+ JACK_DEFAULT_MIDI_TYPE, JackPortIsOutput );
+
+ if ( ports == NULL ) return 0;
+ while ( ports[count] != NULL )
+ count++;
+
+ free( ports );
+
+ return count;
+}
+
+std::string RtMidiIn :: getPortName( unsigned int portNumber )
+{
+ JackMidiData *data = static_cast<JackMidiData *> (apiData_);
+ std::ostringstream ost;
+ std::string retStr("");
+
+ // List of available ports
+ const char **ports = jack_get_ports( data->client, NULL,
+ JACK_DEFAULT_MIDI_TYPE, JackPortIsOutput );
+
+ // Check port validity
+ if ( ports == NULL ) {
+ errorString_ = "RtMidiOut::getPortName: no ports available!";
+ error( RtError::WARNING );
+ return retStr;
+ }
+
+ if ( ports[portNumber] == NULL ) {
+ ost << "RtMidiOut::getPortName: the 'portNumber' argument (" << portNumber << ") is invalid.";
+ errorString_ = ost.str();
+ error( RtError::WARNING );
+ }
+ else retStr.assign( ports[portNumber] );
+
+ free( ports );
+
+ return retStr;
+}
+
+void RtMidiIn :: closePort()
+{
+ JackMidiData *data = static_cast<JackMidiData *> (apiData_);
+
+ if ( data->port == NULL ) return;
+ jack_port_unregister( data->client, data->port );
+}
+
+//*********************************************************************//
+// API: JACK
+// Class Definitions: RtMidiOut
+//*********************************************************************//
+
+// Jack process callback
+int jackProcessOut( jack_nframes_t nframes, void *arg )
+{
+ JackMidiData *data = (JackMidiData *) arg;
+ jack_midi_data_t *midiData;
+ int space;
+
+ // Is port created?
+ if ( data->port == NULL ) return 0;
+
+ void *buff = jack_port_get_buffer( data->port, nframes );
+ jack_midi_clear_buffer( buff );
+
+ while ( jack_ringbuffer_read_space( data->buffSize ) > 0 ) {
+ jack_ringbuffer_read( data->buffSize, (char *) &space, (size_t) sizeof(space) );
+ midiData = jack_midi_event_reserve( buff, 0, space );
+
+ jack_ringbuffer_read( data->buffMessage, (char *) midiData, (size_t) space );
+ }
+
+ return 0;
+}
+
+void RtMidiOut :: initialize( const std::string& clientName )
+{
+ JackMidiData *data = new JackMidiData;
+
+ // Initialize JACK client
+ if (( data->client = jack_client_open( clientName.c_str(), JackNullOption, NULL )) == 0)
+ {
+ errorString_ = "RtMidiOut::initialize: JACK server not running?";
+ error( RtError::DRIVER_ERROR );
+ return;
+ }
+
+ jack_set_process_callback( data->client, jackProcessOut, data );
+ data->buffSize = jack_ringbuffer_create( JACK_RINGBUFFER_SIZE );
+ data->buffMessage = jack_ringbuffer_create( JACK_RINGBUFFER_SIZE );
+ jack_activate( data->client );
+
+ data->port = NULL;
+
+ apiData_ = (void *) data;
+}
+
+RtMidiOut :: ~RtMidiOut()
+{
+ JackMidiData *data = static_cast<JackMidiData *> (apiData_);
+
+ // Cleanup
+ jack_client_close( data->client );
+ jack_ringbuffer_free( data->buffSize );
+ jack_ringbuffer_free( data->buffMessage );
+}
+
+void RtMidiOut :: openPort( unsigned int portNumber, const std::string portName )
+{
+ JackMidiData *data = static_cast<JackMidiData *> (apiData_);
+
+ // Creating new port
+ if ( data->port == NULL )
+ data->port = jack_port_register( data->client, portName.c_str(),
+ JACK_DEFAULT_MIDI_TYPE, JackPortIsOutput, 0 );
+
+ if ( data->port == NULL ) {
+ errorString_ = "RtMidiOut::openVirtualPort: JACK error creating virtual port";
+ error( RtError::DRIVER_ERROR );
+ }
+
+ // Connecting to the output
+ std::string name = getPortName( portNumber );
+ jack_connect( data->client, jack_port_name( data->port ), name.c_str() );
+}
+
+void RtMidiOut :: openVirtualPort( const std::string portName )
+{
+ JackMidiData *data = static_cast<JackMidiData *> (apiData_);
+
+ if ( data->port == NULL )
+ data->port = jack_port_register( data->client, portName.c_str(),
+ JACK_DEFAULT_MIDI_TYPE, JackPortIsOutput, 0 );
+
+ if ( data->port == NULL ) {
+ errorString_ = "RtMidiOut::openVirtualPort: JACK error creating virtual port";
+ error( RtError::DRIVER_ERROR );
+ }
+}
+
+unsigned int RtMidiOut :: getPortCount()
+{
+ int count = 0;
+ JackMidiData *data = static_cast<JackMidiData *> (apiData_);
+
+ // List of available ports
+ const char **ports = jack_get_ports( data->client, NULL,
+ JACK_DEFAULT_MIDI_TYPE, JackPortIsInput );
+
+ if ( ports == NULL ) return 0;
+ while ( ports[count] != NULL )
+ count++;
+
+ free( ports );
+
+ return count;
+}
+
+std::string RtMidiOut :: getPortName( unsigned int portNumber )
+{
+ JackMidiData *data = static_cast<JackMidiData *> (apiData_);
+ std::ostringstream ost;
+ std::string retStr("");
+
+ // List of available ports
+ const char **ports = jack_get_ports( data->client, NULL,
+ JACK_DEFAULT_MIDI_TYPE, JackPortIsInput );
+
+ // Check port validity
+ if ( ports == NULL) {
+ errorString_ = "RtMidiOut::getPortName: no ports available!";
+ error( RtError::WARNING );
+ return retStr;
+ }
+
+ if ( ports[portNumber] == NULL) {
+ ost << "RtMidiOut::getPortName: the 'portNumber' argument (" << portNumber << ") is invalid.";
+ errorString_ = ost.str();
+ error( RtError::WARNING );
+ }
+ else retStr.assign( ports[portNumber] );
+
+ free( ports );
+
+ return retStr;
+}
+
+void RtMidiOut :: closePort()
+{
+ JackMidiData *data = static_cast<JackMidiData *> (apiData_);
+
+ if ( data->port == NULL ) return;
+ jack_port_unregister( data->client, data->port );
+ data->port = NULL;
+}
+
+void RtMidiOut :: sendMessage( std::vector<unsigned char> *message )
+{
+ int nBytes = message->size();
+ JackMidiData *data = static_cast<JackMidiData *> (apiData_);
+
+ // Write full message to buffer
+ jack_ringbuffer_write( data->buffMessage, ( const char * ) &( *message )[0],
+ message->size() );
+ jack_ringbuffer_write( data->buffSize, ( char * ) &nBytes, sizeof( nBytes ) );
+}
+
+#endif // __LINUX_JACK__
Index: pianobooster-src-0.6.7~svn156/src/rtmidi/RtMidi.h
===================================================================
--- pianobooster-src-0.6.7~svn156.orig/src/rtmidi/RtMidi.h
+++ pianobooster-src-0.6.7~svn156/src/rtmidi/RtMidi.h
@@ -8,7 +8,7 @@
RtMidi WWW site: http://music.mcgill.ca/~gary/rtmidi/
RtMidi: realtime MIDI i/o C++ classes
- Copyright (c) 2003-2010 Gary P. Scavone
+ Copyright (c) 2003-2011 Gary P. Scavone
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation files
@@ -35,7 +35,7 @@
*/
/**********************************************************************/
-// RtMidi: Version 1.0.11
+// RtMidi: Version 1.0.15
#ifndef RTMIDI_H
#define RTMIDI_H
@@ -96,7 +96,6 @@ class RtMidi
/**********************************************************************/
#include <vector>
-#include <queue>
class RtMidiIn : public RtMidi
{
@@ -105,11 +104,15 @@ class RtMidiIn : public RtMidi
//! User callback function type definition.
typedef void (*RtMidiCallback)( double timeStamp, std::vector<unsigned char> *message, void *userData);
- //! Default constructor that allows an optional client name.
+ //! Default constructor that allows an optional client name and queue size.
/*!
- An exception will be thrown if a MIDI system initialization error occurs.
+ An exception will be thrown if a MIDI system initialization
+ error occurs. The queue size defines the maximum number of
+ messages that can be held in the MIDI queue (when not using a
+ callback function). If the queue size limit is reached,
+ incoming messages will be ignored.
*/
- RtMidiIn( const std::string clientName = std::string( "RtMidi Input Client") );
+ RtMidiIn( const std::string clientName = std::string( "RtMidi Input Client"), unsigned int queueSizeLimit = 100 );
//! If a MIDI connection is still open, it will be closed by the destructor.
~RtMidiIn();
@@ -154,20 +157,13 @@ class RtMidiIn : public RtMidi
//! Return a string identifier for the specified MIDI input port number.
/*!
- An exception is thrown if an invalid port specifier is provided.
+ An empty string is returned if an invalid port specifier is provided.
*/
std::string getPortName( unsigned int portNumber = 0 );
- //! Set the maximum number of MIDI messages to be saved in the queue.
- /*!
- If the queue size limit is reached, incoming messages will be
- ignored. The default limit is 1024.
- */
- void setQueueSizeLimit( unsigned int queueSize );
-
//! Specify whether certain MIDI message types should be queued or ignored during input.
/*!
- By default, MIDI timing and active sensing messages are ignored
+o By default, MIDI timing and active sensing messages are ignored
during message input because of their relative high data rates.
MIDI sysex messages are ignored by default as well. Variable
values of "true" imply that the respective message type will be
@@ -193,15 +189,26 @@ class RtMidiIn : public RtMidi
// Default constructor.
MidiMessage()
- :bytes(3), timeStamp(0.0) {}
+ :bytes(0), timeStamp(0.0) {}
+ };
+
+ struct MidiQueue {
+ unsigned int front;
+ unsigned int back;
+ unsigned int size;
+ unsigned int ringSize;
+ MidiMessage *ring;
+
+ // Default constructor.
+ MidiQueue()
+ :front(0), back(0), size(0), ringSize(0) {}
};
// The RtMidiInData structure is used to pass private class data to
// the MIDI input handling function or thread.
struct RtMidiInData {
- std::queue<MidiMessage> queue;
+ MidiQueue queue;
MidiMessage message;
- unsigned int queueLimit;
unsigned char ignoreFlags;
bool doInput;
bool firstMessage;
@@ -213,7 +220,7 @@ class RtMidiIn : public RtMidi
// Default constructor.
RtMidiInData()
- : queueLimit(1024), ignoreFlags(7), doInput(false), firstMessage(true),
+ : ignoreFlags(7), doInput(false), firstMessage(true),
apiData(0), usingCallback(false), userCallback(0), userData(0),
continueSysex(false) {}
};
@@ -280,7 +287,7 @@ class RtMidiOut : public RtMidi
//! Return a string identifier for the specified MIDI port type and number.
/*!
- An exception is thrown if an invalid port specifier is provided.
+ An empty string is returned if an invalid port specifier is provided.
*/
std::string getPortName( unsigned int portNumber = 0 );
|