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