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