44.1khz sound, interpolated resampling, killed cvar loadas8bit
authorlordhavoc <lordhavoc@d7cf8633-e32d-0410-b094-e92efae38249>
Thu, 21 Sep 2000 03:07:58 +0000 (03:07 +0000)
committerlordhavoc <lordhavoc@d7cf8633-e32d-0410-b094-e92efae38249>
Thu, 21 Sep 2000 03:07:58 +0000 (03:07 +0000)
git-svn-id: svn://svn.icculus.org/twilight/trunk/darkplaces@38 d7cf8633-e32d-0410-b094-e92efae38249

snd_dma.c
snd_mem.c
snd_win.c

index 7c28e3a..a66e329 100644 (file)
--- a/snd_dma.c
+++ b/snd_dma.c
@@ -64,7 +64,7 @@ int                   num_sfx;
 
 sfx_t          *ambient_sfx[NUM_AMBIENTS];
 
-int            desired_speed = 11025;
+int            desired_speed = 44100; //11025; // LordHavoc: 44.1khz sound
 int            desired_bits = 16;
 
 int sound_started=0;
@@ -74,7 +74,7 @@ cvar_t volume = {"volume", "0.7", true};
 
 cvar_t nosound = {"nosound", "0"};
 cvar_t precache = {"precache", "1"};
-cvar_t loadas8bit = {"loadas8bit", "0"};
+//cvar_t loadas8bit = {"loadas8bit", "0"};
 cvar_t bgmbuffer = {"bgmbuffer", "4096"};
 cvar_t ambient_level = {"ambient_level", "0.3"};
 cvar_t ambient_fade = {"ambient_fade", "100"};
@@ -189,7 +189,7 @@ void S_Init (void)
        Cvar_RegisterVariable(&nosound);
        Cvar_RegisterVariable(&volume);
        Cvar_RegisterVariable(&precache);
-       Cvar_RegisterVariable(&loadas8bit);
+//     Cvar_RegisterVariable(&loadas8bit);
        Cvar_RegisterVariable(&bgmvolume);
        Cvar_RegisterVariable(&bgmbuffer);
        Cvar_RegisterVariable(&ambient_level);
@@ -199,11 +199,13 @@ void S_Init (void)
        Cvar_RegisterVariable(&_snd_mixahead);
        Cvar_RegisterVariable(&snd_swapstereo); // LordHavoc: for people with backwards sound wiring
 
+       /*
        if (host_parms.memsize < 0x800000)
        {
                Cvar_Set ("loadas8bit", "1");
                Con_Printf ("loading all sounds as 8bit\n");
        }
+       */
 
 
 
index ca7fb36..5420d30 100644 (file)
--- a/snd_mem.c
+++ b/snd_mem.c
@@ -30,7 +30,7 @@ byte *S_Alloc (int size);
 ResampleSfx
 ================
 */
-void ResampleSfx (sfx_t *sfx, int inrate, int inwidth, byte *data)
+void ResampleSfx (sfx_t *sfx, int inrate, byte *data)
 {
        int             outcount;
        int             srcsample;
@@ -51,15 +51,15 @@ void ResampleSfx (sfx_t *sfx, int inrate, int inwidth, byte *data)
                sc->loopstart = sc->loopstart / stepscale;
 
        sc->speed = shm->speed;
-       if (loadas8bit.value)
-               sc->width = 1;
-       else
-               sc->width = inwidth;
+//     if (loadas8bit.value)
+//             sc->width = 1;
+//     else
+//             sc->width = inwidth;
 //     sc->stereo = 0;
 
 // resample / decimate to the current source rate
 
-       if (stepscale == 1 && inwidth == 1 && sc->width == 1)
+       if (stepscale == 1/* && inwidth == 1*/ && sc->width == 1)
        {
 // fast special case
                // LordHavoc: I do not serve the readability gods...
@@ -88,34 +88,67 @@ void ResampleSfx (sfx_t *sfx, int inrate, int inwidth, byte *data)
                }
                */
        }
+       else if (stepscale == 1/* && inwidth == 2*/ && sc->width == 2) // LordHavoc: quick case for 16bit
+       {
+               if (sc->stereo) // LordHavoc: stereo sound support
+                       for (i=0 ; i<outcount*2 ;i++)
+                               ((short *)sc->data)[i] = LittleShort (((short *)data)[i]);
+               else
+                       for (i=0 ; i<outcount ;i++)
+                               ((short *)sc->data)[i] = LittleShort (((short *)data)[i]);
+       }
        else
        {
 // general case
                Con_DPrintf("ResampleSfx: resampling sound %s\n", sfx->name);
                samplefrac = 0;
                fracstep = stepscale*256;
-               if (sc->stereo) // LordHavoc: stereo sound support
+               if ((fracstep & 255) == 0) // skipping points on perfect multiple
                {
+                       samplefrac >> 8;
+                       srcsample = 0;
                        if (sc->width == 2)
                        {
-                               for (i=0 ; i<outcount*2 ;)
+                               short *out = (void *)sc->data, *in = (void *)data;
+                               if (sc->stereo) // LordHavoc: stereo sound support
+                               {
+                                       fracstep <<= 1;
+                                       for (i=0 ; i<outcount ;)
+                                       {
+                                               *out++ = (short) LittleShort (in[srcsample  ]);
+                                               *out++ = (short) LittleShort (in[srcsample+1]);
+                                               srcsample += fracstep;
+                                       }
+                               }
+                               else
                                {
-                                       srcsample = samplefrac >> 8;
-                                       samplefrac += fracstep;
-                                       srcsample <<= 1;
-                                       ((short *)sc->data)[i++] = LittleShort ( ((short *)data)[srcsample++] ); // left
-                                       ((short *)sc->data)[i++] = LittleShort ( ((short *)data)[srcsample  ] ); // right
+                                       for (i=0 ; i<outcount ; i++)
+                                       {
+                                               *out++ = (short) LittleShort (in[srcsample  ]);
+                                               srcsample += fracstep;
+                                       }
                                }
                        }
                        else
                        {
-                               for (i=0 ; i<outcount*2 ;)
+                               signed char *out = (void *)sc->data, *in = (void *)data;
+                               if (sc->stereo) // LordHavoc: stereo sound support
+                               {
+                                       fracstep <<= 1;
+                                       for (i=0 ; i<outcount ;)
+                                       {
+                                               *out++ = (signed char) in[srcsample  ] - 128;
+                                               *out++ = (signed char) in[srcsample+1] - 128;
+                                               srcsample += fracstep;
+                                       }
+                               }
+                               else
                                {
-                                       srcsample = samplefrac >> 8;
-                                       samplefrac += fracstep;
-                                       srcsample <<= 1;
-                                       ((signed char *)sc->data)[i++] = ((int)( (unsigned char)(data[srcsample++]) - 128) << 8) >> 8; // left
-                                       ((signed char *)sc->data)[i++] = ((int)( (unsigned char)(data[srcsample  ]) - 128) << 8) >> 8; // right
+                                       for (i=0 ; i<outcount ; i++)
+                                       {
+                                               *out++ = (signed char) in[srcsample  ] - 128;
+                                               srcsample += fracstep;
+                                       }
                                }
                        }
                }
@@ -123,20 +156,48 @@ void ResampleSfx (sfx_t *sfx, int inrate, int inwidth, byte *data)
                {
                        if (sc->width == 2)
                        {
-                               for (i=0 ; i<outcount ; i++)
+                               short *out = (void *)sc->data, *in = (void *)data;
+                               if (sc->stereo) // LordHavoc: stereo sound support
                                {
-                                       srcsample = samplefrac >> 8;
-                                       samplefrac += fracstep;
-                                       ((short *)sc->data)[i] = LittleShort ( ((short *)data)[srcsample] );
+                                       for (i=0 ; i<outcount ;)
+                                       {
+                                               srcsample = (samplefrac >> 7) & ~1;
+                                               samplefrac += fracstep;
+                                               *out++ = (short) ((int) ((LittleShort (in[srcsample  ]) * (256 - (samplefrac & 255)) + LittleShort (in[srcsample+2]) * (samplefrac & 255)) >> 8));
+                                               *out++ = (short) ((int) ((LittleShort (in[srcsample+1]) * (256 - (samplefrac & 255)) + LittleShort (in[srcsample+3]) * (samplefrac & 255)) >> 8));
+                                       }
+                               }
+                               else
+                               {
+                                       for (i=0 ; i<outcount ; i++)
+                                       {
+                                               srcsample = samplefrac >> 8;
+                                               samplefrac += fracstep;
+                                               *out++ = (short) ((int) ((LittleShort (in[srcsample]  ) * (256 - (samplefrac & 255)) + LittleShort (in[srcsample+1]) * (samplefrac & 255)) >> 8));
+                                       }
                                }
                        }
                        else
                        {
-                               for (i=0 ; i<outcount ; i++)
+                               signed char *out = (void *)sc->data, *in = (void *)data;
+                               if (sc->stereo) // LordHavoc: stereo sound support
+                               {
+                                       for (i=0 ; i<outcount ;)
+                                       {
+                                               srcsample = (samplefrac >> 7) & ~1;
+                                               samplefrac += fracstep;
+                                               *out++ = (signed char) ((int) (((((unsigned char) in[srcsample  ] - 128) * (256 - (samplefrac & 255))) + (((unsigned char) in[srcsample+2] - 128) * (samplefrac & 255))) >> 8));
+                                               *out++ = (signed char) ((int) (((((unsigned char) in[srcsample+1] - 128) * (256 - (samplefrac & 255))) + (((unsigned char) in[srcsample+3] - 128) * (samplefrac & 255))) >> 8));
+                                       }
+                               }
+                               else
                                {
-                                       srcsample = samplefrac >> 8;
-                                       samplefrac += fracstep;
-                                       ((signed char *)sc->data)[i] = ((int)( (unsigned char)(data[srcsample]) - 128) << 8) >> 8;
+                                       for (i=0 ; i<outcount ; i++)
+                                       {
+                                               srcsample = samplefrac >> 8;
+                                               samplefrac += fracstep;
+                                               *out++ = (signed char) ((int) (((((unsigned char) in[srcsample  ] - 128) * (256 - (samplefrac & 255))) + (((unsigned char) in[srcsample+1] - 128) * (samplefrac & 255))) >> 8));
+                                       }
                                }
                        }
                }
@@ -210,7 +271,7 @@ sfxcache_t *S_LoadSound (sfx_t *s)
        sc->width = info.width;
        sc->stereo = info.channels == 2;
 
-       ResampleSfx (s, sc->speed, sc->width, data + info.dataofs);
+       ResampleSfx (s, sc->speed, data + info.dataofs);
 
        return sc;
 }
index c43f574..79f36bc 100644 (file)
--- a/snd_win.c
+++ b/snd_win.c
@@ -195,7 +195,7 @@ sndinitstat SNDDMA_InitDirect (void)
 
        shm->channels = 2;
        shm->samplebits = 16;
-       shm->speed = 11025;
+       shm->speed = 44100; // LordHavoc: 44.1khz sound
 
        memset (&format, 0, sizeof(format));
        format.wFormatTag = WAVE_FORMAT_PCM;
@@ -431,7 +431,7 @@ qboolean SNDDMA_InitWav (void)
 
        shm->channels = 2;
        shm->samplebits = 16;
-       shm->speed = 11025;
+       shm->speed = 44100; // LordHavoc: 44.1khz sound
 
        memset (&format, 0, sizeof(format));
        format.wFormatTag = WAVE_FORMAT_PCM;