MIDI: Fix ringing/beeks in music caused by improper parsing of some pitch bend events.


git-svn-id: svn://svn.rockbox.org/rockbox/trunk@15252 a1c6a512-1295-4272-9138-f99709370657
diff --git a/apps/plugins/midi/midiutil.c b/apps/plugins/midi/midiutil.c
index 92ab8db..65ba9c8 100644
--- a/apps/plugins/midi/midiutil.c
+++ b/apps/plugins/midi/midiutil.c
@@ -28,7 +28,8 @@
 int chPBDepth[16] IBSS_ATTR;              /* Channel pitch bend depth */
 int chPBNoteOffset[16] IBSS_ATTR;       /* Pre-computed whole semitone offset */
 int chPBFractBend[16] IBSS_ATTR;        /* Fractional bend applied to delta */
-
+unsigned char chLastCtrlMSB[16]; /* MIDI regs, used for Controller 6. */
+unsigned char chLastCtrlLSB[16]; /* The non-registered ones are ignored */
 
 struct GPatch * gusload(char *);
 struct GPatch * patchSet[128];
diff --git a/apps/plugins/midi/midiutil.h b/apps/plugins/midi/midiutil.h
index cb89e00..dfffe39 100644
--- a/apps/plugins/midi/midiutil.h
+++ b/apps/plugins/midi/midiutil.h
@@ -62,10 +62,19 @@
 #define MIDI_PITCHW 224
 
 /* MIDI Controllers */
-#define CTRL_PWDEPTH    6
+#define CTRL_DATAENT_MSB    6
 #define CTRL_VOLUME     7
 #define CTRL_BALANCE    8
 #define CTRL_PANNING    10
+#define CTRL_NONREG_LSB 98
+#define CTRL_NONREG_MSB 99
+#define CTRL_REG_LSB    100
+#define CTRL_REG_MSB    101
+
+#define REG_PITCHBEND_MSB 0
+#define REG_PITCHBEND_LSB 0
+
+
 #define CHANNEL     1
 
 /* Most of these are deprecated.. rampdown is used, maybe one other one too */
@@ -145,8 +154,8 @@
 extern int chPBDepth[16];   /* Channel pitch bend depth (Controller 6 */
 extern int chPBNoteOffset[16] IBSS_ATTR;       /* Pre-computed whole semitone offset */
 extern int chPBFractBend[16] IBSS_ATTR;        /* Fractional bend applied to delta */
-
-
+extern unsigned char chLastCtrlMSB[16]; /* MIDI regs, used for Controller 6. */
+extern unsigned char chLastCtrlLSB[16]; /* The non-registered ones are ignored */
 
 extern struct GPatch * gusload(char *);
 extern struct GPatch * patchSet[128];
diff --git a/apps/plugins/midi/sequencer.c b/apps/plugins/midi/sequencer.c
index 536d411..c4ddfcf 100644
--- a/apps/plugins/midi/sequencer.c
+++ b/apps/plugins/midi/sequencer.c
@@ -299,10 +299,39 @@
                     chPan[status_low]=d2;
                     return;
                 }
-                case CTRL_PWDEPTH:
+                case CTRL_DATAENT_MSB:
                 {
                     /* TODO: Update all deltas. Is this really needed? */
-                    chPBDepth[status_low] = d2;
+                    if(chLastCtrlMSB[status_low] == REG_PITCHBEND_MSB &&
+                       chLastCtrlLSB[status_low] == REG_PITCHBEND_LSB)
+                    {
+//                         printf("Pitch bend depth set to %d\n", d2);
+                        chPBDepth[status_low] = d2;
+                    }
+                    return;
+                }
+
+                case CTRL_NONREG_LSB:
+                {
+                    chLastCtrlLSB[status_low] = 0xFF;   /* Ignore nonregistered writes */
+                    return;
+                }
+
+                case CTRL_NONREG_MSB:
+                {
+                    chLastCtrlMSB[status_low] = 0xFF;   /* Ignore nonregistered writes */
+                    return;
+                }
+
+                case CTRL_REG_LSB:
+                {
+                    chLastCtrlLSB[status_low] = d2;
+                    return;
+                }
+
+                case CTRL_REG_MSB:
+                {
+                    chLastCtrlMSB[status_low] = d2;
                     return;
                 }
 
diff --git a/apps/plugins/midi/synth.c b/apps/plugins/midi/synth.c
index 7ae7a78..3c60d9b 100644
--- a/apps/plugins/midi/synth.c
+++ b/apps/plugins/midi/synth.c
@@ -68,6 +68,8 @@
         chPBDepth[a]=2;          /* Default bend value is 2 */
         chPBNoteOffset[a]=0;     /* No offset */
         chPBFractBend[a]=65536; /* Center.. no bend */
+        chLastCtrlMSB[a]=0;     /* Set to pitch bend depth */
+        chLastCtrlLSB[a]=0;     /* Set to pitch bend depth */
     }
     for(a=0; a<128; a++)
     {