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.

qmidiin.cpp 2.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. //*****************************************//
  2. // qmidiin.cpp
  3. // by Gary Scavone, 2003-2004.
  4. //
  5. // Simple program to test MIDI input and
  6. // retrieval from the queue.
  7. //
  8. //*****************************************//
  9. #include <iostream>
  10. #include <cstdlib>
  11. #include <signal.h>
  12. #include "RtMidi.h"
  13. // Platform-dependent sleep routines.
  14. #if defined(__WINDOWS_MM__)
  15. #include <windows.h>
  16. #define SLEEP( milliseconds ) Sleep( (DWORD) milliseconds )
  17. #else // Unix variants
  18. #include <unistd.h>
  19. #define SLEEP( milliseconds ) usleep( (unsigned long) (milliseconds * 1000.0) )
  20. #endif
  21. bool done;
  22. static void finish( int ignore ){ done = true; }
  23. void usage( void ) {
  24. // Error function in case of incorrect command-line
  25. // argument specifications.
  26. std::cout << "\nusage: qmidiin <port>\n";
  27. std::cout << " where port = the device to use (default = 0).\n\n";
  28. exit( 0 );
  29. }
  30. int main( int argc, char *argv[] )
  31. {
  32. RtMidiIn *midiin = 0;
  33. std::vector<unsigned char> message;
  34. int nBytes, i;
  35. double stamp;
  36. // Minimal command-line check.
  37. if ( argc > 2 ) usage();
  38. // RtMidiIn constructor
  39. try {
  40. midiin = new RtMidiIn();
  41. }
  42. catch ( RtError &error ) {
  43. error.printMessage();
  44. exit( EXIT_FAILURE );
  45. }
  46. // Check available ports vs. specified.
  47. unsigned int port = 0;
  48. unsigned int nPorts = midiin->getPortCount();
  49. if ( argc == 2 ) port = (unsigned int) atoi( argv[1] );
  50. if ( port >= nPorts ) {
  51. delete midiin;
  52. std::cout << "Invalid port specifier!\n";
  53. usage();
  54. }
  55. try {
  56. midiin->openPort( port );
  57. }
  58. catch ( RtError &error ) {
  59. error.printMessage();
  60. goto cleanup;
  61. }
  62. // Don't ignore sysex, timing, or active sensing messages.
  63. midiin->ignoreTypes( false, false, false );
  64. // Install an interrupt handler function.
  65. done = false;
  66. (void) signal(SIGINT, finish);
  67. // Periodically check input queue.
  68. std::cout << "Reading MIDI from port ... quit with Ctrl-C.\n";
  69. while ( !done ) {
  70. stamp = midiin->getMessage( &message );
  71. nBytes = message.size();
  72. for ( i=0; i<nBytes; i++ )
  73. std::cout << "Byte " << i << " = " << (int)message[i] << ", ";
  74. if ( nBytes > 0 )
  75. std::cout << "stamp = " << stamp << std::endl;
  76. // Sleep for 10 milliseconds.
  77. SLEEP( 10 );
  78. }
  79. // Clean up
  80. cleanup:
  81. delete midiin;
  82. return 0;
  83. }