cmidiin.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. int main( int argc, char *argv[] )
  28. {
  29. RtMidiIn *midiin = 0;
  30. // Minimal command-line check.
  31. if ( argc > 2 ) usage();
  32. // RtMidiIn constructor
  33. try {
  34. midiin = new RtMidiIn();
  35. }
  36. catch ( RtError &error ) {
  37. error.printMessage();
  38. exit( EXIT_FAILURE );
  39. }
  40. // Check available ports vs. specified.
  41. unsigned int port = 0;
  42. unsigned int nPorts = midiin->getPortCount();
  43. if ( argc == 2 ) port = (unsigned int) atoi( argv[1] );
  44. if ( port >= nPorts ) {
  45. delete midiin;
  46. std::cout << "Invalid port specifier!\n";
  47. usage();
  48. }
  49. try {
  50. midiin->openPort( port );
  51. }
  52. catch ( RtError &error ) {
  53. error.printMessage();
  54. goto cleanup;
  55. }
  56. // Set our callback function. This should be done immediately after
  57. // opening the port to avoid having incoming messages written to the
  58. // queue instead of sent to the callback function.
  59. midiin->setCallback( &mycallback );
  60. // Don't ignore sysex, timing, or active sensing messages.
  61. midiin->ignoreTypes( false, false, false );
  62. std::cout << "\nReading MIDI input ... press <enter> to quit.\n";
  63. char input;
  64. std::cin.get(input);
  65. // Clean up
  66. cleanup:
  67. delete midiin;
  68. return 0;
  69. }