linux/sound/core/seq/seq_midi_emul.c
<<
>>
Prefs
   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
  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                snd_printd("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                        snd_printd("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_SAMPLE:
 233        case SNDRV_SEQ_EVENT_SAMPLE_START:
 234        case SNDRV_SEQ_EVENT_SAMPLE_STOP:
 235        case SNDRV_SEQ_EVENT_SAMPLE_FREQ:
 236        case SNDRV_SEQ_EVENT_SAMPLE_VOLUME:
 237        case SNDRV_SEQ_EVENT_SAMPLE_LOOP:
 238        case SNDRV_SEQ_EVENT_SAMPLE_POSITION:
 239        case SNDRV_SEQ_EVENT_ECHO:
 240        not_yet:
 241        default:
 242                /*snd_printd("Unimplemented event %d\n", ev->type);*/
 243                break;
 244        }
 245}
 246
 247
 248/*
 249 * release note
 250 */
 251static void
 252note_off(struct snd_midi_op *ops, void *drv, struct snd_midi_channel *chan,
 253         int note, int vel)
 254{
 255        if (chan->gm_hold) {
 256                /* Hold this note until pedal is turned off */
 257                chan->note[note] |= SNDRV_MIDI_NOTE_RELEASED;
 258        } else if (chan->note[note] & SNDRV_MIDI_NOTE_SOSTENUTO) {
 259                /* Mark this note as release; it will be turned off when sostenuto
 260                 * is turned off */
 261                chan->note[note] |= SNDRV_MIDI_NOTE_RELEASED;
 262        } else {
 263                chan->note[note] = 0;
 264                if (ops->note_off)
 265                        ops->note_off(drv, note, vel, chan);
 266        }
 267}
 268
 269/*
 270 * Do all driver independent operations for this controller and pass
 271 * events that need to take place immediately to the driver.
 272 */
 273static void
 274do_control(struct snd_midi_op *ops, void *drv, struct snd_midi_channel_set *chset,
 275           struct snd_midi_channel *chan, int control, int value)
 276{
 277        int  i;
 278
 279        /* Switches */
 280        if ((control >=64 && control <=69) || (control >= 80 && control <= 83)) {
 281                /* These are all switches; either off or on so set to 0 or 127 */
 282                value = (value >= 64)? 127: 0;
 283        }
 284        chan->control[control] = value;
 285
 286        switch (control) {
 287        case MIDI_CTL_SUSTAIN:
 288                if (value == 0) {
 289                        /* Sustain has been released, turn off held notes */
 290                        for (i = 0; i < 128; i++) {
 291                                if (chan->note[i] & SNDRV_MIDI_NOTE_RELEASED) {
 292                                        chan->note[i] = SNDRV_MIDI_NOTE_OFF;
 293                                        if (ops->note_off)
 294                                                ops->note_off(drv, i, 0, chan);
 295                                }
 296                        }
 297                }
 298                break;
 299        case MIDI_CTL_PORTAMENTO:
 300                break;
 301        case MIDI_CTL_SOSTENUTO:
 302                if (value) {
 303                        /* Mark each note that is currently held down */
 304                        for (i = 0; i < 128; i++) {
 305                                if (chan->note[i] & SNDRV_MIDI_NOTE_ON)
 306                                        chan->note[i] |= SNDRV_MIDI_NOTE_SOSTENUTO;
 307                        }
 308                } else {
 309                        /* release all notes that were held */
 310                        for (i = 0; i < 128; i++) {
 311                                if (chan->note[i] & SNDRV_MIDI_NOTE_SOSTENUTO) {
 312                                        chan->note[i] &= ~SNDRV_MIDI_NOTE_SOSTENUTO;
 313                                        if (chan->note[i] & SNDRV_MIDI_NOTE_RELEASED) {
 314                                                chan->note[i] = SNDRV_MIDI_NOTE_OFF;
 315                                                if (ops->note_off)
 316                                                        ops->note_off(drv, i, 0, chan);
 317                                        }
 318                                }
 319                        }
 320                }
 321                break;
 322        case MIDI_CTL_MSB_DATA_ENTRY:
 323                chan->control[MIDI_CTL_LSB_DATA_ENTRY] = 0;
 324                /* go through here */
 325        case MIDI_CTL_LSB_DATA_ENTRY:
 326                if (chan->param_type == SNDRV_MIDI_PARAM_TYPE_REGISTERED)
 327                        rpn(ops, drv, chan, chset);
 328                else
 329                        nrpn(ops, drv, chan, chset);
 330                break;
 331        case MIDI_CTL_REGIST_PARM_NUM_LSB:
 332        case MIDI_CTL_REGIST_PARM_NUM_MSB:
 333                chan->param_type = SNDRV_MIDI_PARAM_TYPE_REGISTERED;
 334                break;
 335        case MIDI_CTL_NONREG_PARM_NUM_LSB:
 336        case MIDI_CTL_NONREG_PARM_NUM_MSB:
 337                chan->param_type = SNDRV_MIDI_PARAM_TYPE_NONREGISTERED;
 338                break;
 339
 340        case MIDI_CTL_ALL_SOUNDS_OFF:
 341                all_sounds_off(ops, drv, chan);
 342                break;
 343
 344        case MIDI_CTL_ALL_NOTES_OFF:
 345                all_notes_off(ops, drv, chan);
 346                break;
 347
 348        case MIDI_CTL_MSB_BANK:
 349                if (chset->midi_mode == SNDRV_MIDI_MODE_XG) {
 350                        if (value == 127)
 351                                chan->drum_channel = 1;
 352                        else
 353                                chan->drum_channel = 0;
 354                }
 355                break;
 356        case MIDI_CTL_LSB_BANK:
 357                break;
 358
 359        case MIDI_CTL_RESET_CONTROLLERS:
 360                snd_midi_reset_controllers(chan);
 361                break;
 362
 363        case MIDI_CTL_SOFT_PEDAL:
 364        case MIDI_CTL_LEGATO_FOOTSWITCH:
 365        case MIDI_CTL_HOLD2:
 366        case MIDI_CTL_SC1_SOUND_VARIATION:
 367        case MIDI_CTL_SC2_TIMBRE:
 368        case MIDI_CTL_SC3_RELEASE_TIME:
 369        case MIDI_CTL_SC4_ATTACK_TIME:
 370        case MIDI_CTL_SC5_BRIGHTNESS:
 371        case MIDI_CTL_E1_REVERB_DEPTH:
 372        case MIDI_CTL_E2_TREMOLO_DEPTH:
 373        case MIDI_CTL_E3_CHORUS_DEPTH:
 374        case MIDI_CTL_E4_DETUNE_DEPTH:
 375        case MIDI_CTL_E5_PHASER_DEPTH:
 376                goto notyet;
 377        notyet:
 378        default:
 379                if (ops->control)
 380                        ops->control(drv, control, chan);
 381                break;
 382        }
 383}
 384
 385
 386/*
 387 * initialize the MIDI status
 388 */
 389void
 390snd_midi_channel_set_clear(struct snd_midi_channel_set *chset)
 391{
 392        int i;
 393
 394        chset->midi_mode = SNDRV_MIDI_MODE_GM;
 395        chset->gs_master_volume = 127;
 396
 397        for (i = 0; i < chset->max_channels; i++) {
 398                struct snd_midi_channel *chan = chset->channels + i;
 399                memset(chan->note, 0, sizeof(chan->note));
 400
 401                chan->midi_aftertouch = 0;
 402                chan->midi_pressure = 0;
 403                chan->midi_program = 0;
 404                chan->midi_pitchbend = 0;
 405                snd_midi_reset_controllers(chan);
 406                chan->gm_rpn_pitch_bend_range = 256; /* 2 semitones */
 407                chan->gm_rpn_fine_tuning = 0;
 408                chan->gm_rpn_coarse_tuning = 0;
 409
 410                if (i == 9)
 411                        chan->drum_channel = 1;
 412                else
 413                        chan->drum_channel = 0;
 414        }
 415}
 416
 417/*
 418 * Process a rpn message.
 419 */
 420static void
 421rpn(struct snd_midi_op *ops, void *drv, struct snd_midi_channel *chan,
 422    struct snd_midi_channel_set *chset)
 423{
 424        int type;
 425        int val;
 426
 427        if (chset->midi_mode != SNDRV_MIDI_MODE_NONE) {
 428                type = (chan->control[MIDI_CTL_REGIST_PARM_NUM_MSB] << 8) |
 429                        chan->control[MIDI_CTL_REGIST_PARM_NUM_LSB];
 430                val = (chan->control[MIDI_CTL_MSB_DATA_ENTRY] << 7) |
 431                        chan->control[MIDI_CTL_LSB_DATA_ENTRY];
 432
 433                switch (type) {
 434                case 0x0000: /* Pitch bend sensitivity */
 435                        /* MSB only / 1 semitone per 128 */
 436                        chan->gm_rpn_pitch_bend_range = val;
 437                        break;
 438                                        
 439                case 0x0001: /* fine tuning: */
 440                        /* MSB/LSB, 8192=center, 100/8192 cent step */
 441                        chan->gm_rpn_fine_tuning = val - 8192;
 442                        break;
 443
 444                case 0x0002: /* coarse tuning */
 445                        /* MSB only / 8192=center, 1 semitone per 128 */
 446                        chan->gm_rpn_coarse_tuning = val - 8192;
 447                        break;
 448
 449                case 0x7F7F: /* "lock-in" RPN */
 450                        /* ignored */
 451                        break;
 452                }
 453        }
 454        /* should call nrpn or rpn callback here.. */
 455}
 456
 457/*
 458 * Process an nrpn message.
 459 */
 460static void
 461nrpn(struct snd_midi_op *ops, void *drv, struct snd_midi_channel *chan,
 462     struct snd_midi_channel_set *chset)
 463{
 464        /* parse XG NRPNs here if possible */
 465        if (ops->nrpn)
 466                ops->nrpn(drv, chan, chset);
 467}
 468
 469
 470/*
 471 * convert channel parameter in GS sysex
 472 */
 473static int
 474get_channel(unsigned char cmd)
 475{
 476        int p = cmd & 0x0f;
 477        if (p == 0)
 478                p = 9;
 479        else if (p < 10)
 480                p--;
 481        return p;
 482}
 483
 484
 485/*
 486 * Process a sysex message.
 487 */
 488static void
 489sysex(struct snd_midi_op *ops, void *private, unsigned char *buf, int len,
 490      struct snd_midi_channel_set *chset)
 491{
 492        /* GM on */
 493        static unsigned char gm_on_macro[] = {
 494                0x7e,0x7f,0x09,0x01,
 495        };
 496        /* XG on */
 497        static unsigned char xg_on_macro[] = {
 498                0x43,0x10,0x4c,0x00,0x00,0x7e,0x00,
 499        };
 500        /* GS prefix
 501         * drum channel: XX=0x1?(channel), YY=0x15, ZZ=on/off
 502         * reverb mode: XX=0x01, YY=0x30, ZZ=0-7
 503         * chorus mode: XX=0x01, YY=0x38, ZZ=0-7
 504         * master vol:  XX=0x00, YY=0x04, ZZ=0-127
 505         */
 506        static unsigned char gs_pfx_macro[] = {
 507                0x41,0x10,0x42,0x12,0x40,/*XX,YY,ZZ*/
 508        };
 509
 510        int parsed = SNDRV_MIDI_SYSEX_NOT_PARSED;
 511
 512        if (len <= 0 || buf[0] != 0xf0)
 513                return;
 514        /* skip first byte */
 515        buf++;
 516        len--;
 517
 518        /* GM on */
 519        if (len >= (int)sizeof(gm_on_macro) &&
 520            memcmp(buf, gm_on_macro, sizeof(gm_on_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_GM;
 524                        reset_all_channels(chset);
 525                        parsed = SNDRV_MIDI_SYSEX_GM_ON;
 526                }
 527        }
 528
 529        /* GS macros */
 530        else if (len >= 8 &&
 531                 memcmp(buf, gs_pfx_macro, sizeof(gs_pfx_macro)) == 0) {
 532                if (chset->midi_mode != SNDRV_MIDI_MODE_GS &&
 533                    chset->midi_mode != SNDRV_MIDI_MODE_XG)
 534                        chset->midi_mode = SNDRV_MIDI_MODE_GS;
 535
 536                if (buf[5] == 0x00 && buf[6] == 0x7f && buf[7] == 0x00) {
 537                        /* GS reset */
 538                        parsed = SNDRV_MIDI_SYSEX_GS_RESET;
 539                        reset_all_channels(chset);
 540                }
 541
 542                else if ((buf[5] & 0xf0) == 0x10 && buf[6] == 0x15) {
 543                        /* drum pattern */
 544                        int p = get_channel(buf[5]);
 545                        if (p < chset->max_channels) {
 546                                parsed = SNDRV_MIDI_SYSEX_GS_DRUM_CHANNEL;
 547                                if (buf[7])
 548                                        chset->channels[p].drum_channel = 1;
 549                                else
 550                                        chset->channels[p].drum_channel = 0;
 551                        }
 552
 553                } else if ((buf[5] & 0xf0) == 0x10 && buf[6] == 0x21) {
 554                        /* program */
 555                        int p = get_channel(buf[5]);
 556                        if (p < chset->max_channels &&
 557                            ! chset->channels[p].drum_channel) {
 558                                parsed = SNDRV_MIDI_SYSEX_GS_DRUM_CHANNEL;
 559                                chset->channels[p].midi_program = buf[7];
 560                        }
 561
 562                } else if (buf[5] == 0x01 && buf[6] == 0x30) {
 563                        /* reverb mode */
 564                        parsed = SNDRV_MIDI_SYSEX_GS_REVERB_MODE;
 565                        chset->gs_reverb_mode = buf[7];
 566
 567                } else if (buf[5] == 0x01 && buf[6] == 0x38) {
 568                        /* chorus mode */
 569                        parsed = SNDRV_MIDI_SYSEX_GS_CHORUS_MODE;
 570                        chset->gs_chorus_mode = buf[7];
 571
 572                } else if (buf[5] == 0x00 && buf[6] == 0x04) {
 573                        /* master volume */
 574                        parsed = SNDRV_MIDI_SYSEX_GS_MASTER_VOLUME;
 575                        chset->gs_master_volume = buf[7];
 576
 577                }
 578        }
 579
 580        /* XG on */
 581        else if (len >= (int)sizeof(xg_on_macro) &&
 582                 memcmp(buf, xg_on_macro, sizeof(xg_on_macro)) == 0) {
 583                int i;
 584                chset->midi_mode = SNDRV_MIDI_MODE_XG;
 585                parsed = SNDRV_MIDI_SYSEX_XG_ON;
 586                /* reset CC#0 for drums */
 587                for (i = 0; i < chset->max_channels; i++) {
 588                        if (chset->channels[i].drum_channel)
 589                                chset->channels[i].control[MIDI_CTL_MSB_BANK] = 127;
 590                        else
 591                                chset->channels[i].control[MIDI_CTL_MSB_BANK] = 0;
 592                }
 593        }
 594
 595        if (ops->sysex)
 596                ops->sysex(private, buf - 1, len + 1, parsed, chset);
 597}
 598
 599/*
 600 * all sound off
 601 */
 602static void
 603all_sounds_off(struct snd_midi_op *ops, void *drv, struct snd_midi_channel *chan)
 604{
 605        int n;
 606
 607        if (! ops->note_terminate)
 608                return;
 609        for (n = 0; n < 128; n++) {
 610                if (chan->note[n]) {
 611                        ops->note_terminate(drv, n, chan);
 612                        chan->note[n] = 0;
 613                }
 614        }
 615}
 616
 617/*
 618 * all notes off
 619 */
 620static void
 621all_notes_off(struct snd_midi_op *ops, void *drv, struct snd_midi_channel *chan)
 622{
 623        int n;
 624
 625        if (! ops->note_off)
 626                return;
 627        for (n = 0; n < 128; n++) {
 628                if (chan->note[n] == SNDRV_MIDI_NOTE_ON)
 629                        note_off(ops, drv, chan, n, 0);
 630        }
 631}
 632
 633/*
 634 * Initialise a single midi channel control block.
 635 */
 636static void snd_midi_channel_init(struct snd_midi_channel *p, int n)
 637{
 638        if (p == NULL)
 639                return;
 640
 641        memset(p, 0, sizeof(struct snd_midi_channel));
 642        p->private = NULL;
 643        p->number = n;
 644
 645        snd_midi_reset_controllers(p);
 646        p->gm_rpn_pitch_bend_range = 256; /* 2 semitones */
 647        p->gm_rpn_fine_tuning = 0;
 648        p->gm_rpn_coarse_tuning = 0;
 649
 650        if (n == 9)
 651                p->drum_channel = 1;    /* Default ch 10 as drums */
 652}
 653
 654/*
 655 * Allocate and initialise a set of midi channel control blocks.
 656 */
 657static struct snd_midi_channel *snd_midi_channel_init_set(int n)
 658{
 659        struct snd_midi_channel *chan;
 660        int  i;
 661
 662        chan = kmalloc(n * sizeof(struct snd_midi_channel), GFP_KERNEL);
 663        if (chan) {
 664                for (i = 0; i < n; i++)
 665                        snd_midi_channel_init(chan+i, i);
 666        }
 667
 668        return chan;
 669}
 670
 671/*
 672 * reset all midi channels
 673 */
 674static void
 675reset_all_channels(struct snd_midi_channel_set *chset)
 676{
 677        int ch;
 678        for (ch = 0; ch < chset->max_channels; ch++) {
 679                struct snd_midi_channel *chan = chset->channels + ch;
 680                snd_midi_reset_controllers(chan);
 681                chan->gm_rpn_pitch_bend_range = 256; /* 2 semitones */
 682                chan->gm_rpn_fine_tuning = 0;
 683                chan->gm_rpn_coarse_tuning = 0;
 684
 685                if (ch == 9)
 686                        chan->drum_channel = 1;
 687                else
 688                        chan->drum_channel = 0;
 689        }
 690}
 691
 692
 693/*
 694 * Allocate and initialise a midi channel set.
 695 */
 696struct snd_midi_channel_set *snd_midi_channel_alloc_set(int n)
 697{
 698        struct snd_midi_channel_set *chset;
 699
 700        chset = kmalloc(sizeof(*chset), GFP_KERNEL);
 701        if (chset) {
 702                chset->channels = snd_midi_channel_init_set(n);
 703                chset->private_data = NULL;
 704                chset->max_channels = n;
 705        }
 706        return chset;
 707}
 708
 709/*
 710 * Reset the midi controllers on a particular channel to default values.
 711 */
 712static void snd_midi_reset_controllers(struct snd_midi_channel *chan)
 713{
 714        memset(chan->control, 0, sizeof(chan->control));
 715        chan->gm_volume = 127;
 716        chan->gm_expression = 127;
 717        chan->gm_pan = 64;
 718}
 719
 720
 721/*
 722 * Free a midi channel set.
 723 */
 724void snd_midi_channel_free_set(struct snd_midi_channel_set *chset)
 725{
 726        if (chset == NULL)
 727                return;
 728        kfree(chset->channels);
 729        kfree(chset);
 730}
 731
 732static int __init alsa_seq_midi_emul_init(void)
 733{
 734        return 0;
 735}
 736
 737static void __exit alsa_seq_midi_emul_exit(void)
 738{
 739}
 740
 741module_init(alsa_seq_midi_emul_init)
 742module_exit(alsa_seq_midi_emul_exit)
 743
 744EXPORT_SYMBOL(snd_midi_process_event);
 745EXPORT_SYMBOL(snd_midi_channel_set_clear);
 746EXPORT_SYMBOL(snd_midi_channel_alloc_set);
 747EXPORT_SYMBOL(snd_midi_channel_free_set);
 748