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