PageRenderTime 26ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/gt-0.4/src/cfg.l

#
LEX | 1014 lines | 917 code | 90 blank | 7 comment | 0 complexity | f89e814e5f18393bb597ca93a6bb5075 MD5 | raw file
  1. %{
  2. /*
  3. * Cfg file reader, Greg Lee, 8/93.
  4. * Adapted from Adagio for KMidi 12/99.
  5. * $Id: cfg.l,v 1.8 2000/08/08 17:32:37 faure Exp $
  6. */
  7. #include <ctype.h>
  8. #include <stdlib.h>
  9. #include <unistd.h>
  10. #include <string.h>
  11. #include "gtim.h"
  12. #include "common.h"
  13. #include "instrum.h"
  14. #include "playmidi.h"
  15. #include "readmidi.h"
  16. #include "output.h"
  17. #include "controls.h"
  18. #include "tables.h"
  19. #include "sbk.h"
  20. #include "sflayer.h"
  21. #define YY_ALWAYS_INTERACTIVE 0
  22. #define YY_NEVER_INTERACTIVE 1
  23. #define YY_INPUT(buf,result,max_size) \
  24. result = fread( buf, 1, max_size, yyin );
  25. extern int set_play_mode(char *cp);
  26. static int prescanning;
  27. static char doing_drums = 0;
  28. static char doing_sf = 0;
  29. static int patchno = -1;
  30. static char *patchname;
  31. static char cfg_flag = 1;
  32. static int tone_bank;
  33. static void set_patchno(char *m);
  34. static char *s_dirname = 0;
  35. static char *sfname = 0;
  36. static int current_toneset = 0;
  37. static int current_drumset = 0;
  38. static int cfg_condition = -1;
  39. static ToneBank *bank=0;
  40. static int banknum=0;
  41. static int rcf_count=1;
  42. static int font_type=FONT_GUS;
  43. #define MAX_SF_COUNT 4096
  44. static struct cfg_type a_sf[MAX_SF_COUNT];
  45. static int sf_count = 0;
  46. #define MAX_INCLUDE_DEPTH 40
  47. static YY_BUFFER_STATE include_stack[MAX_INCLUDE_DEPTH];
  48. static int include_stack_ptr = 0;
  49. static char *this_fname[MAX_INCLUDE_DEPTH] = { NULL };
  50. static int this_line[MAX_INCLUDE_DEPTH] = { 0 };
  51. static void new_toneset(int n, int highest);
  52. static void new_patch(const char *vname, int num);
  53. static int sf2tab(char *sffname);
  54. static void interpret_sf(void);
  55. %}
  56. %option noyywrap
  57. %option nounput
  58. %option outfile = "cfg.c"
  59. %s P K S
  60. wh [ \t]+
  61. owh [ \t]*
  62. lwh ([ \t]*"\\"[ \t]*\n)?[ \t]*
  63. nm ([^ \t\n\r\"#]+|\"[^\"\n]+\")
  64. cm [ \t]*("#".*)?\n
  65. %%
  66. ^{owh}"if"{wh}[0-9]+ {
  67. int i = 0;
  68. while (isspace(yytext[i])) i++;
  69. i += 2;
  70. cfg_condition = atoi(yytext+i);
  71. }
  72. ^{owh}"else" {
  73. cfg_condition = 0;
  74. }
  75. ^{owh}("sf"|"sbk"){wh}{nm}{owh}[0-9]* {
  76. int sf_oldbank, sf_newbank = banknum;
  77. int i = 0, j;
  78. unsigned namelen;
  79. while (isspace(yytext[i])) i++;
  80. if (yytext[i+1] == 'f') i += 2;
  81. else i += 3;
  82. while (isspace(yytext[i])) i++;
  83. if (yytext[i] == '"') {
  84. i++;
  85. for (j = i; j < yyleng && yytext[j] != '"'; j++) ;
  86. /*j--;*/
  87. }
  88. else for (j = i; j < yyleng && !isspace(yytext[j]); j++) ;
  89. namelen = (unsigned)(j - i + 1);
  90. sfname = strncpy( (char *)malloc(namelen), yytext+i, (unsigned)(j-i) );
  91. sfname[j-i] = '\0';
  92. if (doing_drums) sf_newbank += 256;
  93. sf_oldbank = sf_newbank;
  94. if (j < yyleng) {
  95. while (j < yyleng && isspace(yytext[j])) j++;
  96. if (j < yyleng && isdigit(yytext[j])) sf_oldbank = atoi(yytext+j);
  97. }
  98. init_soundfont(sfname, sf_oldbank, sf_newbank, rcf_count);
  99. }
  100. ^{owh}"soundfont"{wh}{nm} {
  101. int i = 0, j;
  102. unsigned namelen;
  103. while (isspace(yytext[i])) i++;
  104. i += 9;
  105. while (isspace(yytext[i])) i++;
  106. if (yytext[i] == '"') {
  107. i++;
  108. for (j = i; j < yyleng && yytext[j] != '"'; j++) ;
  109. }
  110. else for (j = i; j < yyleng && !isspace(yytext[j]); j++) ;
  111. namelen = (unsigned)(j - i + 1);
  112. if (prescanning && cfg_condition >= 0 && cfg_condition < 30 &&
  113. rcf_count==1 && !cfg_names[cfg_condition]) {
  114. cfg_names[cfg_condition] = strncpy( (char *)malloc(namelen), yytext+i, (unsigned)(j-i) );
  115. cfg_names[cfg_condition][j-i] = '\0';
  116. }
  117. if (!prescanning && (cfg_condition < 0 || cfg_condition == cfg_select)) {
  118. sfname = strncpy( (char *)malloc(namelen), yytext+i, (unsigned)(j-i) );
  119. sfname[j-i] = '\0';
  120. sf_count = 0;
  121. if (sf2tab(sfname) && sf_count) interpret_sf();
  122. }
  123. cfg_condition = -1;
  124. }
  125. ^{owh}("dir"|"PatchDir:"){wh}{nm} {
  126. int i = 0, j;
  127. unsigned namelen;
  128. while (isspace(yytext[i])) i++;
  129. if (yytext[i] == 'd') i += 3;
  130. else i += 9;
  131. while (isspace(yytext[i])) i++;
  132. if (yytext[i] == '"') {
  133. i++;
  134. for (j = i; j < yyleng && yytext[j] != '"'; j++) ;
  135. }
  136. else for (j = i; j < yyleng && !isspace(yytext[j]); j++) ;
  137. namelen = (unsigned)(j - i + 1);
  138. if (!prescanning) {
  139. s_dirname = strncpy( (char *)malloc(namelen), yytext+i, namelen-1 );
  140. s_dirname[j-i] = '\0';
  141. add_to_pathlist(s_dirname, rcf_count);
  142. free(s_dirname);
  143. }
  144. }
  145. ^{owh}"source"{wh}{nm} {
  146. char *fname;
  147. int i = 0, j;
  148. unsigned namelen;
  149. FILE *save_yyin;
  150. while (isspace(yytext[i])) i++;
  151. i += 6;
  152. while (isspace(yytext[i])) i++;
  153. if (yytext[i] == '"') {
  154. i++;
  155. for (j = i; j < yyleng && yytext[j] != '"'; j++) ;
  156. }
  157. else for (j = i; j < yyleng && !isspace(yytext[j]); j++) ;
  158. namelen = (unsigned)(j - i + 1);
  159. if (prescanning && cfg_condition >= 0 && cfg_condition < 30 &&
  160. rcf_count==1 && !cfg_names[cfg_condition])
  161. {
  162. cfg_names[cfg_condition] = (char *)safe_malloc(namelen);
  163. strcpy(cfg_names[cfg_condition], yytext+i);
  164. }
  165. if (!prescanning && (cfg_condition < 0 || cfg_condition == cfg_select)) {
  166. if ( include_stack_ptr >= MAX_INCLUDE_DEPTH ) {
  167. ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
  168. "Probable source loop in configuration files");
  169. return (-1);
  170. }
  171. include_stack[include_stack_ptr++] =
  172. YY_CURRENT_BUFFER;
  173. fname = strcpy( (char *)malloc(namelen), yytext+i );
  174. if (this_fname[include_stack_ptr]) free(this_fname[include_stack_ptr]);
  175. this_fname[include_stack_ptr] = strdup(fname);
  176. save_yyin = yyin;
  177. yyin = NULL;
  178. yyin = open_file(fname, 1, OF_VERBOSE, rcf_count);
  179. if (yyin == NULL) {
  180. ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
  181. "Source file %s not found\n", fname);
  182. include_stack_ptr--;
  183. }
  184. if (yyin == NULL) yyin = save_yyin;
  185. else {
  186. cfg_condition = -1;
  187. rcf_count++;
  188. yy_switch_to_buffer(
  189. yy_create_buffer( yyin, YY_BUF_SIZE ) );
  190. }
  191. }
  192. cfg_condition = -1;
  193. }
  194. <<EOF>> {
  195. if ( --include_stack_ptr < 0 ) {
  196. yyterminate();
  197. }
  198. else {
  199. if (yyin) close_file(yyin);
  200. yy_delete_buffer( YY_CURRENT_BUFFER );
  201. rcf_count--;
  202. yy_switch_to_buffer(
  203. include_stack[include_stack_ptr] );
  204. }
  205. }
  206. ^{owh}"drumset"{wh}[0-9]+{wh}("sf"|"sbk"){wh}("#N"{wh})?{nm} {
  207. char *bank_name = NULL;
  208. int i = 0;
  209. while (isspace(yytext[i])) i++;
  210. i += 7;
  211. current_drumset = atoi(yytext+i);
  212. doing_drums = 1;
  213. doing_sf = 1;
  214. new_toneset(current_drumset, MAXPROG);
  215. font_type=FONT_SF;
  216. if (bank && !bank->name) {
  217. while (isspace(yytext[i])) i++;
  218. while (isdigit(yytext[i])) i++;
  219. while (isspace(yytext[i])) i++;
  220. if (yytext[i+1] == 'f') i += 2; else i += 3;
  221. while (isspace(yytext[i])) i++;
  222. if (yytext[i] == '#') i += 2;
  223. while (isspace(yytext[i])) i++;
  224. patchname = yytext + i;
  225. if (patchname[0] == '"') {
  226. bank_name = strncpy( (char *)malloc(strlen(patchname)-1), patchname + 1, strlen(patchname)-1 );
  227. bank_name[strlen(patchname)-2] = '\0';
  228. }
  229. else bank_name = strcpy( (char *)malloc(strlen(patchname)+1), patchname );
  230. bank->name = bank_name;
  231. }
  232. }
  233. ^{owh}"drumset"{wh}[0-9]+{wh}("sf"|"sbk") {
  234. int i = 0;
  235. while (isspace(yytext[i])) i++;
  236. i += 7;
  237. current_drumset = atoi(yytext+i);
  238. doing_drums = 1;
  239. doing_sf = 1;
  240. new_toneset(current_drumset, MAXPROG);
  241. font_type=FONT_SF;
  242. }
  243. ^{owh}"drums"("et"|"fx"){wh}[0-9]+{wh}("#N"{wh})?{nm} {
  244. char *bank_name = NULL;
  245. int i = 0;
  246. int is_sfx = 0;
  247. while (isspace(yytext[i])) i++;
  248. i += 7;
  249. if (yytext[i-1] == 'x') is_sfx = 1;
  250. current_drumset = atoi(yytext+i);
  251. if (is_sfx) current_drumset += SFXBANK;
  252. doing_drums = 1;
  253. doing_sf = 0;
  254. new_toneset(current_drumset, is_sfx? SFXBANK+MAXPROG : MAXPROG);
  255. font_type=FONT_GUS;
  256. if (bank && !bank->name) {
  257. while (isspace(yytext[i])) i++;
  258. while (isdigit(yytext[i])) i++;
  259. while (isspace(yytext[i])) i++;
  260. if (yytext[i] == '#') i += 2;
  261. while (isspace(yytext[i])) i++;
  262. patchname = yytext + i;
  263. if (patchname[0] == '"') {
  264. bank_name = strncpy( (char *)malloc(strlen(patchname)-1), patchname + 1, strlen(patchname)-1 );
  265. bank_name[strlen(patchname)-2] = '\0';
  266. }
  267. else bank_name = strcpy( (char *)malloc(strlen(patchname)+1), patchname );
  268. bank->name = bank_name;
  269. }
  270. }
  271. ^{owh}"drums"("et"|"fx"){wh}[0-9]+ {
  272. char *bank_name = NULL;
  273. int i = 0;
  274. int is_sfx = 0;
  275. while (isspace(yytext[i])) i++;
  276. i += 7;
  277. if (yytext[i-1] == 'x') is_sfx = 1;
  278. current_drumset = atoi(yytext+i);
  279. if (is_sfx) current_drumset += SFXBANK;
  280. doing_drums = 1;
  281. doing_sf = 0;
  282. new_toneset(current_drumset, is_sfx? SFXBANK+MAXPROG : MAXPROG);
  283. font_type=FONT_GUS;
  284. if (bank && !bank->name) switch (current_drumset) {
  285. case SFXDRUM1:
  286. case SFXDRUM2:
  287. bank_name = "rhythm sfx";
  288. break;
  289. case TECHNO_KS:
  290. bank_name = "techno k/s";
  291. break;
  292. case TECHNO_HI:
  293. bank_name = "techno hi";
  294. break;
  295. case TECHNO_LO:
  296. bank_name = "techno lo";
  297. break;
  298. case SAKURA:
  299. bank_name = "sakura";
  300. break;
  301. case SMALL_LATIN:
  302. bank_name = "latin";
  303. break;
  304. default:
  305. break;
  306. }
  307. if (bank_name && bank)
  308. bank->name = strcpy( (char *)malloc(strlen(bank_name)+1), bank_name );
  309. }
  310. ^{owh}"bank"{wh}[0-9]+{wh}("sf"|"sbk") {
  311. int i = 0;
  312. while (isspace(yytext[i])) i++;
  313. i += 4;
  314. current_toneset = atoi(yytext+i);
  315. doing_drums = 0;
  316. doing_sf = 1;
  317. new_toneset(current_toneset, MAXPROG);
  318. font_type=FONT_SF;
  319. }
  320. ^{owh}"bank"{wh}[0-9]+ {
  321. int i = 0;
  322. while (isspace(yytext[i])) i++;
  323. i += 4;
  324. current_toneset = atoi(yytext+i);
  325. doing_drums = 0;
  326. doing_sf = 0;
  327. new_toneset(current_toneset, MAXPROG);
  328. font_type=FONT_GUS;
  329. }
  330. ^{owh}"mu100"{wh}[0-9]+ {
  331. int i = 0;
  332. while (isspace(yytext[i])) i++;
  333. i += 5;
  334. current_toneset = atoi(yytext+i) + MU100BANK;
  335. doing_drums = 0;
  336. doing_sf = 0;
  337. new_toneset(current_toneset, MU100BANK+MAXPROG);
  338. font_type=FONT_GUS;
  339. }
  340. ^{owh}"vl"{wh}[0-9]+ {
  341. int i = 0;
  342. while (isspace(yytext[i])) i++;
  343. i += 2;
  344. current_toneset = atoi(yytext+i) + VLBANK;
  345. doing_drums = 0;
  346. doing_sf = 0;
  347. new_toneset(current_toneset, VLBANK+7);
  348. font_type=FONT_GUS;
  349. }
  350. ^{owh}("sfx"|"bankxg"){wh}[0-9]+ {
  351. int i = 0;
  352. while (isspace(yytext[i])) i++;
  353. if (yytext[i] == 's') i += 3;
  354. else i += 6;
  355. current_toneset = atoi(yytext+i) + SFXBANK;
  356. doing_drums = 0;
  357. doing_sf = 0;
  358. new_toneset(current_toneset, SFXBANK+MAXVAR);
  359. font_type=FONT_GUS;
  360. }
  361. ^{owh}"sfx" {
  362. current_toneset = SFXBANK;
  363. doing_drums = 0;
  364. doing_sf = 0;
  365. new_toneset(current_toneset, SFXBANK+1);
  366. font_type=FONT_GUS;
  367. }
  368. ^{owh}"drumsfx1" {
  369. current_drumset = SFXDRUM1;
  370. doing_drums = 1;
  371. doing_sf = 0;
  372. new_toneset(current_toneset, SFXDRUM1+1);
  373. font_type=FONT_GUS;
  374. if (bank) bank->name = "drumsfx1";
  375. }
  376. ^{owh}"drumsfx2" {
  377. current_drumset = SFXDRUM2;
  378. doing_drums = 1;
  379. doing_sf = 0;
  380. new_toneset(current_toneset, SFXDRUM2+1);
  381. font_type=FONT_GUS;
  382. if (bank) bank->name = "drumsfx2";
  383. }
  384. ^{owh}"[Melodic Patches]" {
  385. doing_drums = 0;
  386. }
  387. ^{owh}"[Drum Patches]" {
  388. doing_drums = 1;
  389. current_drumset = current_toneset;
  390. }
  391. ^{owh}[0-9,]+("="|{wh}){nm} {
  392. const char *gm_name = NULL;
  393. char *vc_name = NULL;
  394. patchno = atoi(yytext);
  395. tone_bank = 0;
  396. if (patchno >= 0) {
  397. int i = 0;
  398. while (isspace(yytext[i])) i++;
  399. while (isdigit(yytext[i])) i++;
  400. if (yytext[i] == ',') {
  401. i++;
  402. tone_bank = atoi(yytext + i);
  403. while (isdigit(yytext[i])) i++;
  404. }
  405. else {
  406. if (doing_drums) tone_bank = current_drumset;
  407. else tone_bank = current_toneset;
  408. }
  409. new_toneset(tone_bank, 0);
  410. while (isspace(yytext[i])) i++;
  411. if (!cfg_flag && yytext[i] == '=') i++;
  412. patchname = yytext + i;
  413. if (patchname[0] == '"') {
  414. vc_name = strncpy( (char *)malloc(strlen(patchname)-1), patchname + 1, strlen(patchname)-1 );
  415. vc_name[strlen(patchname)-2] = '\0';
  416. }
  417. else vc_name = strcpy( (char *)malloc(strlen(patchname)+1), patchname );
  418. if (patchno < 128) gm_name = gm_voice[doing_drums? patchno+128 : patchno].vname;
  419. else {
  420. ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "Bad patch number %d in cfg file.", patchno);
  421. }
  422. if (gm_name == NULL) gm_name = (doing_drums)? "drum":"instr";
  423. ctl->cmsg(CMSG_INFO, VERB_DEBUG, "config: voice %s[%d,%d] = patch %s",
  424. gm_name, patchno, tone_bank, vc_name);
  425. new_patch(vc_name, patchno);
  426. }
  427. BEGIN(P);
  428. }
  429. ^{owh}[A-Z][^\n\r\t\=,#]+(","{owh}[0-9]+)?{owh}"="{owh}{nm} {
  430. const char *gm_name = NULL;
  431. char *vc_name = NULL;
  432. int i;
  433. set_patchno(yytext);
  434. tone_bank = 0;
  435. if (patchno < 256) {
  436. i = 0;
  437. while (yytext[i] != '=' && yytext[i] != ',') i++;
  438. if (yytext[i] == ',') {
  439. i++;
  440. tone_bank = atoi(yytext + i);
  441. }
  442. else {
  443. if (doing_drums) tone_bank = current_drumset;
  444. else tone_bank = current_toneset;
  445. }
  446. while (yytext[i] != '=') i++;
  447. i++;
  448. while (isspace(yytext[i])) i++;
  449. patchname = yytext + i;
  450. if (patchname[0] == '"') {
  451. vc_name = strncpy( (char *)malloc(strlen(patchname)-1), patchname + 1, strlen(patchname)-1 );
  452. vc_name[strlen(patchname)-2] = '\0';
  453. }
  454. else vc_name = strcpy( (char *)malloc(strlen(patchname)+1), patchname );
  455. gm_name = gm_voice[patchno].vname;
  456. if (gm_name == NULL) gm_name = (doing_drums)? "drum":"instr";
  457. ctl->cmsg(CMSG_INFO, VERB_DEBUG, "config: voice %s[%d,%d] = patch %s",
  458. gm_name, patchno, tone_bank, vc_name);
  459. }
  460. else {
  461. ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "Bad line \"%s ...\" in cfg file.", yytext);
  462. }
  463. new_patch(vc_name, patchno);
  464. BEGIN(P);
  465. }
  466. ^{owh}("#option"{wh})?"-p"{owh}[0-9]+ { if (prescanning) {
  467. int i, j;
  468. i=0; while(isspace(yytext[i])) i++;
  469. if (yytext[i] == '#') {
  470. i += 7;
  471. while(isspace(yytext[i])) i++;
  472. }
  473. j = atoi(yytext+i+2);
  474. if (j > 1 && j < MAX_VOICES) voices_ceiling = j;
  475. } }
  476. ^{owh}("#option"{wh})?"-A"{wh}[0-9]+ { if (prescanning) {
  477. int i, j;
  478. i=0; while(isspace(yytext[i])) i++;
  479. if (yytext[i] == '#') {
  480. i += 7;
  481. while(isspace(yytext[i])) i++;
  482. }
  483. j = atoi(yytext+i+2);
  484. if (j > 1 && j < MAX_AMPLIFICATION) amplification = j;
  485. } }
  486. ^{owh}("#option"{wh})?"-C"{owh}[0-9]+ { if (prescanning) {
  487. int i, j;
  488. i=0; while(isspace(yytext[i])) i++;
  489. if (yytext[i] == '#') {
  490. i += 7;
  491. while(isspace(yytext[i])) i++;
  492. }
  493. j = atoi(yytext+i+2);
  494. if (j > 1 && j < MAX_CONTROL_RATIO) control_ratio = j;
  495. } }
  496. ^{owh}("#option"{wh})?"-s"{owh}[0-9]+ { if (prescanning) {
  497. int i, j;
  498. i=0; while(isspace(yytext[i])) i++;
  499. if (yytext[i] == '#') {
  500. i += 7;
  501. while(isspace(yytext[i])) i++;
  502. }
  503. j = atoi(yytext+i+2);
  504. if (j < 100) j *= 1000;
  505. if (j > MIN_OUTPUT_RATE && j < MAX_OUTPUT_RATE) play_mode->rate = j;
  506. } }
  507. ^{owh}("#option"{wh})?"-r"{owh}[0-9]+ { if (prescanning) {
  508. int i, j;
  509. i=0; while(isspace(yytext[i])) i++;
  510. if (yytext[i] == '#') {
  511. i += 7;
  512. while(isspace(yytext[i])) i++;
  513. }
  514. j = atoi(yytext+i+2);
  515. max_patch_memory = j * 1000000;
  516. } }
  517. ^{owh}("#option"{wh})?"-k"{owh}[0-9]+ { if (prescanning) {
  518. int i, j;
  519. i=0; while(isspace(yytext[i])) i++;
  520. if (yytext[i] == '#') {
  521. i += 7;
  522. while(isspace(yytext[i])) i++;
  523. }
  524. j = atoi(yytext+i+2);
  525. if (j >= 0 && j <= 3)
  526. switch (j) {
  527. case 0: current_interpolation = INTERPOLATION_LINEAR;
  528. break;
  529. case 1: current_interpolation = INTERPOLATION_CSPLINE;
  530. break;
  531. case 2: current_interpolation = INTERPOLATION_LAGRANGE;
  532. break;
  533. default:
  534. case 3: current_interpolation = INTERPOLATION_CSPLINE | INTERPOLATION_BUTTERWORTH;
  535. break;
  536. }
  537. } }
  538. ^{owh}("#option"{wh})?"-X"{owh}[0-9]+ { if (prescanning) {
  539. int i, j;
  540. i=0; while(isspace(yytext[i])) i++;
  541. if (yytext[i] == '#') {
  542. i += 7;
  543. while(isspace(yytext[i])) i++;
  544. }
  545. j = atoi(yytext+i+2);
  546. if (j >= 0 && j <= 2) opt_expression_curve = j;
  547. } }
  548. ^{owh}("#option"{wh})?"-V"{owh}[0-9]+ { if (prescanning) {
  549. int i, j;
  550. i=0; while(isspace(yytext[i])) i++;
  551. if (yytext[i] == '#') {
  552. i += 7;
  553. while(isspace(yytext[i])) i++;
  554. }
  555. j = atoi(yytext+i+2);
  556. if (j >= 0 && j <= 2) opt_volume_curve = j;
  557. } }
  558. ^{owh}("#option"{wh})?"-O"{owh}[a-zA-Z] { if (prescanning) {
  559. int i;
  560. i=0; while(isspace(yytext[i])) i++;
  561. if (yytext[i] == '#') {
  562. i += 7;
  563. while(isspace(yytext[i])) i++;
  564. }
  565. i+=2;
  566. while(isspace(yytext[i])) i++;
  567. set_play_mode(yytext+i);
  568. } }
  569. ^{owh}("#option"{wh})?"-F"{owh}[01] { if (prescanning) {
  570. int i;
  571. i=0; while(isspace(yytext[i])) i++;
  572. if (yytext[i] == '#') {
  573. i += 7;
  574. while(isspace(yytext[i])) i++;
  575. }
  576. i+=2;
  577. fast_load = atoi(yytext+i);
  578. } }
  579. {owh}"#".* ;
  580. ^#.*\n this_line[include_stack_ptr]++;
  581. <P>{lwh}"amp"{owh}"="{owh}[0-9]+ {
  582. int i = 0;
  583. while (yytext[i] != '=') if (yytext[i++]=='\n') this_line[include_stack_ptr]++;
  584. if (bank) bank->tone[patchno].amp = atoi(yytext+i+1);
  585. BEGIN(P);
  586. }
  587. <P>{lwh}"note"{owh}"="{owh}[0-9]+ {
  588. int i = 0;
  589. while (yytext[i] != '=') if (yytext[i++]=='\n') this_line[include_stack_ptr]++;
  590. if (bank) bank->tone[patchno].note = atoi(yytext+i+1);
  591. BEGIN(P);
  592. }
  593. <P>{lwh}"cutoff"{owh}"="{owh}[0-9]+ {
  594. int i = 0;
  595. while (yytext[i] != '=') if (yytext[i++]=='\n') this_line[include_stack_ptr]++;
  596. if (bank) bank->tone[patchno].cutoff = atoi(yytext+i+1);
  597. BEGIN(P);
  598. }
  599. <P>{lwh}"tuning"{owh}"="{owh}("+"|"-")[0-9]+ {
  600. int i = 0;
  601. while (yytext[i] != '=') if (yytext[i++]=='\n') this_line[include_stack_ptr]++;
  602. if (bank) bank->tone[patchno].tuning = atoi(yytext+i+1);
  603. BEGIN(P);
  604. }
  605. <P>{lwh}"keep"{owh}"=" BEGIN(K);
  606. <P>{lwh}"strip"{owh}"=" BEGIN(S);
  607. <K>{owh}"loop" {
  608. if (bank) bank->tone[patchno].strip_loop=0;
  609. BEGIN(P);
  610. }
  611. <K>{owh}"tail" {
  612. if (bank) bank->tone[patchno].strip_tail=0;
  613. BEGIN(P);
  614. }
  615. <K>{owh}"env" {
  616. if (bank) bank->tone[patchno].strip_envelope=0;
  617. BEGIN(P);
  618. }
  619. <K>{owh}"sustain" {
  620. if (bank) bank->tone[patchno].sustain=0;
  621. BEGIN(P);
  622. }
  623. <S>{owh}"loop" {
  624. if (bank) bank->tone[patchno].strip_loop=1;
  625. BEGIN(P);
  626. }
  627. <S>{owh}"tail" {
  628. if (bank) bank->tone[patchno].strip_tail=1;
  629. BEGIN(P);
  630. }
  631. <S>{owh}"env" {
  632. if (bank) bank->tone[patchno].strip_envelope=1;
  633. BEGIN(P);
  634. }
  635. <S>{owh}"sustain" {
  636. if (bank) bank->tone[patchno].sustain=1;
  637. BEGIN(P);
  638. }
  639. ^{owh}[A-Z][^\n\r\t\:#]+{owh}":"{owh}"poly" {
  640. set_patchno(yytext);
  641. if (patchno < 256) gm_voice[patchno].flags &= ~SOLO_MASK;
  642. }
  643. ^{owh}[A-Z][^\n\r\t\:#]+{owh}":"{owh}"solo" {
  644. set_patchno(yytext);
  645. if (patchno < 256) gm_voice[patchno].flags |= SOLO_MASK;
  646. }
  647. \n {
  648. patchno = -1;
  649. this_line[include_stack_ptr]++;
  650. BEGIN(0);
  651. }
  652. . ;
  653. %%
  654. static void new_toneset(int n, int highest)
  655. {
  656. banknum=n;
  657. if (!highest) highest = MAXBANK;
  658. if (n < 0 || n >= MAXBANK || n >= highest) {
  659. bank = NULL;
  660. ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
  661. "Bank index %d out of range (max %d) in file %s line %d",
  662. n, highest, this_fname[include_stack_ptr], this_line[include_stack_ptr]);
  663. return;
  664. }
  665. if (!doing_drums) {
  666. if (!tonebank[n]) {
  667. tonebank[n]=(ToneBank *)safe_malloc(sizeof(ToneBank));
  668. memset(tonebank[n], 0, sizeof(ToneBank));
  669. }
  670. bank=tonebank[n];
  671. }
  672. else {
  673. if (!drumset[n]) {
  674. drumset[n]=(ToneBank *)safe_malloc(sizeof(ToneBank));
  675. memset(drumset[n], 0, sizeof(ToneBank));
  676. }
  677. bank=drumset[n];
  678. }
  679. }
  680. static void new_patch(const char *vname, int num)
  681. {
  682. if (!bank) return;
  683. if (num < 0 || num > 127) {
  684. ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
  685. "Patch index %d out of range in file %s line %d",
  686. num, this_fname[include_stack_ptr], this_line[include_stack_ptr]);
  687. return;
  688. }
  689. if (bank->tone[num].name) return;
  690. bank->tone[num].name=vname;
  691. bank->tone[num].note=bank->tone[num].amp=bank->tone[num].pan=
  692. bank->tone[num].strip_loop=bank->tone[num].strip_envelope=
  693. bank->tone[num].sf_ix=
  694. bank->tone[num].cutoff=
  695. bank->tone[num].sustain=
  696. bank->tone[num].strip_tail=bank->tone[num].last_used=-1;
  697. bank->tone[num].font_type=font_type;
  698. bank->tone[num].tuning=0;
  699. bank->tone[num].layer=0;
  700. }
  701. static void set_patchno(char *m)
  702. {
  703. int i,j,w,y;
  704. const char *vn;
  705. y = w = 0;
  706. while (isspace(m[w])) w++;
  707. for (i = 0; i < 256; i++) {
  708. vn = gm_voice[i].vname;
  709. if (vn == NULL) continue;
  710. for (j = 0; m[j+w] && vn[j+y] && m[j+w] == vn[j+y]; j++)
  711. if (vn[j+y+1] && isspace(vn[j+y+1]) && m[j+w+1] && !isspace(m[j+w+1]) ) y++ ;
  712. if (!m[j+w] || m[j+w] == '=' || m[j+w] == ',' || m[j+w] == ':') break;
  713. if (!vn[j+y] && isspace(m[j+w])) break;
  714. }
  715. if (i < 256 && vn != NULL) patchno = i;
  716. else patchno = 256;
  717. }
  718. char *current_config_file = 0;
  719. int read_config_file(const char *name, int prescan)
  720. {
  721. int retvalue = 0;
  722. prescanning = prescan;
  723. include_stack_ptr = 0;
  724. rcf_count = 1;
  725. if (prescanning) {
  726. current_config_file = (char *)safe_malloc(strlen(name)+1);
  727. strcpy(current_config_file, name);
  728. }
  729. yyin = open_file(name, 1, OF_VERBOSE, rcf_count);
  730. if (this_fname[include_stack_ptr]) free(this_fname[include_stack_ptr]);
  731. this_fname[include_stack_ptr] = strdup(name);
  732. if (!yyin) return -1;
  733. current_toneset = current_drumset = 0;
  734. doing_drums = doing_sf = 0;
  735. BEGIN(0);
  736. if (!(retvalue = yylex())) {
  737. if (prescan) got_a_configuration = 1;
  738. else got_a_configuration = 2;
  739. }
  740. close_file(yyin);
  741. return retvalue;
  742. }
  743. static SFInfo sfinfo;
  744. static char *getname(char *p);
  745. static void print_sbk(SFInfo *sf);
  746. static int check_sbk(SFInfo *sf);
  747. static int sf2tab(char *sffname)
  748. {
  749. FILE *fp;
  750. if ((fp = open_file(sffname, 1, OF_VERBOSE, 0)) == NULL) {
  751. return 0;
  752. }
  753. load_sbk(fp, &sfinfo);
  754. fclose(fp);
  755. if (!check_sbk(&sfinfo)) return 0;
  756. print_sbk(&sfinfo);
  757. if (!sf_count) return 0;
  758. a_sf[sf_count].font_code = FONT_SBK;
  759. a_sf[sf_count].num = 0;
  760. a_sf[sf_count].name = strdup( sffname );
  761. sf_count++;
  762. free_sbk(&sfinfo);
  763. return 1;
  764. }
  765. static char *getname(char *p)
  766. {
  767. int i;
  768. static char buf[21];
  769. strncpy(buf, p, 20);
  770. buf[20] = 0;
  771. for (i = 19; i > 4 && buf[i]==' '; i--) {
  772. buf[i] = 0;
  773. }
  774. for (i = 0; buf[i]; i++) {
  775. if (buf[i] == ' ') buf[i] = '_';
  776. if (buf[i] == '#') buf[i] = '@';
  777. }
  778. i = strlen(buf);
  779. if (i > 4 && buf[i-3] == '(' && buf[i-2] == 'L' && buf[i-1] == ')')
  780. buf[i-3] = '\0';
  781. return buf;
  782. }
  783. static int check_sbk(SFInfo *sf) {
  784. if (sf->version != 2) return 0;
  785. return 1;
  786. }
  787. static void print_sbk(SFInfo *sf)
  788. {
  789. int i, b_nk, preset, lastpatch;
  790. int lastbank = -1;
  791. tpresethdr *ip;
  792. tinsthdr *tp;
  793. sf_count = 0;
  794. for (b_nk = 0; b_nk <= 128; b_nk++) {
  795. for (preset = 0; preset <= 127; preset++) {
  796. lastpatch = -1;
  797. for (i = 0, ip = sf->presethdr; i < sf->nrpresets-1; i++) {
  798. int b, g, inst, sm_idx;
  799. if (ip[i].bank != b_nk) continue;
  800. if (ip[i].preset != preset) continue;
  801. inst = -1;
  802. for (b = ip[i].bagNdx; b < ip[i+1].bagNdx; b++) {
  803. for (g = sf->presetbag[b]; g < sf->presetbag[b+1]; g++)
  804. if (sf->presetgen[g].oper == SF_instrument) {
  805. inst = sf->presetgen[g].amount;
  806. break;
  807. }
  808. }
  809. if (inst < 0) continue;
  810. tp = sf->insthdr;
  811. if (b_nk != 128) {
  812. int realwaves = 0;
  813. if (b_nk != lastbank) {
  814. a_sf[sf_count].font_code = FONT_TONESET;
  815. a_sf[sf_count].num = b_nk;
  816. a_sf[sf_count].name = NULL;
  817. if (sf_count < MAX_SF_COUNT-2)
  818. sf_count++;
  819. lastbank = b_nk;
  820. }
  821. for (b = tp[inst].bagNdx; b < tp[inst+1].bagNdx; b++) {
  822. sm_idx = -1;
  823. for (g = sf->instbag[b]; g < sf->instbag[b+1]; g++) {
  824. if (sf->instgen[g].oper == SF_sampleId) sm_idx = sf->instgen[g].amount;
  825. }
  826. if (sm_idx >= 0 && sf->sampleinfo[sm_idx].sampletype < 0x8000) realwaves++;
  827. }
  828. if (realwaves && ip[i].preset != lastpatch) {
  829. a_sf[sf_count].font_code = FONT_PRESET;
  830. a_sf[sf_count].num = ip[i].preset;
  831. a_sf[sf_count].name = strdup( getname(ip[i].name) );
  832. if (sf_count < MAX_SF_COUNT-2)
  833. sf_count++;
  834. lastpatch = ip[i].preset;
  835. }
  836. }
  837. else {
  838. int keynote, c, dpreset;
  839. a_sf[sf_count].font_code = FONT_DRUMSET;
  840. a_sf[sf_count].num = ip[i].preset;
  841. a_sf[sf_count].name = strdup( getname(ip[i].name) );
  842. if (sf_count < MAX_SF_COUNT-2)
  843. sf_count++;
  844. for (dpreset = 0; dpreset < 128; dpreset++)
  845. for (c = ip[i].bagNdx; c < ip[i+1].bagNdx; c++) {
  846. inst = -1;
  847. for (g = sf->presetbag[c]; g < sf->presetbag[c+1]; g++)
  848. if (sf->presetgen[g].oper == SF_instrument) {
  849. inst = sf->presetgen[g].amount;
  850. break;
  851. }
  852. if (inst >= 0) for (b = tp[inst].bagNdx; b < tp[inst+1].bagNdx; b++) {
  853. int hikeynote = -1;
  854. sm_idx = keynote = -1;
  855. for (g = sf->instbag[b]; g < sf->instbag[b+1]; g++) {
  856. if (sf->instgen[g].oper == SF_sampleId) sm_idx = sf->instgen[g].amount;
  857. else if (sf->instgen[g].oper == SF_keyRange) {
  858. keynote = sf->instgen[g].amount & 0xff;
  859. hikeynote = (sf->instgen[g].amount >> 8) & 0xff;
  860. }
  861. }
  862. if (sm_idx < 0) continue;
  863. if (sf->sampleinfo[sm_idx].sampletype >= 0x8000) continue;
  864. /*if (keynote != dpreset) continue;*/
  865. if (dpreset < keynote) continue;
  866. if (dpreset > hikeynote) continue;
  867. if (sm_idx >= 0 && keynote >= 0 && dpreset != lastpatch) {
  868. a_sf[sf_count].font_code = FONT_PRESET;
  869. a_sf[sf_count].num = dpreset;
  870. a_sf[sf_count].name = strdup( getname( sf->samplenames[sm_idx].name ) );
  871. if (sf_count < MAX_SF_COUNT-2)
  872. sf_count++;
  873. lastpatch = dpreset;
  874. }
  875. }
  876. }
  877. }
  878. }
  879. }
  880. }
  881. }
  882. static void
  883. interpret_sf(void)
  884. {
  885. int i = 0;
  886. while (1) {
  887. int action, which;
  888. const char *sf_name;
  889. //char *vc_name = NULL;
  890. action = a_sf[i].font_code;
  891. which = a_sf[i].num;
  892. sf_name = a_sf[i].name;
  893. switch (action) {
  894. case FONT_TONESET:
  895. current_toneset = which;
  896. doing_drums = 0;
  897. doing_sf = 1;
  898. new_toneset(current_toneset, MAXPROG);
  899. font_type=FONT_SBK;
  900. break;
  901. case FONT_DRUMSET:
  902. current_drumset = which;
  903. doing_drums = 1;
  904. doing_sf = 1;
  905. new_toneset(current_drumset, MAXPROG);
  906. font_type=FONT_SBK;
  907. if (!bank->name) bank->name = sf_name;
  908. break;
  909. case FONT_PRESET:
  910. patchno = which;
  911. if (doing_drums) tone_bank = current_drumset;
  912. else tone_bank = current_toneset;
  913. new_toneset(tone_bank, MAXPROG);
  914. new_patch(sf_name, patchno);
  915. break;
  916. case FONT_SBK:
  917. init_soundfont(sf_name, which, which, 1);
  918. break;
  919. }
  920. if (action == FONT_SBK) break;
  921. i++;
  922. if (i >= sf_count) break;
  923. }
  924. }