seq_midi_emul.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735
  1. /*
  2. * GM/GS/XG midi module.
  3. *
  4. * Copyright (C) 1999 Steve Ratcliffe
  5. *
  6. * Based on awe_wave.c by Takashi Iwai
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. *
  22. */
  23. /*
  24. * This module is used to keep track of the current midi state.
  25. * It can be used for drivers that are required to emulate midi when
  26. * the hardware doesn't.
  27. *
  28. * It was written for a AWE64 driver, but there should be no AWE specific
  29. * code in here. If there is it should be reported as a bug.
  30. */
  31. #include <sound/driver.h>
  32. #include <linux/init.h>
  33. #include <linux/slab.h>
  34. #include <linux/string.h>
  35. #include <sound/core.h>
  36. #include <sound/seq_kernel.h>
  37. #include <sound/seq_midi_emul.h>
  38. #include <sound/initval.h>
  39. #include <sound/asoundef.h>
  40. MODULE_AUTHOR("Takashi Iwai / Steve Ratcliffe");
  41. MODULE_DESCRIPTION("Advanced Linux Sound Architecture sequencer MIDI emulation.");
  42. MODULE_LICENSE("GPL");
  43. /* Prototypes for static functions */
  44. static void note_off(snd_midi_op_t *ops, void *drv, snd_midi_channel_t *chan, int note, int vel);
  45. static void do_control(snd_midi_op_t *ops, void *private,
  46. snd_midi_channel_set_t *chset, snd_midi_channel_t *chan,
  47. int control, int value);
  48. static void rpn(snd_midi_op_t *ops, void *drv, snd_midi_channel_t *chan, snd_midi_channel_set_t *chset);
  49. static void nrpn(snd_midi_op_t *ops, void *drv, snd_midi_channel_t *chan, snd_midi_channel_set_t *chset);
  50. static void sysex(snd_midi_op_t *ops, void *private, unsigned char *sysex, int len, snd_midi_channel_set_t *chset);
  51. static void all_sounds_off(snd_midi_op_t *ops, void *private, snd_midi_channel_t *chan);
  52. static void all_notes_off(snd_midi_op_t *ops, void *private, snd_midi_channel_t *chan);
  53. static void snd_midi_reset_controllers(snd_midi_channel_t *chan);
  54. static void reset_all_channels(snd_midi_channel_set_t *chset);
  55. /*
  56. * Process an event in a driver independent way. This means dealing
  57. * with RPN, NRPN, SysEx etc that are defined for common midi applications
  58. * such as GM, GS and XG.
  59. * There modes that this module will run in are:
  60. * Generic MIDI - no interpretation at all, it will just save current values
  61. * of controlers etc.
  62. * GM - You can use all gm_ prefixed elements of chan. Controls, RPN, NRPN,
  63. * SysEx will be interpreded as defined in General Midi.
  64. * GS - You can use all gs_ prefixed elements of chan. Codes for GS will be
  65. * interpreted.
  66. * XG - You can use all xg_ prefixed elements of chan. Codes for XG will
  67. * be interpreted.
  68. */
  69. void
  70. snd_midi_process_event(snd_midi_op_t *ops,
  71. snd_seq_event_t *ev, snd_midi_channel_set_t *chanset)
  72. {
  73. snd_midi_channel_t *chan;
  74. void *drv;
  75. int dest_channel = 0;
  76. if (ev == NULL || chanset == NULL) {
  77. snd_printd("ev or chanbase NULL (snd_midi_process_event)\n");
  78. return;
  79. }
  80. if (chanset->channels == NULL)
  81. return;
  82. if (snd_seq_ev_is_channel_type(ev)) {
  83. dest_channel = ev->data.note.channel;
  84. if (dest_channel >= chanset->max_channels) {
  85. snd_printd("dest channel is %d, max is %d\n", dest_channel, chanset->max_channels);
  86. return;
  87. }
  88. }
  89. chan = chanset->channels + dest_channel;
  90. drv = chanset->private_data;
  91. /* EVENT_NOTE should be processed before queued */
  92. if (ev->type == SNDRV_SEQ_EVENT_NOTE)
  93. return;
  94. /* Make sure that we don't have a note on that should really be
  95. * a note off */
  96. if (ev->type == SNDRV_SEQ_EVENT_NOTEON && ev->data.note.velocity == 0)
  97. ev->type = SNDRV_SEQ_EVENT_NOTEOFF;
  98. /* Make sure the note is within array range */
  99. if (ev->type == SNDRV_SEQ_EVENT_NOTEON ||
  100. ev->type == SNDRV_SEQ_EVENT_NOTEOFF ||
  101. ev->type == SNDRV_SEQ_EVENT_KEYPRESS) {
  102. if (ev->data.note.note >= 128)
  103. return;
  104. }
  105. switch (ev->type) {
  106. case SNDRV_SEQ_EVENT_NOTEON:
  107. if (chan->note[ev->data.note.note] & SNDRV_MIDI_NOTE_ON) {
  108. if (ops->note_off)
  109. ops->note_off(drv, ev->data.note.note, 0, chan);
  110. }
  111. chan->note[ev->data.note.note] = SNDRV_MIDI_NOTE_ON;
  112. if (ops->note_on)
  113. ops->note_on(drv, ev->data.note.note, ev->data.note.velocity, chan);
  114. break;
  115. case SNDRV_SEQ_EVENT_NOTEOFF:
  116. if (! (chan->note[ev->data.note.note] & SNDRV_MIDI_NOTE_ON))
  117. break;
  118. if (ops->note_off)
  119. note_off(ops, drv, chan, ev->data.note.note, ev->data.note.velocity);
  120. break;
  121. case SNDRV_SEQ_EVENT_KEYPRESS:
  122. if (ops->key_press)
  123. ops->key_press(drv, ev->data.note.note, ev->data.note.velocity, chan);
  124. break;
  125. case SNDRV_SEQ_EVENT_CONTROLLER:
  126. do_control(ops, drv, chanset, chan,
  127. ev->data.control.param, ev->data.control.value);
  128. break;
  129. case SNDRV_SEQ_EVENT_PGMCHANGE:
  130. chan->midi_program = ev->data.control.value;
  131. break;
  132. case SNDRV_SEQ_EVENT_PITCHBEND:
  133. chan->midi_pitchbend = ev->data.control.value;
  134. if (ops->control)
  135. ops->control(drv, MIDI_CTL_PITCHBEND, chan);
  136. break;
  137. case SNDRV_SEQ_EVENT_CHANPRESS:
  138. chan->midi_pressure = ev->data.control.value;
  139. if (ops->control)
  140. ops->control(drv, MIDI_CTL_CHAN_PRESSURE, chan);
  141. break;
  142. case SNDRV_SEQ_EVENT_CONTROL14:
  143. /* Best guess is that this is any of the 14 bit controller values */
  144. if (ev->data.control.param < 32) {
  145. /* set low part first */
  146. chan->control[ev->data.control.param + 32] =
  147. ev->data.control.value & 0x7f;
  148. do_control(ops, drv, chanset, chan,
  149. ev->data.control.param,
  150. ((ev->data.control.value>>7) & 0x7f));
  151. } else
  152. do_control(ops, drv, chanset, chan,
  153. ev->data.control.param,
  154. ev->data.control.value);
  155. break;
  156. case SNDRV_SEQ_EVENT_NONREGPARAM:
  157. /* Break it back into its controler values */
  158. chan->param_type = SNDRV_MIDI_PARAM_TYPE_NONREGISTERED;
  159. chan->control[MIDI_CTL_MSB_DATA_ENTRY]
  160. = (ev->data.control.value >> 7) & 0x7f;
  161. chan->control[MIDI_CTL_LSB_DATA_ENTRY]
  162. = ev->data.control.value & 0x7f;
  163. chan->control[MIDI_CTL_NONREG_PARM_NUM_MSB]
  164. = (ev->data.control.param >> 7) & 0x7f;
  165. chan->control[MIDI_CTL_NONREG_PARM_NUM_LSB]
  166. = ev->data.control.param & 0x7f;
  167. nrpn(ops, drv, chan, chanset);
  168. break;
  169. case SNDRV_SEQ_EVENT_REGPARAM:
  170. /* Break it back into its controler values */
  171. chan->param_type = SNDRV_MIDI_PARAM_TYPE_REGISTERED;
  172. chan->control[MIDI_CTL_MSB_DATA_ENTRY]
  173. = (ev->data.control.value >> 7) & 0x7f;
  174. chan->control[MIDI_CTL_LSB_DATA_ENTRY]
  175. = ev->data.control.value & 0x7f;
  176. chan->control[MIDI_CTL_REGIST_PARM_NUM_MSB]
  177. = (ev->data.control.param >> 7) & 0x7f;
  178. chan->control[MIDI_CTL_REGIST_PARM_NUM_LSB]
  179. = ev->data.control.param & 0x7f;
  180. rpn(ops, drv, chan, chanset);
  181. break;
  182. case SNDRV_SEQ_EVENT_SYSEX:
  183. if ((ev->flags & SNDRV_SEQ_EVENT_LENGTH_MASK) == SNDRV_SEQ_EVENT_LENGTH_VARIABLE) {
  184. unsigned char sysexbuf[64];
  185. int len;
  186. len = snd_seq_expand_var_event(ev, sizeof(sysexbuf), sysexbuf, 1, 0);
  187. if (len > 0)
  188. sysex(ops, drv, sysexbuf, len, chanset);
  189. }
  190. break;
  191. case SNDRV_SEQ_EVENT_SONGPOS:
  192. case SNDRV_SEQ_EVENT_SONGSEL:
  193. case SNDRV_SEQ_EVENT_CLOCK:
  194. case SNDRV_SEQ_EVENT_START:
  195. case SNDRV_SEQ_EVENT_CONTINUE:
  196. case SNDRV_SEQ_EVENT_STOP:
  197. case SNDRV_SEQ_EVENT_QFRAME:
  198. case SNDRV_SEQ_EVENT_TEMPO:
  199. case SNDRV_SEQ_EVENT_TIMESIGN:
  200. case SNDRV_SEQ_EVENT_KEYSIGN:
  201. goto not_yet;
  202. case SNDRV_SEQ_EVENT_SENSING:
  203. break;
  204. case SNDRV_SEQ_EVENT_CLIENT_START:
  205. case SNDRV_SEQ_EVENT_CLIENT_EXIT:
  206. case SNDRV_SEQ_EVENT_CLIENT_CHANGE:
  207. case SNDRV_SEQ_EVENT_PORT_START:
  208. case SNDRV_SEQ_EVENT_PORT_EXIT:
  209. case SNDRV_SEQ_EVENT_PORT_CHANGE:
  210. case SNDRV_SEQ_EVENT_SAMPLE:
  211. case SNDRV_SEQ_EVENT_SAMPLE_START:
  212. case SNDRV_SEQ_EVENT_SAMPLE_STOP:
  213. case SNDRV_SEQ_EVENT_SAMPLE_FREQ:
  214. case SNDRV_SEQ_EVENT_SAMPLE_VOLUME:
  215. case SNDRV_SEQ_EVENT_SAMPLE_LOOP:
  216. case SNDRV_SEQ_EVENT_SAMPLE_POSITION:
  217. case SNDRV_SEQ_EVENT_ECHO:
  218. not_yet:
  219. default:
  220. /*snd_printd("Unimplemented event %d\n", ev->type);*/
  221. break;
  222. }
  223. }
  224. /*
  225. * release note
  226. */
  227. static void
  228. note_off(snd_midi_op_t *ops, void *drv, snd_midi_channel_t *chan, int note, int vel)
  229. {
  230. if (chan->gm_hold) {
  231. /* Hold this note until pedal is turned off */
  232. chan->note[note] |= SNDRV_MIDI_NOTE_RELEASED;
  233. } else if (chan->note[note] & SNDRV_MIDI_NOTE_SOSTENUTO) {
  234. /* Mark this note as release; it will be turned off when sostenuto
  235. * is turned off */
  236. chan->note[note] |= SNDRV_MIDI_NOTE_RELEASED;
  237. } else {
  238. chan->note[note] = 0;
  239. if (ops->note_off)
  240. ops->note_off(drv, note, vel, chan);
  241. }
  242. }
  243. /*
  244. * Do all driver independent operations for this controler and pass
  245. * events that need to take place immediately to the driver.
  246. */
  247. static void
  248. do_control(snd_midi_op_t *ops, void *drv, snd_midi_channel_set_t *chset,
  249. snd_midi_channel_t *chan, int control, int value)
  250. {
  251. int i;
  252. /* Switches */
  253. if ((control >=64 && control <=69) || (control >= 80 && control <= 83)) {
  254. /* These are all switches; either off or on so set to 0 or 127 */
  255. value = (value >= 64)? 127: 0;
  256. }
  257. chan->control[control] = value;
  258. switch (control) {
  259. case MIDI_CTL_SUSTAIN:
  260. if (value == 0) {
  261. /* Sustain has been released, turn off held notes */
  262. for (i = 0; i < 128; i++) {
  263. if (chan->note[i] & SNDRV_MIDI_NOTE_RELEASED) {
  264. chan->note[i] = SNDRV_MIDI_NOTE_OFF;
  265. if (ops->note_off)
  266. ops->note_off(drv, i, 0, chan);
  267. }
  268. }
  269. }
  270. break;
  271. case MIDI_CTL_PORTAMENTO:
  272. break;
  273. case MIDI_CTL_SOSTENUTO:
  274. if (value) {
  275. /* Mark each note that is currently held down */
  276. for (i = 0; i < 128; i++) {
  277. if (chan->note[i] & SNDRV_MIDI_NOTE_ON)
  278. chan->note[i] |= SNDRV_MIDI_NOTE_SOSTENUTO;
  279. }
  280. } else {
  281. /* release all notes that were held */
  282. for (i = 0; i < 128; i++) {
  283. if (chan->note[i] & SNDRV_MIDI_NOTE_SOSTENUTO) {
  284. chan->note[i] &= ~SNDRV_MIDI_NOTE_SOSTENUTO;
  285. if (chan->note[i] & SNDRV_MIDI_NOTE_RELEASED) {
  286. chan->note[i] = SNDRV_MIDI_NOTE_OFF;
  287. if (ops->note_off)
  288. ops->note_off(drv, i, 0, chan);
  289. }
  290. }
  291. }
  292. }
  293. break;
  294. case MIDI_CTL_MSB_DATA_ENTRY:
  295. chan->control[MIDI_CTL_LSB_DATA_ENTRY] = 0;
  296. /* go through here */
  297. case MIDI_CTL_LSB_DATA_ENTRY:
  298. if (chan->param_type == SNDRV_MIDI_PARAM_TYPE_REGISTERED)
  299. rpn(ops, drv, chan, chset);
  300. else
  301. nrpn(ops, drv, chan, chset);
  302. break;
  303. case MIDI_CTL_REGIST_PARM_NUM_LSB:
  304. case MIDI_CTL_REGIST_PARM_NUM_MSB:
  305. chan->param_type = SNDRV_MIDI_PARAM_TYPE_REGISTERED;
  306. break;
  307. case MIDI_CTL_NONREG_PARM_NUM_LSB:
  308. case MIDI_CTL_NONREG_PARM_NUM_MSB:
  309. chan->param_type = SNDRV_MIDI_PARAM_TYPE_NONREGISTERED;
  310. break;
  311. case MIDI_CTL_ALL_SOUNDS_OFF:
  312. all_sounds_off(ops, drv, chan);
  313. break;
  314. case MIDI_CTL_ALL_NOTES_OFF:
  315. all_notes_off(ops, drv, chan);
  316. break;
  317. case MIDI_CTL_MSB_BANK:
  318. if (chset->midi_mode == SNDRV_MIDI_MODE_XG) {
  319. if (value == 127)
  320. chan->drum_channel = 1;
  321. else
  322. chan->drum_channel = 0;
  323. }
  324. break;
  325. case MIDI_CTL_LSB_BANK:
  326. break;
  327. case MIDI_CTL_RESET_CONTROLLERS:
  328. snd_midi_reset_controllers(chan);
  329. break;
  330. case MIDI_CTL_SOFT_PEDAL:
  331. case MIDI_CTL_LEGATO_FOOTSWITCH:
  332. case MIDI_CTL_HOLD2:
  333. case MIDI_CTL_SC1_SOUND_VARIATION:
  334. case MIDI_CTL_SC2_TIMBRE:
  335. case MIDI_CTL_SC3_RELEASE_TIME:
  336. case MIDI_CTL_SC4_ATTACK_TIME:
  337. case MIDI_CTL_SC5_BRIGHTNESS:
  338. case MIDI_CTL_E1_REVERB_DEPTH:
  339. case MIDI_CTL_E2_TREMOLO_DEPTH:
  340. case MIDI_CTL_E3_CHORUS_DEPTH:
  341. case MIDI_CTL_E4_DETUNE_DEPTH:
  342. case MIDI_CTL_E5_PHASER_DEPTH:
  343. goto notyet;
  344. notyet:
  345. default:
  346. if (ops->control)
  347. ops->control(drv, control, chan);
  348. break;
  349. }
  350. }
  351. /*
  352. * initialize the MIDI status
  353. */
  354. void
  355. snd_midi_channel_set_clear(snd_midi_channel_set_t *chset)
  356. {
  357. int i;
  358. chset->midi_mode = SNDRV_MIDI_MODE_GM;
  359. chset->gs_master_volume = 127;
  360. for (i = 0; i < chset->max_channels; i++) {
  361. snd_midi_channel_t *chan = chset->channels + i;
  362. memset(chan->note, 0, sizeof(chan->note));
  363. chan->midi_aftertouch = 0;
  364. chan->midi_pressure = 0;
  365. chan->midi_program = 0;
  366. chan->midi_pitchbend = 0;
  367. snd_midi_reset_controllers(chan);
  368. chan->gm_rpn_pitch_bend_range = 256; /* 2 semitones */
  369. chan->gm_rpn_fine_tuning = 0;
  370. chan->gm_rpn_coarse_tuning = 0;
  371. if (i == 9)
  372. chan->drum_channel = 1;
  373. else
  374. chan->drum_channel = 0;
  375. }
  376. }
  377. /*
  378. * Process a rpn message.
  379. */
  380. static void
  381. rpn(snd_midi_op_t *ops, void *drv, snd_midi_channel_t *chan,
  382. snd_midi_channel_set_t *chset)
  383. {
  384. int type;
  385. int val;
  386. if (chset->midi_mode != SNDRV_MIDI_MODE_NONE) {
  387. type = (chan->control[MIDI_CTL_REGIST_PARM_NUM_MSB] << 8) |
  388. chan->control[MIDI_CTL_REGIST_PARM_NUM_LSB];
  389. val = (chan->control[MIDI_CTL_MSB_DATA_ENTRY] << 7) |
  390. chan->control[MIDI_CTL_LSB_DATA_ENTRY];
  391. switch (type) {
  392. case 0x0000: /* Pitch bend sensitivity */
  393. /* MSB only / 1 semitone per 128 */
  394. chan->gm_rpn_pitch_bend_range = val;
  395. break;
  396. case 0x0001: /* fine tuning: */
  397. /* MSB/LSB, 8192=center, 100/8192 cent step */
  398. chan->gm_rpn_fine_tuning = val - 8192;
  399. break;
  400. case 0x0002: /* coarse tuning */
  401. /* MSB only / 8192=center, 1 semitone per 128 */
  402. chan->gm_rpn_coarse_tuning = val - 8192;
  403. break;
  404. case 0x7F7F: /* "lock-in" RPN */
  405. /* ignored */
  406. break;
  407. }
  408. }
  409. /* should call nrpn or rpn callback here.. */
  410. }
  411. /*
  412. * Process an nrpn message.
  413. */
  414. static void
  415. nrpn(snd_midi_op_t *ops, void *drv, snd_midi_channel_t *chan,
  416. snd_midi_channel_set_t *chset)
  417. {
  418. /* parse XG NRPNs here if possible */
  419. if (ops->nrpn)
  420. ops->nrpn(drv, chan, chset);
  421. }
  422. /*
  423. * convert channel parameter in GS sysex
  424. */
  425. static int
  426. get_channel(unsigned char cmd)
  427. {
  428. int p = cmd & 0x0f;
  429. if (p == 0)
  430. p = 9;
  431. else if (p < 10)
  432. p--;
  433. return p;
  434. }
  435. /*
  436. * Process a sysex message.
  437. */
  438. static void
  439. sysex(snd_midi_op_t *ops, void *private, unsigned char *buf, int len, snd_midi_channel_set_t *chset)
  440. {
  441. /* GM on */
  442. static unsigned char gm_on_macro[] = {
  443. 0x7e,0x7f,0x09,0x01,
  444. };
  445. /* XG on */
  446. static unsigned char xg_on_macro[] = {
  447. 0x43,0x10,0x4c,0x00,0x00,0x7e,0x00,
  448. };
  449. /* GS prefix
  450. * drum channel: XX=0x1?(channel), YY=0x15, ZZ=on/off
  451. * reverb mode: XX=0x01, YY=0x30, ZZ=0-7
  452. * chorus mode: XX=0x01, YY=0x38, ZZ=0-7
  453. * master vol: XX=0x00, YY=0x04, ZZ=0-127
  454. */
  455. static unsigned char gs_pfx_macro[] = {
  456. 0x41,0x10,0x42,0x12,0x40,/*XX,YY,ZZ*/
  457. };
  458. int parsed = SNDRV_MIDI_SYSEX_NOT_PARSED;
  459. if (len <= 0 || buf[0] != 0xf0)
  460. return;
  461. /* skip first byte */
  462. buf++;
  463. len--;
  464. /* GM on */
  465. if (len >= (int)sizeof(gm_on_macro) &&
  466. memcmp(buf, gm_on_macro, sizeof(gm_on_macro)) == 0) {
  467. if (chset->midi_mode != SNDRV_MIDI_MODE_GS &&
  468. chset->midi_mode != SNDRV_MIDI_MODE_XG) {
  469. chset->midi_mode = SNDRV_MIDI_MODE_GM;
  470. reset_all_channels(chset);
  471. parsed = SNDRV_MIDI_SYSEX_GM_ON;
  472. }
  473. }
  474. /* GS macros */
  475. else if (len >= 8 &&
  476. memcmp(buf, gs_pfx_macro, sizeof(gs_pfx_macro)) == 0) {
  477. if (chset->midi_mode != SNDRV_MIDI_MODE_GS &&
  478. chset->midi_mode != SNDRV_MIDI_MODE_XG)
  479. chset->midi_mode = SNDRV_MIDI_MODE_GS;
  480. if (buf[5] == 0x00 && buf[6] == 0x7f && buf[7] == 0x00) {
  481. /* GS reset */
  482. parsed = SNDRV_MIDI_SYSEX_GS_RESET;
  483. reset_all_channels(chset);
  484. }
  485. else if ((buf[5] & 0xf0) == 0x10 && buf[6] == 0x15) {
  486. /* drum pattern */
  487. int p = get_channel(buf[5]);
  488. if (p < chset->max_channels) {
  489. parsed = SNDRV_MIDI_SYSEX_GS_DRUM_CHANNEL;
  490. if (buf[7])
  491. chset->channels[p].drum_channel = 1;
  492. else
  493. chset->channels[p].drum_channel = 0;
  494. }
  495. } else if ((buf[5] & 0xf0) == 0x10 && buf[6] == 0x21) {
  496. /* program */
  497. int p = get_channel(buf[5]);
  498. if (p < chset->max_channels &&
  499. ! chset->channels[p].drum_channel) {
  500. parsed = SNDRV_MIDI_SYSEX_GS_DRUM_CHANNEL;
  501. chset->channels[p].midi_program = buf[7];
  502. }
  503. } else if (buf[5] == 0x01 && buf[6] == 0x30) {
  504. /* reverb mode */
  505. parsed = SNDRV_MIDI_SYSEX_GS_REVERB_MODE;
  506. chset->gs_reverb_mode = buf[7];
  507. } else if (buf[5] == 0x01 && buf[6] == 0x38) {
  508. /* chorus mode */
  509. parsed = SNDRV_MIDI_SYSEX_GS_CHORUS_MODE;
  510. chset->gs_chorus_mode = buf[7];
  511. } else if (buf[5] == 0x00 && buf[6] == 0x04) {
  512. /* master volume */
  513. parsed = SNDRV_MIDI_SYSEX_GS_MASTER_VOLUME;
  514. chset->gs_master_volume = buf[7];
  515. }
  516. }
  517. /* XG on */
  518. else if (len >= (int)sizeof(xg_on_macro) &&
  519. memcmp(buf, xg_on_macro, sizeof(xg_on_macro)) == 0) {
  520. int i;
  521. chset->midi_mode = SNDRV_MIDI_MODE_XG;
  522. parsed = SNDRV_MIDI_SYSEX_XG_ON;
  523. /* reset CC#0 for drums */
  524. for (i = 0; i < chset->max_channels; i++) {
  525. if (chset->channels[i].drum_channel)
  526. chset->channels[i].control[MIDI_CTL_MSB_BANK] = 127;
  527. else
  528. chset->channels[i].control[MIDI_CTL_MSB_BANK] = 0;
  529. }
  530. }
  531. if (ops->sysex)
  532. ops->sysex(private, buf - 1, len + 1, parsed, chset);
  533. }
  534. /*
  535. * all sound off
  536. */
  537. static void
  538. all_sounds_off(snd_midi_op_t *ops, void *drv, snd_midi_channel_t *chan)
  539. {
  540. int n;
  541. if (! ops->note_terminate)
  542. return;
  543. for (n = 0; n < 128; n++) {
  544. if (chan->note[n]) {
  545. ops->note_terminate(drv, n, chan);
  546. chan->note[n] = 0;
  547. }
  548. }
  549. }
  550. /*
  551. * all notes off
  552. */
  553. static void
  554. all_notes_off(snd_midi_op_t *ops, void *drv, snd_midi_channel_t *chan)
  555. {
  556. int n;
  557. if (! ops->note_off)
  558. return;
  559. for (n = 0; n < 128; n++) {
  560. if (chan->note[n] == SNDRV_MIDI_NOTE_ON)
  561. note_off(ops, drv, chan, n, 0);
  562. }
  563. }
  564. /*
  565. * Initialise a single midi channel control block.
  566. */
  567. static void snd_midi_channel_init(snd_midi_channel_t *p, int n)
  568. {
  569. if (p == NULL)
  570. return;
  571. memset(p, 0, sizeof(snd_midi_channel_t));
  572. p->private = NULL;
  573. p->number = n;
  574. snd_midi_reset_controllers(p);
  575. p->gm_rpn_pitch_bend_range = 256; /* 2 semitones */
  576. p->gm_rpn_fine_tuning = 0;
  577. p->gm_rpn_coarse_tuning = 0;
  578. if (n == 9)
  579. p->drum_channel = 1; /* Default ch 10 as drums */
  580. }
  581. /*
  582. * Allocate and initialise a set of midi channel control blocks.
  583. */
  584. static snd_midi_channel_t *snd_midi_channel_init_set(int n)
  585. {
  586. snd_midi_channel_t *chan;
  587. int i;
  588. chan = kmalloc(n * sizeof(snd_midi_channel_t), GFP_KERNEL);
  589. if (chan) {
  590. for (i = 0; i < n; i++)
  591. snd_midi_channel_init(chan+i, i);
  592. }
  593. return chan;
  594. }
  595. /*
  596. * reset all midi channels
  597. */
  598. static void
  599. reset_all_channels(snd_midi_channel_set_t *chset)
  600. {
  601. int ch;
  602. for (ch = 0; ch < chset->max_channels; ch++) {
  603. snd_midi_channel_t *chan = chset->channels + ch;
  604. snd_midi_reset_controllers(chan);
  605. chan->gm_rpn_pitch_bend_range = 256; /* 2 semitones */
  606. chan->gm_rpn_fine_tuning = 0;
  607. chan->gm_rpn_coarse_tuning = 0;
  608. if (ch == 9)
  609. chan->drum_channel = 1;
  610. else
  611. chan->drum_channel = 0;
  612. }
  613. }
  614. /*
  615. * Allocate and initialise a midi channel set.
  616. */
  617. snd_midi_channel_set_t *snd_midi_channel_alloc_set(int n)
  618. {
  619. snd_midi_channel_set_t *chset;
  620. chset = kmalloc(sizeof(*chset), GFP_KERNEL);
  621. if (chset) {
  622. chset->channels = snd_midi_channel_init_set(n);
  623. chset->private_data = NULL;
  624. chset->max_channels = n;
  625. }
  626. return chset;
  627. }
  628. /*
  629. * Reset the midi controllers on a particular channel to default values.
  630. */
  631. static void snd_midi_reset_controllers(snd_midi_channel_t *chan)
  632. {
  633. memset(chan->control, 0, sizeof(chan->control));
  634. chan->gm_volume = 127;
  635. chan->gm_expression = 127;
  636. chan->gm_pan = 64;
  637. }
  638. /*
  639. * Free a midi channel set.
  640. */
  641. void snd_midi_channel_free_set(snd_midi_channel_set_t *chset)
  642. {
  643. if (chset == NULL)
  644. return;
  645. kfree(chset->channels);
  646. kfree(chset);
  647. }
  648. static int __init alsa_seq_midi_emul_init(void)
  649. {
  650. return 0;
  651. }
  652. static void __exit alsa_seq_midi_emul_exit(void)
  653. {
  654. }
  655. module_init(alsa_seq_midi_emul_init)
  656. module_exit(alsa_seq_midi_emul_exit)
  657. EXPORT_SYMBOL(snd_midi_process_event);
  658. EXPORT_SYMBOL(snd_midi_channel_set_clear);
  659. EXPORT_SYMBOL(snd_midi_channel_alloc_set);
  660. EXPORT_SYMBOL(snd_midi_channel_free_set);