2 * GM/GS/XG midi module.
4 * Copyright (C) 1999 Steve Ratcliffe
6 * Based on awe_wave.c by Takashi Iwai
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.
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.
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
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.
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.
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>
42 MODULE_AUTHOR("Takashi Iwai / Steve Ratcliffe");
43 MODULE_DESCRIPTION("Advanced Linux Sound Architecture sequencer MIDI emulation.");
44 MODULE_LICENSE("GPL");
46 /* Prototypes for static functions */
47 static void note_off(snd_midi_op_t *ops, void *drv, snd_midi_channel_t *chan, int note, int vel);
48 static void do_control(snd_midi_op_t *ops, void *private,
49 snd_midi_channel_set_t *chset, snd_midi_channel_t *chan,
50 int control, int value);
51 static void rpn(snd_midi_op_t *ops, void *drv, snd_midi_channel_t *chan, snd_midi_channel_set_t *chset);
52 static void nrpn(snd_midi_op_t *ops, void *drv, snd_midi_channel_t *chan, snd_midi_channel_set_t *chset);
53 static void sysex(snd_midi_op_t *ops, void *private, unsigned char *sysex, int len, snd_midi_channel_set_t *chset);
54 static void all_sounds_off(snd_midi_op_t *ops, void *private, snd_midi_channel_t *chan);
55 static void all_notes_off(snd_midi_op_t *ops, void *private, snd_midi_channel_t *chan);
56 static void snd_midi_reset_controllers(snd_midi_channel_t *chan);
57 static void reset_all_channels(snd_midi_channel_set_t *chset);
61 * Process an event in a driver independent way. This means dealing
62 * with RPN, NRPN, SysEx etc that are defined for common midi applications
63 * such as GM, GS and XG.
64 * There modes that this module will run in are:
65 * Generic MIDI - no interpretation at all, it will just save current values
67 * GM - You can use all gm_ prefixed elements of chan. Controls, RPN, NRPN,
68 * SysEx will be interpreded as defined in General Midi.
69 * GS - You can use all gs_ prefixed elements of chan. Codes for GS will be
71 * XG - You can use all xg_ prefixed elements of chan. Codes for XG will
75 snd_midi_process_event(snd_midi_op_t *ops,
76 snd_seq_event_t *ev, snd_midi_channel_set_t *chanset)
78 snd_midi_channel_t *chan;
82 if (ev == NULL || chanset == NULL) {
83 snd_printd("ev or chanbase NULL (snd_midi_process_event)\n");
86 if (chanset->channels == NULL)
89 if (snd_seq_ev_is_channel_type(ev)) {
90 dest_channel = ev->data.note.channel;
91 if (dest_channel >= chanset->max_channels) {
92 snd_printd("dest channel is %d, max is %d\n", dest_channel, chanset->max_channels);
97 chan = chanset->channels + dest_channel;
98 drv = chanset->private_data;
100 /* EVENT_NOTE should be processed before queued */
101 if (ev->type == SNDRV_SEQ_EVENT_NOTE)
104 /* Make sure that we don't have a note on that should really be
106 if (ev->type == SNDRV_SEQ_EVENT_NOTEON && ev->data.note.velocity == 0)
107 ev->type = SNDRV_SEQ_EVENT_NOTEOFF;
109 /* Make sure the note is within array range */
110 if (ev->type == SNDRV_SEQ_EVENT_NOTEON ||
111 ev->type == SNDRV_SEQ_EVENT_NOTEOFF ||
112 ev->type == SNDRV_SEQ_EVENT_KEYPRESS) {
113 if (ev->data.note.note >= 128)
118 case SNDRV_SEQ_EVENT_NOTEON:
119 if (chan->note[ev->data.note.note] & SNDRV_MIDI_NOTE_ON) {
121 ops->note_off(drv, ev->data.note.note, 0, chan);
123 chan->note[ev->data.note.note] = SNDRV_MIDI_NOTE_ON;
125 ops->note_on(drv, ev->data.note.note, ev->data.note.velocity, chan);
127 case SNDRV_SEQ_EVENT_NOTEOFF:
128 if (! (chan->note[ev->data.note.note] & SNDRV_MIDI_NOTE_ON))
131 note_off(ops, drv, chan, ev->data.note.note, ev->data.note.velocity);
133 case SNDRV_SEQ_EVENT_KEYPRESS:
135 ops->key_press(drv, ev->data.note.note, ev->data.note.velocity, chan);
137 case SNDRV_SEQ_EVENT_CONTROLLER:
138 do_control(ops, drv, chanset, chan,
139 ev->data.control.param, ev->data.control.value);
141 case SNDRV_SEQ_EVENT_PGMCHANGE:
142 chan->midi_program = ev->data.control.value;
144 case SNDRV_SEQ_EVENT_PITCHBEND:
145 chan->midi_pitchbend = ev->data.control.value;
147 ops->control(drv, MIDI_CTL_PITCHBEND, chan);
149 case SNDRV_SEQ_EVENT_CHANPRESS:
150 chan->midi_pressure = ev->data.control.value;
152 ops->control(drv, MIDI_CTL_CHAN_PRESSURE, chan);
154 case SNDRV_SEQ_EVENT_CONTROL14:
155 /* Best guess is that this is any of the 14 bit controller values */
156 if (ev->data.control.param < 32) {
157 /* set low part first */
158 chan->control[ev->data.control.param + 32] =
159 ev->data.control.value & 0x7f;
160 do_control(ops, drv, chanset, chan,
161 ev->data.control.param,
162 ((ev->data.control.value>>7) & 0x7f));
164 do_control(ops, drv, chanset, chan,
165 ev->data.control.param,
166 ev->data.control.value);
168 case SNDRV_SEQ_EVENT_NONREGPARAM:
169 /* Break it back into its controler values */
170 chan->param_type = SNDRV_MIDI_PARAM_TYPE_NONREGISTERED;
171 chan->control[MIDI_CTL_MSB_DATA_ENTRY]
172 = (ev->data.control.value >> 7) & 0x7f;
173 chan->control[MIDI_CTL_LSB_DATA_ENTRY]
174 = ev->data.control.value & 0x7f;
175 chan->control[MIDI_CTL_NONREG_PARM_NUM_MSB]
176 = (ev->data.control.param >> 7) & 0x7f;
177 chan->control[MIDI_CTL_NONREG_PARM_NUM_LSB]
178 = ev->data.control.param & 0x7f;
179 nrpn(ops, drv, chan, chanset);
181 case SNDRV_SEQ_EVENT_REGPARAM:
182 /* Break it back into its controler values */
183 chan->param_type = SNDRV_MIDI_PARAM_TYPE_REGISTERED;
184 chan->control[MIDI_CTL_MSB_DATA_ENTRY]
185 = (ev->data.control.value >> 7) & 0x7f;
186 chan->control[MIDI_CTL_LSB_DATA_ENTRY]
187 = ev->data.control.value & 0x7f;
188 chan->control[MIDI_CTL_REGIST_PARM_NUM_MSB]
189 = (ev->data.control.param >> 7) & 0x7f;
190 chan->control[MIDI_CTL_REGIST_PARM_NUM_LSB]
191 = ev->data.control.param & 0x7f;
192 rpn(ops, drv, chan, chanset);
194 case SNDRV_SEQ_EVENT_SYSEX:
195 if ((ev->flags & SNDRV_SEQ_EVENT_LENGTH_MASK) == SNDRV_SEQ_EVENT_LENGTH_VARIABLE) {
196 unsigned char sysexbuf[64];
198 len = snd_seq_expand_var_event(ev, sizeof(sysexbuf), sysexbuf, 1, 0);
200 sysex(ops, drv, sysexbuf, len, chanset);
203 case SNDRV_SEQ_EVENT_SONGPOS:
204 case SNDRV_SEQ_EVENT_SONGSEL:
205 case SNDRV_SEQ_EVENT_CLOCK:
206 case SNDRV_SEQ_EVENT_START:
207 case SNDRV_SEQ_EVENT_CONTINUE:
208 case SNDRV_SEQ_EVENT_STOP:
209 case SNDRV_SEQ_EVENT_QFRAME:
210 case SNDRV_SEQ_EVENT_TEMPO:
211 case SNDRV_SEQ_EVENT_TIMESIGN:
212 case SNDRV_SEQ_EVENT_KEYSIGN:
214 case SNDRV_SEQ_EVENT_SENSING:
216 case SNDRV_SEQ_EVENT_CLIENT_START:
217 case SNDRV_SEQ_EVENT_CLIENT_EXIT:
218 case SNDRV_SEQ_EVENT_CLIENT_CHANGE:
219 case SNDRV_SEQ_EVENT_PORT_START:
220 case SNDRV_SEQ_EVENT_PORT_EXIT:
221 case SNDRV_SEQ_EVENT_PORT_CHANGE:
222 case SNDRV_SEQ_EVENT_SAMPLE:
223 case SNDRV_SEQ_EVENT_SAMPLE_START:
224 case SNDRV_SEQ_EVENT_SAMPLE_STOP:
225 case SNDRV_SEQ_EVENT_SAMPLE_FREQ:
226 case SNDRV_SEQ_EVENT_SAMPLE_VOLUME:
227 case SNDRV_SEQ_EVENT_SAMPLE_LOOP:
228 case SNDRV_SEQ_EVENT_SAMPLE_POSITION:
229 case SNDRV_SEQ_EVENT_ECHO:
232 /*snd_printd("Unimplemented event %d\n", ev->type);*/
242 note_off(snd_midi_op_t *ops, void *drv, snd_midi_channel_t *chan, int note, int vel)
245 /* Hold this note until pedal is turned off */
246 chan->note[note] |= SNDRV_MIDI_NOTE_RELEASED;
247 } else if (chan->note[note] & SNDRV_MIDI_NOTE_SOSTENUTO) {
248 /* Mark this note as release; it will be turned off when sostenuto
250 chan->note[note] |= SNDRV_MIDI_NOTE_RELEASED;
252 chan->note[note] = 0;
254 ops->note_off(drv, note, vel, chan);
259 * Do all driver independent operations for this controler and pass
260 * events that need to take place immediately to the driver.
263 do_control(snd_midi_op_t *ops, void *drv, snd_midi_channel_set_t *chset,
264 snd_midi_channel_t *chan, int control, int value)
269 if ((control >=64 && control <=69) || (control >= 80 && control <= 83)) {
270 /* These are all switches; either off or on so set to 0 or 127 */
271 value = (value >= 64)? 127: 0;
273 chan->control[control] = value;
276 case MIDI_CTL_SUSTAIN:
278 /* Sustain has been released, turn off held notes */
279 for (i = 0; i < 128; i++) {
280 if (chan->note[i] & SNDRV_MIDI_NOTE_RELEASED) {
281 chan->note[i] = SNDRV_MIDI_NOTE_OFF;
283 ops->note_off(drv, i, 0, chan);
288 case MIDI_CTL_PORTAMENTO:
290 case MIDI_CTL_SOSTENUTO:
292 /* Mark each note that is currently held down */
293 for (i = 0; i < 128; i++) {
294 if (chan->note[i] & SNDRV_MIDI_NOTE_ON)
295 chan->note[i] |= SNDRV_MIDI_NOTE_SOSTENUTO;
298 /* release all notes that were held */
299 for (i = 0; i < 128; i++) {
300 if (chan->note[i] & SNDRV_MIDI_NOTE_SOSTENUTO) {
301 chan->note[i] &= ~SNDRV_MIDI_NOTE_SOSTENUTO;
302 if (chan->note[i] & SNDRV_MIDI_NOTE_RELEASED) {
303 chan->note[i] = SNDRV_MIDI_NOTE_OFF;
305 ops->note_off(drv, i, 0, chan);
311 case MIDI_CTL_MSB_DATA_ENTRY:
312 chan->control[MIDI_CTL_LSB_DATA_ENTRY] = 0;
313 /* go through here */
314 case MIDI_CTL_LSB_DATA_ENTRY:
315 if (chan->param_type == SNDRV_MIDI_PARAM_TYPE_REGISTERED)
316 rpn(ops, drv, chan, chset);
318 nrpn(ops, drv, chan, chset);
320 case MIDI_CTL_REGIST_PARM_NUM_LSB:
321 case MIDI_CTL_REGIST_PARM_NUM_MSB:
322 chan->param_type = SNDRV_MIDI_PARAM_TYPE_REGISTERED;
324 case MIDI_CTL_NONREG_PARM_NUM_LSB:
325 case MIDI_CTL_NONREG_PARM_NUM_MSB:
326 chan->param_type = SNDRV_MIDI_PARAM_TYPE_NONREGISTERED;
329 case MIDI_CTL_ALL_SOUNDS_OFF:
330 all_sounds_off(ops, drv, chan);
333 case MIDI_CTL_ALL_NOTES_OFF:
334 all_notes_off(ops, drv, chan);
337 case MIDI_CTL_MSB_BANK:
338 if (chset->midi_mode == SNDRV_MIDI_MODE_XG) {
340 chan->drum_channel = 1;
342 chan->drum_channel = 0;
345 case MIDI_CTL_LSB_BANK:
348 case MIDI_CTL_RESET_CONTROLLERS:
349 snd_midi_reset_controllers(chan);
352 case MIDI_CTL_SOFT_PEDAL:
353 case MIDI_CTL_LEGATO_FOOTSWITCH:
355 case MIDI_CTL_SC1_SOUND_VARIATION:
356 case MIDI_CTL_SC2_TIMBRE:
357 case MIDI_CTL_SC3_RELEASE_TIME:
358 case MIDI_CTL_SC4_ATTACK_TIME:
359 case MIDI_CTL_SC5_BRIGHTNESS:
360 case MIDI_CTL_E1_REVERB_DEPTH:
361 case MIDI_CTL_E2_TREMOLO_DEPTH:
362 case MIDI_CTL_E3_CHORUS_DEPTH:
363 case MIDI_CTL_E4_DETUNE_DEPTH:
364 case MIDI_CTL_E5_PHASER_DEPTH:
369 ops->control(drv, control, chan);
376 * initialize the MIDI status
379 snd_midi_channel_set_clear(snd_midi_channel_set_t *chset)
383 chset->midi_mode = SNDRV_MIDI_MODE_GM;
384 chset->gs_master_volume = 127;
386 for (i = 0; i < chset->max_channels; i++) {
387 snd_midi_channel_t *chan = chset->channels + i;
388 memset(chan->note, 0, sizeof(chan->note));
390 chan->midi_aftertouch = 0;
391 chan->midi_pressure = 0;
392 chan->midi_program = 0;
393 chan->midi_pitchbend = 0;
394 snd_midi_reset_controllers(chan);
395 chan->gm_rpn_pitch_bend_range = 256; /* 2 semitones */
396 chan->gm_rpn_fine_tuning = 0;
397 chan->gm_rpn_coarse_tuning = 0;
400 chan->drum_channel = 1;
402 chan->drum_channel = 0;
407 * Process a rpn message.
410 rpn(snd_midi_op_t *ops, void *drv, snd_midi_channel_t *chan,
411 snd_midi_channel_set_t *chset)
416 if (chset->midi_mode != SNDRV_MIDI_MODE_NONE) {
417 type = (chan->control[MIDI_CTL_REGIST_PARM_NUM_MSB] << 8) |
418 chan->control[MIDI_CTL_REGIST_PARM_NUM_LSB];
419 val = (chan->control[MIDI_CTL_MSB_DATA_ENTRY] << 7) |
420 chan->control[MIDI_CTL_LSB_DATA_ENTRY];
423 case 0x0000: /* Pitch bend sensitivity */
424 /* MSB only / 1 semitone per 128 */
425 chan->gm_rpn_pitch_bend_range = val;
428 case 0x0001: /* fine tuning: */
429 /* MSB/LSB, 8192=center, 100/8192 cent step */
430 chan->gm_rpn_fine_tuning = val - 8192;
433 case 0x0002: /* coarse tuning */
434 /* MSB only / 8192=center, 1 semitone per 128 */
435 chan->gm_rpn_coarse_tuning = val - 8192;
438 case 0x7F7F: /* "lock-in" RPN */
443 /* should call nrpn or rpn callback here.. */
447 * Process an nrpn message.
450 nrpn(snd_midi_op_t *ops, void *drv, snd_midi_channel_t *chan,
451 snd_midi_channel_set_t *chset)
453 /* parse XG NRPNs here if possible */
455 ops->nrpn(drv, chan, chset);
460 * convert channel parameter in GS sysex
463 get_channel(unsigned char cmd)
475 * Process a sysex message.
478 sysex(snd_midi_op_t *ops, void *private, unsigned char *buf, int len, snd_midi_channel_set_t *chset)
481 static unsigned char gm_on_macro[] = {
485 static unsigned char xg_on_macro[] = {
486 0x43,0x10,0x4c,0x00,0x00,0x7e,0x00,
489 * drum channel: XX=0x1?(channel), YY=0x15, ZZ=on/off
490 * reverb mode: XX=0x01, YY=0x30, ZZ=0-7
491 * chorus mode: XX=0x01, YY=0x38, ZZ=0-7
492 * master vol: XX=0x00, YY=0x04, ZZ=0-127
494 static unsigned char gs_pfx_macro[] = {
495 0x41,0x10,0x42,0x12,0x40,/*XX,YY,ZZ*/
498 int parsed = SNDRV_MIDI_SYSEX_NOT_PARSED;
500 if (len <= 0 || buf[0] != 0xf0)
502 /* skip first byte */
507 if (len >= (int)sizeof(gm_on_macro) &&
508 memcmp(buf, gm_on_macro, sizeof(gm_on_macro)) == 0) {
509 if (chset->midi_mode != SNDRV_MIDI_MODE_GS &&
510 chset->midi_mode != SNDRV_MIDI_MODE_XG) {
511 chset->midi_mode = SNDRV_MIDI_MODE_GM;
512 reset_all_channels(chset);
513 parsed = SNDRV_MIDI_SYSEX_GM_ON;
519 memcmp(buf, gs_pfx_macro, sizeof(gs_pfx_macro)) == 0) {
520 if (chset->midi_mode != SNDRV_MIDI_MODE_GS &&
521 chset->midi_mode != SNDRV_MIDI_MODE_XG)
522 chset->midi_mode = SNDRV_MIDI_MODE_GS;
524 if (buf[5] == 0x00 && buf[6] == 0x7f && buf[7] == 0x00) {
526 parsed = SNDRV_MIDI_SYSEX_GS_RESET;
527 reset_all_channels(chset);
530 else if ((buf[5] & 0xf0) == 0x10 && buf[6] == 0x15) {
532 int p = get_channel(buf[5]);
533 if (p < chset->max_channels) {
534 parsed = SNDRV_MIDI_SYSEX_GS_DRUM_CHANNEL;
536 chset->channels[p].drum_channel = 1;
538 chset->channels[p].drum_channel = 0;
541 } else if ((buf[5] & 0xf0) == 0x10 && buf[6] == 0x21) {
543 int p = get_channel(buf[5]);
544 if (p < chset->max_channels &&
545 ! chset->channels[p].drum_channel) {
546 parsed = SNDRV_MIDI_SYSEX_GS_DRUM_CHANNEL;
547 chset->channels[p].midi_program = buf[7];
550 } else if (buf[5] == 0x01 && buf[6] == 0x30) {
552 parsed = SNDRV_MIDI_SYSEX_GS_REVERB_MODE;
553 chset->gs_reverb_mode = buf[7];
555 } else if (buf[5] == 0x01 && buf[6] == 0x38) {
557 parsed = SNDRV_MIDI_SYSEX_GS_CHORUS_MODE;
558 chset->gs_chorus_mode = buf[7];
560 } else if (buf[5] == 0x00 && buf[6] == 0x04) {
562 parsed = SNDRV_MIDI_SYSEX_GS_MASTER_VOLUME;
563 chset->gs_master_volume = buf[7];
569 else if (len >= (int)sizeof(xg_on_macro) &&
570 memcmp(buf, xg_on_macro, sizeof(xg_on_macro)) == 0) {
572 chset->midi_mode = SNDRV_MIDI_MODE_XG;
573 parsed = SNDRV_MIDI_SYSEX_XG_ON;
574 /* reset CC#0 for drums */
575 for (i = 0; i < chset->max_channels; i++) {
576 if (chset->channels[i].drum_channel)
577 chset->channels[i].control[MIDI_CTL_MSB_BANK] = 127;
579 chset->channels[i].control[MIDI_CTL_MSB_BANK] = 0;
584 ops->sysex(private, buf - 1, len + 1, parsed, chset);
591 all_sounds_off(snd_midi_op_t *ops, void *drv, snd_midi_channel_t *chan)
595 if (! ops->note_terminate)
597 for (n = 0; n < 128; n++) {
599 ops->note_terminate(drv, n, chan);
609 all_notes_off(snd_midi_op_t *ops, void *drv, snd_midi_channel_t *chan)
615 for (n = 0; n < 128; n++) {
616 if (chan->note[n] == SNDRV_MIDI_NOTE_ON)
617 note_off(ops, drv, chan, n, 0);
622 * Initialise a single midi channel control block.
624 static void snd_midi_channel_init(snd_midi_channel_t *p, int n)
629 memset(p, 0, sizeof(snd_midi_channel_t));
633 snd_midi_reset_controllers(p);
634 p->gm_rpn_pitch_bend_range = 256; /* 2 semitones */
635 p->gm_rpn_fine_tuning = 0;
636 p->gm_rpn_coarse_tuning = 0;
639 p->drum_channel = 1; /* Default ch 10 as drums */
643 * Allocate and initialise a set of midi channel control blocks.
645 static snd_midi_channel_t *snd_midi_channel_init_set(int n)
647 snd_midi_channel_t *chan;
650 chan = kmalloc(n * sizeof(snd_midi_channel_t), GFP_KERNEL);
652 for (i = 0; i < n; i++)
653 snd_midi_channel_init(chan+i, i);
660 * reset all midi channels
663 reset_all_channels(snd_midi_channel_set_t *chset)
666 for (ch = 0; ch < chset->max_channels; ch++) {
667 snd_midi_channel_t *chan = chset->channels + ch;
668 snd_midi_reset_controllers(chan);
669 chan->gm_rpn_pitch_bend_range = 256; /* 2 semitones */
670 chan->gm_rpn_fine_tuning = 0;
671 chan->gm_rpn_coarse_tuning = 0;
674 chan->drum_channel = 1;
676 chan->drum_channel = 0;
682 * Allocate and initialise a midi channel set.
684 snd_midi_channel_set_t *snd_midi_channel_alloc_set(int n)
686 snd_midi_channel_set_t *chset;
688 chset = kmalloc(sizeof(*chset), GFP_KERNEL);
690 chset->channels = snd_midi_channel_init_set(n);
691 chset->private_data = NULL;
692 chset->max_channels = n;
698 * Reset the midi controllers on a particular channel to default values.
700 static void snd_midi_reset_controllers(snd_midi_channel_t *chan)
702 memset(chan->control, 0, sizeof(chan->control));
703 chan->gm_volume = 127;
704 chan->gm_expression = 127;
710 * Free a midi channel set.
712 void snd_midi_channel_free_set(snd_midi_channel_set_t *chset)
716 kfree(chset->channels);
720 static int __init alsa_seq_midi_emul_init(void)
725 static void __exit alsa_seq_midi_emul_exit(void)
729 module_init(alsa_seq_midi_emul_init)
730 module_exit(alsa_seq_midi_emul_exit)
732 EXPORT_SYMBOL(snd_midi_process_event);
733 EXPORT_SYMBOL(snd_midi_channel_set_clear);
734 EXPORT_SYMBOL(snd_midi_channel_alloc_set);
735 EXPORT_SYMBOL(snd_midi_channel_free_set);