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.

112 lines
2.8KB

  1. //*****************************************//
  2. // cmidiin.cpp
  3. // by Gary Scavone, 2003-2004.
  4. //
  5. // Simple program to test MIDI input and
  6. // use of a user callback function.
  7. //
  8. //*****************************************//
  9. #include <iostream>
  10. #include <cstdlib>
  11. #include "RtMidi.h"
  12. void usage( void ) {
  13. // Error function in case of incorrect command-line
  14. // argument specifications.
  15. std::cout << "\nuseage: cmidiin <port>\n";
  16. std::cout << " where port = the device to use (default = 0).\n\n";
  17. exit( 0 );
  18. }
  19. void mycallback( double deltatime, std::vector< unsigned char > *message, void *userData )
  20. {
  21. unsigned int nBytes = message->size();
  22. for ( unsigned int i=0; i<nBytes; i++ )
  23. std::cout << "Byte " << i << " = " << (int)message->at(i) << ", ";
  24. if ( nBytes > 0 )
  25. std::cout << "stamp = " << deltatime << std::endl;
  26. }
  27. // This function should be embedded in a try/catch block in case of
  28. // an exception. It offers the user a choice of MIDI ports to open.
  29. // It returns false if there are no ports available.
  30. bool chooseMidiPort( RtMidiIn *rtmidi );
  31. int main( int argc, char *argv[] )
  32. {
  33. RtMidiIn *midiin = 0;
  34. // Minimal command-line check.
  35. if ( argc > 2 ) usage();
  36. try {
  37. // RtMidiIn constructor
  38. midiin = new RtMidiIn();
  39. // Call function to select port.
  40. if ( chooseMidiPort( midiin ) == false ) goto cleanup;
  41. // Set our callback function. This should be done immediately after
  42. // opening the port to avoid having incoming messages written to the
  43. // queue instead of sent to the callback function.
  44. midiin->setCallback( &mycallback );
  45. // Don't ignore sysex, timing, or active sensing messages.
  46. midiin->ignoreTypes( false, false, false );
  47. std::cout << "\nReading MIDI input ... press <enter> to quit.\n";
  48. char input;
  49. std::cin.get(input);
  50. } catch ( RtError &error ) {
  51. error.printMessage();
  52. }
  53. cleanup:
  54. delete midiin;
  55. return 0;
  56. }
  57. bool chooseMidiPort( RtMidiIn *rtmidi )
  58. {
  59. std::cout << "\nWould you like to open a virtual input port? [y/N] ";
  60. std::string keyHit;
  61. std::getline( std::cin, keyHit );
  62. if ( keyHit == "y" ) {
  63. rtmidi->openVirtualPort();
  64. return true;
  65. }
  66. std::string portName;
  67. unsigned int i = 0, nPorts = rtmidi->getPortCount();
  68. if ( nPorts == 0 ) {
  69. std::cout << "No input ports available!" << std::endl;
  70. return false;
  71. }
  72. if ( nPorts == 1 ) {
  73. std::cout << "\nOpening " << rtmidi->getPortName() << std::endl;
  74. }
  75. else {
  76. for ( i=0; i<nPorts; i++ ) {
  77. portName = rtmidi->getPortName(i);
  78. std::cout << " Input port #" << i << ": " << portName << '\n';
  79. }
  80. do {
  81. std::cout << "\nChoose a port number: ";
  82. std::cin >> i;
  83. } while ( i >= nPorts );
  84. }
  85. std::getline( std::cin, keyHit ); // used to clear out stdin
  86. rtmidi->openPort( i );
  87. return true;
  88. }