timidity.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. /*
  2. TiMidity -- Experimental MIDI to WAVE converter
  3. Copyright (C) 1995 Tuukka Toivonen <toivonen@clinet.fi>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. */
  16. #include "../../neo/idlib/precompiled.h"
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. //#include "SDL.h"
  21. #include "config.h"
  22. #include "common.h"
  23. #include "instrum.h"
  24. #include "playmidi.h"
  25. #include "readmidi.h"
  26. #include "output.h"
  27. #include "controls.h"
  28. #include "timidity.h"
  29. #include "tables.h"
  30. //void *Real_Tim_Malloc( int sz );
  31. //void Real_Tim_Free( void *pt );
  32. void (*s32tobuf)(void *dp, int32_t *lp, int32_t c);
  33. int free_instruments_afterwards=0;
  34. static char def_instr_name[256]="";
  35. int AUDIO_BUFFER_SIZE;
  36. sample_t *resample_buffer;
  37. int32_t *common_buffer;
  38. #define MAXWORDS 10
  39. // Alternative to FGets
  40. char* Gets( idFile & file, char *buf, int bsize ) {
  41. int i;
  42. char c;
  43. int done = 0;
  44. if (buf == 0 || bsize <= 0 )
  45. return 0;
  46. for (i = 0; !done && i < bsize - 1; i++) {
  47. int red = file.ReadChar(c);
  48. if (red == 0) {
  49. done = 1;
  50. i--;
  51. } else {
  52. buf[i] = c;
  53. if (c == '\n')
  54. done = 1;
  55. }
  56. }
  57. buf[i] = '\0';
  58. if (i == 0)
  59. return 0;
  60. else
  61. return buf;
  62. }
  63. static int read_config_file(const char *name)
  64. {
  65. idFile * fp;
  66. char *w[MAXWORDS], *cp;
  67. ToneBank *bank=0;
  68. int i, j, k, line=0, words;
  69. static int rcf_count=0;
  70. if (rcf_count>50)
  71. {
  72. ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
  73. "Probable source loop in configuration files");
  74. return (-1);
  75. }
  76. if (!(fp=open_file(name, 1, OF_VERBOSE)))
  77. return -2;
  78. char tokTmp[1024];
  79. while ( Gets( *fp, tokTmp, 1024 ) )
  80. {
  81. line++;
  82. w[words=0]=strtok(tokTmp, " \t\r\n\240");
  83. if (!w[0] || (*w[0]=='#')) continue;
  84. while (w[words] && (words < MAXWORDS))
  85. w[++words]=strtok(0," \t\r\n\240");
  86. if (!strcmp(w[0], "dir"))
  87. {
  88. if (words < 2)
  89. {
  90. ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
  91. "%s: line %d: No directory given\n", name, line);
  92. return -3;
  93. }
  94. for (i=1; i<words; i++)
  95. add_to_pathlist(w[i]);
  96. }
  97. else if (!strcmp(w[0], "source"))
  98. {
  99. if (words < 2)
  100. {
  101. ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
  102. "%s: line %d: No file name given\n", name, line);
  103. return -4;
  104. }
  105. for (i=1; i<words; i++)
  106. {
  107. rcf_count++;
  108. read_config_file(w[i]);
  109. rcf_count--;
  110. }
  111. }
  112. else if (!strcmp(w[0], "default"))
  113. {
  114. if (words != 2)
  115. {
  116. ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
  117. "%s: line %d: Must specify exactly one patch name\n",
  118. name, line);
  119. return -5;
  120. }
  121. strncpy(def_instr_name, w[1], 255);
  122. def_instr_name[255]='\0';
  123. }
  124. else if (!strcmp(w[0], "drumset"))
  125. {
  126. if (words < 2)
  127. {
  128. ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
  129. "%s: line %d: No drum set number given\n",
  130. name, line);
  131. return -6;
  132. }
  133. i=atoi(w[1]);
  134. if (i<0 || i>127)
  135. {
  136. ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
  137. "%s: line %d: Drum set must be between 0 and 127\n",
  138. name, line);
  139. return -7;
  140. }
  141. if (!drumset[i])
  142. {
  143. drumset[i]=(ToneBank*)safe_malloc(sizeof(ToneBank));
  144. memset(drumset[i], 0, sizeof(ToneBank));
  145. }
  146. bank=drumset[i];
  147. }
  148. else if (!strcmp(w[0], "bank"))
  149. {
  150. if (words < 2)
  151. {
  152. ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
  153. "%s: line %d: No bank number given\n",
  154. name, line);
  155. return -8;
  156. }
  157. i=atoi(w[1]);
  158. if (i<0 || i>127)
  159. {
  160. ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
  161. "%s: line %d: Tone bank must be between 0 and 127\n",
  162. name, line);
  163. return -9;
  164. }
  165. if (!tonebank[i])
  166. {
  167. tonebank[i]=(ToneBank*)safe_malloc(sizeof(ToneBank));
  168. memset(tonebank[i], 0, sizeof(ToneBank));
  169. }
  170. bank=tonebank[i];
  171. }
  172. else {
  173. if ((words < 2) || (*w[0] < '0' || *w[0] > '9'))
  174. {
  175. ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
  176. "%s: line %d: syntax error\n", name, line);
  177. return -10;
  178. }
  179. i=atoi(w[0]);
  180. if (i<0 || i>127)
  181. {
  182. ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
  183. "%s: line %d: Program must be between 0 and 127\n",
  184. name, line);
  185. return -11;
  186. }
  187. if (!bank)
  188. {
  189. ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
  190. "%s: line %d: Must specify tone bank or drum set "
  191. "before assignment\n",
  192. name, line);
  193. return -12;
  194. }
  195. if (bank->tone[i].name)
  196. Real_Tim_Free(bank->tone[i].name);
  197. strcpy((bank->tone[i].name=(char*)safe_malloc(strlen(w[1])+1)),w[1]);
  198. bank->tone[i].note=bank->tone[i].amp=bank->tone[i].pan=
  199. bank->tone[i].strip_loop=bank->tone[i].strip_envelope=
  200. bank->tone[i].strip_tail=-1;
  201. for (j=2; j<words; j++)
  202. {
  203. if (!(cp=strchr(w[j], '=')))
  204. {
  205. ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "%s: line %d: bad patch option %s\n",
  206. name, line, w[j]);
  207. return -13;
  208. }
  209. *cp++=0;
  210. if (!strcmp(w[j], "amp"))
  211. {
  212. k=atoi(cp);
  213. if ((k<0 || k>MAX_AMPLIFICATION) || (*cp < '0' || *cp > '9'))
  214. {
  215. ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
  216. "%s: line %d: amplification must be between "
  217. "0 and %d\n", name, line, MAX_AMPLIFICATION);
  218. return -14;
  219. }
  220. bank->tone[i].amp=k;
  221. }
  222. else if (!strcmp(w[j], "note"))
  223. {
  224. k=atoi(cp);
  225. if ((k<0 || k>127) || (*cp < '0' || *cp > '9'))
  226. {
  227. ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
  228. "%s: line %d: note must be between 0 and 127\n",
  229. name, line);
  230. return -15;
  231. }
  232. bank->tone[i].note=k;
  233. }
  234. else if (!strcmp(w[j], "pan"))
  235. {
  236. if (!strcmp(cp, "center"))
  237. k=64;
  238. else if (!strcmp(cp, "left"))
  239. k=0;
  240. else if (!strcmp(cp, "right"))
  241. k=127;
  242. else
  243. k=((atoi(cp)+100) * 100) / 157;
  244. if ((k<0 || k>127) ||
  245. (k==0 && *cp!='-' && (*cp < '0' || *cp > '9')))
  246. {
  247. ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
  248. "%s: line %d: panning must be left, right, "
  249. "center, or between -100 and 100\n",
  250. name, line);
  251. return -16;
  252. }
  253. bank->tone[i].pan=k;
  254. }
  255. else if (!strcmp(w[j], "keep"))
  256. {
  257. if (!strcmp(cp, "env"))
  258. bank->tone[i].strip_envelope=0;
  259. else if (!strcmp(cp, "loop"))
  260. bank->tone[i].strip_loop=0;
  261. else
  262. {
  263. ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
  264. "%s: line %d: keep must be env or loop\n", name, line);
  265. return -17;
  266. }
  267. }
  268. else if (!strcmp(w[j], "strip"))
  269. {
  270. if (!strcmp(cp, "env"))
  271. bank->tone[i].strip_envelope=1;
  272. else if (!strcmp(cp, "loop"))
  273. bank->tone[i].strip_loop=1;
  274. else if (!strcmp(cp, "tail"))
  275. bank->tone[i].strip_tail=1;
  276. else
  277. {
  278. ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
  279. "%s: line %d: strip must be env, loop, or tail\n",
  280. name, line);
  281. return -18;
  282. }
  283. }
  284. else
  285. {
  286. ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "%s: line %d: bad patch option %s\n",
  287. name, line, w[j]);
  288. return -19;
  289. }
  290. }
  291. }
  292. }
  293. if ( fp == 0 ) //(ferror(fp))
  294. {
  295. ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "Can't read from %s\n", name);
  296. close_file(fp);
  297. return -20;
  298. }
  299. close_file(fp);
  300. return 0;
  301. }
  302. int Timidity_Init(int rate, int format, int channels, int samples, const char* config)
  303. {
  304. int ret;
  305. ret = read_config_file(config);
  306. if (ret < 0) {
  307. return(ret);
  308. }
  309. /* Set play mode parameters */
  310. play_mode->rate = rate;
  311. play_mode->encoding = 0;
  312. if ( (format&0xFF) == 16 ) {
  313. play_mode->encoding |= PE_16BIT;
  314. }
  315. if ( (format&0x8000) ) {
  316. play_mode->encoding |= PE_SIGNED;
  317. }
  318. if ( channels == 1 ) {
  319. play_mode->encoding |= PE_MONO;
  320. }
  321. switch (format) {
  322. case AUDIO_S8:
  323. s32tobuf = s32tos8;
  324. break;
  325. case AUDIO_U8:
  326. s32tobuf = s32tou8;
  327. break;
  328. case AUDIO_S16LSB:
  329. s32tobuf = s32tos16l;
  330. break;
  331. case AUDIO_S16MSB:
  332. s32tobuf = s32tos16b;
  333. break;
  334. case AUDIO_U16LSB:
  335. s32tobuf = s32tou16l;
  336. break;
  337. case AUDIO_U16MSB:
  338. s32tobuf = s32tou16b;
  339. break;
  340. default:
  341. ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "Unsupported audio format");
  342. return(-1);
  343. }
  344. AUDIO_BUFFER_SIZE = samples;
  345. /* Allocate memory for mixing (WARNING: Memory leak!) */
  346. resample_buffer = (sample_t*)safe_malloc(AUDIO_BUFFER_SIZE*sizeof(sample_t));
  347. common_buffer = (int32*)safe_malloc(AUDIO_BUFFER_SIZE*2*sizeof(int32_t));
  348. init_tables();
  349. if (ctl->open(0, 0)) {
  350. ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "Couldn't open %s\n", ctl->id_name);
  351. return(-1);
  352. }
  353. if (!control_ratio) {
  354. control_ratio = play_mode->rate / CONTROLS_PER_SECOND;
  355. if(control_ratio<1)
  356. control_ratio=1;
  357. else if (control_ratio > MAX_CONTROL_RATIO)
  358. control_ratio=MAX_CONTROL_RATIO;
  359. }
  360. if (*def_instr_name)
  361. set_default_instrument(def_instr_name);
  362. return(0);
  363. }
  364. char timidity_error[TIMIDITY_ERROR_MAX_CHARS] = "";
  365. char *Timidity_Error(void)
  366. {
  367. return(timidity_error);
  368. }