seq_midi_emul.c 20 KB

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