seq_midi_emul.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747
  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(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 controlers 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. snd_printd("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. snd_printd("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 controler 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 controler 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_SAMPLE:
  221. case SNDRV_SEQ_EVENT_SAMPLE_START:
  222. case SNDRV_SEQ_EVENT_SAMPLE_STOP:
  223. case SNDRV_SEQ_EVENT_SAMPLE_FREQ:
  224. case SNDRV_SEQ_EVENT_SAMPLE_VOLUME:
  225. case SNDRV_SEQ_EVENT_SAMPLE_LOOP:
  226. case SNDRV_SEQ_EVENT_SAMPLE_POSITION:
  227. case SNDRV_SEQ_EVENT_ECHO:
  228. not_yet:
  229. default:
  230. /*snd_printd("Unimplemented event %d\n", ev->type);*/
  231. break;
  232. }
  233. }
  234. /*
  235. * release note
  236. */
  237. static void
  238. note_off(struct snd_midi_op *ops, void *drv, struct snd_midi_channel *chan,
  239. int note, int vel)
  240. {
  241. if (chan->gm_hold) {
  242. /* Hold this note until pedal is turned off */
  243. chan->note[note] |= SNDRV_MIDI_NOTE_RELEASED;
  244. } else if (chan->note[note] & SNDRV_MIDI_NOTE_SOSTENUTO) {
  245. /* Mark this note as release; it will be turned off when sostenuto
  246. * is turned off */
  247. chan->note[note] |= SNDRV_MIDI_NOTE_RELEASED;
  248. } else {
  249. chan->note[note] = 0;
  250. if (ops->note_off)
  251. ops->note_off(drv, note, vel, chan);
  252. }
  253. }
  254. /*
  255. * Do all driver independent operations for this controler and pass
  256. * events that need to take place immediately to the driver.
  257. */
  258. static void
  259. do_control(struct snd_midi_op *ops, void *drv, struct snd_midi_channel_set *chset,
  260. struct snd_midi_channel *chan, int control, int value)
  261. {
  262. int i;
  263. /* Switches */
  264. if ((control >=64 && control <=69) || (control >= 80 && control <= 83)) {
  265. /* These are all switches; either off or on so set to 0 or 127 */
  266. value = (value >= 64)? 127: 0;
  267. }
  268. chan->control[control] = value;
  269. switch (control) {
  270. case MIDI_CTL_SUSTAIN:
  271. if (value == 0) {
  272. /* Sustain has been released, turn off held notes */
  273. for (i = 0; i < 128; i++) {
  274. if (chan->note[i] & SNDRV_MIDI_NOTE_RELEASED) {
  275. chan->note[i] = SNDRV_MIDI_NOTE_OFF;
  276. if (ops->note_off)
  277. ops->note_off(drv, i, 0, chan);
  278. }
  279. }
  280. }
  281. break;
  282. case MIDI_CTL_PORTAMENTO:
  283. break;
  284. case MIDI_CTL_SOSTENUTO:
  285. if (value) {
  286. /* Mark each note that is currently held down */
  287. for (i = 0; i < 128; i++) {
  288. if (chan->note[i] & SNDRV_MIDI_NOTE_ON)
  289. chan->note[i] |= SNDRV_MIDI_NOTE_SOSTENUTO;
  290. }
  291. } else {
  292. /* release all notes that were held */
  293. for (i = 0; i < 128; i++) {
  294. if (chan->note[i] & SNDRV_MIDI_NOTE_SOSTENUTO) {
  295. chan->note[i] &= ~SNDRV_MIDI_NOTE_SOSTENUTO;
  296. if (chan->note[i] & SNDRV_MIDI_NOTE_RELEASED) {
  297. chan->note[i] = SNDRV_MIDI_NOTE_OFF;
  298. if (ops->note_off)
  299. ops->note_off(drv, i, 0, chan);
  300. }
  301. }
  302. }
  303. }
  304. break;
  305. case MIDI_CTL_MSB_DATA_ENTRY:
  306. chan->control[MIDI_CTL_LSB_DATA_ENTRY] = 0;
  307. /* go through here */
  308. case MIDI_CTL_LSB_DATA_ENTRY:
  309. if (chan->param_type == SNDRV_MIDI_PARAM_TYPE_REGISTERED)
  310. rpn(ops, drv, chan, chset);
  311. else
  312. nrpn(ops, drv, chan, chset);
  313. break;
  314. case MIDI_CTL_REGIST_PARM_NUM_LSB:
  315. case MIDI_CTL_REGIST_PARM_NUM_MSB:
  316. chan->param_type = SNDRV_MIDI_PARAM_TYPE_REGISTERED;
  317. break;
  318. case MIDI_CTL_NONREG_PARM_NUM_LSB:
  319. case MIDI_CTL_NONREG_PARM_NUM_MSB:
  320. chan->param_type = SNDRV_MIDI_PARAM_TYPE_NONREGISTERED;
  321. break;
  322. case MIDI_CTL_ALL_SOUNDS_OFF:
  323. all_sounds_off(ops, drv, chan);
  324. break;
  325. case MIDI_CTL_ALL_NOTES_OFF:
  326. all_notes_off(ops, drv, chan);
  327. break;
  328. case MIDI_CTL_MSB_BANK:
  329. if (chset->midi_mode == SNDRV_MIDI_MODE_XG) {
  330. if (value == 127)
  331. chan->drum_channel = 1;
  332. else
  333. chan->drum_channel = 0;
  334. }
  335. break;
  336. case MIDI_CTL_LSB_BANK:
  337. break;
  338. case MIDI_CTL_RESET_CONTROLLERS:
  339. snd_midi_reset_controllers(chan);
  340. break;
  341. case MIDI_CTL_SOFT_PEDAL:
  342. case MIDI_CTL_LEGATO_FOOTSWITCH:
  343. case MIDI_CTL_HOLD2:
  344. case MIDI_CTL_SC1_SOUND_VARIATION:
  345. case MIDI_CTL_SC2_TIMBRE:
  346. case MIDI_CTL_SC3_RELEASE_TIME:
  347. case MIDI_CTL_SC4_ATTACK_TIME:
  348. case MIDI_CTL_SC5_BRIGHTNESS:
  349. case MIDI_CTL_E1_REVERB_DEPTH:
  350. case MIDI_CTL_E2_TREMOLO_DEPTH:
  351. case MIDI_CTL_E3_CHORUS_DEPTH:
  352. case MIDI_CTL_E4_DETUNE_DEPTH:
  353. case MIDI_CTL_E5_PHASER_DEPTH:
  354. goto notyet;
  355. notyet:
  356. default:
  357. if (ops->control)
  358. ops->control(drv, control, chan);
  359. break;
  360. }
  361. }
  362. /*
  363. * initialize the MIDI status
  364. */
  365. void
  366. snd_midi_channel_set_clear(struct snd_midi_channel_set *chset)
  367. {
  368. int i;
  369. chset->midi_mode = SNDRV_MIDI_MODE_GM;
  370. chset->gs_master_volume = 127;
  371. for (i = 0; i < chset->max_channels; i++) {
  372. struct snd_midi_channel *chan = chset->channels + i;
  373. memset(chan->note, 0, sizeof(chan->note));
  374. chan->midi_aftertouch = 0;
  375. chan->midi_pressure = 0;
  376. chan->midi_program = 0;
  377. chan->midi_pitchbend = 0;
  378. snd_midi_reset_controllers(chan);
  379. chan->gm_rpn_pitch_bend_range = 256; /* 2 semitones */
  380. chan->gm_rpn_fine_tuning = 0;
  381. chan->gm_rpn_coarse_tuning = 0;
  382. if (i == 9)
  383. chan->drum_channel = 1;
  384. else
  385. chan->drum_channel = 0;
  386. }
  387. }
  388. /*
  389. * Process a rpn message.
  390. */
  391. static void
  392. rpn(struct snd_midi_op *ops, void *drv, struct snd_midi_channel *chan,
  393. struct snd_midi_channel_set *chset)
  394. {
  395. int type;
  396. int val;
  397. if (chset->midi_mode != SNDRV_MIDI_MODE_NONE) {
  398. type = (chan->control[MIDI_CTL_REGIST_PARM_NUM_MSB] << 8) |
  399. chan->control[MIDI_CTL_REGIST_PARM_NUM_LSB];
  400. val = (chan->control[MIDI_CTL_MSB_DATA_ENTRY] << 7) |
  401. chan->control[MIDI_CTL_LSB_DATA_ENTRY];
  402. switch (type) {
  403. case 0x0000: /* Pitch bend sensitivity */
  404. /* MSB only / 1 semitone per 128 */
  405. chan->gm_rpn_pitch_bend_range = val;
  406. break;
  407. case 0x0001: /* fine tuning: */
  408. /* MSB/LSB, 8192=center, 100/8192 cent step */
  409. chan->gm_rpn_fine_tuning = val - 8192;
  410. break;
  411. case 0x0002: /* coarse tuning */
  412. /* MSB only / 8192=center, 1 semitone per 128 */
  413. chan->gm_rpn_coarse_tuning = val - 8192;
  414. break;
  415. case 0x7F7F: /* "lock-in" RPN */
  416. /* ignored */
  417. break;
  418. }
  419. }
  420. /* should call nrpn or rpn callback here.. */
  421. }
  422. /*
  423. * Process an nrpn message.
  424. */
  425. static void
  426. nrpn(struct snd_midi_op *ops, void *drv, struct snd_midi_channel *chan,
  427. struct snd_midi_channel_set *chset)
  428. {
  429. /* parse XG NRPNs here if possible */
  430. if (ops->nrpn)
  431. ops->nrpn(drv, chan, chset);
  432. }
  433. /*
  434. * convert channel parameter in GS sysex
  435. */
  436. static int
  437. get_channel(unsigned char cmd)
  438. {
  439. int p = cmd & 0x0f;
  440. if (p == 0)
  441. p = 9;
  442. else if (p < 10)
  443. p--;
  444. return p;
  445. }
  446. /*
  447. * Process a sysex message.
  448. */
  449. static void
  450. sysex(struct snd_midi_op *ops, void *private, unsigned char *buf, int len,
  451. struct snd_midi_channel_set *chset)
  452. {
  453. /* GM on */
  454. static unsigned char gm_on_macro[] = {
  455. 0x7e,0x7f,0x09,0x01,
  456. };
  457. /* XG on */
  458. static unsigned char xg_on_macro[] = {
  459. 0x43,0x10,0x4c,0x00,0x00,0x7e,0x00,
  460. };
  461. /* GS prefix
  462. * drum channel: XX=0x1?(channel), YY=0x15, ZZ=on/off
  463. * reverb mode: XX=0x01, YY=0x30, ZZ=0-7
  464. * chorus mode: XX=0x01, YY=0x38, ZZ=0-7
  465. * master vol: XX=0x00, YY=0x04, ZZ=0-127
  466. */
  467. static unsigned char gs_pfx_macro[] = {
  468. 0x41,0x10,0x42,0x12,0x40,/*XX,YY,ZZ*/
  469. };
  470. int parsed = SNDRV_MIDI_SYSEX_NOT_PARSED;
  471. if (len <= 0 || buf[0] != 0xf0)
  472. return;
  473. /* skip first byte */
  474. buf++;
  475. len--;
  476. /* GM on */
  477. if (len >= (int)sizeof(gm_on_macro) &&
  478. memcmp(buf, gm_on_macro, sizeof(gm_on_macro)) == 0) {
  479. if (chset->midi_mode != SNDRV_MIDI_MODE_GS &&
  480. chset->midi_mode != SNDRV_MIDI_MODE_XG) {
  481. chset->midi_mode = SNDRV_MIDI_MODE_GM;
  482. reset_all_channels(chset);
  483. parsed = SNDRV_MIDI_SYSEX_GM_ON;
  484. }
  485. }
  486. /* GS macros */
  487. else if (len >= 8 &&
  488. memcmp(buf, gs_pfx_macro, sizeof(gs_pfx_macro)) == 0) {
  489. if (chset->midi_mode != SNDRV_MIDI_MODE_GS &&
  490. chset->midi_mode != SNDRV_MIDI_MODE_XG)
  491. chset->midi_mode = SNDRV_MIDI_MODE_GS;
  492. if (buf[5] == 0x00 && buf[6] == 0x7f && buf[7] == 0x00) {
  493. /* GS reset */
  494. parsed = SNDRV_MIDI_SYSEX_GS_RESET;
  495. reset_all_channels(chset);
  496. }
  497. else if ((buf[5] & 0xf0) == 0x10 && buf[6] == 0x15) {
  498. /* drum pattern */
  499. int p = get_channel(buf[5]);
  500. if (p < chset->max_channels) {
  501. parsed = SNDRV_MIDI_SYSEX_GS_DRUM_CHANNEL;
  502. if (buf[7])
  503. chset->channels[p].drum_channel = 1;
  504. else
  505. chset->channels[p].drum_channel = 0;
  506. }
  507. } else if ((buf[5] & 0xf0) == 0x10 && buf[6] == 0x21) {
  508. /* program */
  509. int p = get_channel(buf[5]);
  510. if (p < chset->max_channels &&
  511. ! chset->channels[p].drum_channel) {
  512. parsed = SNDRV_MIDI_SYSEX_GS_DRUM_CHANNEL;
  513. chset->channels[p].midi_program = buf[7];
  514. }
  515. } else if (buf[5] == 0x01 && buf[6] == 0x30) {
  516. /* reverb mode */
  517. parsed = SNDRV_MIDI_SYSEX_GS_REVERB_MODE;
  518. chset->gs_reverb_mode = buf[7];
  519. } else if (buf[5] == 0x01 && buf[6] == 0x38) {
  520. /* chorus mode */
  521. parsed = SNDRV_MIDI_SYSEX_GS_CHORUS_MODE;
  522. chset->gs_chorus_mode = buf[7];
  523. } else if (buf[5] == 0x00 && buf[6] == 0x04) {
  524. /* master volume */
  525. parsed = SNDRV_MIDI_SYSEX_GS_MASTER_VOLUME;
  526. chset->gs_master_volume = buf[7];
  527. }
  528. }
  529. /* XG on */
  530. else if (len >= (int)sizeof(xg_on_macro) &&
  531. memcmp(buf, xg_on_macro, sizeof(xg_on_macro)) == 0) {
  532. int i;
  533. chset->midi_mode = SNDRV_MIDI_MODE_XG;
  534. parsed = SNDRV_MIDI_SYSEX_XG_ON;
  535. /* reset CC#0 for drums */
  536. for (i = 0; i < chset->max_channels; i++) {
  537. if (chset->channels[i].drum_channel)
  538. chset->channels[i].control[MIDI_CTL_MSB_BANK] = 127;
  539. else
  540. chset->channels[i].control[MIDI_CTL_MSB_BANK] = 0;
  541. }
  542. }
  543. if (ops->sysex)
  544. ops->sysex(private, buf - 1, len + 1, parsed, chset);
  545. }
  546. /*
  547. * all sound off
  548. */
  549. static void
  550. all_sounds_off(struct snd_midi_op *ops, void *drv, struct snd_midi_channel *chan)
  551. {
  552. int n;
  553. if (! ops->note_terminate)
  554. return;
  555. for (n = 0; n < 128; n++) {
  556. if (chan->note[n]) {
  557. ops->note_terminate(drv, n, chan);
  558. chan->note[n] = 0;
  559. }
  560. }
  561. }
  562. /*
  563. * all notes off
  564. */
  565. static void
  566. all_notes_off(struct snd_midi_op *ops, void *drv, struct snd_midi_channel *chan)
  567. {
  568. int n;
  569. if (! ops->note_off)
  570. return;
  571. for (n = 0; n < 128; n++) {
  572. if (chan->note[n] == SNDRV_MIDI_NOTE_ON)
  573. note_off(ops, drv, chan, n, 0);
  574. }
  575. }
  576. /*
  577. * Initialise a single midi channel control block.
  578. */
  579. static void snd_midi_channel_init(struct snd_midi_channel *p, int n)
  580. {
  581. if (p == NULL)
  582. return;
  583. memset(p, 0, sizeof(struct snd_midi_channel));
  584. p->private = NULL;
  585. p->number = n;
  586. snd_midi_reset_controllers(p);
  587. p->gm_rpn_pitch_bend_range = 256; /* 2 semitones */
  588. p->gm_rpn_fine_tuning = 0;
  589. p->gm_rpn_coarse_tuning = 0;
  590. if (n == 9)
  591. p->drum_channel = 1; /* Default ch 10 as drums */
  592. }
  593. /*
  594. * Allocate and initialise a set of midi channel control blocks.
  595. */
  596. static struct snd_midi_channel *snd_midi_channel_init_set(int n)
  597. {
  598. struct snd_midi_channel *chan;
  599. int i;
  600. chan = kmalloc(n * sizeof(struct snd_midi_channel), GFP_KERNEL);
  601. if (chan) {
  602. for (i = 0; i < n; i++)
  603. snd_midi_channel_init(chan+i, i);
  604. }
  605. return chan;
  606. }
  607. /*
  608. * reset all midi channels
  609. */
  610. static void
  611. reset_all_channels(struct snd_midi_channel_set *chset)
  612. {
  613. int ch;
  614. for (ch = 0; ch < chset->max_channels; ch++) {
  615. struct snd_midi_channel *chan = chset->channels + ch;
  616. snd_midi_reset_controllers(chan);
  617. chan->gm_rpn_pitch_bend_range = 256; /* 2 semitones */
  618. chan->gm_rpn_fine_tuning = 0;
  619. chan->gm_rpn_coarse_tuning = 0;
  620. if (ch == 9)
  621. chan->drum_channel = 1;
  622. else
  623. chan->drum_channel = 0;
  624. }
  625. }
  626. /*
  627. * Allocate and initialise a midi channel set.
  628. */
  629. struct snd_midi_channel_set *snd_midi_channel_alloc_set(int n)
  630. {
  631. struct snd_midi_channel_set *chset;
  632. chset = kmalloc(sizeof(*chset), GFP_KERNEL);
  633. if (chset) {
  634. chset->channels = snd_midi_channel_init_set(n);
  635. chset->private_data = NULL;
  636. chset->max_channels = n;
  637. }
  638. return chset;
  639. }
  640. /*
  641. * Reset the midi controllers on a particular channel to default values.
  642. */
  643. static void snd_midi_reset_controllers(struct snd_midi_channel *chan)
  644. {
  645. memset(chan->control, 0, sizeof(chan->control));
  646. chan->gm_volume = 127;
  647. chan->gm_expression = 127;
  648. chan->gm_pan = 64;
  649. }
  650. /*
  651. * Free a midi channel set.
  652. */
  653. void snd_midi_channel_free_set(struct snd_midi_channel_set *chset)
  654. {
  655. if (chset == NULL)
  656. return;
  657. kfree(chset->channels);
  658. kfree(chset);
  659. }
  660. static int __init alsa_seq_midi_emul_init(void)
  661. {
  662. return 0;
  663. }
  664. static void __exit alsa_seq_midi_emul_exit(void)
  665. {
  666. }
  667. module_init(alsa_seq_midi_emul_init)
  668. module_exit(alsa_seq_midi_emul_exit)
  669. EXPORT_SYMBOL(snd_midi_process_event);
  670. EXPORT_SYMBOL(snd_midi_channel_set_clear);
  671. EXPORT_SYMBOL(snd_midi_channel_alloc_set);
  672. EXPORT_SYMBOL(snd_midi_channel_free_set);