sysextest.cpp 3.6 KB

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