]> git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - snd_dma.c
Modified the sound code so it can handle sounds outside of a "sound" subdirectory.
[xonotic/darkplaces.git] / snd_dma.c
index 24525fee956237fd4e42020ca014fe6a4f33c2ba..01e48254e40049b73f734bdbec91995acb982c08 100644 (file)
--- a/snd_dma.c
+++ b/snd_dma.c
@@ -40,6 +40,9 @@ void S_Update_();
 void S_StopAllSounds(qboolean clear);
 void S_StopAllSoundsC(void);
 
+void S_ClearBuffer (void);
+
+
 // =======================================================================
 // Internal sound data & structures
 // =======================================================================
@@ -48,7 +51,6 @@ channel_t channels[MAX_CHANNELS];
 unsigned int total_channels;
 
 int snd_blocked = 0;
-static qboolean snd_ambient = 1;
 cvar_t snd_initialized = { CVAR_READONLY, "snd_initialized", "0"};
 cvar_t snd_streaming = { CVAR_SAVE, "snd_streaming", "1"};
 
@@ -107,18 +109,6 @@ qboolean fakedma = false;
 int fakedma_updates = 15;
 
 
-void S_AmbientOff (void)
-{
-       snd_ambient = false;
-}
-
-
-void S_AmbientOn (void)
-{
-       snd_ambient = true;
-}
-
-
 void S_SoundInfo_f(void)
 {
        if (!sound_started || !shm)
@@ -132,7 +122,7 @@ void S_SoundInfo_f(void)
        Con_Printf("%5d samplepos\n", shm->samplepos);
        Con_Printf("%5d samplebits\n", shm->format.width * 8);
        Con_Printf("%5d speed\n", shm->format.speed);
-       Con_Printf("0x%x dma buffer\n", shm->buffer);
+       Con_Printf("%p dma buffer\n", shm->buffer);
        Con_Printf("%5u total_channels\n", total_channels);
 }
 
@@ -261,8 +251,8 @@ void S_Init(void)
 
        SND_InitScaletable ();
 
-       ambient_sfx[AMBIENT_WATER] = S_PrecacheSound ("ambience/water1.wav", false);
-       ambient_sfx[AMBIENT_SKY] = S_PrecacheSound ("ambience/wind2.wav", false);
+       ambient_sfx[AMBIENT_WATER] = S_PrecacheSound ("ambience/water1.wav", false, true);
+       ambient_sfx[AMBIENT_SKY] = S_PrecacheSound ("ambience/wind2.wav", false, true);
 
        total_channels = MAX_DYNAMIC_CHANNELS + NUM_AMBIENTS;   // no statics
        memset(channels, 0, MAX_CHANNELS * sizeof(channel_t));
@@ -281,21 +271,25 @@ S_GetCached
 
 =========
 */
-sfx_t *S_GetCached (const char *name)
+sfx_t *S_GetCached (const char *name, qboolean stdpath)
 {
+       char namebuffer [MAX_QPATH];
+       size_t len;
        int i;
 
        if (!snd_initialized.integer)
                return NULL;
 
        if (!name)
-               Host_Error("S_IsCached: NULL\n");
+               Host_Error("S_GetCached: NULL");
 
-       if (strlen(name) >= MAX_QPATH)
-               Host_Error("Sound name too long: %s", name);
+       // Add the default sound directory to the path
+       len = snprintf (namebuffer, sizeof (namebuffer), stdpath ? "sound/%s" : "%s", name);
+       if (len >= sizeof (namebuffer))
+               Host_Error ("S_GetCached: sound name too long (%s)", name);
 
        for(i = 0;i < num_sfx;i++)
-               if(!strcmp(known_sfx[i].name, name))
+               if(!strcmp(known_sfx[i].name, namebuffer))
                        return &known_sfx[i];
 
        return NULL;
@@ -307,31 +301,22 @@ S_FindName
 
 ==================
 */
-sfx_t *S_FindName (char *name)
+sfx_t *S_FindName (const char *name, qboolean stdpath)
 {
-       int i;
        sfx_t *sfx;
 
-       if (!snd_initialized.integer)
-               return NULL;
-
-       if (!name)
-               Host_Error("S_FindName: NULL\n");
-
-       if (strlen(name) >= MAX_QPATH)
-               Host_Error("Sound name too long: %s", name);
+       sfx = S_GetCached (name, stdpath);
 
-// see if already loaded
-       for (i = 0;i < num_sfx;i++)
-               if (!strcmp(known_sfx[i].name, name))
-                       return &known_sfx[i];
-
-       if (num_sfx == MAX_SFX)
-               Sys_Error("S_FindName: out of sfx_t");
+       // If we haven't allocated a sfx_t struct for it yet
+       if (sfx == NULL)
+       {
+               if (num_sfx == MAX_SFX)
+                       Sys_Error ("S_FindName: out of sfx_t");
 
-       sfx = &known_sfx[num_sfx++];
-       memset(sfx, 0, sizeof(*sfx));
-       strlcpy (sfx->name, name, sizeof (sfx->name));
+               sfx = &known_sfx[num_sfx++];
+               memset (sfx, 0, sizeof(*sfx));
+               snprintf (sfx->name, sizeof (sfx->name), stdpath ? "sound/%s" : "%s", name);
+       }
        return sfx;
 }
 
@@ -342,11 +327,11 @@ S_TouchSound
 
 ==================
 */
-void S_TouchSound (char *name)
+void S_TouchSound (const char *name, qboolean stdpath)
 {
        sfx_t *sfx;
 
-       sfx = S_FindName (name);
+       sfx = S_FindName (name, stdpath);
 
        // Set the "used" flag for this sound
        if (sfx != NULL)
@@ -397,14 +382,14 @@ S_PrecacheSound
 
 ==================
 */
-sfx_t *S_PrecacheSound (char *name, int complain)
+sfx_t *S_PrecacheSound (const char *name, qboolean complain, qboolean stdpath)
 {
        sfx_t *sfx;
 
        if (!snd_initialized.integer)
                return NULL;
 
-       sfx = S_FindName(name);
+       sfx = S_FindName (name, stdpath);
 
        if (!nosound.integer && snd_precache.integer)
                S_LoadSound(sfx, complain);
@@ -527,7 +512,7 @@ void SND_Spatialize(channel_t *ch, int isstatic)
 // Start a sound effect
 // =======================================================================
 
-void S_StartSound(int entnum, int entchannel, sfx_t *sfx, vec3_t origin, float fvol, float attenuation)
+int S_StartSound(int entnum, int entchannel, sfx_t *sfx, vec3_t origin, float fvol, float attenuation)
 {
        channel_t *target_chan, *check;
        int             vol;
@@ -535,14 +520,14 @@ void S_StartSound(int entnum, int entchannel, sfx_t *sfx, vec3_t origin, float f
        size_t  skip;
 
        if (!sound_started || !sfx || !sfx->fetcher || nosound.integer)
-               return;
+               return -1;
 
        vol = fvol*255;
 
 // pick a channel to play on
        target_chan = SND_PickChannel(entnum, entchannel);
        if (!target_chan)
-               return;
+               return -1;
 
 // spatialize
        memset (target_chan, 0, sizeof(*target_chan));
@@ -561,7 +546,7 @@ void S_StartSound(int entnum, int entchannel, sfx_t *sfx, vec3_t origin, float f
        if (!S_LoadSound (sfx, true))
        {
                target_chan->sfx = NULL;
-               return;         // couldn't load the sound's data
+               return -1;              // couldn't load the sound's data
        }
 
        target_chan->sfx = sfx;
@@ -590,26 +575,66 @@ void S_StartSound(int entnum, int entchannel, sfx_t *sfx, vec3_t origin, float f
                        break;
                }
        }
+
+       return (target_chan - channels);
 }
 
-void S_StopSound(int entnum, int entchannel)
+void S_StopChannel (unsigned int channel_ind)
 {
-       int i;
+       channel_t *ch;
+
+       if (channel_ind >= total_channels)
+               return;
 
-       for (i=0 ; i<MAX_DYNAMIC_CHANNELS ; i++)
+       ch = &channels[channel_ind];
+       if (ch->sfx != NULL)
        {
-               if (channels[i].entnum == entnum
-                       && channels[i].entchannel == entchannel)
+               if (ch->sfx->fetcher != NULL)
                {
-                       channels[i].end = 0;
-                       channels[i].sfx = NULL;
-                       return;
+                       snd_fetcher_end_t fetcher_end = ch->sfx->fetcher->end;
+                       if (fetcher_end != NULL)
+                               fetcher_end (ch);
                }
+               ch->sfx = NULL;
        }
+       ch->end = 0;
+}
+
+void S_PauseChannel (unsigned int channel_ind, qboolean toggle)
+{
+       if (toggle)
+               channels[channel_ind].flags |= CHANNELFLAG_PAUSED;
+       else
+               channels[channel_ind].flags &= ~CHANNELFLAG_PAUSED;
+}
+
+void S_LoopChannel (unsigned int channel_ind, qboolean toggle)
+{
+       if (toggle)
+               channels[channel_ind].flags |= CHANNELFLAG_FORCELOOP;
+       else
+               channels[channel_ind].flags &= ~CHANNELFLAG_FORCELOOP;
+}
+
+void S_StopSound(int entnum, int entchannel)
+{
+       unsigned int i;
+
+       for (i = 0; i < MAX_DYNAMIC_CHANNELS; i++)
+               if (channels[i].entnum == entnum && channels[i].entchannel == entchannel)
+               {
+                       S_StopChannel (i);
+                       return;
+               }
 }
 
 void S_StopAllSounds(qboolean clear)
 {
+       unsigned int i;
+
+       for (i = 0; i < total_channels; i++)
+               S_StopChannel (i);
+
        total_channels = MAX_DYNAMIC_CHANNELS + NUM_AMBIENTS;   // no statics
        memset(channels, 0, MAX_CHANNELS * sizeof(channel_t));
 
@@ -622,6 +647,40 @@ void S_StopAllSoundsC(void)
        S_StopAllSounds(true);
 }
 
+void S_PauseGameSounds (void)
+{
+       unsigned int i;
+
+       for (i = 0; i < total_channels; i++)
+       {
+               channel_t *ch;
+
+               ch = &channels[i];
+               if (ch->sfx != NULL && ! (ch->flags & CHANNELFLAG_LOCALSOUND))
+                       ch->flags |= CHANNELFLAG_PAUSED;
+       }
+}
+
+void S_ResumeGameSounds (void)
+{
+       unsigned int i;
+
+       for (i = 0; i < total_channels; i++)
+       {
+               channel_t *ch;
+
+               ch = &channels[i];
+               if (ch->sfx != NULL && ! (ch->flags & CHANNELFLAG_LOCALSOUND))
+                       ch->flags &= ~CHANNELFLAG_PAUSED;
+       }
+}
+
+void S_SetChannelVolume (unsigned int ch_ind, float fvol)
+{
+       channels[ch_ind].master_vol = fvol * 255;
+}
+
+
 void S_ClearBuffer(void)
 {
        int             clear;
@@ -740,7 +799,7 @@ void S_UpdateAmbientSounds (void)
        for (ambient_channel = 0 ; ambient_channel < NUM_AMBIENTS;ambient_channel++)
                channels[ambient_channel].sfx = NULL;
 
-       if (!snd_ambient || ambient_level.value <= 0 || !cl.worldmodel || !cl.worldmodel->brush.AmbientSoundLevelsForPoint)
+       if (ambient_level.value <= 0 || !cl.worldmodel || !cl.worldmodel->brush.AmbientSoundLevelsForPoint)
                return;
 
        cl.worldmodel->brush.AmbientSoundLevelsForPoint(cl.worldmodel, listener_vieworigin, ambientlevels, sizeof(ambientlevels));
@@ -963,7 +1022,7 @@ console functions
 
 static void S_Play_Common(float fvol, float attenuation)
 {
-       int     i;
+       int     i, ch_ind;
        char name[256];
        sfx_t   *sfx;
 
@@ -974,7 +1033,7 @@ static void S_Play_Common(float fvol, float attenuation)
                        snprintf(name, sizeof(name), "%s.wav", Cmd_Argv(i));
                else
                        strlcpy(name, Cmd_Argv(i), sizeof(name));
-               sfx = S_PrecacheSound(name, true);
+               sfx = S_PrecacheSound(name, true, true);
 
                // If we need to get the volume from the command line
                if (fvol == -1.0f)
@@ -985,7 +1044,9 @@ static void S_Play_Common(float fvol, float attenuation)
                else
                        i++;
 
-               S_StartSound(-1, 0, sfx, listener_vieworigin, fvol, attenuation);
+               ch_ind = S_StartSound(-1, 0, sfx, listener_vieworigin, fvol, attenuation);
+               if (ch_ind >= 0)
+                       channels[ch_ind].flags |= CHANNELFLAG_LOCALSOUND;
        }
 }
 
@@ -1017,27 +1078,37 @@ void S_SoundList(void)
                {
                        size = sfx->mempool->totalsize;
                        total += size;
-                       Con_Printf("%c(%2db) %7i : %s\n", sfx->loopstart >= 0 ? 'L' : ' ', sfx->format.width * 8, size, sfx->name);
+                       Con_Printf ("%c%c(%2db, %6s) %8i : %s\n",
+                                               (sfx->loopstart >= 0) ? 'L' : ' ',
+                                               (sfx->flags & SFXFLAG_STREAMED) ? 'S' : ' ',
+                                               sfx->format.width * 8,
+                                               (sfx->format.channels == 2) ? "stereo" : "mono",
+                                               size,
+                                               sfx->name);
                }
        }
        Con_Printf("Total resident: %i\n", total);
 }
 
 
-void S_LocalSound (char *sound)
+void S_LocalSound (const char *sound, qboolean stdpath)
 {
        sfx_t   *sfx;
+       int             ch_ind;
 
        if (!snd_initialized.integer || nosound.integer)
                return;
 
-       sfx = S_PrecacheSound (sound, true);
+       sfx = S_PrecacheSound (sound, true, stdpath);
        if (!sfx)
        {
                Con_Printf("S_LocalSound: can't precache %s\n", sound);
                return;
        }
-       S_StartSound (cl.viewentity, -1, sfx, vec3_origin, 1, 1);
+
+       ch_ind = S_StartSound (cl.viewentity, -1, sfx, vec3_origin, 1, 1);
+       if (ch_ind >= 0)
+               channels[ch_ind].flags |= CHANNELFLAG_LOCALSOUND;
 }