]> git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - snd_main.c
renamed NetConn_UpdateServerStuff to NetConn_UpdateSockets and made it non-static...
[xonotic/darkplaces.git] / snd_main.c
index 0708a15b32a39e5816ea9dcc717c4a90d3c8c661..4030d3878dcd94a36aee7c4ce853f48ad3f7b67d 100644 (file)
@@ -39,7 +39,6 @@ typedef struct speakerlayout_s
 {
        const char *name;
        unsigned int channels;
-       qboolean headphones;
        listener_t listeners[SND_LISTENERS];
 }
 speakerlayout_t;
@@ -61,9 +60,8 @@ channel_t channels[MAX_CHANNELS];
 unsigned int total_channels;
 
 int snd_blocked = 0;
-cvar_t snd_initialized = { CVAR_READONLY, "snd_initialized", "0"};
-cvar_t snd_streaming = { CVAR_SAVE, "snd_streaming", "1"};
-cvar_t snd_headphones = { CVAR_SAVE, "snd_headphones", "0"};
+cvar_t snd_initialized = { CVAR_READONLY, "snd_initialized", "0", "indicates the sound subsystem is active"};
+cvar_t snd_streaming = { CVAR_SAVE, "snd_streaming", "1", "enables keeping compressed ogg sound files compressed, decompressing them only as needed, otherwise they will be decompressed completely at load (may use a lot of memory)"};
 
 volatile dma_t *shm = 0;
 volatile dma_t sn;
@@ -79,26 +77,25 @@ int paintedtime;
 // Linked list of known sfx
 sfx_t *known_sfx = NULL;
 
-qboolean sound_started = false;
 qboolean sound_spatialized = false;
 
 // Fake dma is a synchronous faking of the DMA progress used for
 // isolating performance in the renderer.
 qboolean fakedma = false;
 
-cvar_t bgmvolume = {CVAR_SAVE, "bgmvolume", "1"};
-cvar_t volume = {CVAR_SAVE, "volume", "0.7"};
-cvar_t snd_staticvolume = {CVAR_SAVE, "snd_staticvolume", "1"};
+cvar_t bgmvolume = {CVAR_SAVE, "bgmvolume", "1", "volume of background music (such as CD music or replacement files such as sound/cdtracks/track002.ogg)"};
+cvar_t volume = {CVAR_SAVE, "volume", "0.7", "volume of sound effects"};
+cvar_t snd_staticvolume = {CVAR_SAVE, "snd_staticvolume", "1", "volume of ambient sound effects (such as swampy sounds at the start of e1m2)"};
 
-cvar_t nosound = {0, "nosound", "0"};
-cvar_t snd_precache = {0, "snd_precache", "1"};
-cvar_t bgmbuffer = {0, "bgmbuffer", "4096"};
-cvar_t ambient_level = {0, "ambient_level", "0.3"};
-cvar_t ambient_fade = {0, "ambient_fade", "100"};
-cvar_t snd_noextraupdate = {0, "snd_noextraupdate", "0"};
-cvar_t snd_show = {0, "snd_show", "0"};
-cvar_t _snd_mixahead = {CVAR_SAVE, "_snd_mixahead", "0.1"};
-cvar_t snd_swapstereo = {CVAR_SAVE, "snd_swapstereo", "0"};
+cvar_t nosound = {0, "nosound", "0", "disables sound"};
+cvar_t snd_precache = {0, "snd_precache", "1", "loads sounds before they are used"};
+//cvar_t bgmbuffer = {0, "bgmbuffer", "4096", "unused quake cvar"};
+cvar_t ambient_level = {0, "ambient_level", "0.3", "volume of environment noises (water and wind)"};
+cvar_t ambient_fade = {0, "ambient_fade", "100", "rate of volume fading when moving from one environment to another"};
+cvar_t snd_noextraupdate = {0, "snd_noextraupdate", "0", "disables extra sound mixer calls that are meant to reduce the chance of sound breakup at very low framerates"};
+cvar_t snd_show = {0, "snd_show", "0", "shows some statistics about sound mixing"};
+cvar_t _snd_mixahead = {CVAR_SAVE, "_snd_mixahead", "0.1", "how much sound to mix ahead of time"};
+cvar_t snd_swapstereo = {CVAR_SAVE, "snd_swapstereo", "0", "swaps left/right speakers for old ISA soundblaster cards"};
 
 // Ambient sounds
 sfx_t* ambient_sfxs [2] = { NULL, NULL };
@@ -114,7 +111,7 @@ void S_FreeSfx (sfx_t *sfx, qboolean force);
 
 void S_SoundInfo_f(void)
 {
-       if (!sound_started)
+       if (!shm)
        {
                Con_Print("sound system not started\n");
                return;
@@ -156,21 +153,18 @@ void S_Startup(void)
                {
                        Con_Print("S_Startup: SNDDMA_Init failed.\n");
                        shm = NULL;
-                       sound_started = false;
                        sound_spatialized = false;
                        return;
                }
        }
 
-       sound_started = true;
-
        Con_Printf("Sound format: %dHz, %d bit, %d channels\n", shm->format.speed,
                           shm->format.width * 8, shm->format.channels);
 }
 
 void S_Shutdown(void)
 {
-       if (!sound_started)
+       if (!shm)
                return;
 
        if (fakedma)
@@ -179,7 +173,6 @@ void S_Shutdown(void)
                SNDDMA_Shutdown();
 
        shm = NULL;
-       sound_started = false;
        sound_spatialized = false;
 }
 
@@ -201,7 +194,6 @@ void S_Init(void)
        Cvar_RegisterVariable(&volume);
        Cvar_RegisterVariable(&bgmvolume);
        Cvar_RegisterVariable(&snd_staticvolume);
-       Cvar_RegisterVariable(&snd_headphones);
 
 // COMMANDLINEOPTION: Sound: -nosound disables sound (including CD audio)
        if (COM_CheckParm("-nosound") || COM_CheckParm("-safe"))
@@ -213,19 +205,19 @@ void S_Init(void)
        if (COM_CheckParm("-simsound"))
                fakedma = true;
 
-       Cmd_AddCommand("play", S_Play);
-       Cmd_AddCommand("play2", S_Play2);
-       Cmd_AddCommand("playvol", S_PlayVol);
-       Cmd_AddCommand("stopsound", S_StopAllSounds);
-       Cmd_AddCommand("soundlist", S_SoundList);
-       Cmd_AddCommand("soundinfo", S_SoundInfo_f);
-       Cmd_AddCommand("snd_restart", S_Restart_f);
+       Cmd_AddCommand("play", S_Play, "play a sound at your current location (not heard by anyone else)");
+       Cmd_AddCommand("play2", S_Play2, "play a sound globally throughout the level (not heard by anyone else)");
+       Cmd_AddCommand("playvol", S_PlayVol, "play a sound at the specified volume level at your current location (not heard by anyone else)");
+       Cmd_AddCommand("stopsound", S_StopAllSounds, "silence");
+       Cmd_AddCommand("soundlist", S_SoundList, "list loaded sounds");
+       Cmd_AddCommand("soundinfo", S_SoundInfo_f, "print sound system information (such as channels and speed)");
+       Cmd_AddCommand("snd_restart", S_Restart_f, "restart sound system");
 
        Cvar_RegisterVariable(&nosound);
        Cvar_RegisterVariable(&snd_precache);
        Cvar_RegisterVariable(&snd_initialized);
        Cvar_RegisterVariable(&snd_streaming);
-       Cvar_RegisterVariable(&bgmbuffer);
+       //Cvar_RegisterVariable(&bgmbuffer);
        Cvar_RegisterVariable(&ambient_level);
        Cvar_RegisterVariable(&ambient_fade);
        Cvar_RegisterVariable(&snd_noextraupdate);
@@ -626,7 +618,7 @@ int S_StartSound (int entnum, int entchannel, sfx_t *sfx, vec3_t origin, float f
        int             ch_idx;
        int             skip;
 
-       if (!sound_started || !sfx || nosound.integer)
+       if (!shm || !sfx || nosound.integer)
                return -1;
        if (!sfx->fetcher)
        {
@@ -734,7 +726,7 @@ void S_StopAllSounds (void)
        unsigned int i;
        unsigned char *pbuf;
 
-       if (!sound_started)
+       if (!shm)
                return;
 
        for (i = 0; i < total_channels; i++)
@@ -791,7 +783,7 @@ void S_StaticSound (sfx_t *sfx, vec3_t origin, float fvol, float attenuation)
 {
        channel_t       *target_chan;
 
-       if (!sound_started || !sfx || nosound.integer)
+       if (!shm || !sfx || nosound.integer)
                return;
        if (!sfx->fetcher)
        {
@@ -866,7 +858,7 @@ void S_UpdateAmbientSounds (void)
 static speakerlayout_t snd_speakerlayouts[SND_SPEAKERLAYOUTS] =
 {
        {
-               "surround71", 8, false,
+               "surround71", 8,
                {
                        {45, 0.2, 0.2, 0.5}, // front left
                        {315, 0.2, 0.2, 0.5}, // front right
@@ -879,7 +871,7 @@ static speakerlayout_t snd_speakerlayouts[SND_SPEAKERLAYOUTS] =
                }
        },
        {
-               "surround51", 6, false,
+               "surround51", 6,
                {
                        {45, 0.2, 0.2, 0.5}, // front left
                        {315, 0.2, 0.2, 0.5}, // front right
@@ -894,7 +886,7 @@ static speakerlayout_t snd_speakerlayouts[SND_SPEAKERLAYOUTS] =
        {
                // these systems sometimes have a subwoofer as well, but it has no
                // channel of its own
-               "surround40", 4, false,
+               "surround40", 4,
                {
                        {45, 0.3, 0.3, 0.8}, // front left
                        {315, 0.3, 0.3, 0.8}, // front right
@@ -907,7 +899,9 @@ static speakerlayout_t snd_speakerlayouts[SND_SPEAKERLAYOUTS] =
                }
        },
        {
-               "headphones", 2, true,
+               // these systems sometimes have a subwoofer as well, but it has no
+               // channel of its own
+               "stereo", 2,
                {
                        {90, 0.5, 0.5, 1}, // side left
                        {270, 0.5, 0.5, 1}, // side right
@@ -920,12 +914,10 @@ static speakerlayout_t snd_speakerlayouts[SND_SPEAKERLAYOUTS] =
                }
        },
        {
-               // these systems sometimes have a subwoofer as well, but it has no
-               // channel of its own
-               "stereo", 2, false,
+               "mono", 1,
                {
-                       {45, 0.5, 0.5, 1}, // front left
-                       {315, 0.5, 0.5, 1}, // front right
+                       {0, 0, 1, 1}, // center
+                       {0, 0, 0, 0},
                        {0, 0, 0, 0},
                        {0, 0, 0, 0},
                        {0, 0, 0, 0},
@@ -949,7 +941,7 @@ void S_Update(const matrix4x4_t *listenermatrix)
        channel_t *ch, *combine;
        matrix4x4_t basematrix, rotatematrix;
 
-       if (!snd_initialized.integer || (snd_blocked > 0))
+       if (!snd_initialized.integer || (snd_blocked > 0) || !shm)
                return;
 
        Matrix4x4_Invert_Simple(&basematrix, listenermatrix);
@@ -957,15 +949,25 @@ void S_Update(const matrix4x4_t *listenermatrix)
 
        // select speaker layout
        for (i = 0;i < SND_SPEAKERLAYOUTS - 1;i++)
-               if (snd_speakerlayouts[i].channels == shm->format.channels && (!snd_speakerlayouts[i].headphones || snd_headphones.integer))
+               if (snd_speakerlayouts[i].channels == shm->format.channels)
                        break;
        snd_speakerlayout = snd_speakerlayouts[i];
 
        // calculate the current matrices
        for (j = 0;j < SND_LISTENERS;j++)
        {
-               Matrix4x4_CreateFromQuakeEntity(&rotatematrix, 0, 0, 0, 0, snd_speakerlayout.listeners[j].yawangle, 0, 1);
-               Matrix4x4_Concat(&listener_matrix[j], &basematrix, &rotatematrix);
+               Matrix4x4_CreateFromQuakeEntity(&rotatematrix, 0, 0, 0, 0, -snd_speakerlayout.listeners[j].yawangle, 0, 1);
+               Matrix4x4_Concat(&listener_matrix[j], &rotatematrix, &basematrix);
+               // I think this should now do this:
+               //   1. create a rotation matrix for rotating by e.g. -90 degrees CCW
+               //      (note: the matrix will rotate the OBJECT, not the VIEWER, so its
+               //       angle has to be taken negative)
+               //   2. create a transform which first rotates and moves its argument
+               //      into the player's view coordinates (using basematrix which is
+               //      an inverted "absolute" listener matrix), then applies the
+               //      rotation matrix for the ear
+               // Isn't Matrix4x4_CreateFromQuakeEntity a bit misleading because this
+               // does not actually refer to an entity?
        }
 
        // update general area ambient sound sources
@@ -1084,7 +1086,7 @@ void S_Update_(void)
 {
        unsigned        endtime;
 
-       if (!sound_started || (snd_blocked > 0))
+       if (!shm || (snd_blocked > 0))
                return;
 
        // Updates DMA time