sysextest.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. //*****************************************//
  2. // sysextest.cpp
  3. // by Gary Scavone, 2003-2005.
  4. //
  5. // Simple program to test MIDI sysex sending and receiving.
  6. //
  7. //*****************************************//
  8. #include <iostream>
  9. #include "RtMidi.h"
  10. void usage(void) {
  11. std::cout << "\nuseage: sysextest N\n";
  12. std::cout << " where N = length of sysex message to send / receive.\n\n";
  13. exit(0);
  14. }
  15. // Platform-dependent sleep routines.
  16. #if defined(__WINDOWS_MM__)
  17. #include <windows.h>
  18. #define SLEEP( milliseconds ) Sleep( (DWORD) milliseconds )
  19. #else // Unix variants
  20. #include <unistd.h>
  21. #define SLEEP( milliseconds ) usleep( (unsigned long) (milliseconds * 1000.0) )
  22. #endif
  23. // This function should be embedded in a try/catch block in case of
  24. // an exception. It offers the user a choice of MIDI ports to open.
  25. // It returns false if there are no ports available.
  26. bool chooseMidiPort( RtMidi *rtmidi );
  27. int main(int argc, char *argv[])
  28. {
  29. RtMidiOut *midiout = 0;
  30. RtMidiIn *midiin = 0;
  31. std::vector<unsigned char> message;
  32. double stamp;
  33. unsigned int i, nBytes;
  34. // Minimal command-line check.
  35. if ( argc != 2 ) usage();
  36. nBytes = (unsigned int) atoi( argv[1] );
  37. // RtMidiOut and RtMidiIn constructors
  38. try {
  39. midiout = new RtMidiOut();
  40. midiin = new RtMidiIn();
  41. }
  42. catch (RtError &error) {
  43. error.printMessage();
  44. goto cleanup;
  45. }
  46. // Don't ignore sysex, timing, or active sensing messages.
  47. midiin->ignoreTypes( false, true, true );
  48. // Call function to select ports
  49. try {
  50. if ( chooseMidiPort( midiin ) == false ) goto cleanup;
  51. if ( chooseMidiPort( midiout ) == false ) goto cleanup;
  52. }
  53. catch (RtError &error) {
  54. error.printMessage();
  55. goto cleanup;
  56. }
  57. // Create a long sysex messages of numbered bytes and send it out.
  58. message.push_back( 240 );
  59. for ( i=0; i<nBytes; i++ )
  60. message.push_back( i % 128 );
  61. message.push_back( 247 );
  62. midiout->sendMessage( &message );
  63. SLEEP( 50 ); // pause a little
  64. // Look for one message (hopefully the previously sent sysex if the
  65. // ports were connected together) and print out the values.
  66. stamp = midiin->getMessage( &message );
  67. nBytes = message.size();
  68. for ( i=0; i<nBytes; i++ )
  69. std::cout << "Byte " << i << " = " << (int)message[i] << std::endl;
  70. // Clean up
  71. cleanup:
  72. delete midiout;
  73. delete midiin;
  74. return 0;
  75. }
  76. bool chooseMidiPort( RtMidi *rtmidi )
  77. {
  78. bool isInput = false;
  79. if ( typeid( *rtmidi ) == typeid( RtMidiIn ) )
  80. isInput = true;
  81. if ( isInput )
  82. std::cout << "\nWould you like to open a virtual input port? [y/N] ";
  83. else
  84. std::cout << "\nWould you like to open a virtual output port? [y/N] ";
  85. std::string keyHit;
  86. std::getline( std::cin, keyHit );
  87. if ( keyHit == "y" ) {
  88. rtmidi->openVirtualPort();
  89. return true;
  90. }
  91. std::string portName;
  92. unsigned int i = 0, nPorts = rtmidi->getPortCount();
  93. if ( nPorts == 0 ) {
  94. if ( isInput )
  95. std::cout << "No input ports available!" << std::endl;
  96. else
  97. std::cout << "No output ports available!" << std::endl;
  98. return false;
  99. }
  100. if ( nPorts == 1 ) {
  101. std::cout << "\nOpening " << rtmidi->getPortName() << std::endl;
  102. }
  103. else {
  104. for ( i=0; i<nPorts; i++ ) {
  105. portName = rtmidi->getPortName(i);
  106. if ( isInput )
  107. std::cout << " Input port #" << i << ": " << portName << '\n';
  108. else
  109. std::cout << " Output port #" << i << ": " << portName << '\n';
  110. }
  111. do {
  112. std::cout << "\nChoose a port number: ";
  113. std::cin >> i;
  114. } while ( i >= nPorts );
  115. }
  116. std::cout << "\n";
  117. rtmidi->openPort( i );
  118. return true;
  119. }