You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

266 lines
6.8KB

  1. /***************************************************/
  2. /*
  3. Simple realtime MIDI to SKINI parser.
  4. This object takes MIDI from the input stream
  5. (via the RtMidi class), parses it, and turns it
  6. into SKINI messages.
  7. by Perry R. Cook and Gary P. Scavone, 1995 - 2004.
  8. */
  9. /***************************************************/
  10. #include "RtMidi.h"
  11. #include "SKINImsg.h"
  12. #include <iostream>
  13. #include <stdlib.h>
  14. #include <stdio.h>
  15. void usage(void) {
  16. std::cout << "\nuseage: Md2Skini <flag(s)>\n\n";
  17. std::cout << " With no arguments, Md2Skini converts MIDI input to SKINI\n";
  18. std::cout << " format and sends the output directly to stdout.\n";
  19. std::cout << " With flag = -f <filename>, the output stream is simultaneously\n";
  20. std::cout << " written to the file specified by the optional <filename>\n";
  21. std::cout << " (default = test.ski).\n";
  22. std::cout << " With flag = -c, MIDI control change messages will not be\n";
  23. std::cout << " converted to SKINI-specific named controls.\n";
  24. std::cout << " A MIDI input port can be specified with flag = -p portNumber.\n" << std::endl;
  25. exit(0);
  26. }
  27. #include <signal.h>
  28. static void finish( int ignore ){ std::cout << "Type 'Exit' to quit." << std::endl; }
  29. bool parseSkiniControl = true;
  30. void midiCallback( double deltatime, std::vector< unsigned char > *bytes, void *userData )
  31. {
  32. if ( bytes->size() < 2 ) return;
  33. // Parse the MIDI bytes ... only keep MIDI channel messages.
  34. if ( bytes->at(0) > 239 ) return;
  35. register long type = bytes->at(0) & 0xF0;
  36. register int channel = bytes->at(0) & 0x0F;
  37. register long databyte1 = bytes->at(1);
  38. register long databyte2 = 0;
  39. if ( ( type != 0xC0 ) && ( type != 0xD0 ) ) {
  40. if ( bytes->size() < 3 ) return;
  41. databyte2 = bytes->at(2);
  42. }
  43. std::string typeName;
  44. switch( type ) {
  45. case __SK_NoteOn_:
  46. if ( databyte2 == 0 ) {
  47. typeName = "NoteOff\t\t";
  48. databyte2 = 64;
  49. }
  50. else typeName = "NoteOn\t\t";
  51. break;
  52. case __SK_NoteOff_:
  53. typeName = "NoteOff\t\t";
  54. break;
  55. case __SK_PolyPressure_:
  56. typeName = "PolyPressure\t";
  57. break;
  58. case __SK_ProgramChange_:
  59. typeName = "ProgramChange\t";
  60. break;
  61. case __SK_ChannelPressure_:
  62. typeName = "ChannelPressure\t";
  63. break;
  64. case __SK_PitchBend_:
  65. typeName = "PitchBend\t";
  66. break;
  67. case __SK_ControlChange_:
  68. if ( parseSkiniControl != true ) {
  69. typeName = "ControlChange\t";
  70. goto output;
  71. }
  72. switch( databyte1 ) {
  73. case __SK_PitchChange_:
  74. typeName = "PitchChange\t";
  75. goto output;
  76. case __SK_Volume_:
  77. typeName = "Volume\t";
  78. goto output;
  79. case __SK_ModWheel_:
  80. typeName = "ModWheel\t";
  81. goto output;
  82. case __SK_Breath_:
  83. typeName = "Breath\t\t";
  84. goto output;
  85. case __SK_FootControl_:
  86. typeName = "FootControl\t";
  87. goto output;
  88. case __SK_Portamento_:
  89. typeName = "Portamento\t";
  90. goto output;
  91. case __SK_Balance_:
  92. typeName = "Balance\t";
  93. goto output;
  94. case __SK_Pan_:
  95. typeName = "Pan\t\t";
  96. goto output;
  97. case __SK_Sustain_:
  98. typeName = "Sustain\t";
  99. goto output;
  100. case __SK_Expression_:
  101. typeName = "Expression\t";
  102. goto output;
  103. default:
  104. typeName = "ControlChange\t";
  105. goto output;
  106. }
  107. default:
  108. typeName = "Unknown\t";
  109. }
  110. output:
  111. FILE *file = (FILE *) userData;
  112. if ( type == 0xC0 || type == 0xD0 || type == 0xE0 ) { // program change, channel pressure, or pitchbend
  113. fprintf( stdout, "%s %.3f %d %.1f\n", typeName.c_str(), 0.0, channel, (float)databyte1 );
  114. if ( file != NULL )
  115. fprintf( file, "%s %.3f %d %.1f\n", typeName.c_str(), deltatime, channel, (float)databyte1 );
  116. }
  117. else if ( type == 0xB0 ) { // control change
  118. if ( typeName == "ControlChange\t" ) {
  119. fprintf( stdout, "%s %.3f %d %.1f %.1f\n", typeName.c_str(), 0.0, channel, (float)databyte1, (float)databyte2 );
  120. if ( file != NULL )
  121. fprintf( file, "%s %.3f %d %.1f %.1f\n", typeName.c_str(), deltatime, channel, (float)databyte1, (float)databyte2 );
  122. }
  123. else {
  124. fprintf( stdout, "%s %.3f %d %.1f\n", typeName.c_str(), 0.0, channel, (float)databyte2 );
  125. if ( file != NULL )
  126. fprintf( file, "%s %.3f %d %.1f\n", typeName.c_str(), deltatime, channel, (float)databyte2 );
  127. }
  128. }
  129. else { // noteon, noteoff, aftertouch, and unknown
  130. fprintf( stdout, "%s %.3f %d %.1f %.1f\n", typeName.c_str(), 0.0, channel, (float)databyte1, (float)databyte2 );
  131. if ( file != NULL )
  132. fprintf( file, "%s %.3f %d %.1f %.1f\n", typeName.c_str(), deltatime, channel, (float)databyte1, (float)databyte2 );
  133. }
  134. fflush( stdout );
  135. }
  136. int main( int argc,char *argv[] )
  137. {
  138. FILE *file = NULL;
  139. std::string fileName;
  140. RtMidiIn *midiin = 0;
  141. unsigned int port = 0;
  142. std::string input;
  143. if ( argc > 5 ) usage();
  144. // Parse the command-line arguments.
  145. int i = 1;
  146. while ( i < argc ) {
  147. if (argv[i][0] == '-') {
  148. switch(argv[i][1]) {
  149. case 'f':
  150. if ( (i+1 < argc) && argv[i+1][0] != '-' ) {
  151. i++;
  152. fileName = argv[i];
  153. if ( fileName.find( ".ski" ) == std::string::npos ) fileName.append( ".ski" );
  154. }
  155. else fileName = "test.ski";
  156. file = fopen( fileName.c_str(), "wb" );
  157. break;
  158. case 'p':
  159. if ( i++ >= argc) usage();
  160. port = (unsigned int) atoi( argv[i] );
  161. break;
  162. case 'c':
  163. parseSkiniControl = false;
  164. break;
  165. default:
  166. usage();
  167. break;
  168. }
  169. }
  170. else usage();
  171. i++;
  172. }
  173. try {
  174. midiin = new RtMidiIn();
  175. }
  176. catch (RtMidiError &error) {
  177. error.printMessage();
  178. if ( file != NULL ) fclose( file );
  179. exit(EXIT_FAILURE);
  180. }
  181. // Check available ports vs. specified.
  182. unsigned int nPorts = midiin->getPortCount();
  183. if ( nPorts == 0 ) {
  184. std::cout << "No MIDI ports available!\n";
  185. goto cleanup;
  186. }
  187. else if ( port >= nPorts ) {
  188. std::cout << "Invalid port specifier!\n";
  189. goto cleanup;
  190. }
  191. // Open the port.
  192. try {
  193. midiin->openPort( port );
  194. }
  195. catch (RtMidiError &error) {
  196. error.printMessage();
  197. goto cleanup;
  198. }
  199. // Set our callback function. This should be done immediately after
  200. // opening the port to avoid having incoming messages written to the
  201. // queue instead of sent to the callback function.
  202. midiin->setCallback( &midiCallback, file );
  203. // We'll ignore sysex, timing, and active sensing messages.
  204. midiin->ignoreTypes( true, true, true );
  205. // Install an interrupt handler function.
  206. (void) signal(SIGINT, finish);
  207. std::cout << "\nReading MIDI input ... type 'Exit' to quit.\n";
  208. while ( input != "Exit" && input != "exit" ) {
  209. input.erase();
  210. std::cin >> input;
  211. std::cout << input << std::endl;
  212. }
  213. cleanup:
  214. delete midiin;
  215. if ( file != NULL ) fclose( file );
  216. std::cout << "Md2Skini finished ... bye!" << std::endl;
  217. return 0;
  218. }