1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * GM/GS/XG midi module.
5 * Copyright (C) 1999 Steve Ratcliffe
7 * Based on awe_wave.c by Takashi Iwai
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.
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.
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>
28 MODULE_AUTHOR("Takashi Iwai / Steve Ratcliffe");
29 MODULE_DESCRIPTION("Advanced Linux Sound Architecture sequencer MIDI emulation.");
30 MODULE_LICENSE("GPL");
32 /* Prototypes for static functions */
33 static void note_off(const struct snd_midi_op *ops, void *drv,
34 struct snd_midi_channel *chan,
36 static void do_control(const 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);
40 static void rpn(const struct snd_midi_op *ops, void *drv,
41 struct snd_midi_channel *chan,
42 struct snd_midi_channel_set *chset);
43 static void nrpn(const struct snd_midi_op *ops, void *drv,
44 struct snd_midi_channel *chan,
45 struct snd_midi_channel_set *chset);
46 static void sysex(const struct snd_midi_op *ops, void *private,
48 int len, struct snd_midi_channel_set *chset);
49 static void all_sounds_off(const struct snd_midi_op *ops, void *private,
50 struct snd_midi_channel *chan);
51 static void all_notes_off(const struct snd_midi_op *ops, void *private,
52 struct snd_midi_channel *chan);
53 static void snd_midi_reset_controllers(struct snd_midi_channel *chan);
54 static void reset_all_channels(struct snd_midi_channel_set *chset);
58 * Process an event in a driver independent way. This means dealing
59 * with RPN, NRPN, SysEx etc that are defined for common midi applications
60 * such as GM, GS and XG.
61 * There modes that this module will run in are:
62 * Generic MIDI - no interpretation at all, it will just save current values
64 * GM - You can use all gm_ prefixed elements of chan. Controls, RPN, NRPN,
65 * SysEx will be interpreded as defined in General Midi.
66 * GS - You can use all gs_ prefixed elements of chan. Codes for GS will be
68 * XG - You can use all xg_ prefixed elements of chan. Codes for XG will
72 snd_midi_process_event(const struct snd_midi_op *ops,
73 struct snd_seq_event *ev,
74 struct snd_midi_channel_set *chanset)
76 struct snd_midi_channel *chan;
80 if (ev == NULL || chanset == NULL) {
81 pr_debug("ALSA: seq_midi_emul: ev or chanbase NULL (snd_midi_process_event)\n");
84 if (chanset->channels == NULL)
87 if (snd_seq_ev_is_channel_type(ev)) {
88 dest_channel = ev->data.note.channel;
89 if (dest_channel >= chanset->max_channels) {
90 pr_debug("ALSA: seq_midi_emul: dest channel is %d, max is %d\n",
91 dest_channel, chanset->max_channels);
96 chan = chanset->channels + dest_channel;
97 drv = chanset->private_data;
99 /* EVENT_NOTE should be processed before queued */
100 if (ev->type == SNDRV_SEQ_EVENT_NOTE)
103 /* Make sure that we don't have a note on that should really be
105 if (ev->type == SNDRV_SEQ_EVENT_NOTEON && ev->data.note.velocity == 0)
106 ev->type = SNDRV_SEQ_EVENT_NOTEOFF;
108 /* Make sure the note is within array range */
109 if (ev->type == SNDRV_SEQ_EVENT_NOTEON ||
110 ev->type == SNDRV_SEQ_EVENT_NOTEOFF ||
111 ev->type == SNDRV_SEQ_EVENT_KEYPRESS) {
112 if (ev->data.note.note >= 128)
117 case SNDRV_SEQ_EVENT_NOTEON:
118 if (chan->note[ev->data.note.note] & SNDRV_MIDI_NOTE_ON) {
120 ops->note_off(drv, ev->data.note.note, 0, chan);
122 chan->note[ev->data.note.note] = SNDRV_MIDI_NOTE_ON;
124 ops->note_on(drv, ev->data.note.note, ev->data.note.velocity, chan);
126 case SNDRV_SEQ_EVENT_NOTEOFF:
127 if (! (chan->note[ev->data.note.note] & SNDRV_MIDI_NOTE_ON))
130 note_off(ops, drv, chan, ev->data.note.note, ev->data.note.velocity);
132 case SNDRV_SEQ_EVENT_KEYPRESS:
134 ops->key_press(drv, ev->data.note.note, ev->data.note.velocity, chan);
136 case SNDRV_SEQ_EVENT_CONTROLLER:
137 do_control(ops, drv, chanset, chan,
138 ev->data.control.param, ev->data.control.value);
140 case SNDRV_SEQ_EVENT_PGMCHANGE:
141 chan->midi_program = ev->data.control.value;
143 case SNDRV_SEQ_EVENT_PITCHBEND:
144 chan->midi_pitchbend = ev->data.control.value;
146 ops->control(drv, MIDI_CTL_PITCHBEND, chan);
148 case SNDRV_SEQ_EVENT_CHANPRESS:
149 chan->midi_pressure = ev->data.control.value;
151 ops->control(drv, MIDI_CTL_CHAN_PRESSURE, chan);
153 case SNDRV_SEQ_EVENT_CONTROL14:
154 /* Best guess is that this is any of the 14 bit controller values */
155 if (ev->data.control.param < 32) {
156 /* set low part first */
157 chan->control[ev->data.control.param + 32] =
158 ev->data.control.value & 0x7f;
159 do_control(ops, drv, chanset, chan,
160 ev->data.control.param,
161 ((ev->data.control.value>>7) & 0x7f));
163 do_control(ops, drv, chanset, chan,
164 ev->data.control.param,
165 ev->data.control.value);
167 case SNDRV_SEQ_EVENT_NONREGPARAM:
168 /* Break it back into its controller values */
169 chan->param_type = SNDRV_MIDI_PARAM_TYPE_NONREGISTERED;
170 chan->control[MIDI_CTL_MSB_DATA_ENTRY]
171 = (ev->data.control.value >> 7) & 0x7f;
172 chan->control[MIDI_CTL_LSB_DATA_ENTRY]
173 = ev->data.control.value & 0x7f;
174 chan->control[MIDI_CTL_NONREG_PARM_NUM_MSB]
175 = (ev->data.control.param >> 7) & 0x7f;
176 chan->control[MIDI_CTL_NONREG_PARM_NUM_LSB]
177 = ev->data.control.param & 0x7f;
178 nrpn(ops, drv, chan, chanset);
180 case SNDRV_SEQ_EVENT_REGPARAM:
181 /* Break it back into its controller values */
182 chan->param_type = SNDRV_MIDI_PARAM_TYPE_REGISTERED;
183 chan->control[MIDI_CTL_MSB_DATA_ENTRY]
184 = (ev->data.control.value >> 7) & 0x7f;
185 chan->control[MIDI_CTL_LSB_DATA_ENTRY]
186 = ev->data.control.value & 0x7f;
187 chan->control[MIDI_CTL_REGIST_PARM_NUM_MSB]
188 = (ev->data.control.param >> 7) & 0x7f;
189 chan->control[MIDI_CTL_REGIST_PARM_NUM_LSB]
190 = ev->data.control.param & 0x7f;
191 rpn(ops, drv, chan, chanset);
193 case SNDRV_SEQ_EVENT_SYSEX:
194 if ((ev->flags & SNDRV_SEQ_EVENT_LENGTH_MASK) == SNDRV_SEQ_EVENT_LENGTH_VARIABLE) {
195 unsigned char sysexbuf[64];
197 len = snd_seq_expand_var_event(ev, sizeof(sysexbuf), sysexbuf, 1, 0);
199 sysex(ops, drv, sysexbuf, len, chanset);
202 case SNDRV_SEQ_EVENT_SONGPOS:
203 case SNDRV_SEQ_EVENT_SONGSEL:
204 case SNDRV_SEQ_EVENT_CLOCK:
205 case SNDRV_SEQ_EVENT_START:
206 case SNDRV_SEQ_EVENT_CONTINUE:
207 case SNDRV_SEQ_EVENT_STOP:
208 case SNDRV_SEQ_EVENT_QFRAME:
209 case SNDRV_SEQ_EVENT_TEMPO:
210 case SNDRV_SEQ_EVENT_TIMESIGN:
211 case SNDRV_SEQ_EVENT_KEYSIGN:
213 case SNDRV_SEQ_EVENT_SENSING:
215 case SNDRV_SEQ_EVENT_CLIENT_START:
216 case SNDRV_SEQ_EVENT_CLIENT_EXIT:
217 case SNDRV_SEQ_EVENT_CLIENT_CHANGE:
218 case SNDRV_SEQ_EVENT_PORT_START:
219 case SNDRV_SEQ_EVENT_PORT_EXIT:
220 case SNDRV_SEQ_EVENT_PORT_CHANGE:
221 case SNDRV_SEQ_EVENT_ECHO:
224 /*pr_debug("ALSA: seq_midi_emul: Unimplemented event %d\n", ev->type);*/
228 EXPORT_SYMBOL(snd_midi_process_event);
235 note_off(const struct snd_midi_op *ops, void *drv,
236 struct snd_midi_channel *chan,
240 /* Hold this note until pedal is turned off */
241 chan->note[note] |= SNDRV_MIDI_NOTE_RELEASED;
242 } else if (chan->note[note] & SNDRV_MIDI_NOTE_SOSTENUTO) {
243 /* Mark this note as release; it will be turned off when sostenuto
245 chan->note[note] |= SNDRV_MIDI_NOTE_RELEASED;
247 chan->note[note] = 0;
249 ops->note_off(drv, note, vel, chan);
254 * Do all driver independent operations for this controller and pass
255 * events that need to take place immediately to the driver.
258 do_control(const struct snd_midi_op *ops, void *drv,
259 struct snd_midi_channel_set *chset,
260 struct snd_midi_channel *chan, int control, int value)
264 if (control >= ARRAY_SIZE(chan->control))
268 if ((control >=64 && control <=69) || (control >= 80 && control <= 83)) {
269 /* These are all switches; either off or on so set to 0 or 127 */
270 value = (value >= 64)? 127: 0;
272 chan->control[control] = value;
275 case MIDI_CTL_SUSTAIN:
277 /* Sustain has been released, turn off held notes */
278 for (i = 0; i < 128; i++) {
279 if (chan->note[i] & SNDRV_MIDI_NOTE_RELEASED) {
280 chan->note[i] = SNDRV_MIDI_NOTE_OFF;
282 ops->note_off(drv, i, 0, chan);
287 case MIDI_CTL_PORTAMENTO:
289 case MIDI_CTL_SOSTENUTO:
291 /* Mark each note that is currently held down */
292 for (i = 0; i < 128; i++) {
293 if (chan->note[i] & SNDRV_MIDI_NOTE_ON)
294 chan->note[i] |= SNDRV_MIDI_NOTE_SOSTENUTO;
297 /* release all notes that were held */
298 for (i = 0; i < 128; i++) {
299 if (chan->note[i] & SNDRV_MIDI_NOTE_SOSTENUTO) {
300 chan->note[i] &= ~SNDRV_MIDI_NOTE_SOSTENUTO;
301 if (chan->note[i] & SNDRV_MIDI_NOTE_RELEASED) {
302 chan->note[i] = SNDRV_MIDI_NOTE_OFF;
304 ops->note_off(drv, i, 0, chan);
310 case MIDI_CTL_MSB_DATA_ENTRY:
311 chan->control[MIDI_CTL_LSB_DATA_ENTRY] = 0;
313 case MIDI_CTL_LSB_DATA_ENTRY:
314 if (chan->param_type == SNDRV_MIDI_PARAM_TYPE_REGISTERED)
315 rpn(ops, drv, chan, chset);
317 nrpn(ops, drv, chan, chset);
319 case MIDI_CTL_REGIST_PARM_NUM_LSB:
320 case MIDI_CTL_REGIST_PARM_NUM_MSB:
321 chan->param_type = SNDRV_MIDI_PARAM_TYPE_REGISTERED;
323 case MIDI_CTL_NONREG_PARM_NUM_LSB:
324 case MIDI_CTL_NONREG_PARM_NUM_MSB:
325 chan->param_type = SNDRV_MIDI_PARAM_TYPE_NONREGISTERED;
328 case MIDI_CTL_ALL_SOUNDS_OFF:
329 all_sounds_off(ops, drv, chan);
332 case MIDI_CTL_ALL_NOTES_OFF:
333 all_notes_off(ops, drv, chan);
336 case MIDI_CTL_MSB_BANK:
337 if (chset->midi_mode == SNDRV_MIDI_MODE_XG) {
339 chan->drum_channel = 1;
341 chan->drum_channel = 0;
344 case MIDI_CTL_LSB_BANK:
347 case MIDI_CTL_RESET_CONTROLLERS:
348 snd_midi_reset_controllers(chan);
351 case MIDI_CTL_SOFT_PEDAL:
352 case MIDI_CTL_LEGATO_FOOTSWITCH:
354 case MIDI_CTL_SC1_SOUND_VARIATION:
355 case MIDI_CTL_SC2_TIMBRE:
356 case MIDI_CTL_SC3_RELEASE_TIME:
357 case MIDI_CTL_SC4_ATTACK_TIME:
358 case MIDI_CTL_SC5_BRIGHTNESS:
359 case MIDI_CTL_E1_REVERB_DEPTH:
360 case MIDI_CTL_E2_TREMOLO_DEPTH:
361 case MIDI_CTL_E3_CHORUS_DEPTH:
362 case MIDI_CTL_E4_DETUNE_DEPTH:
363 case MIDI_CTL_E5_PHASER_DEPTH:
368 ops->control(drv, control, chan);
375 * initialize the MIDI status
378 snd_midi_channel_set_clear(struct snd_midi_channel_set *chset)
382 chset->midi_mode = SNDRV_MIDI_MODE_GM;
383 chset->gs_master_volume = 127;
385 for (i = 0; i < chset->max_channels; i++) {
386 struct snd_midi_channel *chan = chset->channels + i;
387 memset(chan->note, 0, sizeof(chan->note));
389 chan->midi_aftertouch = 0;
390 chan->midi_pressure = 0;
391 chan->midi_program = 0;
392 chan->midi_pitchbend = 0;
393 snd_midi_reset_controllers(chan);
394 chan->gm_rpn_pitch_bend_range = 256; /* 2 semitones */
395 chan->gm_rpn_fine_tuning = 0;
396 chan->gm_rpn_coarse_tuning = 0;
399 chan->drum_channel = 1;
401 chan->drum_channel = 0;
404 EXPORT_SYMBOL(snd_midi_channel_set_clear);
407 * Process a rpn message.
410 rpn(const struct snd_midi_op *ops, void *drv, struct snd_midi_channel *chan,
411 struct snd_midi_channel_set *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(const struct snd_midi_op *ops, void *drv, struct snd_midi_channel *chan,
451 struct snd_midi_channel_set *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(const struct snd_midi_op *ops, void *private, unsigned char *buf, int len,
479 struct snd_midi_channel_set *chset)
482 static const unsigned char gm_on_macro[] = {
486 static const unsigned char xg_on_macro[] = {
487 0x43,0x10,0x4c,0x00,0x00,0x7e,0x00,
490 * drum channel: XX=0x1?(channel), YY=0x15, ZZ=on/off
491 * reverb mode: XX=0x01, YY=0x30, ZZ=0-7
492 * chorus mode: XX=0x01, YY=0x38, ZZ=0-7
493 * master vol: XX=0x00, YY=0x04, ZZ=0-127
495 static const unsigned char gs_pfx_macro[] = {
496 0x41,0x10,0x42,0x12,0x40,/*XX,YY,ZZ*/
499 int parsed = SNDRV_MIDI_SYSEX_NOT_PARSED;
501 if (len <= 0 || buf[0] != 0xf0)
503 /* skip first byte */
508 if (len >= (int)sizeof(gm_on_macro) &&
509 memcmp(buf, gm_on_macro, sizeof(gm_on_macro)) == 0) {
510 if (chset->midi_mode != SNDRV_MIDI_MODE_GS &&
511 chset->midi_mode != SNDRV_MIDI_MODE_XG) {
512 chset->midi_mode = SNDRV_MIDI_MODE_GM;
513 reset_all_channels(chset);
514 parsed = SNDRV_MIDI_SYSEX_GM_ON;
520 memcmp(buf, gs_pfx_macro, sizeof(gs_pfx_macro)) == 0) {
521 if (chset->midi_mode != SNDRV_MIDI_MODE_GS &&
522 chset->midi_mode != SNDRV_MIDI_MODE_XG)
523 chset->midi_mode = SNDRV_MIDI_MODE_GS;
525 if (buf[5] == 0x00 && buf[6] == 0x7f && buf[7] == 0x00) {
527 parsed = SNDRV_MIDI_SYSEX_GS_RESET;
528 reset_all_channels(chset);
531 else if ((buf[5] & 0xf0) == 0x10 && buf[6] == 0x15) {
533 int p = get_channel(buf[5]);
534 if (p < chset->max_channels) {
535 parsed = SNDRV_MIDI_SYSEX_GS_DRUM_CHANNEL;
537 chset->channels[p].drum_channel = 1;
539 chset->channels[p].drum_channel = 0;
542 } else if ((buf[5] & 0xf0) == 0x10 && buf[6] == 0x21) {
544 int p = get_channel(buf[5]);
545 if (p < chset->max_channels &&
546 ! chset->channels[p].drum_channel) {
547 parsed = SNDRV_MIDI_SYSEX_GS_DRUM_CHANNEL;
548 chset->channels[p].midi_program = buf[7];
551 } else if (buf[5] == 0x01 && buf[6] == 0x30) {
553 parsed = SNDRV_MIDI_SYSEX_GS_REVERB_MODE;
554 chset->gs_reverb_mode = buf[7];
556 } else if (buf[5] == 0x01 && buf[6] == 0x38) {
558 parsed = SNDRV_MIDI_SYSEX_GS_CHORUS_MODE;
559 chset->gs_chorus_mode = buf[7];
561 } else if (buf[5] == 0x00 && buf[6] == 0x04) {
563 parsed = SNDRV_MIDI_SYSEX_GS_MASTER_VOLUME;
564 chset->gs_master_volume = buf[7];
570 else if (len >= (int)sizeof(xg_on_macro) &&
571 memcmp(buf, xg_on_macro, sizeof(xg_on_macro)) == 0) {
573 chset->midi_mode = SNDRV_MIDI_MODE_XG;
574 parsed = SNDRV_MIDI_SYSEX_XG_ON;
575 /* reset CC#0 for drums */
576 for (i = 0; i < chset->max_channels; i++) {
577 if (chset->channels[i].drum_channel)
578 chset->channels[i].control[MIDI_CTL_MSB_BANK] = 127;
580 chset->channels[i].control[MIDI_CTL_MSB_BANK] = 0;
585 ops->sysex(private, buf - 1, len + 1, parsed, chset);
592 all_sounds_off(const struct snd_midi_op *ops, void *drv,
593 struct snd_midi_channel *chan)
597 if (! ops->note_terminate)
599 for (n = 0; n < 128; n++) {
601 ops->note_terminate(drv, n, chan);
611 all_notes_off(const struct snd_midi_op *ops, void *drv,
612 struct snd_midi_channel *chan)
618 for (n = 0; n < 128; n++) {
619 if (chan->note[n] == SNDRV_MIDI_NOTE_ON)
620 note_off(ops, drv, chan, n, 0);
625 * Initialise a single midi channel control block.
627 static void snd_midi_channel_init(struct snd_midi_channel *p, int n)
632 memset(p, 0, sizeof(struct snd_midi_channel));
636 snd_midi_reset_controllers(p);
637 p->gm_rpn_pitch_bend_range = 256; /* 2 semitones */
638 p->gm_rpn_fine_tuning = 0;
639 p->gm_rpn_coarse_tuning = 0;
642 p->drum_channel = 1; /* Default ch 10 as drums */
646 * Allocate and initialise a set of midi channel control blocks.
648 static struct snd_midi_channel *snd_midi_channel_init_set(int n)
650 struct snd_midi_channel *chan;
653 chan = kmalloc_array(n, sizeof(struct snd_midi_channel), GFP_KERNEL);
655 for (i = 0; i < n; i++)
656 snd_midi_channel_init(chan+i, i);
663 * reset all midi channels
666 reset_all_channels(struct snd_midi_channel_set *chset)
669 for (ch = 0; ch < chset->max_channels; ch++) {
670 struct snd_midi_channel *chan = chset->channels + ch;
671 snd_midi_reset_controllers(chan);
672 chan->gm_rpn_pitch_bend_range = 256; /* 2 semitones */
673 chan->gm_rpn_fine_tuning = 0;
674 chan->gm_rpn_coarse_tuning = 0;
677 chan->drum_channel = 1;
679 chan->drum_channel = 0;
685 * Allocate and initialise a midi channel set.
687 struct snd_midi_channel_set *snd_midi_channel_alloc_set(int n)
689 struct snd_midi_channel_set *chset;
691 chset = kmalloc(sizeof(*chset), GFP_KERNEL);
693 chset->channels = snd_midi_channel_init_set(n);
694 chset->private_data = NULL;
695 chset->max_channels = n;
699 EXPORT_SYMBOL(snd_midi_channel_alloc_set);
702 * Reset the midi controllers on a particular channel to default values.
704 static void snd_midi_reset_controllers(struct snd_midi_channel *chan)
706 memset(chan->control, 0, sizeof(chan->control));
707 chan->gm_volume = 127;
708 chan->gm_expression = 127;
714 * Free a midi channel set.
716 void snd_midi_channel_free_set(struct snd_midi_channel_set *chset)
720 kfree(chset->channels);
723 EXPORT_SYMBOL(snd_midi_channel_free_set);