]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - snd_wav.c
gave names to nearly all structs and enums which should make for better C++ error...
[xonotic/darkplaces.git] / snd_wav.c
index 439ec73cbaf4cab9dd281e75976cdb516785c966..59b2d49b52c492360d021feac5e82f6167439d0e 100644 (file)
--- a/snd_wav.c
+++ b/snd_wav.c
@@ -27,7 +27,7 @@
 #include "snd_wav.h"
 
 
-typedef struct
+typedef struct wavinfo_s
 {
        int             rate;
        int             width;
@@ -86,7 +86,7 @@ static void FindNextChunk(char *name)
                }
                data_p -= 8;
                last_chunk = data_p + 8 + ( (iff_chunk_len + 1) & ~1 );
-               if (!strncmp(data_p, name, 4))
+               if (!strncmp((const char *)data_p, name, 4))
                        return;
        }
 }
@@ -139,7 +139,7 @@ static wavinfo_t GetWavinfo (char *name, qbyte *wav, int wavlength)
 
        // find "RIFF" chunk
        FindChunk("RIFF");
-       if (!(data_p && !strncmp(data_p+8, "WAVE", 4)))
+       if (!(data_p && !strncmp((const char *)data_p+8, "WAVE", 4)))
        {
                Con_Print("Missing RIFF/WAVE chunks\n");
                return info;
@@ -179,7 +179,7 @@ static wavinfo_t GetWavinfo (char *name, qbyte *wav, int wavlength)
                FindNextChunk ("LIST");
                if (data_p)
                {
-                       if (!strncmp (data_p + 28, "mark", 4))
+                       if (!strncmp ((const char *)data_p + 28, "mark", 4))
                        {       // this is not a proper parse, but it works with cooledit...
                                data_p += 24;
                                i = GetLittleLong ();   // samples in loop
@@ -225,11 +225,27 @@ WAV_FetchSound
 */
 static const sfxbuffer_t* WAV_FetchSound (channel_t* ch, unsigned int start, unsigned int nbsamples)
 {
-       return ch->sfx->fetcher_data;
+       return (sfxbuffer_t *)ch->sfx->fetcher_data;
 }
 
+/*
+====================
+WAV_FreeSfx
+====================
+*/
+static void WAV_FreeSfx (sfx_t* sfx)
+{
+       sfxbuffer_t* sb = (sfxbuffer_t *)sfx->fetcher_data;
 
-snd_fetcher_t wav_fetcher = { WAV_FetchSound, NULL };
+       // 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;
+}
+
+const snd_fetcher_t wav_fetcher = { WAV_FetchSound, NULL, WAV_FreeSfx };
 
 
 /*
@@ -242,23 +258,22 @@ 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, 0, NULL);
+       // 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;
        }
 
@@ -269,7 +284,7 @@ qboolean S_LoadWavFile (const char *filename, sfx_t *s)
        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)
@@ -279,13 +294,15 @@ qboolean S_LoadWavFile (const char *filename, sfx_t *s)
        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 = (sfxbuffer_t *)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;