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.

208 lines
5.5KB

  1. // Miscellaneous parsing and error functions for use with STK projects.
  2. //
  3. // Gary P. Scavone, 1999.
  4. #include "utilities.h"
  5. #include <cstring>
  6. #include <stdlib.h>
  7. #if defined(__STK_REALTIME__)
  8. #include "RtAudio.h"
  9. #endif
  10. using namespace stk;
  11. void usage(char *function) {
  12. // Error function in case of incorrect command-line argument specifications
  13. printf("\nusage: %s flag(s)\n", function);
  14. printf(" where flag(s) = \n");
  15. printf(" -s RATE to specify a sample rate,\n");
  16. printf(" -ow <file name> for .wav audio output file,\n");
  17. printf(" -os <file name> for .snd audio output file,\n");
  18. printf(" -om <file name> for .mat audio output file,\n");
  19. printf(" -oa <file name> for .aif audio output file,\n");
  20. printf(" -if <file name> to read control input from SKINI file,\n");
  21. #if defined(__STK_REALTIME__)
  22. printf(" -or for realtime audio output,\n");
  23. printf(" -ip for realtime control input by pipe,\n");
  24. printf(" -im <port> for realtime control input by MIDI (virtual port = 0, default = 1).");
  25. #endif
  26. printf("\n");
  27. printf("\n Simultaneous multiple output types are supported.\n");
  28. printf(" Likewise, simultaneous control input types are supported.\n");
  29. printf(" SKINI formatted scorefiles can be piped or redirected\n");
  30. printf(" to %s, though realtime control flags should be omitted\n", function);
  31. printf(" when doing so. If the optional <file names> are not\n");
  32. printf(" specified, default names will be indicated. Each flag\n");
  33. printf(" must include its own '-' sign.\n\n");
  34. exit(0);
  35. }
  36. int checkArgs(int nArgs, char *args[])
  37. {
  38. int w, i = 1, j = 0;
  39. int nWvOuts = 0;
  40. char flags[2][50] = {""};
  41. bool realtime = false;
  42. if (nArgs < 3 || nArgs > 22) usage(args[0]);
  43. while (i < nArgs) {
  44. if (args[i][0] == '-') {
  45. if (args[i][1] == 'o') {
  46. if ( args[i][2] == 'r' ) realtime = true;
  47. if ( (args[i][2] == 's') || (args[i][2] == 'w') ||
  48. (args[i][2] == 'm') || (args[i][2] == 'a') )
  49. nWvOuts++;
  50. flags[0][j] = 'o';
  51. flags[1][j++] = args[i][2];
  52. }
  53. else if (args[i][1] == 'i') {
  54. if ( (args[i][2] != 'p') &&
  55. (args[i][2] != 'm') && (args[i][2] != 'f') ) usage(args[0]);
  56. flags[0][j] = 'i';
  57. flags[1][j++] = args[i][2];
  58. }
  59. else if (args[i][1] == 's' && (i+1 < nArgs) && args[i+1][0] != '-' ) {
  60. Stk::setSampleRate( atoi(args[i+1]) );
  61. flags[0][j++] = 's';
  62. }
  63. else usage(args[0]);
  64. }
  65. i++;
  66. }
  67. // Check for multiple flags of the same type
  68. for ( i=0; i<=j; i++ ) {
  69. w = i+1;
  70. while (w <= j ) {
  71. if ( flags[0][i] == flags[0][w] && flags[1][i] == flags[1][w] ) {
  72. printf("\nError: Multiple command line flags of the same type specified.\n\n");
  73. usage(args[0]);
  74. }
  75. w++;
  76. }
  77. }
  78. // Make sure we have at least one output type
  79. if ( nWvOuts < 1 && !realtime ) usage(args[0]);
  80. return nWvOuts;
  81. }
  82. bool parseArgs(int nArgs, char *args[], WvOut **output, Messager& messager)
  83. {
  84. int i = 1, j = 0, nWvIns = 0;
  85. bool realtime = false;
  86. char fileName[256];
  87. while (i < nArgs) {
  88. if ( (args[i][0] == '-') && (args[i][1] == 'i') ) {
  89. switch(args[i][2]) {
  90. case 'f':
  91. strcpy(fileName,args[++i]);
  92. if ( !messager.setScoreFile( fileName ) ) exit(0);
  93. nWvIns++;
  94. break;
  95. case 'p':
  96. #if defined(__STK_REALTIME__)
  97. if ( !messager.startStdInput() ) exit(0);
  98. nWvIns++;
  99. break;
  100. #else
  101. usage(args[0]);
  102. #endif
  103. case 'm':
  104. #if defined(__STK_REALTIME__)
  105. // Check for an optional MIDI port argument.
  106. if ((i+1 < nArgs) && args[i+1][0] != '-') {
  107. int port = atoi(args[++i]);
  108. if ( !messager.startMidiInput( port-1 ) ) exit(0);
  109. }
  110. else if ( !messager.startMidiInput() ) exit(0);
  111. nWvIns++;
  112. break;
  113. #else
  114. usage(args[0]);
  115. #endif
  116. default:
  117. usage(args[0]);
  118. break;
  119. }
  120. }
  121. else if ( (args[i][0] == '-') && (args[i][1] == 'o') ) {
  122. switch(args[i][2]) {
  123. case 'r':
  124. #if defined(__STK_REALTIME__)
  125. realtime = true;
  126. break;
  127. #else
  128. usage(args[0]);
  129. #endif
  130. case 'w':
  131. if ((i+1 < nArgs) && args[i+1][0] != '-') {
  132. i++;
  133. strcpy(fileName,args[i]);
  134. }
  135. else strcpy(fileName,"testwav");
  136. output[j] = new FileWvOut(fileName, 1, FileWrite::FILE_WAV );
  137. j++;
  138. break;
  139. case 's':
  140. if ((i+1 < nArgs) && args[i+1][0] != '-') {
  141. i++;
  142. strcpy(fileName,args[i]);
  143. }
  144. else strcpy(fileName,"testsnd");
  145. output[j] = new FileWvOut(fileName,1, FileWrite::FILE_SND);
  146. j++;
  147. break;
  148. case 'm':
  149. if ((i+1 < nArgs) && args[i+1][0] != '-') {
  150. i++;
  151. strcpy(fileName,args[i]);
  152. }
  153. else strcpy(fileName,"testmat");
  154. output[j] = new FileWvOut(fileName,1, FileWrite::FILE_MAT);
  155. j++;
  156. break;
  157. case 'a':
  158. if ((i+1 < nArgs) && args[i+1][0] != '-') {
  159. i++;
  160. strcpy(fileName,args[i]);
  161. }
  162. else strcpy(fileName,"testaif");
  163. output[j] = new FileWvOut(fileName,1, FileWrite::FILE_AIF );
  164. j++;
  165. break;
  166. default:
  167. usage(args[0]);
  168. break;
  169. }
  170. }
  171. i++;
  172. }
  173. if ( nWvIns == 0 ) {
  174. #if defined(__STK_REALTIME__)
  175. if ( !messager.startStdInput() ) exit(0);
  176. #else
  177. printf("\nError: The -if file input flag must be specified for non-realtime use.\n\n");
  178. usage(args[0]);
  179. #endif
  180. }
  181. return realtime;
  182. }