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.

334 lines
9.4KB

  1. // Miscellaneous parsing and error functions for use with STK demo program.
  2. //
  3. // Gary P. Scavone, 1999.
  4. #include <cstring>
  5. #include "utilities.h"
  6. // STK Instrument Classes
  7. #include "Clarinet.h"
  8. #include "BlowHole.h"
  9. #include "Saxofony.h"
  10. #include "Flute.h"
  11. #include "Brass.h"
  12. #include "BlowBotl.h"
  13. #include "Bowed.h"
  14. #include "Plucked.h"
  15. #include "StifKarp.h"
  16. #include "Sitar.h"
  17. #include "Mandolin.h"
  18. #include "Rhodey.h"
  19. #include "Wurley.h"
  20. #include "TubeBell.h"
  21. #include "HevyMetl.h"
  22. #include "PercFlut.h"
  23. #include "BeeThree.h"
  24. #include "FMVoices.h"
  25. #include "VoicForm.h"
  26. #include "Moog.h"
  27. #include "Simple.h"
  28. #include "Drummer.h"
  29. #include "BandedWG.h"
  30. #include "Shakers.h"
  31. #include "ModalBar.h"
  32. #include "Mesh2D.h"
  33. #include "Resonate.h"
  34. #include "Whistle.h"
  35. using namespace stk;
  36. #define NUM_INSTS 28
  37. // The order of the following list is important. The location of a particular
  38. // instrument in the list should correspond to that instrument's ProgramChange
  39. // number (i.e. Clarinet = ProgramChange 0).
  40. char insts[NUM_INSTS][10] = { "Clarinet", "BlowHole", "Saxofony", "Flute", "Brass",
  41. "BlowBotl", "Bowed", "Plucked", "StifKarp", "Sitar", "Mandolin",
  42. "Rhodey", "Wurley", "TubeBell", "HevyMetl", "PercFlut",
  43. "BeeThree", "FMVoices", "VoicForm", "Moog", "Simple", "Drummer",
  44. "BandedWG", "Shakers", "ModalBar", "Mesh2D", "Resonate", "Whistle" };
  45. int voiceByNumber(int number, Instrmnt **instrument)
  46. {
  47. int temp = number;
  48. if (number==0) *instrument = new Clarinet(10.0);
  49. else if (number==1) *instrument = new BlowHole(10.0);
  50. else if (number==2) *instrument = new Saxofony(10.0);
  51. else if (number==3) *instrument = new Flute(10.0);
  52. else if (number==4) *instrument = new Brass(10.0);
  53. else if (number==5) *instrument = new BlowBotl;
  54. else if (number==6) *instrument = new Bowed(10.0);
  55. else if (number==7) *instrument = new Plucked(5.0);
  56. else if (number==8) *instrument = new StifKarp(5.0);
  57. else if (number==9) *instrument = new Sitar(5.0);
  58. else if (number==10) *instrument = new Mandolin(5.0);
  59. else if (number==11) *instrument = new Rhodey;
  60. else if (number==12) *instrument = new Wurley;
  61. else if (number==13) *instrument = new TubeBell;
  62. else if (number==14) *instrument = new HevyMetl;
  63. else if (number==15) *instrument = new PercFlut;
  64. else if (number==16) *instrument = new BeeThree;
  65. else if (number==17) *instrument = new FMVoices;
  66. else if (number==18) *instrument = new VoicForm;
  67. else if (number==19) *instrument = new Moog;
  68. else if (number==20) *instrument = new Simple;
  69. else if (number==21) *instrument = new Drummer;
  70. else if (number==22) *instrument = new BandedWG;
  71. else if (number==23) *instrument = new Shakers;
  72. else if (number==24) *instrument = new ModalBar;
  73. else if (number==25) *instrument = new Mesh2D(10, 10);
  74. else if (number==26) *instrument = new Resonate;
  75. else if (number==27) *instrument = new Whistle;
  76. else {
  77. printf("\nUnknown instrument or program change requested!\n");
  78. temp = -1;
  79. }
  80. return temp;
  81. }
  82. int voiceByName(char *name, Instrmnt **instrument)
  83. {
  84. int i = 0, temp = -1, notFound = 1;;
  85. while ( i < NUM_INSTS && notFound ) {
  86. if ( !strcmp(name, insts[i]) ) {
  87. notFound = 0;
  88. temp = voiceByNumber(i, instrument);
  89. }
  90. i++;
  91. }
  92. if (temp < 0)
  93. printf("\nUnknown instrument or program change requested!\n");
  94. return temp;
  95. }
  96. void usage(char *function) {
  97. // Error function in case of incorrect command-line argument specifications
  98. int i, j;
  99. printf("\nuseage: %s Instrument flag(s) \n", function);
  100. printf(" where flag = -s RATE to specify a sample rate,\n");
  101. printf(" -n NUMBER specifies the number of voices to allocate,\n");
  102. printf(" -ow <file name> for .wav audio output file,\n");
  103. printf(" -os <file name> for .snd audio output file,\n");
  104. printf(" -om <file name> for .mat audio output file,\n");
  105. printf(" -oa <file name> for .aif audio output file,\n");
  106. #if defined(__STK_REALTIME__)
  107. printf(" -or for realtime audio output,\n");
  108. #endif
  109. printf(" -if <file name> to read control input from SKINI file,\n");
  110. #if defined(__STK_REALTIME__)
  111. printf(" -ip for realtime control input by pipe,\n");
  112. printf(" -im <port> for realtime control input by MIDI (virtual port = 0, default = 1),\n");
  113. #endif
  114. printf(" and Instrument = one of these:\n");
  115. for ( i=0; i<NUM_INSTS; i+=8 ) {
  116. for ( j=0; j<8 && (i+j)<NUM_INSTS; j++ ) {
  117. printf("%s ",insts[i+j]);
  118. }
  119. printf("\n");
  120. }
  121. printf("\nSimultaneous multiple output types are supported.\n");
  122. printf("Likewise, simultaneous control input types are supported.\n");
  123. printf("SKINI formatted scorefiles can be piped or redirected\n");
  124. printf("to %s, though realtime control flags should be omitted\n", function);
  125. printf("when doing so. If the optional <file names> are not\n");
  126. printf("specified, default names will be indicated. Each flag\n");
  127. printf("must include its own '-' sign.\n\n");
  128. exit(0);
  129. }
  130. int checkArgs(int nArgs, char *args[])
  131. {
  132. int w, i = 2, j = 0;
  133. int nWvOuts = 0;
  134. char flags[2][50] = {""};
  135. bool realtime = false;
  136. if (nArgs < 3 || nArgs > 22) usage(args[0]);
  137. while (i < nArgs) {
  138. if (args[i][0] == '-') {
  139. if (args[i][1] == 'o') {
  140. if ( args[i][2] == 'r' ) realtime = true;
  141. if ( (args[i][2] == 's') || (args[i][2] == 'w') ||
  142. (args[i][2] == 'm') || (args[i][2] == 'a') )
  143. nWvOuts++;
  144. flags[0][j] = 'o';
  145. flags[1][j++] = args[i][2];
  146. }
  147. else if (args[i][1] == 'i') {
  148. if ( (args[i][2] != 'p') && (args[i][2] != 'm') &&
  149. (args[i][2] != 'f') ) usage(args[0]);
  150. flags[0][j] = 'i';
  151. flags[1][j++] = args[i][2];
  152. }
  153. else if (args[i][1] == 's' && (i+1 < nArgs) && args[i+1][0] != '-' ) {
  154. Stk::setSampleRate( atoi(args[i+1]) );
  155. flags[0][j++] = 's';
  156. }
  157. else if (args[i][1] == 'n' && (i+1 < nArgs) && args[i+1][0] != '-' ) {
  158. flags[0][j++] = 'n';
  159. }
  160. else usage(args[0]);
  161. }
  162. i++;
  163. }
  164. // Check for multiple flags of the same type
  165. for ( i=0; i<=j; i++ ) {
  166. w = i+1;
  167. while (w <= j ) {
  168. if ( flags[0][i] == flags[0][w] && flags[1][i] == flags[1][w] ) {
  169. printf("\nError: Multiple command line flags of the same type specified.\n\n");
  170. usage(args[0]);
  171. }
  172. w++;
  173. }
  174. }
  175. // Make sure we have at least one output type
  176. if ( nWvOuts < 1 && !realtime ) usage(args[0]);
  177. return nWvOuts;
  178. }
  179. int countVoices(int nArgs, char *args[])
  180. {
  181. int i = 2, nInstruments = 1;
  182. while ( i < nArgs ) {
  183. if ( strncmp( args[i], "-n", 2) == 0 ) {
  184. if ( i+1 < nArgs && args[i+1][0] != '-' ) {
  185. nInstruments = atoi( args[i+1] );
  186. if ( nInstruments < 1 ) nInstruments = 1;
  187. }
  188. }
  189. i++;
  190. }
  191. return nInstruments;
  192. }
  193. bool parseArgs(int nArgs, char *args[], WvOut **output, Messager& messager)
  194. {
  195. int i = 2, j = 0, nWvIns = 0;
  196. bool realtime = false;
  197. char fileName[256];
  198. while (i < nArgs) {
  199. if ( (args[i][0] == '-') && (args[i][1] == 'i') ) {
  200. switch(args[i][2]) {
  201. case 'f':
  202. strcpy(fileName,args[++i]);
  203. if ( !messager.setScoreFile( fileName ) ) exit(0);
  204. nWvIns++;
  205. break;
  206. case 'p':
  207. #if defined(__STK_REALTIME__)
  208. if ( !messager.startStdInput() ) exit(0);
  209. nWvIns++;
  210. break;
  211. #else
  212. usage(args[0]);
  213. #endif
  214. case 'm':
  215. #if defined(__STK_REALTIME__)
  216. // Check for an optional MIDI port argument.
  217. if ((i+1 < nArgs) && args[i+1][0] != '-') {
  218. int port = atoi(args[++i]);
  219. if ( !messager.startMidiInput( port-1 ) ) exit(0);
  220. }
  221. else if ( !messager.startMidiInput() ) exit(0);
  222. nWvIns++;
  223. break;
  224. #else
  225. usage(args[0]);
  226. #endif
  227. default:
  228. usage(args[0]);
  229. break;
  230. }
  231. }
  232. else if ( (args[i][0] == '-') && (args[i][1] == 'o') ) {
  233. switch(args[i][2]) {
  234. case 'r':
  235. #if defined(__STK_REALTIME__)
  236. realtime = true;
  237. break;
  238. #else
  239. usage(args[0]);
  240. #endif
  241. case 'w':
  242. if ((i+1 < nArgs) && args[i+1][0] != '-') {
  243. i++;
  244. strcpy(fileName,args[i]);
  245. }
  246. else strcpy(fileName,"testwav");
  247. output[j] = new FileWvOut(fileName, 1, FileWrite::FILE_WAV );
  248. j++;
  249. break;
  250. case 's':
  251. if ((i+1 < nArgs) && args[i+1][0] != '-') {
  252. i++;
  253. strcpy(fileName,args[i]);
  254. }
  255. else strcpy(fileName,"testsnd");
  256. output[j] = new FileWvOut(fileName,1, FileWrite::FILE_SND);
  257. j++;
  258. break;
  259. case 'm':
  260. if ((i+1 < nArgs) && args[i+1][0] != '-') {
  261. i++;
  262. strcpy(fileName,args[i]);
  263. }
  264. else strcpy(fileName,"testmat");
  265. output[j] = new FileWvOut(fileName,1, FileWrite::FILE_MAT);
  266. j++;
  267. break;
  268. case 'a':
  269. if ((i+1 < nArgs) && args[i+1][0] != '-') {
  270. i++;
  271. strcpy(fileName,args[i]);
  272. }
  273. else strcpy(fileName,"testaif");
  274. output[j] = new FileWvOut(fileName,1, FileWrite::FILE_AIF );
  275. j++;
  276. break;
  277. default:
  278. usage(args[0]);
  279. break;
  280. }
  281. }
  282. i++;
  283. }
  284. if ( nWvIns == 0 ) {
  285. #if defined(__STK_REALTIME__)
  286. if ( !messager.startStdInput() ) exit(0);
  287. #else
  288. printf("\nError: The -if file input flag must be specified for non-realtime use.\n\n");
  289. usage(args[0]);
  290. #endif
  291. }
  292. return realtime;
  293. }