cmidiin.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 "RtMidi.h"
  11. void usage(void) {
  12. // Error function in case of incorrect command-line
  13. // argument specifications.
  14. std::cout << "\nuseage: cmidiin <port>\n";
  15. std::cout << " where port = the device to use (default = 0).\n\n";
  16. exit(0);
  17. }
  18. void mycallback( double deltatime, std::vector< unsigned char > *message, void *userData )
  19. {
  20. unsigned int nBytes = message->size();
  21. for ( unsigned int i=0; i<nBytes; i++ )
  22. std::cout << "Byte " << i << " = " << (int)message->at(i) << ", ";
  23. if ( nBytes > 0 )
  24. std::cout << "stamp = " << deltatime << '\n';
  25. }
  26. int main(int argc, char *argv[])
  27. {
  28. RtMidiIn *midiin = 0;
  29. // Minimal command-line check.
  30. if ( argc > 2 ) usage();
  31. // RtMidiIn constructor
  32. try {
  33. midiin = new RtMidiIn();
  34. }
  35. catch (RtError &error) {
  36. error.printMessage();
  37. exit(EXIT_FAILURE);
  38. }
  39. // Check available ports vs. specified.
  40. unsigned int port = 0;
  41. unsigned int nPorts = midiin->getPortCount();
  42. if ( argc == 2 ) port = (unsigned int) atoi( argv[1] );
  43. if ( port >= nPorts ) {
  44. delete midiin;
  45. std::cout << "Invalid port specifier!\n";
  46. usage();
  47. }
  48. try {
  49. midiin->openPort( port );
  50. }
  51. catch (RtError &error) {
  52. error.printMessage();
  53. goto cleanup;
  54. }
  55. // Set our callback function. This should be done immediately after
  56. // opening the port to avoid having incoming messages written to the
  57. // queue instead of sent to the callback function.
  58. midiin->setCallback( &mycallback );
  59. // Don't ignore sysex, timing, or active sensing messages.
  60. midiin->ignoreTypes( false, false, false );
  61. std::cout << "\nReading MIDI input ... press <enter> to quit.\n";
  62. char input;
  63. std::cin.get(input);
  64. // Clean up
  65. cleanup:
  66. delete midiin;
  67. return 0;
  68. }