Rearrange logic in the synthVoice loop to do less tests and remove need of a struct member for a small speedup, move some memory lookups out of the loop for a small speedup, further cosmetic changes to the synthVoice function. Change isUsed to a bool for clearer logic and also a tiny speedup

git-svn-id: svn://svn.rockbox.org/rockbox/trunk@15563 a1c6a512-1295-4272-9138-f99709370657
diff --git a/apps/plugins/midi/midiplay.c b/apps/plugins/midi/midiplay.c
index cc87463..934cea7 100644
--- a/apps/plugins/midi/midiplay.c
+++ b/apps/plugins/midi/midiplay.c
@@ -263,7 +263,7 @@
     {
         notesUsed = 0;
         for(a=0; a<MAX_VOICES; a++)
-            if(voices[a].isUsed == 1)
+            if(voices[a].isUsed)
                 notesUsed++;
         tick();
     } while(notesUsed == 0);
diff --git a/apps/plugins/midi/midiutil.h b/apps/plugins/midi/midiutil.h
index dfffe39..f26f120 100644
--- a/apps/plugins/midi/midiutil.h
+++ b/apps/plugins/midi/midiutil.h
@@ -109,11 +109,12 @@
     int delta;
     int decay;
     unsigned int cp; /* unsigned int */
-    int state, loopState;
-    int note, vol, ch, isUsed;
+    int state;
+    int note, vol, ch;
     int curRate, curOffset, targetOffset;
     int curPoint;
     signed short int volscale;
+    bool isUsed;
 };
 
 struct Event
diff --git a/apps/plugins/midi/sequencer.c b/apps/plugins/midi/sequencer.c
index 4e6c15f..7847c37 100644
--- a/apps/plugins/midi/sequencer.c
+++ b/apps/plugins/midi/sequencer.c
@@ -156,7 +156,7 @@
     int a=0;
     for(a = 0; a<MAX_VOICES; a++)
     {
-        if(voices[a].isUsed==1 && voices[a].ch == ch)
+        if(voices[a].isUsed && voices[a].ch == ch)
         {
             findDelta(&voices[a], ch, voices[a].note);
         }
@@ -202,7 +202,7 @@
         if(voices[a].ch == ch && voices[a].note == note)
             break;
 
-        if(voices[a].isUsed==0)
+        if(!voices[a].isUsed)
             break;
     }
     if(a==MAX_VOICES)
@@ -227,7 +227,6 @@
 
     setVolScale(a);
 
-    voices[a].loopState=STATE_NONLOOPING;
     /*
      * OKAY. Gt = Gus Table value
      * rf = Root Frequency of wave
@@ -239,7 +238,7 @@
     {
         findDelta(&voices[a], ch, note);
         /* Turn it on */
-        voices[a].isUsed=1;
+        voices[a].isUsed=true;
         setPoint(&voices[a], 0);
     } else
     {
@@ -256,7 +255,7 @@
             wf->mode = wf->mode & (255-28);
 
             /* Turn it on */
-            voices[a].isUsed=1;
+            voices[a].isUsed=true;
             setPoint(&voices[a], 0);
 
         } else
@@ -411,7 +410,7 @@
     {
         notesUsed = 0;
         for(a=0; a<MAX_VOICES; a++)
-            if(voices[a].isUsed == 1)
+            if(voices[a].isUsed)
                 notesUsed++;
         tick();
     } while(notesUsed == 0);
diff --git a/apps/plugins/midi/synth.c b/apps/plugins/midi/synth.c
index ca59c76..b2efce1 100644
--- a/apps/plugins/midi/synth.c
+++ b/apps/plugins/midi/synth.c
@@ -51,7 +51,7 @@
         voices[a].cp=0;
         voices[a].vol=0;
         voices[a].ch=0;
-        voices[a].isUsed=0;
+        voices[a].isUsed=false;
         voices[a].note=0;
     }
 
@@ -271,7 +271,6 @@
 static inline void synthVoice(struct SynthObject * so, int32_t * out, unsigned int samples)
 {
     struct GWaveform * wf;
-    register int s;
     register int s1;
     register int s2;
 
@@ -279,6 +278,9 @@
 
     wf = so->wf;
 
+    const unsigned int pan = chPan[so->ch];
+    const int volscale = so->volscale;
+
     const int mode_mask24 = wf->mode&24;
     const int mode_mask28 = wf->mode&28;
     const int mode_mask_looprev = wf->mode&LOOP_REVERSE;
@@ -289,9 +291,8 @@
     const unsigned int start_loop = wf->startLoop << FRACTSIZE;
     const int diff_loop = end_loop-start_loop;
 
-    while(samples > 0)
+    while(samples-- > 0)
     {
-        samples--;
         /* Is voice being ramped? */
         if(so->state == STATE_RAMPDOWN)
         {
@@ -300,12 +301,12 @@
                 so->decay = so->decay / 2;
 
                 if(so->decay < 10 && so->decay > -10)
-                    so->isUsed = 0;
+                    so->isUsed = false;
 
                 s1=so->decay;
-                s2 = s1*chPan[so->ch];
+                s2 = s1*pan;
                 s1 = (s1<<7) -s2;
-                *(out++)+=(((s1&0x7FFF80) << 9) | ((s2&0x7FFF80) >> 7));
+                *(out++)+=((s1 << 9) & 0xFFFF0000) | ((s2 >> 7) &0xFFFF);
                 continue;
             }
         } else  /* OK to advance voice */
@@ -315,23 +316,8 @@
 
         s2 = getSample((cp_temp >> FRACTSIZE)+1, wf);
 
-        /* LOOP_REVERSE|LOOP_PINGPONG  = 24  */
-        if(mode_mask24 && so->loopState == STATE_LOOPING && (cp_temp < start_loop))
+        if(mode_mask28 && cp_temp >= end_loop)
         {
-            if(mode_mask_looprev)
-            {
-                cp_temp += diff_loop;
-                s2=getSample((cp_temp >> FRACTSIZE), wf);
-            }
-            else
-            {
-                so->delta = -so->delta; /* At this point cp_temp is wrong. We need to take a step */
-            }
-        }
-
-        if(mode_mask28 && (cp_temp >= end_loop))
-        {
-            so->loopState = STATE_LOOPING;
             if(!mode_mask24)
             {
                 cp_temp -= diff_loop;
@@ -340,6 +326,20 @@
             else
             {
                 so->delta = -so->delta;
+
+                /* LOOP_REVERSE|LOOP_PINGPONG  = 24  */
+                if(cp_temp < start_loop) /* this appears to never be true in here */
+                {
+                    if(mode_mask_looprev)
+                    {
+                        cp_temp += diff_loop;
+                        s2=getSample((cp_temp >> FRACTSIZE), wf);
+                    }
+                    else
+                    {
+                        so->delta = -so->delta; /* At this point cp_temp is wrong. We need to take a step */
+                    }
+                }
             }
         }
 
@@ -354,12 +354,12 @@
         /* Better, working, linear interpolation    */
         s1=getSample((cp_temp >> FRACTSIZE), wf);
 
-        s = s1 + ((signed)((s2 - s1) * (cp_temp & ((1<<FRACTSIZE)-1)))>>FRACTSIZE);
+        s1 +=((signed)((s2 - s1) * (cp_temp & ((1<<FRACTSIZE)-1)))>>FRACTSIZE);
 
         if(so->curRate == 0)
         {
             stopVoice(so);
-//          so->isUsed = 0;
+//          so->isUsed = false;
 
         }
 
@@ -404,25 +404,23 @@
             stopVoice(so);
         }
 
-        s = (s * (so->curOffset >> 22) >> 8);
+        s1 = s1 * (so->curOffset >> 22) >> 8;
+
+        /* Scaling by channel volume and note volume is done in sequencer.c */
+        /* That saves us some multiplication and pointer operations         */
+        s1 = s1 * volscale >> 14;
 
         /* need to set ramp beginning */
         if(so->state == STATE_RAMPDOWN && so->decay == 0)
         {
-            so->decay = s*so->volscale>>14;
+            so->decay = s1;
             if(so->decay == 0)
                 so->decay = 1;  /* stupid junk.. */
         }
 
-
-        /* Scaling by channel volume and note volume is done in sequencer.c */
-        /* That saves us some multiplication and pointer operations         */
-        s1=s*so->volscale>>14;
-
-        s2 = s1*chPan[so->ch];
+        s2 = s1*pan;
         s1 = (s1<<7) - s2;
-        *(out++)+=(((s1&0x7FFF80) << 9) | ((s2&0x7FFF80) >> 7));
-
+        *(out++)+=((s1 << 9) & 0xFFFF0000) | ((s2 >> 7) &0xFFFF);
     }
 
     so->cp=cp_temp; /* store this again */
@@ -451,7 +449,7 @@
         for(i=0; i < MAX_VOICES; i++)
         {
             voicept=&voices[i];
-            if(voicept->isUsed==1)
+            if(voicept->isUsed)
             {
                 synthVoice(voicept, samp_buf, num_samples);
             }