]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - snd_wav.c
- SFXs no longer allocate mempools, they use the sound mempool directly.
[xonotic/darkplaces.git] / snd_wav.c
index e11c24563f5684ea674d2b19768a66b33f374dd8..d8af145438f82936a48b894b86bb201ec9b59a0d 100644 (file)
--- a/snd_wav.c
+++ b/snd_wav.c
@@ -23,6 +23,7 @@
 
 
 #include "quakedef.h"
+#include "snd_main.h"
 #include "snd_wav.h"
 
 
@@ -203,7 +204,10 @@ static wavinfo_t GetWavinfo (char *name, qbyte *wav, int wavlength)
        if (info.samples)
        {
                if (samples < info.samples)
-                       Host_Error ("Sound %s has a bad loop length", name);
+               {
+                       Con_Printf ("Sound %s has a bad loop length", name);
+                       info.samples = samples;
+               }
        }
        else
                info.samples = samples;
@@ -224,8 +228,24 @@ static const sfxbuffer_t* WAV_FetchSound (channel_t* ch, unsigned int start, uns
        return ch->sfx->fetcher_data;
 }
 
+/*
+====================
+WAV_FreeSfx
+====================
+*/
+static void WAV_FreeSfx (sfx_t* sfx)
+{
+       sfxbuffer_t* sb = sfx->fetcher_data;
+
+       // Free the sound buffer
+       sfx->memsize -= (sb->length * sfx->format.channels * sfx->format.width) + sizeof (*sb) - sizeof (sb->data);
+       Mem_Free(sb);
+
+       sfx->fetcher_data = NULL;
+       sfx->fetcher = NULL;
+}
 
-snd_fetcher_t wav_fetcher = { WAV_FetchSound, NULL };
+const snd_fetcher_t wav_fetcher = { WAV_FetchSound, NULL, WAV_FreeSfx };
 
 
 /*
@@ -238,48 +258,51 @@ qboolean S_LoadWavFile (const char *filename, sfx_t *s)
        qbyte *data;
        wavinfo_t info;
        int len;
+       size_t memsize;
        sfxbuffer_t* sb;
 
-       Mem_FreePool (&s->mempool);
-       s->mempool = Mem_AllocPool(s->name);
+       // Already loaded?
+       if (s->fetcher != NULL)
+               return true;
 
        // Load the file
-       data = FS_LoadFile(filename, s->mempool, false);
+       data = FS_LoadFile(filename, snd_mempool, false);
        if (!data)
-       {
-               Mem_FreePool (&s->mempool);
                return false;
-       }
 
        // Don't try to load it if it's not a WAV file
        if (memcmp (data, "RIFF", 4) || memcmp (data + 8, "WAVE", 4))
        {
-               Mem_FreePool (&s->mempool);
+               Mem_Free(data);
                return false;
        }
 
        Con_DPrintf ("Loading WAV file \"%s\"\n", filename);
 
-       info = GetWavinfo (s->name, data, fs_filesize);
+       info = GetWavinfo (s->name, data, (int)fs_filesize);
        // Stereo sounds are allowed (intended for music)
        if (info.channels < 1 || info.channels > 2)
        {
                Con_Printf("%s has an unsupported number of channels (%i)\n",s->name, info.channels);
-               Mem_FreePool (&s->mempool);
+               Mem_Free(data);
                return false;
        }
+       //if (info.channels == 2)
+       //      Log_Printf("stereosounds.log", "%s\n", s->name);
 
        // calculate resampled length
        len = (int) ((double) info.samples * (double) shm->format.speed / (double) info.rate);
        len = len * info.width * info.channels;
 
-       sb = Mem_Alloc (s->mempool, len + sizeof (*sb) - sizeof (sb->data));
+       memsize = len + sizeof (*sb) - sizeof (sb->data);
+       sb = Mem_Alloc (snd_mempool, memsize);
        if (sb == NULL)
        {
                Con_Printf("failed to allocate memory for sound \"%s\"\n", s->name);
-               Mem_FreePool(&s->mempool);
+               Mem_Free(data);
                return false;
        }
+       s->memsize += memsize;
 
        s->fetcher = &wav_fetcher;
        s->fetcher_data = sb;
@@ -290,6 +313,7 @@ qboolean S_LoadWavFile (const char *filename, sfx_t *s)
                s->loopstart = -1;
        else
                s->loopstart = (double)info.loopstart * (double)shm->format.speed / (double)s->format.speed;
+       s->flags &= ~SFXFLAG_STREAMED;
 
 #if BYTE_ORDER != LITTLE_ENDIAN
        // We must convert the WAV data from little endian
@@ -306,7 +330,7 @@ qboolean S_LoadWavFile (const char *filename, sfx_t *s)
        }
 #endif
 
-       sb->length = ResampleSfx (data + info.dataofs, info.samples, &s->format, sb->data, s->name);
+       sb->length = (int)ResampleSfx (data + info.dataofs, info.samples, &s->format, sb->data, s->name);
        s->format.speed = shm->format.speed;
        s->total_length = sb->length;
        sb->offset = 0;