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.

147 lines
3.1KB

  1. //*****************************************//
  2. // midiout.cpp
  3. // by Gary Scavone, 2003-2004.
  4. //
  5. // Simple program to test MIDI output.
  6. //
  7. //*****************************************//
  8. #include <iostream>
  9. #include <cstdlib>
  10. #include "RtMidi.h"
  11. // Platform-dependent sleep routines.
  12. #if defined(__WINDOWS_MM__)
  13. #include <windows.h>
  14. #define SLEEP( milliseconds ) Sleep( (DWORD) milliseconds )
  15. #else // Unix variants
  16. #include <unistd.h>
  17. #define SLEEP( milliseconds ) usleep( (unsigned long) (milliseconds * 1000.0) )
  18. #endif
  19. // This function should be embedded in a try/catch block in case of
  20. // an exception. It offers the user a choice of MIDI ports to open.
  21. // It returns false if there are no ports available.
  22. bool chooseMidiPort( RtMidiOut *rtmidi );
  23. int main( int argc, char *argv[] )
  24. {
  25. RtMidiOut *midiout = 0;
  26. std::vector<unsigned char> message;
  27. // RtMidiOut constructor
  28. try {
  29. midiout = new RtMidiOut();
  30. }
  31. catch ( RtError &error ) {
  32. error.printMessage();
  33. exit( EXIT_FAILURE );
  34. }
  35. // Call function to select port.
  36. try {
  37. if ( chooseMidiPort( midiout ) == false ) goto cleanup;
  38. }
  39. catch ( RtError &error ) {
  40. error.printMessage();
  41. goto cleanup;
  42. }
  43. // Send out a series of MIDI messages.
  44. // Program change: 192, 5
  45. message.push_back( 192 );
  46. message.push_back( 5 );
  47. midiout->sendMessage( &message );
  48. SLEEP( 500 );
  49. message[0] = 0xF1;
  50. message[1] = 60;
  51. midiout->sendMessage( &message );
  52. // Control Change: 176, 7, 100 (volume)
  53. message[0] = 176;
  54. message[1] = 7;
  55. message.push_back( 100 );
  56. midiout->sendMessage( &message );
  57. // Note On: 144, 64, 90
  58. message[0] = 144;
  59. message[1] = 64;
  60. message[2] = 90;
  61. midiout->sendMessage( &message );
  62. SLEEP( 500 );
  63. // Note Off: 128, 64, 40
  64. message[0] = 128;
  65. message[1] = 64;
  66. message[2] = 40;
  67. midiout->sendMessage( &message );
  68. SLEEP( 500 );
  69. // Control Change: 176, 7, 40
  70. message[0] = 176;
  71. message[1] = 7;
  72. message[2] = 40;
  73. midiout->sendMessage( &message );
  74. SLEEP( 500 );
  75. // Sysex: 240, 67, 4, 3, 2, 247
  76. message[0] = 240;
  77. message[1] = 67;
  78. message[2] = 4;
  79. message.push_back( 3 );
  80. message.push_back( 2 );
  81. message.push_back( 247 );
  82. midiout->sendMessage( &message );
  83. // Clean up
  84. cleanup:
  85. delete midiout;
  86. return 0;
  87. }
  88. bool chooseMidiPort( RtMidiOut *rtmidi )
  89. {
  90. std::cout << "\nWould you like to open a virtual output port? [y/N] ";
  91. std::string keyHit;
  92. std::getline( std::cin, keyHit );
  93. if ( keyHit == "y" ) {
  94. rtmidi->openVirtualPort();
  95. return true;
  96. }
  97. std::string portName;
  98. unsigned int i = 0, nPorts = rtmidi->getPortCount();
  99. if ( nPorts == 0 ) {
  100. std::cout << "No output ports available!" << std::endl;
  101. return false;
  102. }
  103. if ( nPorts == 1 ) {
  104. std::cout << "\nOpening " << rtmidi->getPortName() << std::endl;
  105. }
  106. else {
  107. for ( i=0; i<nPorts; i++ ) {
  108. portName = rtmidi->getPortName(i);
  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. }