qmidiin.cpp 2.2 KB

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