/project/jni/sdl_mixer/native_midi_gpl/emumidi.c

https://github.com/aichunyu/FFPlayer · C · 390 lines · 314 code · 29 blank · 47 comment · 143 complexity · d614e4d995abc61d57a31c397d15b771 MD5 · raw file

  1. /************************************************************************
  2. emumidi.c -- emulation of midi device for FM/OPL3/GUS
  3. Copyright (C) 1994-1996 Nathan I. Laredo
  4. This program is modifiable/redistributable under the terms
  5. of the GNU General Public Licence.
  6. You should have received a copy of the GNU General Public License
  7. along with this program; if not, write to the Free Software
  8. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  9. Send your comments and all your spare pocket change to
  10. laredo@gnu.ai.mit.edu (Nathan Laredo) or to PSC 1, BOX 709, 2401
  11. Kelly Drive, Lackland AFB, TX 78236-5128, USA.
  12. *************************************************************************/
  13. /* edited by Peter Kutak */
  14. /* email : kutak@stonline.sk */
  15. #if defined(linux) || defined(__FreeBSD__)
  16. #include "emumidi.h"
  17. SEQ_USE_EXTBUF();
  18. extern int seqfd, play_ext, play_gus, play_fm, play_awe;
  19. extern int gus_dev, sb_dev, ext_dev, awe_dev;
  20. extern struct synth_info card_info[MAX_CARDS];
  21. extern int chanmask, perc, ticks, dochan, wantopl3, MT32;
  22. extern int patchloaded[256], fmloaded[256], useprog[16];
  23. int note_vel[16][128];
  24. struct voicestate voice[2][36];
  25. struct chanstate channel[16];
  26. #define C_GUS 0
  27. #define C_FM 1
  28. #define CN (ISGUS(chn) ? C_GUS : C_FM)
  29. #define CHANNEL (dochan ? chn : 0)
  30. void load_sysex(length, data, type)
  31. int length;
  32. unsigned char *data;
  33. int type;
  34. {
  35. unsigned long int i, j;
  36. /*
  37. * If the system exclusive is for roland, evaluate it. More than
  38. * roland could be evaluated here if i had documentation. Please
  39. * submit patches for any other hardware to laredo@gnu.ai.mit.edu
  40. * Complete emulation of all GS sysex messages in the works....
  41. */
  42. if (length > 7 && data[0] == 0x41 && data[2] == 0x42 && data[3] == 0x12) {
  43. /* GS DATA SET MESSAGES */
  44. if (data[4] == 0x40 && (data[5] & 0xf0) == 0x10 && data[6] == 0x15) {
  45. /* USE RHYTHM PART */
  46. if (!(i = (data[5] & 0xf)))
  47. i = 0x09;
  48. else if (i < 10)
  49. i--;
  50. i = 1<<i;
  51. if (data[7])
  52. perc |= i;
  53. else
  54. perc &= ~i;
  55. }
  56. if ((data[4] == 0x40 || data[4] == 0) &&
  57. data[5] == 0x00 && data[6] == 0x7f) { /* GS RESET */
  58. perc = 0x0200; /* percussion in channel 10 only */
  59. for (i = 0; i < 16; i++) { /* set state info */
  60. for (j = 0; j < 128; j++)
  61. note_vel[i][j] = 0;
  62. channel[i].bender = channel[i].oldbend = 8192;
  63. channel[i].bender_range = channel[i].oldrange = 2;
  64. channel[i].controller[CTL_PAN] = 64;
  65. channel[i].controller[CTL_SUSTAIN] = 0;
  66. }
  67. }
  68. }
  69. if (!play_ext)
  70. return;
  71. if (type == MIDI_SYSTEM_PREFIX)
  72. SEQ_MIDIOUT(ext_dev, MIDI_SYSTEM_PREFIX);
  73. for (i = 0; i < length; i++)
  74. SEQ_MIDIOUT(ext_dev, data[i]);
  75. }
  76. int seq_set_patch(chn, pgm)
  77. int chn, pgm;
  78. {
  79. if (MT32 && pgm < 128)
  80. pgm = mt32pgm[pgm];
  81. if (useprog[chn])
  82. pgm = useprog[chn] - 1;
  83. if (ISMIDI(chn)) {
  84. SEQ_MIDIOUT(ext_dev, MIDI_PGM_CHANGE + CHANNEL);
  85. SEQ_MIDIOUT(ext_dev, pgm);
  86. } else if (ISAWE(chn)) {
  87. SEQ_SET_PATCH(awe_dev, chn, pgm);
  88. } else if (ISPERC(chn)) {
  89. if (ISGUS(chn) && patchloaded[pgm] != 1)
  90. return -1;
  91. else if (ISFM(chn) && !fmloaded[pgm])
  92. return -1;
  93. } else if (ISGUS(chn) && patchloaded[pgm] != 1)
  94. /* find first loaded gus program to replace missing one */
  95. for (pgm = 0; patchloaded[pgm] != 1; pgm++);
  96. return (channel[chn].program = pgm);
  97. }
  98. /* finetune returns exact frequency with bender applied. Not used */
  99. /*
  100. int finetune(chn, note)
  101. int chn, note;
  102. {
  103. long int r, b, d;
  104. r = channel[chn].bender_range;
  105. b = channel[chn].bender - 8192;
  106. if (!b || r + note > 127 || r - note < 0)
  107. return n_freq[note];
  108. r = n_freq[note + r] - n_freq[note - r];
  109. d = b * r;
  110. d /= 8192;
  111. return n_freq[note] + d;
  112. }
  113. */
  114. extern int _seqbufptr;
  115. void seq_stop_note(dev, chn, note, vel)
  116. int dev, chn, note, vel;
  117. {
  118. int i, card = CN;
  119. note_vel[chn][note] = 0;
  120. if (ISMIDI(chn)) {
  121. SEQ_MIDIOUT(dev, MIDI_NOTEOFF + CHANNEL);
  122. SEQ_MIDIOUT(dev, note);
  123. SEQ_MIDIOUT(dev, vel);
  124. } else if (ISAWE(chn)) {
  125. SEQ_STOP_NOTE(dev, chn, note, vel);
  126. } else
  127. for (i = 0; i < card_info[dev].nr_voices; i++)
  128. if (voice[card][i].channel == chn &&
  129. voice[card][i].note == note) {
  130. voice[card][i].dead = 1;
  131. voice[card][i].timestamp /= 2;
  132. if (!channel[chn].controller[CTL_SUSTAIN] && !ISPERC(chn))
  133. SEQ_STOP_NOTE(dev, i, note, vel);
  134. }
  135. }
  136. void seq_key_pressure(dev, chn, note, vel)
  137. int dev, chn, note, vel;
  138. {
  139. int i, card = CN;
  140. if (ISMIDI(chn)) {
  141. SEQ_MIDIOUT(dev, MIDI_KEY_PRESSURE + CHANNEL);
  142. SEQ_MIDIOUT(dev, note);
  143. SEQ_MIDIOUT(dev, vel);
  144. } else if (ISAWE(chn)) {
  145. AWE_KEY_PRESSURE(dev, chn, note, vel);
  146. } else
  147. for (i = 0; i < card_info[dev].nr_voices; i++)
  148. if (voice[card][i].channel == chn &&
  149. voice[card][i].note == note)
  150. SEQ_KEY_PRESSURE(dev, i, note, vel);
  151. }
  152. int new_voice(dev, chn)
  153. int dev, chn;
  154. {
  155. int i, oldest, last, card = CN;
  156. if (ISFM(chn) && fmloaded[channel[chn].program] == OPL3_PATCH)
  157. last = 6; /* 4-op voice can only use first six voices */
  158. else
  159. last = card_info[dev].nr_voices;
  160. for (i = oldest = 0; i < last; i++)
  161. if (voice[card][i].timestamp < voice[card][oldest].timestamp)
  162. oldest = i;
  163. return oldest;
  164. }
  165. void seq_start_note(dev, chn, note, vel)
  166. int dev, chn, note, vel;
  167. {
  168. int v, c, card = CN;
  169. note_vel[chn][note] = vel;
  170. if (ISMIDI(chn)) {
  171. SEQ_MIDIOUT(dev, MIDI_NOTEON + CHANNEL);
  172. SEQ_MIDIOUT(dev, note);
  173. SEQ_MIDIOUT(dev, vel);
  174. } else if (vel == 0)
  175. seq_stop_note(dev, chn, note, 64);
  176. else if (ISAWE(chn)) {
  177. SEQ_START_NOTE(dev, chn, note, vel);
  178. } else {
  179. v = new_voice(dev, chn);
  180. SEQ_SET_PATCH(dev, v, channel[chn].program);
  181. SEQ_BENDER_RANGE(dev, v, (channel[chn].bender_range * 100));
  182. SEQ_BENDER(dev, v, channel[chn].bender);
  183. SEQ_CONTROL(dev, v, CTL_PAN,
  184. channel[chn].controller[CTL_PAN]);
  185. SEQ_START_NOTE(dev, v, note, vel);
  186. voice[card][v].note = note;
  187. voice[card][v].channel = chn;
  188. voice[card][v].timestamp = ticks;
  189. voice[card][v].dead = 0;
  190. if ((c = channel[chn].controller[CTL_CHORUS_DEPTH] * 8)) {
  191. if (channel[chn].bender_range)
  192. c /= channel[chn].bender_range;
  193. v = new_voice(dev, chn);
  194. SEQ_SET_PATCH(dev, v, channel[chn].program);
  195. SEQ_BENDER_RANGE(dev, v, (channel[chn].bender_range * 100));
  196. if (channel[chn].bender + c < 0x4000) {
  197. SEQ_BENDER(dev, v, channel[chn].bender + c);
  198. } else {
  199. SEQ_BENDER(dev, v, channel[chn].bender - c);
  200. }
  201. /* put chorus note on the "extreme" side */
  202. c = channel[chn].controller[CTL_PAN];
  203. if (c < 64)
  204. c = 0;
  205. else if (c > 64)
  206. c = 127;
  207. SEQ_CONTROL(dev, v, CTL_PAN, c);
  208. SEQ_START_NOTE(dev, v, note, vel);
  209. voice[card][v].note = note;
  210. voice[card][v].channel = chn;
  211. /* allow chorus note to be stolen very quickly */
  212. voice[card][v].timestamp = ticks / 2;
  213. voice[card][v].dead = 0;
  214. }
  215. }
  216. }
  217. static int rpn1[16] =
  218. {127, 127, 127, 127, 127, 127, 127, 127,
  219. 127, 127, 127, 127, 127, 127, 127, 127};
  220. static int rpn2[16] =
  221. {127, 127, 127, 127, 127, 127, 127, 127,
  222. 127, 127, 127, 127, 127, 127, 127, 127};
  223. void seq_control(dev, chn, p1, p2)
  224. int dev, chn, p1, p2;
  225. {
  226. int i, card = CN;
  227. channel[chn].controller[p1] = p2;
  228. if (ISMIDI(chn)) {
  229. SEQ_MIDIOUT(dev, MIDI_CTL_CHANGE + CHANNEL);
  230. SEQ_MIDIOUT(dev, p1);
  231. SEQ_MIDIOUT(dev, p2);
  232. }
  233. if (p1 == 7 || p1 == 39)
  234. return;
  235. switch (p1) {
  236. case CTL_SUSTAIN:
  237. if (ISAWE(chn)) {
  238. SEQ_CONTROL(dev, chn, p1, p2);
  239. } else if (!ISMIDI(chn))
  240. if (p1 == CTL_SUSTAIN && !p2) {
  241. for (i = 0; i < card_info[card].nr_voices; i++)
  242. if (voice[card][i].channel == chn
  243. && voice[card][i].dead) {
  244. SEQ_STOP_NOTE(dev, i, voice[card][i].note, 64);
  245. voice[card][i].dead = 0;
  246. }
  247. }
  248. break;
  249. case CTL_REGIST_PARM_NUM_MSB:
  250. rpn1[chn] = p2;
  251. break;
  252. case CTL_REGIST_PARM_NUM_LSB:
  253. rpn2[chn] = p2;
  254. break;
  255. case CTL_DATA_ENTRY:
  256. if (rpn1[chn] == 0 && rpn2[chn] == 0) {
  257. channel[chn].oldrange = channel[chn].bender_range;
  258. channel[chn].bender_range = p2;
  259. rpn1[chn] = rpn2[chn] = 127;
  260. if (ISAWE(chn)) {
  261. SEQ_BENDER_RANGE(dev, chn, p2 * 100);
  262. } else if (!ISMIDI(chn))
  263. for (i = 0; i < card_info[card].nr_voices; i++)
  264. SEQ_BENDER_RANGE(dev, i, p2 * 100);
  265. }
  266. break;
  267. default:
  268. /* sent on the off chance the sound driver is enhanced */
  269. if (ISAWE(chn)) {
  270. SEQ_CONTROL(dev, chn, p1, p2);
  271. } else if (!ISMIDI(chn) && (p1 < 0x10 || (p1 & 0xf0) == 0x50))
  272. for (i = 0; i < card_info[card].nr_voices; i++)
  273. if (voice[card][i].channel == chn)
  274. SEQ_CONTROL(dev, i, p1, p2);
  275. break;
  276. }
  277. }
  278. void seq_chn_pressure(dev, chn, vel)
  279. int dev, chn, vel;
  280. {
  281. int card = CN, i;
  282. channel[chn].pressure = vel;
  283. if (ISMIDI(chn)) {
  284. SEQ_MIDIOUT(dev, MIDI_CHN_PRESSURE + CHANNEL);
  285. SEQ_MIDIOUT(dev, vel);
  286. } else if (ISAWE(chn)) {
  287. AWE_CHN_PRESSURE(dev, chn, vel);
  288. } else
  289. for (i = 0; i < card_info[dev].nr_voices; i++)
  290. if (voice[card][i].channel == chn)
  291. SEQ_KEY_PRESSURE(dev, i, voice[card][i].note, vel);
  292. }
  293. void seq_bender(dev, chn, p1, p2)
  294. int dev, chn, p1, p2;
  295. {
  296. int card = CN, i, val;
  297. val = (p2 << 7) + p1;
  298. channel[chn].oldbend = channel[chn].bender;
  299. channel[chn].bender = val;
  300. if (ISMIDI(chn)) {
  301. SEQ_MIDIOUT(dev, MIDI_PITCH_BEND + CHANNEL);
  302. SEQ_MIDIOUT(dev, p1);
  303. SEQ_MIDIOUT(dev, p2);
  304. } else if (ISAWE(chn)) {
  305. SEQ_BENDER(dev, chn, val);
  306. } else
  307. for (i = 0; i < card_info[dev].nr_voices; i++)
  308. if (voice[card][i].channel == chn)
  309. SEQ_BENDER(dev, i, val);
  310. }
  311. void seq_reset()
  312. {
  313. int i, j;
  314. _seqbufptr = ticks = 0;
  315. ioctl(seqfd, SNDCTL_SEQ_RESET);
  316. for (i = 0; i < 16; i++) {
  317. if (ISMIDI(i)) {
  318. seq_control(ext_dev,i,0,0);
  319. seq_control(ext_dev,i,32,0);
  320. }
  321. seq_set_patch(i, 0);
  322. for (j = 0; j < 128; j++)
  323. note_vel[i][j] = 0;
  324. channel[i].bender = channel[i].oldbend = 8192;
  325. channel[i].bender_range = channel[i].oldrange = 2;
  326. channel[i].controller[CTL_PAN] = 64;
  327. channel[i].controller[CTL_SUSTAIN] = 0;
  328. }
  329. if (play_gus)
  330. for (i = 0; i < card_info[gus_dev].nr_voices; i++) {
  331. SEQ_CONTROL(gus_dev, i, SEQ_VOLMODE, VOL_METHOD_LINEAR);
  332. if (voice[0][i].note)
  333. SEQ_STOP_NOTE(gus_dev, i, voice[0][i].note, 64);
  334. voice[0][i].dead = voice[0][i].timestamp = -1;
  335. }
  336. if (play_fm) {
  337. if (wantopl3)
  338. ioctl(seqfd, SNDCTL_FM_4OP_ENABLE, &sb_dev);
  339. for (i = 0; i < card_info[sb_dev].nr_voices; i++) {
  340. SEQ_CONTROL(sb_dev, i, SEQ_VOLMODE, VOL_METHOD_LINEAR);
  341. if (voice[1][i].note)
  342. SEQ_STOP_NOTE(sb_dev, i, voice[1][i].note, 64);
  343. voice[1][i].dead = voice[1][i].timestamp = -1;
  344. }
  345. }
  346. if (play_awe) {
  347. AWE_SET_CHANNEL_MODE(awe_dev, 1);
  348. AWE_DRUM_CHANNELS(awe_dev, perc);
  349. AWE_TERMINATE_ALL(awe_dev);
  350. for (i = 0; i < card_info[awe_dev].nr_voices; i++) {
  351. voice[0][i].dead = voice[0][i].timestamp = -1;
  352. }
  353. }
  354. SEQ_DUMPBUF();
  355. }
  356. #endif /* linux || FreeBSD */