cmidiin.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. {
  14. // Error function in case of incorrect command-line
  15. // argument specifications.
  16. std::cout << "\nuseage: cmidiin <port>\n";
  17. std::cout << " where port = the device to use (default = 0).\n\n";
  18. exit(0);
  19. }
  20. void mycallback(double deltatime, std::vector<unsigned char> *message, void *userData)
  21. {
  22. unsigned int nBytes = message->size();
  23. for (unsigned int i = 0; i < nBytes; i++)
  24. std::cout << "Byte " << i << " = " << (int)message->at(i) << ", ";
  25. if (nBytes > 0)
  26. std::cout << "stamp = " << deltatime << std::endl;
  27. }
  28. // This function should be embedded in a try/catch block in case of
  29. // an exception. It offers the user a choice of MIDI ports to open.
  30. // It returns false if there are no ports available.
  31. bool chooseMidiPort(RtMidiIn *rtmidi);
  32. int main(int argc, char *argv[])
  33. {
  34. RtMidiIn *midiin = 0;
  35. // Minimal command-line check.
  36. if (argc > 2) usage();
  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. std::cin.get(input);
  51. cleanup:
  52. delete midiin;
  53. return 0;
  54. }
  55. bool chooseMidiPort(RtMidiIn *rtmidi)
  56. {
  57. /*
  58. std::cout << "\nWould you like to open a virtual input port? [y/N] ";
  59. std::string keyHit;
  60. std::getline( std::cin, keyHit );
  61. if ( keyHit == "y" ) {
  62. rtmidi->openVirtualPort();
  63. return true;
  64. }
  65. */
  66. std::string portName;
  67. unsigned int i = 0, nPorts = rtmidi->getPortCount();
  68. if (nPorts == 0)
  69. {
  70. std::cout << "No input ports available!" << std::endl;
  71. return false;
  72. }
  73. if (nPorts == 1)
  74. {
  75. std::cout << "\nOpening " << rtmidi->getPortName() << std::endl;
  76. }
  77. else
  78. {
  79. for (i = 0; i < nPorts; i++)
  80. {
  81. portName = rtmidi->getPortName(i);
  82. std::cout << " Input port #" << i << ": " << portName << '\n';
  83. }
  84. do
  85. {
  86. std::cout << "\nChoose a port number: ";
  87. std::cin >> i;
  88. } while (i >= nPorts);
  89. }
  90. // std::getline( std::cin, keyHit ); // used to clear out stdin
  91. rtmidi->openPort(i);
  92. return true;
  93. }