Add "MikMod for Rockbox 0.1" from 2007-06-29
[mikmod-rockbox.git] / apps / plugins / mikmod / playercode / virtch.c
diff --git a/apps/plugins/mikmod/playercode/virtch.c b/apps/plugins/mikmod/playercode/virtch.c
new file mode 100644 (file)
index 0000000..a7b49d8
--- /dev/null
@@ -0,0 +1,968 @@
+/*     MikMod sound library\r
+       (c) 1998, 1999, 2000, 2001, 2002 Miodrag Vallat and others - see file\r
+       AUTHORS for complete list.\r
+\r
+       This library is free software; you can redistribute it and/or modify\r
+       it under the terms of the GNU Library General Public License as\r
+       published by the Free Software Foundation; either version 2 of\r
+       the License, or (at your option) any later version.\r
\r
+       This program is distributed in the hope that it will be useful,\r
+       but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
+       GNU Library General Public License for more details.\r
\r
+       You should have received a copy of the GNU Library General Public\r
+       License along with this library; if not, write to the Free Software\r
+       Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA\r
+       02111-1307, USA.\r
+*/\r
+\r
+/*==============================================================================\r
+\r
+  $Id: virtch.c,v 1.2 2004/02/13 13:31:54 raph Exp $\r
+\r
+  Sample mixing routines, using a 32 bits mixing buffer.\r
+\r
+==============================================================================*/\r
+\r
+/*\r
+\r
+  Optional features include:\r
+    (a) 4-step reverb (for 16 bit output only)\r
+    (b) Interpolation of sample data during mixing\r
+    (c) Dolby Surround Sound\r
+*/\r
+\r
+#ifdef HAVE_CONFIG_H\r
+#include "config.h"\r
+#endif\r
+\r
+#include <stddef.h>\r
+#ifdef HAVE_MEMORY_H\r
+#include <memory.h>\r
+#endif\r
+#include <string.h>\r
+\r
+#include "mikmod_internals.h"\r
+\r
+/*\r
+   Constant definitions\r
+   ====================\r
+\r
+       BITSHIFT\r
+               Controls the maximum volume of the sound output.  All data is shifted\r
+               right by BITSHIFT after being mixed. Higher values result in quieter\r
+               sound and less chance of distortion.\r
+\r
+       REVERBERATION\r
+               Controls the duration of the reverb. Larger values represent a shorter\r
+               reverb loop. Smaller values extend the reverb but can result in more of\r
+               an echo-ish sound.\r
+\r
+*/\r
+\r
+#define BITSHIFT               9\r
+#define REVERBERATION  110000L\r
+\r
+#define FRACBITS 11\r
+#define FRACMASK ((1L<<FRACBITS)-1L)\r
+\r
+#define TICKLSIZE 8192\r
+#define TICKWSIZE (TICKLSIZE<<1)\r
+#define TICKBSIZE (TICKWSIZE<<1)\r
+\r
+#define CLICK_SHIFT  6\r
+#define CLICK_BUFFER (1L<<CLICK_SHIFT)\r
+\r
+#ifndef MIN\r
+#define MIN(a,b) (((a)<(b)) ? (a) : (b))\r
+#endif\r
+\r
+typedef struct VINFO {\r
+       UBYTE     kick;              /* =1 -> sample has to be restarted */\r
+       UBYTE     active;            /* =1 -> sample is playing */\r
+       UWORD     flags;             /* 16/8 bits looping/one-shot */\r
+       SWORD     handle;            /* identifies the sample */\r
+       ULONG     start;             /* start index */\r
+       ULONG     size;              /* samplesize */\r
+       ULONG     reppos;            /* loop start */\r
+       ULONG     repend;            /* loop end */\r
+       ULONG     frq;               /* current frequency */\r
+       int       vol;               /* current volume */\r
+       int       pan;               /* current panning position */\r
+\r
+       int       rampvol;\r
+       int       lvolsel,rvolsel;   /* Volume factor in range 0-255 */\r
+       int       oldlvol,oldrvol;\r
+\r
+       SLONGLONG current;           /* current index in the sample */\r
+       SLONGLONG increment;         /* increment value */\r
+} VINFO;\r
+\r
+static SWORD **Samples;\r
+static VINFO *vinf=NULL,*vnf;\r
+static long tickleft,samplesthatfit,vc_memory=0;\r
+static int vc_softchn;\r
+static SLONGLONG idxsize,idxlpos,idxlend;\r
+static SLONG *vc_tickbuf=NULL;\r
+static UWORD vc_mode;\r
+\r
+/* Reverb control variables */\r
+\r
+static int RVc1, RVc2, RVc3, RVc4, RVc5, RVc6, RVc7, RVc8;\r
+static ULONG RVRindex;\r
+\r
+/* For Mono or Left Channel */\r
+static SLONG *RVbufL1=NULL,*RVbufL2=NULL,*RVbufL3=NULL,*RVbufL4=NULL,\r
+                     *RVbufL5=NULL,*RVbufL6=NULL,*RVbufL7=NULL,*RVbufL8=NULL;\r
+\r
+/* For Stereo only (Right Channel) */\r
+static SLONG *RVbufR1=NULL,*RVbufR2=NULL,*RVbufR3=NULL,*RVbufR4=NULL,\r
+                     *RVbufR5=NULL,*RVbufR6=NULL,*RVbufR7=NULL,*RVbufR8=NULL;\r
+\r
+#ifdef NATIVE_64BIT_INT\r
+#define NATIVE SLONGLONG\r
+#else\r
+#define NATIVE SLONG\r
+#endif\r
+\r
+/*========== 32 bit sample mixers - only for 32 bit platforms */\r
+#ifndef NATIVE_64BIT_INT\r
+\r
+static SLONG Mix32MonoNormal(SWORD* srce,SLONG* dest,SLONG index,SLONG increment,SLONG todo)\r
+{\r
+       SWORD sample;\r
+       SLONG lvolsel = vnf->lvolsel;\r
+\r
+       while(todo--) {\r
+               sample = srce[index >> FRACBITS];\r
+               index += increment;\r
+\r
+               *dest++ += lvolsel * sample;\r
+       }\r
+       return index;\r
+}\r
+\r
+static SLONG Mix32StereoNormal(SWORD* srce,SLONG* dest,SLONG index,SLONG increment,SLONG todo)\r
+{\r
+       SWORD sample;\r
+       SLONG lvolsel = vnf->lvolsel;\r
+       SLONG rvolsel = vnf->rvolsel;\r
+\r
+       while(todo--) {\r
+               sample=srce[index >> FRACBITS];\r
+               index += increment;\r
+\r
+               *dest++ += lvolsel * sample;\r
+               *dest++ += rvolsel * sample;\r
+       }\r
+       return index;\r
+}\r
+\r
+static SLONG Mix32SurroundNormal(SWORD* srce,SLONG* dest,SLONG index,SLONG increment,SLONG todo)\r
+{\r
+       SWORD sample;\r
+       SLONG lvolsel = vnf->lvolsel;\r
+       SLONG rvolsel = vnf->rvolsel;\r
+\r
+       if (lvolsel>=rvolsel) {\r
+               while(todo--) {\r
+                       sample = srce[index >> FRACBITS];\r
+                       index += increment;\r
+\r
+                       *dest++ += lvolsel*sample;\r
+                       *dest++ -= lvolsel*sample;\r
+               }\r
+       } else {\r
+               while(todo--) {\r
+                       sample = srce[index >> FRACBITS];\r
+                       index += increment;\r
+\r
+                       *dest++ -= rvolsel*sample;\r
+                       *dest++ += rvolsel*sample;\r
+               }\r
+       }\r
+       return index;\r
+}\r
+\r
+static SLONG Mix32MonoInterp(SWORD* srce,SLONG* dest,SLONG index,SLONG increment,SLONG todo)\r
+{\r
+       SLONG sample;\r
+       SLONG lvolsel = vnf->lvolsel;\r
+       SLONG rampvol = vnf->rampvol;\r
+\r
+       if (rampvol) {\r
+               SLONG oldlvol = vnf->oldlvol - lvolsel;\r
+               while(todo--) {\r
+                       sample=(SLONG)srce[index>>FRACBITS]+\r
+                              ((SLONG)(srce[(index>>FRACBITS)+1]-srce[index>>FRACBITS])\r
+                               *(index&FRACMASK)>>FRACBITS);\r
+                       index += increment;\r
+\r
+                       *dest++ += ((lvolsel << CLICK_SHIFT) + oldlvol * rampvol)\r
+                                  * sample >> CLICK_SHIFT;\r
+                       if (!--rampvol)\r
+                               break;\r
+               }\r
+               vnf->rampvol = rampvol;\r
+               if (todo < 0)\r
+                       return index;\r
+       }\r
+\r
+       while(todo--) {\r
+               sample=(SLONG)srce[index>>FRACBITS]+\r
+                      ((SLONG)(srce[(index>>FRACBITS)+1]-srce[index>>FRACBITS])\r
+                       *(index&FRACMASK)>>FRACBITS);\r
+               index += increment;\r
+\r
+               *dest++ += lvolsel * sample;\r
+       }\r
+       return index;\r
+}\r
+\r
+static SLONG Mix32StereoInterp(SWORD* srce,SLONG* dest,SLONG index,SLONG increment,SLONG todo)\r
+{\r
+       SLONG sample;\r
+       SLONG lvolsel = vnf->lvolsel;\r
+       SLONG rvolsel = vnf->rvolsel;\r
+       SLONG rampvol = vnf->rampvol;\r
+\r
+       if (rampvol) {\r
+               SLONG oldlvol = vnf->oldlvol - lvolsel;\r
+               SLONG oldrvol = vnf->oldrvol - rvolsel;\r
+               while(todo--) {\r
+                       sample=(SLONG)srce[index>>FRACBITS]+\r
+                              ((SLONG)(srce[(index>>FRACBITS)+1]-srce[index>>FRACBITS])\r
+                               *(index&FRACMASK)>>FRACBITS);\r
+                       index += increment;\r
+\r
+                       *dest++ += ((lvolsel << CLICK_SHIFT) + oldlvol * rampvol)\r
+                                  * sample >> CLICK_SHIFT;\r
+                       *dest++ += ((rvolsel << CLICK_SHIFT) + oldrvol * rampvol)\r
+                                          * sample >> CLICK_SHIFT;\r
+                       if (!--rampvol)\r
+                               break;\r
+               }\r
+               vnf->rampvol = rampvol;\r
+               if (todo < 0)\r
+                       return index;\r
+       }\r
+\r
+       while(todo--) {\r
+               sample=(SLONG)srce[index>>FRACBITS]+\r
+                      ((SLONG)(srce[(index>>FRACBITS)+1]-srce[index>>FRACBITS])\r
+                       *(index&FRACMASK)>>FRACBITS);\r
+               index += increment;\r
+\r
+               *dest++ += lvolsel * sample;\r
+               *dest++ += rvolsel * sample;\r
+       }\r
+       return index;\r
+}\r
+\r
+static SLONG Mix32SurroundInterp(SWORD* srce,SLONG* dest,SLONG index,SLONG increment,SLONG todo)\r
+{\r
+       SLONG sample;\r
+       SLONG lvolsel = vnf->lvolsel;\r
+       SLONG rvolsel = vnf->rvolsel;\r
+       SLONG rampvol = vnf->rampvol;\r
+       SLONG oldvol, vol;\r
+\r
+       if (lvolsel >= rvolsel) {\r
+               vol = lvolsel;\r
+               oldvol = vnf->oldlvol;\r
+       } else {\r
+               vol = rvolsel;\r
+               oldvol = vnf->oldrvol;\r
+       }\r
+\r
+       if (rampvol) {\r
+               oldvol -= vol;\r
+               while(todo--) {\r
+                       sample=(SLONG)srce[index>>FRACBITS]+\r
+                              ((SLONG)(srce[(index>>FRACBITS)+1]-srce[index>>FRACBITS])\r
+                               *(index&FRACMASK)>>FRACBITS);\r
+                       index += increment;\r
+\r
+                       sample=((vol << CLICK_SHIFT) + oldvol * rampvol)\r
+                                  * sample >> CLICK_SHIFT;\r
+                       *dest++ += sample;\r
+                       *dest++ -= sample;\r
+\r
+                       if (!--rampvol)\r
+                               break;\r
+               }\r
+               vnf->rampvol = rampvol;\r
+               if (todo < 0)\r
+                       return index;\r
+       }\r
+\r
+       while(todo--) {\r
+               sample=(SLONG)srce[index>>FRACBITS]+\r
+                      ((SLONG)(srce[(index>>FRACBITS)+1]-srce[index>>FRACBITS])\r
+                       *(index&FRACMASK)>>FRACBITS);\r
+               index += increment;\r
+\r
+               *dest++ += vol*sample;\r
+               *dest++ -= vol*sample;\r
+       }\r
+       return index;\r
+}\r
+#endif\r
+\r
+/*========== 64 bit sample mixers - all platforms */\r
+\r
+static SLONGLONG MixMonoNormal(SWORD* srce,SLONG* dest,SLONGLONG index,SLONGLONG increment,SLONG todo)\r
+{\r
+       SWORD sample;\r
+       SLONG lvolsel = vnf->lvolsel;\r
+\r
+       while(todo--) {\r
+               sample = srce[index >> FRACBITS];\r
+               index += increment;\r
+\r
+               *dest++ += lvolsel * sample;\r
+       }\r
+       return index;\r
+}\r
+\r
+static SLONGLONG MixStereoNormal(SWORD* srce,SLONG* dest,SLONGLONG index,SLONGLONG increment,SLONG todo)\r
+{\r
+       SWORD sample;\r
+       SLONG lvolsel = vnf->lvolsel;\r
+       SLONG rvolsel = vnf->rvolsel;\r
+\r
+       while(todo--) {\r
+               sample=srce[index >> FRACBITS];\r
+               index += increment;\r
+\r
+               *dest++ += lvolsel * sample;\r
+               *dest++ += rvolsel * sample;\r
+       }\r
+       return index;\r
+}\r
+\r
+static SLONGLONG MixSurroundNormal(SWORD* srce,SLONG* dest,SLONGLONG index,SLONGLONG increment,SLONG todo)\r
+{\r
+       SWORD sample;\r
+       SLONG lvolsel = vnf->lvolsel;\r
+       SLONG rvolsel = vnf->rvolsel;\r
+\r
+       if(vnf->lvolsel>=vnf->rvolsel) {\r
+               while(todo--) {\r
+                       sample = srce[index >> FRACBITS];\r
+                       index += increment;\r
+\r
+                       *dest++ += lvolsel*sample;\r
+                       *dest++ -= lvolsel*sample;\r
+               }\r
+       } else {\r
+               while(todo--) {\r
+                       sample = srce[index >> FRACBITS];\r
+                       index += increment;\r
+\r
+                       *dest++ -= rvolsel*sample;\r
+                       *dest++ += rvolsel*sample;\r
+               }\r
+       }\r
+       return index;\r
+}\r
+\r
+static SLONGLONG MixMonoInterp(SWORD* srce,SLONG* dest,SLONGLONG index,SLONGLONG increment,SLONG todo)\r
+{\r
+       SLONG sample;\r
+       SLONG lvolsel = vnf->lvolsel;\r
+       SLONG rampvol = vnf->rampvol;\r
+\r
+       if (rampvol) {\r
+               SLONG oldlvol = vnf->oldlvol - lvolsel;\r
+               while(todo--) {\r
+                       sample=(SLONG)srce[index>>FRACBITS]+\r
+                              ((SLONG)(srce[(index>>FRACBITS)+1]-srce[index>>FRACBITS])\r
+                               *(index&FRACMASK)>>FRACBITS);\r
+                       index += increment;\r
+\r
+                       *dest++ += ((lvolsel << CLICK_SHIFT) + oldlvol * rampvol)\r
+                                          * sample >> CLICK_SHIFT;\r
+                       if (!--rampvol)\r
+                               break;\r
+               }\r
+               vnf->rampvol = rampvol;\r
+               if (todo < 0)\r
+                       return index;\r
+       }\r
+\r
+       while(todo--) {\r
+               sample=(SLONG)srce[index>>FRACBITS]+\r
+                      ((SLONG)(srce[(index>>FRACBITS)+1]-srce[index>>FRACBITS])\r
+                       *(index&FRACMASK)>>FRACBITS);\r
+               index += increment;\r
+\r
+               *dest++ += lvolsel * sample;\r
+       }\r
+       return index;\r
+}\r
+\r
+static SLONGLONG MixStereoInterp(SWORD* srce,SLONG* dest,SLONGLONG index,SLONGLONG increment,SLONG todo)\r
+{\r
+       SLONG sample;\r
+       SLONG lvolsel = vnf->lvolsel;\r
+       SLONG rvolsel = vnf->rvolsel;\r
+       SLONG rampvol = vnf->rampvol;\r
+\r
+       if (rampvol) {\r
+               SLONG oldlvol = vnf->oldlvol - lvolsel;\r
+               SLONG oldrvol = vnf->oldrvol - rvolsel;\r
+               while(todo--) {\r
+                       sample=(SLONG)srce[index>>FRACBITS]+\r
+                              ((SLONG)(srce[(index>>FRACBITS)+1]-srce[index>>FRACBITS])\r
+                               *(index&FRACMASK)>>FRACBITS);\r
+                       index += increment;\r
+\r
+                       *dest++ +=((lvolsel << CLICK_SHIFT) + oldlvol * rampvol)\r
+                                          * sample >> CLICK_SHIFT;\r
+                       *dest++ +=((rvolsel << CLICK_SHIFT) + oldrvol * rampvol)\r
+                                          * sample >> CLICK_SHIFT;\r
+                       if (!--rampvol)\r
+                               break;\r
+               }\r
+               vnf->rampvol = rampvol;\r
+               if (todo < 0)\r
+                       return index;\r
+       }\r
+\r
+       while(todo--) {\r
+               sample=(SLONG)srce[index>>FRACBITS]+\r
+                      ((SLONG)(srce[(index>>FRACBITS)+1]-srce[index>>FRACBITS])\r
+                       *(index&FRACMASK)>>FRACBITS);\r
+               index += increment;\r
+\r
+               *dest++ += lvolsel * sample;\r
+               *dest++ += rvolsel * sample;\r
+       }\r
+       return index;\r
+}\r
+\r
+static SLONGLONG MixSurroundInterp(SWORD* srce,SLONG* dest,SLONGLONG index,SLONGLONG increment,SLONG todo)\r
+{\r
+       SLONG sample;\r
+       SLONG lvolsel = vnf->lvolsel;\r
+       SLONG rvolsel = vnf->rvolsel;\r
+       SLONG rampvol = vnf->rampvol;\r
+       SLONG oldvol, vol;\r
+\r
+       if (lvolsel >= rvolsel) {\r
+               vol = lvolsel;\r
+               oldvol = vnf->oldlvol;\r
+       } else {\r
+               vol = rvolsel;\r
+               oldvol = vnf->oldrvol;\r
+       }\r
+\r
+       if (rampvol) {\r
+               oldvol -= vol;\r
+               while(todo--) {\r
+                       sample=(SLONG)srce[index>>FRACBITS]+\r
+                              ((SLONG)(srce[(index>>FRACBITS)+1]-srce[index>>FRACBITS])\r
+                               *(index&FRACMASK)>>FRACBITS);\r
+                       index += increment;\r
+\r
+                       sample=((vol << CLICK_SHIFT) + oldvol * rampvol)\r
+                                  * sample >> CLICK_SHIFT;\r
+                       *dest++ += sample;\r
+                       *dest++ -= sample;\r
+                       if (!--rampvol)\r
+                               break;\r
+               }\r
+               vnf->rampvol = rampvol;\r
+               if (todo < 0)\r
+                       return index;\r
+       }\r
+\r
+       while(todo--) {\r
+               sample=(SLONG)srce[index>>FRACBITS]+\r
+                      ((SLONG)(srce[(index>>FRACBITS)+1]-srce[index>>FRACBITS])\r
+                       *(index&FRACMASK)>>FRACBITS);\r
+               index += increment;\r
+\r
+               *dest++ += vol*sample;\r
+               *dest++ -= vol*sample;\r
+       }\r
+       return index;\r
+}\r
+\r
+static void (*MixReverb)(SLONG* srce,NATIVE count);\r
+\r
+/* Reverb macros */\r
+#define COMPUTE_LOC(n) loc##n = RVRindex % RVc##n\r
+#define COMPUTE_LECHO(n) RVbufL##n [loc##n ]=speedup+((ReverbPct*RVbufL##n [loc##n ])>>7)\r
+#define COMPUTE_RECHO(n) RVbufR##n [loc##n ]=speedup+((ReverbPct*RVbufR##n [loc##n ])>>7)\r
+\r
+static void MixReverb_Normal(SLONG* srce,NATIVE count)\r
+{\r
+       unsigned int speedup;\r
+       int ReverbPct;\r
+       unsigned int loc1,loc2,loc3,loc4;\r
+       unsigned int loc5,loc6,loc7,loc8;\r
+\r
+       ReverbPct=58+(md_reverb<<2);\r
+\r
+       COMPUTE_LOC(1); COMPUTE_LOC(2); COMPUTE_LOC(3); COMPUTE_LOC(4);\r
+       COMPUTE_LOC(5); COMPUTE_LOC(6); COMPUTE_LOC(7); COMPUTE_LOC(8);\r
+\r
+       while(count--) {\r
+               /* Compute the left channel echo buffers */\r
+               speedup = *srce >> 3;\r
+\r
+               COMPUTE_LECHO(1); COMPUTE_LECHO(2); COMPUTE_LECHO(3); COMPUTE_LECHO(4);\r
+               COMPUTE_LECHO(5); COMPUTE_LECHO(6); COMPUTE_LECHO(7); COMPUTE_LECHO(8);\r
+\r
+               /* Prepare to compute actual finalized data */\r
+               RVRindex++;\r
+\r
+               COMPUTE_LOC(1); COMPUTE_LOC(2); COMPUTE_LOC(3); COMPUTE_LOC(4);\r
+               COMPUTE_LOC(5); COMPUTE_LOC(6); COMPUTE_LOC(7); COMPUTE_LOC(8);\r
+\r
+               /* left channel */\r
+               *srce++ +=RVbufL1[loc1]-RVbufL2[loc2]+RVbufL3[loc3]-RVbufL4[loc4]+\r
+                         RVbufL5[loc5]-RVbufL6[loc6]+RVbufL7[loc7]-RVbufL8[loc8];\r
+       }\r
+}\r
+\r
+static void MixReverb_Stereo(SLONG* srce,NATIVE count)\r
+{\r
+       unsigned int speedup;\r
+       int          ReverbPct;\r
+       unsigned int loc1, loc2, loc3, loc4;\r
+       unsigned int loc5, loc6, loc7, loc8;\r
+\r
+       ReverbPct = 92+(md_reverb<<1);\r
+\r
+       COMPUTE_LOC(1); COMPUTE_LOC(2); COMPUTE_LOC(3); COMPUTE_LOC(4);\r
+       COMPUTE_LOC(5); COMPUTE_LOC(6); COMPUTE_LOC(7); COMPUTE_LOC(8);\r
+\r
+       while(count--) {\r
+               /* Compute the left channel echo buffers */\r
+               speedup = *srce >> 3;\r
+\r
+               COMPUTE_LECHO(1); COMPUTE_LECHO(2); COMPUTE_LECHO(3); COMPUTE_LECHO(4);\r
+               COMPUTE_LECHO(5); COMPUTE_LECHO(6); COMPUTE_LECHO(7); COMPUTE_LECHO(8);\r
+\r
+               /* Compute the right channel echo buffers */\r
+               speedup = srce[1] >> 3;\r
+\r
+               COMPUTE_RECHO(1); COMPUTE_RECHO(2); COMPUTE_RECHO(3); COMPUTE_RECHO(4);\r
+               COMPUTE_RECHO(5); COMPUTE_RECHO(6); COMPUTE_RECHO(7); COMPUTE_RECHO(8);\r
+\r
+               /* Prepare to compute actual finalized data */\r
+               RVRindex++;\r
+\r
+               COMPUTE_LOC(1); COMPUTE_LOC(2); COMPUTE_LOC(3); COMPUTE_LOC(4);\r
+               COMPUTE_LOC(5); COMPUTE_LOC(6); COMPUTE_LOC(7); COMPUTE_LOC(8);\r
+\r
+               /* left channel then right channel */\r
+               *srce++ +=RVbufL1[loc1]-RVbufL2[loc2]+RVbufL3[loc3]-RVbufL4[loc4]+\r
+                         RVbufL5[loc5]-RVbufL6[loc6]+RVbufL7[loc7]-RVbufL8[loc8];\r
+\r
+               *srce++ +=RVbufR1[loc1]-RVbufR2[loc2]+RVbufR3[loc3]-RVbufR4[loc4]+\r
+                         RVbufR5[loc5]-RVbufR6[loc6]+RVbufR7[loc7]-RVbufR8[loc8];\r
+       }\r
+}\r
+\r
+/* Mixing macros */\r
+#define EXTRACT_SAMPLE_FP(var,size) var=(*srce++>>(BITSHIFT-size)) * ((1.0f / 32768.0f) / (1 << size))\r
+#define CHECK_SAMPLE_FP(var,bound) var=(var>bound)?bound:(var<-bound)?-bound:var\r
+#define PUT_SAMPLE_FP(var) *dste++=var\r
+\r
+static void Mix32ToFP(float* dste,SLONG* srce,NATIVE count)\r
+{\r
+       float x1,x2,x3,x4;\r
+       int     remain;\r
+\r
+       #define FP_SHIFT        4\r
+       \r
+       remain=count&3;\r
+       for(count>>=2;count;count--) {\r
+               EXTRACT_SAMPLE_FP(x1,FP_SHIFT); EXTRACT_SAMPLE_FP(x2,FP_SHIFT);\r
+               EXTRACT_SAMPLE_FP(x3,FP_SHIFT); EXTRACT_SAMPLE_FP(x4,FP_SHIFT);\r
+\r
+               CHECK_SAMPLE_FP(x1,1.0f); CHECK_SAMPLE_FP(x2,1.0f);\r
+               CHECK_SAMPLE_FP(x3,1.0f); CHECK_SAMPLE_FP(x4,1.0f);\r
+\r
+               PUT_SAMPLE_FP(x1); PUT_SAMPLE_FP(x2);\r
+               PUT_SAMPLE_FP(x3); PUT_SAMPLE_FP(x4);\r
+       }\r
+       while(remain--) {\r
+               EXTRACT_SAMPLE_FP(x1,FP_SHIFT);\r
+               CHECK_SAMPLE_FP(x1,1.0f);\r
+               PUT_SAMPLE_FP(x1);\r
+       }\r
+}\r
+\r
+\r
+/* Mixing macros */\r
+#define EXTRACT_SAMPLE(var,size) var=*srce++>>(BITSHIFT+16-size)\r
+#define CHECK_SAMPLE(var,bound) var=(var>=bound)?bound-1:(var<-bound)?-bound:var\r
+#define PUT_SAMPLE(var) *dste++=var\r
+\r
+static void Mix32To16(SWORD* dste,SLONG* srce,NATIVE count)\r
+{\r
+       SLONG x1,x2,x3,x4;\r
+       int     remain;\r
+\r
+       remain=count&3;\r
+       for(count>>=2;count;count--) {\r
+               EXTRACT_SAMPLE(x1,16); EXTRACT_SAMPLE(x2,16);\r
+               EXTRACT_SAMPLE(x3,16); EXTRACT_SAMPLE(x4,16);\r
+\r
+               CHECK_SAMPLE(x1,32768); CHECK_SAMPLE(x2,32768);\r
+               CHECK_SAMPLE(x3,32768); CHECK_SAMPLE(x4,32768);\r
+\r
+               PUT_SAMPLE(x1); PUT_SAMPLE(x2); PUT_SAMPLE(x3); PUT_SAMPLE(x4);\r
+       }\r
+       while(remain--) {\r
+               EXTRACT_SAMPLE(x1,16);\r
+               CHECK_SAMPLE(x1,32768);\r
+               PUT_SAMPLE(x1);\r
+       }\r
+}\r
+\r
+static void Mix32To8(SBYTE* dste,SLONG* srce,NATIVE count)\r
+{\r
+       SWORD x1,x2,x3,x4;\r
+       int     remain;\r
+\r
+       remain=count&3;\r
+       for(count>>=2;count;count--) {\r
+               EXTRACT_SAMPLE(x1,8); EXTRACT_SAMPLE(x2,8);\r
+               EXTRACT_SAMPLE(x3,8); EXTRACT_SAMPLE(x4,8);\r
+\r
+               CHECK_SAMPLE(x1,128); CHECK_SAMPLE(x2,128);\r
+               CHECK_SAMPLE(x3,128); CHECK_SAMPLE(x4,128);\r
+\r
+               PUT_SAMPLE(x1+128); PUT_SAMPLE(x2+128);\r
+               PUT_SAMPLE(x3+128); PUT_SAMPLE(x4+128);\r
+       }\r
+       while(remain--) {\r
+               EXTRACT_SAMPLE(x1,8);\r
+               CHECK_SAMPLE(x1,128);\r
+               PUT_SAMPLE(x1+128);\r
+       }\r
+}\r
+\r
+static void AddChannel(SLONG* ptr,NATIVE todo)\r
+{\r
+       SLONGLONG end,done;\r
+       SWORD *s;\r
+\r
+       if(!(s=Samples[vnf->handle])) {\r
+               vnf->current = vnf->active  = 0;\r
+               return;\r
+       }\r
+\r
+       /* update the 'current' index so the sample loops, or stops playing if it\r
+          reached the end of the sample */\r
+       while(todo>0) {\r
+               SLONGLONG endpos;\r
+\r
+               if(vnf->flags & SF_REVERSE) {\r
+                       /* The sample is playing in reverse */\r
+                       if((vnf->flags&SF_LOOP)&&(vnf->current<idxlpos)) {\r
+                               /* the sample is looping and has reached the loopstart index */\r
+                               if(vnf->flags & SF_BIDI) {\r
+                                       /* sample is doing bidirectional loops, so 'bounce' the\r
+                                          current index against the idxlpos */\r
+                                       vnf->current = idxlpos+(idxlpos-vnf->current);\r
+                                       vnf->flags &= ~SF_REVERSE;\r
+                                       vnf->increment = -vnf->increment;\r
+                               } else\r
+                                       /* normal backwards looping, so set the current position to\r
+                                          loopend index */\r
+                                       vnf->current=idxlend-(idxlpos-vnf->current);\r
+                       } else {\r
+                               /* the sample is not looping, so check if it reached index 0 */\r
+                               if(vnf->current < 0) {\r
+                                       /* playing index reached 0, so stop playing this sample */\r
+                                       vnf->current = vnf->active  = 0;\r
+                                       break;\r
+                               }\r
+                       }\r
+               } else {\r
+                       /* The sample is playing forward */\r
+                       if((vnf->flags & SF_LOOP) &&\r
+                          (vnf->current >= idxlend)) {\r
+                               /* the sample is looping, check the loopend index */\r
+                               if(vnf->flags & SF_BIDI) {\r
+                                       /* sample is doing bidirectional loops, so 'bounce' the\r
+                                          current index against the idxlend */\r
+                                       vnf->flags |= SF_REVERSE;\r
+                                       vnf->increment = -vnf->increment;\r
+                                       vnf->current = idxlend-(vnf->current-idxlend);\r
+                               } else\r
+                                       /* normal backwards looping, so set the current position\r
+                                          to loopend index */\r
+                                       vnf->current=idxlpos+(vnf->current-idxlend);\r
+                       } else {\r
+                               /* sample is not looping, so check if it reached the last\r
+                                  position */\r
+                               if(vnf->current >= idxsize) {\r
+                                       /* yes, so stop playing this sample */\r
+                                       vnf->current = vnf->active  = 0;\r
+                                       break;\r
+                               }\r
+                       }\r
+               }\r
+\r
+               end=(vnf->flags&SF_REVERSE)?(vnf->flags&SF_LOOP)?idxlpos:0:\r
+                    (vnf->flags&SF_LOOP)?idxlend:idxsize;\r
+\r
+               /* if the sample is not blocked... */\r
+               if((end==vnf->current)||(!vnf->increment))\r
+                       done=0;\r
+               else {\r
+                       done=MIN((end-vnf->current)/vnf->increment+1,todo);\r
+                       if(done<0) done=0;\r
+               }\r
+\r
+               if(!done) {\r
+                       vnf->active = 0;\r
+                       break;\r
+               }\r
+\r
+               endpos=vnf->current+done*vnf->increment;\r
+\r
+               if(vnf->vol) {\r
+#ifndef NATIVE_64BIT_INT\r
+                       /* use the 32 bit mixers as often as we can (they're much faster) */\r
+                       if((vnf->current<0x7fffffff)&&(endpos<0x7fffffff)) {\r
+                               if((md_mode & DMODE_INTERP)) {\r
+                                       if(vc_mode & DMODE_STEREO) {\r
+                                               if((vnf->pan==PAN_SURROUND)&&(md_mode&DMODE_SURROUND))\r
+                                                       vnf->current=Mix32SurroundInterp\r
+                                                                  (s,ptr,vnf->current,vnf->increment,done);\r
+                                               else\r
+                                                       vnf->current=Mix32StereoInterp\r
+                                                                  (s,ptr,vnf->current,vnf->increment,done);\r
+                                       } else\r
+                                               vnf->current=Mix32MonoInterp\r
+                                                              (s,ptr,vnf->current,vnf->increment,done);\r
+                               } else if(vc_mode & DMODE_STEREO) {\r
+                                       if((vnf->pan==PAN_SURROUND)&&(md_mode&DMODE_SURROUND))\r
+                                               vnf->current=Mix32SurroundNormal\r
+                                                              (s,ptr,vnf->current,vnf->increment,done);\r
+                                       else\r
+                                               vnf->current=Mix32StereoNormal\r
+                                                              (s,ptr,vnf->current,vnf->increment,done);\r
+                               } else\r
+                                       vnf->current=Mix32MonoNormal\r
+                                                          (s,ptr,vnf->current,vnf->increment,done);\r
+                       } else\r
+#endif\r
+                              {\r
+                               if((md_mode & DMODE_INTERP)) {\r
+                                       if(vc_mode & DMODE_STEREO) {\r
+                                               if((vnf->pan==PAN_SURROUND)&&(md_mode&DMODE_SURROUND))\r
+                                                       vnf->current=MixSurroundInterp\r
+                                                                  (s,ptr,vnf->current,vnf->increment,done);\r
+                                               else\r
+                                                       vnf->current=MixStereoInterp\r
+                                                                  (s,ptr,vnf->current,vnf->increment,done);\r
+                                       } else\r
+                                               vnf->current=MixMonoInterp\r
+                                                              (s,ptr,vnf->current,vnf->increment,done);\r
+                               } else if(vc_mode & DMODE_STEREO) {\r
+                                       if((vnf->pan==PAN_SURROUND)&&(md_mode&DMODE_SURROUND))\r
+                                               vnf->current=MixSurroundNormal\r
+                                                              (s,ptr,vnf->current,vnf->increment,done);\r
+                                       else\r
+                                               vnf->current=MixStereoNormal\r
+                                                              (s,ptr,vnf->current,vnf->increment,done);\r
+                               } else\r
+                                       vnf->current=MixMonoNormal\r
+                                                          (s,ptr,vnf->current,vnf->increment,done);\r
+                       }\r
+               } else\r
+                       /* update sample position */\r
+                       vnf->current=endpos;\r
+\r
+               todo-=done;\r
+               ptr +=(vc_mode & DMODE_STEREO)?(done<<1):done;\r
+       }\r
+}\r
+\r
+#define _IN_VIRTCH_\r
+#include "virtch_common.c"\r
+#undef _IN_VIRTCH_\r
+\r
+void VC1_WriteSamples(SBYTE* buf,ULONG todo)\r
+{\r
+       int left,portion=0,count;\r
+       SBYTE  *buffer;\r
+       int t, pan, vol;\r
+\r
+       while(todo) {\r
+               if(!tickleft) {\r
+                       if(vc_mode & DMODE_SOFT_MUSIC) md_player();\r
+                       tickleft=(md_mixfreq*125L)/(md_bpm*50L);\r
+               }\r
+               left = MIN(tickleft, todo);\r
+               buffer    = buf;\r
+               tickleft -= left;\r
+               todo     -= left;\r
+               buf += samples2bytes(left);\r
+\r
+               while(left) {\r
+                       portion = MIN(left, samplesthatfit);\r
+                       count   = (vc_mode & DMODE_STEREO)?(portion<<1):portion;\r
+                       memset(vc_tickbuf, 0, count<<2);\r
+                       for(t=0;t<vc_softchn;t++) {\r
+                               vnf = &vinf[t];\r
+\r
+                               if(vnf->kick) {\r
+                                       vnf->current=((SLONGLONG)vnf->start)<<FRACBITS;\r
+                                       vnf->kick   =0;\r
+                                       vnf->active =1;\r
+                               }\r
+\r
+                               if(!vnf->frq) vnf->active = 0;\r
+\r
+                               if(vnf->active) {\r
+                                       vnf->increment=((SLONGLONG)(vnf->frq<<FRACBITS))/md_mixfreq;\r
+                                       if(vnf->flags&SF_REVERSE) vnf->increment=-vnf->increment;\r
+                                       vol = vnf->vol;  pan = vnf->pan;\r
+\r
+                                       vnf->oldlvol=vnf->lvolsel;vnf->oldrvol=vnf->rvolsel;\r
+                                       if(vc_mode & DMODE_STEREO) {\r
+                                               if(pan != PAN_SURROUND) {\r
+                                                       vnf->lvolsel=(vol*(PAN_RIGHT-pan))>>8;\r
+                                                       vnf->rvolsel=(vol*pan)>>8;\r
+                                               } else\r
+                                                       vnf->lvolsel=vnf->rvolsel=vol/2;\r
+                                       } else\r
+                                               vnf->lvolsel=vol;\r
+\r
+                                       idxsize = (vnf->size)? ((SLONGLONG)vnf->size << FRACBITS)-1 : 0;\r
+                                       idxlend = (vnf->repend)? ((SLONGLONG)vnf->repend << FRACBITS)-1 : 0;\r
+                                       idxlpos = (SLONGLONG)vnf->reppos << FRACBITS;\r
+                                       AddChannel(vc_tickbuf, portion);\r
+                               }\r
+                       }\r
+\r
+                       if(md_reverb) {\r
+                               if(md_reverb>15) md_reverb=15;\r
+                               MixReverb(vc_tickbuf, portion);\r
+                       }\r
+\r
+                       if(vc_mode & DMODE_FLOAT)\r
+                               Mix32ToFP((float*) buffer, vc_tickbuf, count);\r
+                       else if(vc_mode & DMODE_16BITS)\r
+                               Mix32To16((SWORD*) buffer, vc_tickbuf, count);\r
+                       else\r
+                               Mix32To8((SBYTE*) buffer, vc_tickbuf, count);\r
+\r
+                       buffer += samples2bytes(portion);\r
+                       left   -= portion;\r
+               }\r
+       }\r
+}\r
+\r
+BOOL VC1_Init(void)\r
+{\r
+       VC_SetupPointers();\r
+       \r
+       if (md_mode&DMODE_HQMIXER)\r
+               return VC2_Init();\r
+\r
+       if(!(Samples=(SWORD**)_mm_calloc(MAXSAMPLEHANDLES,sizeof(SWORD*)))) {\r
+               _mm_errno = MMERR_INITIALIZING_MIXER;\r
+               return 1;\r
+       }\r
+       if(!vc_tickbuf)\r
+               if(!(vc_tickbuf=(SLONG*)_mm_malloc((TICKLSIZE+32)*sizeof(SLONG)))) {\r
+                       _mm_errno = MMERR_INITIALIZING_MIXER;\r
+                       return 1;\r
+               }\r
+\r
+       MixReverb=(md_mode&DMODE_STEREO)?MixReverb_Stereo:MixReverb_Normal;\r
+       vc_mode = md_mode;\r
+       return 0;\r
+}\r
+\r
+BOOL VC1_PlayStart(void)\r
+{\r
+       samplesthatfit=TICKLSIZE;\r
+       if(vc_mode & DMODE_STEREO) samplesthatfit >>= 1;\r
+       tickleft = 0;\r
+\r
+       RVc1 = (5000L * md_mixfreq) / REVERBERATION;\r
+       RVc2 = (5078L * md_mixfreq) / REVERBERATION;\r
+       RVc3 = (5313L * md_mixfreq) / REVERBERATION;\r
+       RVc4 = (5703L * md_mixfreq) / REVERBERATION;\r
+       RVc5 = (6250L * md_mixfreq) / REVERBERATION;\r
+       RVc6 = (6953L * md_mixfreq) / REVERBERATION;\r
+       RVc7 = (7813L * md_mixfreq) / REVERBERATION;\r
+       RVc8 = (8828L * md_mixfreq) / REVERBERATION;\r
+\r
+       if(!(RVbufL1=(SLONG*)_mm_calloc((RVc1+1),sizeof(SLONG)))) return 1;\r
+       if(!(RVbufL2=(SLONG*)_mm_calloc((RVc2+1),sizeof(SLONG)))) return 1;\r
+       if(!(RVbufL3=(SLONG*)_mm_calloc((RVc3+1),sizeof(SLONG)))) return 1;\r
+       if(!(RVbufL4=(SLONG*)_mm_calloc((RVc4+1),sizeof(SLONG)))) return 1;\r
+       if(!(RVbufL5=(SLONG*)_mm_calloc((RVc5+1),sizeof(SLONG)))) return 1;\r
+       if(!(RVbufL6=(SLONG*)_mm_calloc((RVc6+1),sizeof(SLONG)))) return 1;\r
+       if(!(RVbufL7=(SLONG*)_mm_calloc((RVc7+1),sizeof(SLONG)))) return 1;\r
+       if(!(RVbufL8=(SLONG*)_mm_calloc((RVc8+1),sizeof(SLONG)))) return 1;\r
+\r
+       if(!(RVbufR1=(SLONG*)_mm_calloc((RVc1+1),sizeof(SLONG)))) return 1;\r
+       if(!(RVbufR2=(SLONG*)_mm_calloc((RVc2+1),sizeof(SLONG)))) return 1;\r
+       if(!(RVbufR3=(SLONG*)_mm_calloc((RVc3+1),sizeof(SLONG)))) return 1;\r
+       if(!(RVbufR4=(SLONG*)_mm_calloc((RVc4+1),sizeof(SLONG)))) return 1;\r
+       if(!(RVbufR5=(SLONG*)_mm_calloc((RVc5+1),sizeof(SLONG)))) return 1;\r
+       if(!(RVbufR6=(SLONG*)_mm_calloc((RVc6+1),sizeof(SLONG)))) return 1;\r
+       if(!(RVbufR7=(SLONG*)_mm_calloc((RVc7+1),sizeof(SLONG)))) return 1;\r
+       if(!(RVbufR8=(SLONG*)_mm_calloc((RVc8+1),sizeof(SLONG)))) return 1;\r
+\r
+       RVRindex = 0;\r
+       return 0;\r
+}\r
+\r
+void VC1_PlayStop(void)\r
+{\r
+       if(RVbufL1) free(RVbufL1);\r
+       if(RVbufL2) free(RVbufL2);\r
+       if(RVbufL3) free(RVbufL3);\r
+       if(RVbufL4) free(RVbufL4);\r
+       if(RVbufL5) free(RVbufL5);\r
+       if(RVbufL6) free(RVbufL6);\r
+       if(RVbufL7) free(RVbufL7);\r
+       if(RVbufL8) free(RVbufL8);\r
+       RVbufL1=RVbufL2=RVbufL3=RVbufL4=RVbufL5=RVbufL6=RVbufL7=RVbufL8=NULL;\r
+       if(RVbufR1) free(RVbufR1);\r
+       if(RVbufR2) free(RVbufR2);\r
+       if(RVbufR3) free(RVbufR3);\r
+       if(RVbufR4) free(RVbufR4);\r
+       if(RVbufR5) free(RVbufR5);\r
+       if(RVbufR6) free(RVbufR6);\r
+       if(RVbufR7) free(RVbufR7);\r
+       if(RVbufR8) free(RVbufR8);\r
+       RVbufR1=RVbufR2=RVbufR3=RVbufR4=RVbufR5=RVbufR6=RVbufR7=RVbufR8=NULL;\r
+}\r
+\r
+BOOL VC1_SetNumVoices(void)\r
+{\r
+       int t;\r
+\r
+       if(!(vc_softchn=md_softchn)) return 0;\r
+\r
+       if(vinf) free(vinf);\r
+       if(!(vinf= _mm_calloc(sizeof(VINFO),vc_softchn))) return 1;\r
+\r
+       for(t=0;t<vc_softchn;t++) {\r
+               vinf[t].frq=10000;\r
+               vinf[t].pan=(t&1)?PAN_LEFT:PAN_RIGHT;\r
+       }\r
+\r
+       return 0;\r
+}\r
+\r
+/* ex:set ts=4: */\r