Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

155 lines
3.8KB

  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. void mycallback( double deltatime, std::vector< unsigned char > *message, void *userData )
  30. {
  31. unsigned int nBytes = message->size();
  32. for ( unsigned int i=0; i<nBytes; i++ )
  33. std::cout << "Byte " << i << " = " << (int)message->at(i) << ", ";
  34. if ( nBytes > 0 )
  35. std::cout << "stamp = " << deltatime << std::endl;
  36. }
  37. int main( int argc, char *argv[] )
  38. {
  39. RtMidiOut *midiout = 0;
  40. RtMidiIn *midiin = 0;
  41. std::vector<unsigned char> message;
  42. unsigned int i, nBytes;
  43. // Minimal command-line check.
  44. if ( argc != 2 ) usage();
  45. nBytes = (unsigned int) atoi( argv[1] );
  46. // RtMidiOut and RtMidiIn constructors
  47. try {
  48. midiout = new RtMidiOut();
  49. midiin = new RtMidiIn();
  50. }
  51. catch ( RtError &error ) {
  52. error.printMessage();
  53. goto cleanup;
  54. }
  55. // Don't ignore sysex, timing, or active sensing messages.
  56. midiin->ignoreTypes( false, true, true );
  57. // Call function to select ports
  58. try {
  59. if ( chooseMidiPort( midiin ) == false ) goto cleanup;
  60. if ( chooseMidiPort( midiout ) == false ) goto cleanup;
  61. }
  62. catch ( RtError &error ) {
  63. error.printMessage();
  64. goto cleanup;
  65. }
  66. midiin->setCallback( &mycallback );
  67. message.push_back( 0xF6 );
  68. midiout->sendMessage( &message );
  69. SLEEP( 500 ); // pause a little
  70. // Create a long sysex messages of numbered bytes and send it out.
  71. for ( int n=0; n<2; n++ ) {
  72. message.clear();
  73. message.push_back( 240 );
  74. for ( i=0; i<nBytes; i++ )
  75. message.push_back( i % 128 );
  76. message.push_back( 247 );
  77. midiout->sendMessage( &message );
  78. SLEEP( 500 ); // pause a little
  79. }
  80. // Clean up
  81. cleanup:
  82. delete midiout;
  83. delete midiin;
  84. return 0;
  85. }
  86. bool chooseMidiPort( RtMidi *rtmidi )
  87. {
  88. bool isInput = false;
  89. if ( typeid( *rtmidi ) == typeid( RtMidiIn ) )
  90. isInput = true;
  91. if ( isInput )
  92. std::cout << "\nWould you like to open a virtual input port? [y/N] ";
  93. else
  94. std::cout << "\nWould you like to open a virtual output port? [y/N] ";
  95. std::string keyHit;
  96. std::getline( std::cin, keyHit );
  97. if ( keyHit == "y" ) {
  98. rtmidi->openVirtualPort();
  99. return true;
  100. }
  101. std::string portName;
  102. unsigned int i = 0, nPorts = rtmidi->getPortCount();
  103. if ( nPorts == 0 ) {
  104. if ( isInput )
  105. std::cout << "No input ports available!" << std::endl;
  106. else
  107. std::cout << "No output ports available!" << std::endl;
  108. return false;
  109. }
  110. if ( nPorts == 1 ) {
  111. std::cout << "\nOpening " << rtmidi->getPortName() << std::endl;
  112. }
  113. else {
  114. for ( i=0; i<nPorts; i++ ) {
  115. portName = rtmidi->getPortName(i);
  116. if ( isInput )
  117. std::cout << " Input port #" << i << ": " << portName << '\n';
  118. else
  119. std::cout << " Output port #" << i << ": " << portName << '\n';
  120. }
  121. do {
  122. std::cout << "\nChoose a port number: ";
  123. std::cin >> i;
  124. } while ( i >= nPorts );
  125. }
  126. std::cout << std::endl;
  127. rtmidi->openPort( i );
  128. return true;
  129. }