diff jssplr.c @ 365:5a003246a735

Refactor volume and panning handling.
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 16 Oct 2012 22:44:41 +0300
parents 26ead9094940
children 31652085440a
line wrap: on
line diff
--- a/jssplr.c	Tue Oct 16 22:34:09 2012 +0300
+++ b/jssplr.c	Tue Oct 16 22:44:41 2012 +0300
@@ -108,31 +108,12 @@
 }
 
 
-static void jmpCSetVolume(JSSPlayer * mp, JSSPlayerChannel *chn, int channel, int volume, int init)
+static void jmpCSetVolume(JSSPlayerChannel *chn, int channel, int volume)
 {
-    assert(mp != NULL);
-    assert(mp->device != NULL);
-
-    volume = dmClamp(volume, mpMinVol, mpMaxVol);
-
-//fprintf(stderr, "chn %d: vol=%d, fad=%d, env=%d\n", channel, volume, chn->fadeOutVol, chn->volumeEnv.value);
+    (void) channel;
 
-    jvmSetVolumeRamp(mp->device, channel,
-        init ? 0 : jvmGetVolume(mp->device, channel),
-        (chn->fadeOutVol * chn->volumeEnv.value * volume) / (16 * 65536),
-        init ? 5 : 0);
-}
-
-
-static void jmpCSetPanning(JSSPlayer * mp, JSSPlayerChannel *chn, int channel, int panning)
-{
-    assert(mp != NULL);
-    assert(mp->device != NULL);
-
-    jvmSetPanRamp(mp->device, channel,
-        jvmGetPan(mp->device, channel),
-        panning + (((chn->panningEnv.value - 32) * (128 - abs(panning - 128))) / 32),
-        0);
+    chn->volume = dmClamp(volume, mpMinVol, mpMaxVol);
+    JMPSETNDFLAGS(cdfNewVolume);
 }
 
 
@@ -578,7 +559,7 @@
 {
     (void) channel;
 
-    chn->volume = dmClamp(volume, mpMinVol, mpMaxVol);
+    chn->volume = chn->cvolume = dmClamp(volume, mpMinVol, mpMaxVol);
     JMPSETNDFLAGS(cdfNewVolume);
 }
 
@@ -638,7 +619,7 @@
 
 /* Do a tremolo effect for given module channel.
  */
-static void jmpDoTremolo(JSSPlayer * mp, JSSPlayerChannel *chn, int channel)
+static void jmpDoTremolo(JSSPlayerChannel *chn, int channel)
 {
     (void) channel;
     
@@ -664,8 +645,7 @@
         }
 
         delta = (delta * chn->tremolo.depth) >> 6;
-
-//        jmpCSetVolume(mp, chn, channel, chn->volume + (chn->tremolo.pos >= 0 ? ), FALSE);
+        jmpCSetVolume(chn, channel, chn->cvolume + (chn->tremolo.pos >= 0 ? delta : -delta));
 
         chn->tremolo.pos += chn->tremolo.speed;
         if (chn->tremolo.pos > 31)
@@ -676,7 +656,7 @@
 
 /* Do a vibrato effect for given module channel.
  */
-static void jmpDoVibrato(JSSPlayer * mp, JSSPlayerChannel *chn, int channel)
+static void jmpDoVibrato(JSSPlayerChannel *chn, int channel)
 {
     (void) channel;
 
@@ -757,10 +737,7 @@
         }
         jmpTriggerNote(mp, chn, FALSE);
         if (change)
-        {
-            chn->volume = dmClamp(volume, mpMinVol, mpMaxVol);
-            JMPSETNDFLAGS(cdfNewVolume);
-        }
+            jmpSetVolume(chn, channel, volume);
     }
 }
 
@@ -809,6 +786,7 @@
 static void jmpDoArpeggio(JSSPlayer * mp, JSSPlayerChannel *chn, int channel, int paramY, int paramX)
 {
     JSSInstrument *inst = chn->instrument;
+    (void) channel;
 
     if (inst != NULL)
     {
@@ -846,8 +824,8 @@
     else
     {
         chn->extInstrument = NULL;
-        chn->instrument = NULL;
-        chn->ninstrument = jsetNotSet;
+        chn->instrument    = NULL;
+        chn->ninstrument   = jsetNotSet;
     }
 
     if (chn->extInstrument != NULL)
@@ -867,8 +845,8 @@
 
             if (newExtInstrument)
             {
-                chn->volume      = chn->instrument->volume;
-                chn->panning     = chn->instrument->EPanning;
+                chn->volume  = chn->instrument->volume;
+                chn->panning = chn->instrument->EPanning;
                 JMPSETNDFLAGS(cdfNewPanPos | cdfNewVolume);
             }
         }
@@ -1301,7 +1279,7 @@
             break;
 
         case 0x0a: // Vx = Vibrato : IMPL.VERIFIED
-            jmpDoVibrato(mp, chn, channel);
+            jmpDoVibrato(chn, channel);
             break;
 
         case 0x0e: // Mx = Porta To Note : IMPL.VERIFIED
@@ -1338,7 +1316,7 @@
             break;
 
         case '4': // 4xy = Vibrato
-            jmpDoVibrato(mp, chn, channel);
+            jmpDoVibrato(chn, channel);
             break;
 
         case '5': // 5xy = Portamento + Volume Slide
@@ -1347,12 +1325,12 @@
             break;
 
         case '6': // 6xy = Vibrato + Volume Slide
-            jmpDoVibrato(mp, chn, channel);
+            jmpDoVibrato(chn, channel);
             jmpDoVolumeSlide(chn, channel, chn->iLastVolSlideParam);
             break;
 
         case '7': // 7xy = Tremolo
-            jmpDoTremolo(mp, chn, channel);
+            jmpDoTremolo(chn, channel);
             break;
 
         case 'A': // Axy = Volume slide
@@ -1387,13 +1365,14 @@
             JMPMAKEPARAM(chn->iLastTremorParam, paramX, paramY)
             paramX++;
             paramY++;
-            tmp = (chn->iTremorCount % (paramX + paramY));
+            tmp = chn->iTremorCount % (paramX + paramY);
+
             if (tmp < paramX)
-                jmpCSetVolume(mp, chn, channel, chn->volume, FALSE);
+                jmpCSetVolume(chn, channel, chn->cvolume);
             else
-                jmpCSetVolume(mp, chn, channel, jsetMinVol, FALSE);
+                jmpCSetVolume(chn, channel, jsetMinVol);
 
-            chn->iTremorCount = (tmp + 1);
+            chn->iTremorCount = tmp + 1;
             break;
     }
 }
@@ -1522,13 +1501,12 @@
     for (channel = 0; channel < mp->module->nchannels; channel++)
     {
         JSSPlayerChannel *chn = &mp->channels[channel];
-        int flags;
 
         // Process extended instruments
         jmpProcessExtInstrument(chn, channel);
         
         // Check NDFlags and update channel data
-        flags = chn->newDataFlags;
+        int flags = chn->newDataFlags;
         if (!flags)
             continue;
 
@@ -1568,10 +1546,21 @@
                 jvmSetPos(mp->device, channel, chn->position);
 
             if (flags & cdfNewVolume)
-                jmpCSetVolume(mp, chn, channel, chn->volume, flags & cdfNewInstr);
+            {
+                BOOL init = flags & (cdfNewInstr | cdfPlay);
+                jvmSetVolumeRamp(mp->device, channel,
+                    init ? 0 : jvmGetVolume(mp->device, channel),
+                    (chn->fadeOutVol * chn->volumeEnv.value * chn->volume) / (16 * 65536),
+                    init ? 5 : 0);
+            }
 
             if (flags & cdfNewPanPos)
-                jmpCSetPanning(mp, chn, channel, chn->panning);
+            {
+                jvmSetPanRamp(mp->device, channel,
+                    jvmGetPan(mp->device, channel),
+                    chn->panning + (((chn->panningEnv.value - 32) * (128 - abs(chn->panning - 128))) / 32),
+                    0);
+            }
 
             if (flags & cdfNewGlobalVol)
                 jvmSetGlobalVol(mp->device, mp->globalVol);