seq_midi_emul.c 19 KB

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