sysextest.cpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. //*****************************************//
  2. // sysextest.cpp
  3. // by Gary Scavone, 2003-2005.
  4. //
  5. // Simple program to test MIDI sysex sending and receiving.
  6. //
  7. //*****************************************//
  8. #include <iostream>
  9. #include <cstdlib>
  10. #include <typeinfo>
  11. #include "RtMidi.h"
  12. void usage( void ) {
  13. std::cout << "\nuseage: sysextest N\n";
  14. std::cout << " where N = length of sysex message to send / receive.\n\n";
  15. exit( 0 );
  16. }
  17. // Platform-dependent sleep routines.
  18. #if defined(__WINDOWS_MM__)
  19. #include <windows.h>
  20. #define SLEEP( milliseconds ) Sleep( (DWORD) milliseconds )
  21. #else // Unix variants
  22. #include <unistd.h>
  23. #define SLEEP( milliseconds ) usleep( (unsigned long) (milliseconds * 1000.0) )
  24. #endif
  25. void mycallback( double deltatime, std::vector< unsigned char > *message, void *userData )
  26. {
  27. unsigned int nBytes = message->size();
  28. for ( unsigned int i=0; i<nBytes; i++ )
  29. std::cout << "Byte " << i << " = " << (int)message->at(i) << ", ";
  30. if ( nBytes > 0 )
  31. std::cout << "stamp = " << deltatime << std::endl;
  32. }
  33. int main( int argc, char *argv[] )
  34. {
  35. RtMidiOut *midiout = 0;
  36. RtMidiIn *midiin = 0;
  37. std::vector<unsigned char> message;
  38. unsigned int i, nBytes;
  39. // Minimal command-line check.
  40. if ( argc != 2 ) usage();
  41. nBytes = (unsigned int) atoi( argv[1] );
  42. // RtMidiOut and RtMidiIn constructors
  43. try {
  44. midiout = new RtMidiOut();
  45. midiin = new RtMidiIn();
  46. }
  47. catch ( RtMidiError &error ) {
  48. error.printMessage();
  49. goto cleanup;
  50. }
  51. // Don't ignore sysex, timing, or active sensing messages.
  52. midiin->ignoreTypes( false, true, true );
  53. try {
  54. midiin->openVirtualPort( "MyVirtualInputPort" );
  55. midiout->openPort( 0 );
  56. }
  57. catch ( RtMidiError &error ) {
  58. error.printMessage();
  59. goto cleanup;
  60. }
  61. midiin->setCallback( &mycallback );
  62. message.push_back( 0xF6 );
  63. midiout->sendMessage( &message );
  64. SLEEP( 500 ); // pause a little
  65. // Create a long sysex message of numbered bytes and send it out ... twice.
  66. for ( int n=0; n<2; n++ ) {
  67. message.clear();
  68. message.push_back( 240 );
  69. for ( i=0; i<nBytes; i++ )
  70. message.push_back( i % 128 );
  71. message.push_back( 247 );
  72. midiout->sendMessage( &message );
  73. SLEEP( 500 ); // pause a little
  74. }
  75. // Clean up
  76. cleanup:
  77. delete midiout;
  78. delete midiin;
  79. return 0;
  80. }