home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 25 / amigaformatcd25.iso / websites / amidoom / adoom_src-0.7.lha / ADoom_src / s_sound.c < prev    next >
C/C++ Source or Header  |  1997-12-29  |  17KB  |  881 lines

  1. // Emacs style mode select   -*- C++ -*- 
  2. //-----------------------------------------------------------------------------
  3. //
  4. // $Id:$
  5. //
  6. // Copyright (C) 1993-1996 by id Software, Inc.
  7. //
  8. // This source is available for distribution and/or modification
  9. // only under the terms of the DOOM Source Code License as
  10. // published by id Software. All rights reserved.
  11. //
  12. // The source is distributed in the hope that it will be useful,
  13. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. // FITNESS FOR A PARTICULAR PURPOSE. See the DOOM Source Code License
  15. // for more details.
  16. //
  17. // $Log:$
  18. //
  19. // DESCRIPTION:  none
  20. //
  21. //-----------------------------------------------------------------------------
  22.  
  23.  
  24. static const char
  25. rcsid[] = "$Id: s_sound.c,v 1.6 1997/02/03 22:45:12 b1 Exp $";
  26.  
  27.  
  28.  
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31.  
  32. #include "i_system.h"
  33. #include "i_sound.h"
  34. #include "sounds.h"
  35. #include "s_sound.h"
  36.  
  37. #include "z_zone.h"
  38. #include "m_random.h"
  39. #include "w_wad.h"
  40.  
  41. #include "doomdef.h"
  42. #include "p_local.h"
  43.  
  44. #include "doomstat.h"
  45.  
  46.  
  47. // Purpose?
  48. const char snd_prefixen[]
  49. = { 'P', 'P', 'A', 'S', 'S', 'S', 'M', 'M', 'M', 'S', 'S', 'S' };
  50.  
  51. #define S_MAX_VOLUME        127
  52.  
  53. // when to clip out sounds
  54. // Does not fit the large outdoor areas.
  55. #define S_CLIPPING_DIST        (1200*0x10000)
  56.  
  57. // Distance tp origin when sounds should be maxed out.
  58. // This should relate to movement clipping resolution
  59. // (see BLOCKMAP handling).
  60. // Originally: (200*0x10000).
  61. #define S_CLOSE_DIST        (160*0x10000)
  62.  
  63.  
  64. #define S_ATTENUATOR        ((S_CLIPPING_DIST-S_CLOSE_DIST)>>FRACBITS)
  65.  
  66. // Adjustable by menu.
  67. #define NORM_VOLUME            snd_MaxVolume
  68.  
  69. #define NORM_PITCH             128
  70. #define NORM_PRIORITY        64
  71. #define NORM_SEP        128
  72.  
  73. #define S_PITCH_PERTURB        1
  74. #define S_STEREO_SWING        (96*0x10000)
  75.  
  76. // percent attenuation from front to back
  77. #define S_IFRACVOL        30
  78.  
  79. #define NA            0
  80. #define S_NUMCHANNELS        2
  81.  
  82.  
  83. // Current music/sfx card - index useless
  84. //  w/o a reference LUT in a sound module.
  85. extern int snd_MusicDevice;
  86. extern int snd_SfxDevice;
  87. // Config file? Same disclaimer as above.
  88. extern int snd_DesiredMusicDevice;
  89. extern int snd_DesiredSfxDevice;
  90.  
  91.  
  92.  
  93. typedef struct
  94. {
  95.     // sound information (if null, channel avail.)
  96.     sfxinfo_t*    sfxinfo;
  97.  
  98.     // origin of sound
  99.     void*    origin;
  100.  
  101.     // handle of the sound being played
  102.     int        handle;
  103.     
  104. } channel_t;
  105.  
  106.  
  107. // the set of channels available
  108. static channel_t*    channels;
  109.  
  110. // These are not used, but should be (menu).
  111. // Maximum volume of a sound effect.
  112. // Internal default is max out of 0-15.
  113. int         snd_SfxVolume = 15;
  114.  
  115. // Maximum volume of music. Useless so far.
  116. int         snd_MusicVolume = 15; 
  117.  
  118.  
  119.  
  120. // whether songs are mus_paused
  121. static boolean        mus_paused;    
  122.  
  123. // music currently being played
  124. static musicinfo_t*    mus_playing=0;
  125.  
  126. // following is set
  127. //  by the defaults code in M_misc:
  128. // number of channels available
  129. int            numChannels;    
  130.  
  131. static int        nextcleanup;
  132.  
  133.  
  134.  
  135. //
  136. // Internals.
  137. //
  138. int
  139. S_getChannel
  140. ( void*        origin,
  141.   sfxinfo_t*    sfxinfo );
  142.  
  143.  
  144. int
  145. S_AdjustSoundParams
  146. ( mobj_t*    listener,
  147.   mobj_t*    source,
  148.   int*        vol,
  149.   int*        sep,
  150.   int*        pitch );
  151.  
  152. void S_StopChannel(int cnum);
  153.  
  154.  
  155.  
  156. //
  157. // Initializes sound stuff, including volume
  158. // Sets channels, SFX and music volume,
  159. //  allocates channel buffer, sets S_sfx lookup.
  160. //
  161. void S_Init
  162. ( int        sfxVolume,
  163.   int        musicVolume )
  164. {  
  165.   int        i;
  166.  
  167.   fprintf( stderr, "S_Init: default sfx volume %d\n", sfxVolume);
  168.  
  169.   // Whatever these did with DMX, these are rather dummies now.
  170.   I_SetChannels();
  171.   
  172.   S_SetSfxVolume(sfxVolume);
  173.   // No music with Linux - another dummy.
  174.   S_SetMusicVolume(musicVolume);
  175.  
  176.   // Allocating the internal channels for mixing
  177.   // (the maximum numer of sounds rendered
  178.   // simultaneously) within zone memory.
  179.   channels =
  180.     (channel_t *) Z_Malloc(numChannels*sizeof(channel_t), PU_STATIC, 0);
  181.   
  182.   // Free all channels for use
  183.   for (i=0 ; i<numChannels ; i++)
  184.     channels[i].sfxinfo = 0;
  185.   
  186.   // no sounds are playing, and they are not mus_paused
  187.   mus_paused = 0;
  188.  
  189.   // Note that sounds have not been cached (yet).
  190.   for (i=1 ; i<NUMSFX ; i++)
  191.     S_sfx[i].lumpnum = S_sfx[i].usefulness = -1;
  192. }
  193.  
  194.  
  195.  
  196.  
  197. //
  198. // Per level startup code.
  199. // Kills playing sounds at start of level,
  200. //  determines music if any, changes music.
  201. //
  202. void S_Start(void)
  203. {
  204.   int cnum;
  205.   int mnum;
  206.  
  207.   // kill all playing sounds at start of level
  208.   //  (trust me - a good idea)
  209.   for (cnum=0 ; cnum<numChannels ; cnum++)
  210.     if (channels[cnum].sfxinfo)
  211.       S_StopChannel(cnum);
  212.   
  213.   // start new music for the level
  214.   mus_paused = 0;
  215.   
  216.   if (gamemode == commercial)
  217.     mnum = mus_runnin + gamemap - 1;
  218.   else
  219.   {
  220.     int spmus[]=
  221.     {
  222.       // Song - Who? - Where?
  223.       
  224.       mus_e3m4,    // American    e4m1
  225.       mus_e3m2,    // Romero    e4m2
  226.       mus_e3m3,    // Shawn    e4m3
  227.       mus_e1m5,    // American    e4m4
  228.       mus_e2m7,    // Tim     e4m5
  229.       mus_e2m4,    // Romero    e4m6
  230.       mus_e2m6,    // J.Anderson    e4m7 CHIRON.WAD
  231.       mus_e2m5,    // Shawn    e4m8
  232.       mus_e1m9    // Tim        e4m9
  233.     };
  234.     
  235.     if (gameepisode < 4)
  236.       mnum = mus_e1m1 + (gameepisode-1)*9 + gamemap-1;
  237.     else
  238.       mnum = spmus[gamemap-1];
  239.     }    
  240.   
  241.   // HACK FOR COMMERCIAL
  242.   //  if (commercial && mnum > mus_e3m9)    
  243.   //      mnum -= mus_e3m9;
  244.   
  245.   S_ChangeMusic(mnum, true);
  246.   
  247.   nextcleanup = 15;
  248. }    
  249.  
  250.  
  251.  
  252.  
  253.  
  254. void
  255. S_StartSoundAtVolume
  256. ( void*        origin_p,
  257.   int        sfx_id,
  258.   int        volume )
  259. {
  260.  
  261.   int        rc;
  262.   int        sep;
  263.   int        pitch;
  264.   int        priority;
  265.   sfxinfo_t*    sfx;
  266.   int        cnum;
  267.   
  268.   mobj_t*    origin = (mobj_t *) origin_p;
  269.   
  270.   
  271.   // Debug.
  272.   /*fprintf( stderr,
  273.          "S_StartSoundAtVolume: playing sound %d (%s)\n",
  274.          sfx_id, S_sfx[sfx_id].name );*/
  275.   
  276.   // check for bogus sound #
  277.   if (sfx_id < 1 || sfx_id > NUMSFX)
  278.     I_Error("Bad sfx #: %d", sfx_id);
  279.   
  280.   sfx = &S_sfx[sfx_id];
  281.   
  282.   // Initialize sound parameters
  283.   if (sfx->link)
  284.   {
  285.     pitch = sfx->pitch;
  286.     priority = sfx->priority;
  287.     volume += sfx->volume;
  288.     
  289.     if (volume < 1)
  290.       return;
  291.     
  292.     if (volume > snd_SfxVolume)
  293.       volume = snd_SfxVolume;
  294.   }    
  295.   else
  296.   {
  297.     pitch = NORM_PITCH;
  298.     priority = NORM_PRIORITY;
  299.   }
  300.  
  301.  
  302.   // Check to see if it is audible,
  303.   //  and if not, modify the params
  304.   if (origin && origin != players[consoleplayer].mo)
  305.   {
  306.     rc = S_AdjustSoundParams(players[consoleplayer].mo,
  307.                  origin,
  308.                  &volume,
  309.                  &sep,
  310.                  &pitch);
  311.     
  312.     if ( origin->x == players[consoleplayer].mo->x
  313.      && origin->y == players[consoleplayer].mo->y)
  314.     {    
  315.       sep     = NORM_SEP;
  316.     }
  317.     
  318.     if (!rc)
  319.       return;
  320.   }    
  321.   else
  322.   {
  323.     sep = NORM_SEP;
  324.   }
  325.   
  326.   // hacks to vary the sfx pitches
  327.   if (sfx_id >= sfx_sawup
  328.       && sfx_id <= sfx_sawhit)
  329.   {    
  330.     pitch += 8 - (M_Random()&15);
  331.     
  332.     if (pitch<0)
  333.       pitch = 0;
  334.     else if (pitch>255)
  335.       pitch = 255;
  336.   }
  337.   else if (sfx_id != sfx_itemup
  338.        && sfx_id != sfx_tink)
  339.   {
  340.     pitch += 16 - (M_Random()&31);
  341.     
  342.     if (pitch<0)
  343.       pitch = 0;
  344.     else if (pitch>255)
  345.       pitch = 255;
  346.   }
  347.  
  348.   // kill old sound
  349.   S_StopSound(origin);
  350.  
  351.   // try to find a channel
  352.   cnum = S_getChannel(origin, sfx);
  353.   
  354.   if (cnum<0)
  355.     return;
  356.  
  357.   //
  358.   // This is supposed to handle the loading/caching.
  359.   // For some odd reason, the caching is done nearly
  360.   //  each time the sound is needed?
  361.   //
  362.   
  363.   // get lumpnum if necessary
  364.   if (sfx->lumpnum < 0)
  365.     sfx->lumpnum = I_GetSfxLumpNum(sfx);
  366.  
  367. #ifndef SNDSRV
  368.   // cache data if necessary
  369.   if (!sfx->data)
  370.   {
  371.     fprintf( stderr,
  372.          "S_StartSoundAtVolume: 16bit and not pre-cached - wtf?\n");
  373.  
  374.     // DOS remains, 8bit handling
  375.     //sfx->data = (void *) W_CacheLumpNum(sfx->lumpnum, PU_MUSIC);
  376.     // fprintf( stderr,
  377.     //         "S_StartSoundAtVolume: loading %d (lump %d) : 0x%x\n",
  378.     //       sfx_id, sfx->lumpnum, (int)sfx->data );
  379.     
  380.   }
  381. #endif
  382.   
  383.   // increase the usefulness
  384.   if (sfx->usefulness++ < 0)
  385.     sfx->usefulness = 1;
  386.   
  387.   // Assigns the handle to one of the channels in the
  388.   //  mix/output buffer.
  389.   channels[cnum].handle = I_StartSound(sfx_id,
  390.                        cnum,
  391.                        /*sfx->data,*/
  392.                        volume,
  393.                        sep,
  394.                        pitch,
  395.                        priority);
  396. }    
  397.  
  398. void
  399. S_StartSound
  400. ( void*        origin,
  401.   int        sfx_id )
  402. {
  403. #ifdef SAWDEBUG
  404.     // if (sfx_id == sfx_sawful)
  405.     // sfx_id = sfx_itemup;
  406. #endif
  407.   
  408.     S_StartSoundAtVolume(origin, sfx_id, snd_SfxVolume);
  409.  
  410.  
  411.     // UNUSED. We had problems, had we not?
  412. #ifdef SAWDEBUG
  413. {
  414.     int i;
  415.     int n;
  416.     
  417.     static mobj_t*      last_saw_origins[10] = {1,1,1,1,1,1,1,1,1,1};
  418.     static int        first_saw=0;
  419.     static int        next_saw=0;
  420.     
  421.     if (sfx_id == sfx_sawidl
  422.     || sfx_id == sfx_sawful
  423.     || sfx_id == sfx_sawhit)
  424.     {
  425.     for (i=first_saw;i!=next_saw;i=(i+1)%10)
  426.         if (last_saw_origins[i] != origin)
  427.         fprintf(stderr, "old origin 0x%lx != "
  428.             "origin 0x%lx for sfx %d\n",
  429.             last_saw_origins[i],
  430.             origin,
  431.             sfx_id);
  432.         
  433.     last_saw_origins[next_saw] = origin;
  434.     next_saw = (next_saw + 1) % 10;
  435.     if (next_saw == first_saw)
  436.         first_saw = (first_saw + 1) % 10;
  437.         
  438.     for (n=i=0; i<numChannels ; i++)
  439.     {
  440.         if (channels[i].sfxinfo == &S_sfx[sfx_sawidl]
  441.         || channels[i].sfxinfo == &S_sfx[sfx_sawful]
  442.         || channels[i].sfxinfo == &S_sfx[sfx_sawhit]) n++;
  443.     }
  444.         
  445.     if (n>1)
  446.     {
  447.         for (i=0; i<numChannels ; i++)
  448.         {
  449.         if (channels[i].sfxinfo == &S_sfx[sfx_sawidl]
  450.             || channels[i].sfxinfo == &S_sfx[sfx_sawful]
  451.             || channels[i].sfxinfo == &S_sfx[sfx_sawhit])
  452.         {
  453.             fprintf(stderr,
  454.                 "chn: sfxinfo=0x%lx, origin=0x%lx, "
  455.                 "handle=%d\n",
  456.                 channels[i].sfxinfo,
  457.                 channels[i].origin,
  458.                 channels[i].handle);
  459.         }
  460.         }
  461.         fprintf(stderr, "\n");
  462.     }
  463.     }
  464. }
  465. #endif
  466.  
  467. }
  468.  
  469.  
  470.  
  471.  
  472. void S_StopSound(void *origin)
  473. {
  474.  
  475.     int cnum;
  476.  
  477.     for (cnum=0 ; cnum<numChannels ; cnum++)
  478.     {
  479.     if (channels[cnum].sfxinfo && channels[cnum].origin == origin)
  480.     {
  481.         S_StopChannel(cnum);
  482.         break;
  483.     }
  484.     }
  485. }
  486.  
  487.  
  488.  
  489.  
  490.  
  491.  
  492.  
  493.  
  494.  
  495. //
  496. // Stop and resume music, during game PAUSE.
  497. //
  498. void S_PauseSound(void)
  499. {
  500.     if (mus_playing && !mus_paused)
  501.     {
  502.     I_PauseSong(mus_playing->handle);
  503.     mus_paused = true;
  504.     }
  505. }
  506.  
  507. void S_ResumeSound(void)
  508. {
  509.     if (mus_playing && mus_paused)
  510.     {
  511.     I_ResumeSong(mus_playing->handle);
  512.     mus_paused = false;
  513.     }
  514. }
  515.  
  516.  
  517. //
  518. // Updates music & sounds
  519. //
  520. void S_UpdateSounds(void* listener_p)
  521. {
  522.     int        audible;
  523.     int        cnum;
  524.     int        volume;
  525.     int        sep;
  526.     int        pitch;
  527.     sfxinfo_t*    sfx;
  528.     channel_t*    c;
  529.     
  530.     mobj_t*    listener = (mobj_t*)listener_p;
  531.  
  532.  
  533.     
  534.     // Clean up unused data.
  535.     // This is currently not done for 16bit (sounds cached static).
  536.     // DOS 8bit remains. 
  537.     /*if (gametic > nextcleanup)
  538.     {
  539.     for (i=1 ; i<NUMSFX ; i++)
  540.     {
  541.         if (S_sfx[i].usefulness < 1
  542.         && S_sfx[i].usefulness > -1)
  543.         {
  544.         if (--S_sfx[i].usefulness == -1)
  545.         {
  546.             Z_ChangeTag(S_sfx[i].data, PU_CACHE);
  547.             S_sfx[i].data = 0;
  548.         }
  549.         }
  550.     }
  551.     nextcleanup = gametic + 15;
  552.     }*/
  553.     
  554.     for (cnum=0 ; cnum<numChannels ; cnum++)
  555.     {
  556.     c = &channels[cnum];
  557.     sfx = c->sfxinfo;
  558.  
  559.     if (c->sfxinfo)
  560.     {
  561.         if (I_SoundIsPlaying(c->handle))
  562.         {
  563.         // initialize parameters
  564.         volume = snd_SfxVolume;
  565.         pitch = NORM_PITCH;
  566.         sep = NORM_SEP;
  567.  
  568.         if (sfx->link)
  569.         {
  570.             pitch = sfx->pitch;
  571.             volume += sfx->volume;
  572.             if (volume < 1)
  573.             {
  574.             S_StopChannel(cnum);
  575.             continue;
  576.             }
  577.             else if (volume > snd_SfxVolume)
  578.             {
  579.             volume = snd_SfxVolume;
  580.             }
  581.         }
  582.  
  583.         // check non-local sounds for distance clipping
  584.         //  or modify their params
  585.         if (c->origin && listener_p != c->origin)
  586.         {
  587.             audible = S_AdjustSoundParams(listener,
  588.                           c->origin,
  589.                           &volume,
  590.                           &sep,
  591.                           &pitch);
  592.             
  593.             if (!audible)
  594.             {
  595.             S_StopChannel(cnum);
  596.             }
  597.             else
  598.             I_UpdateSoundParams(c->handle, volume, sep, pitch);
  599.         }
  600.         }
  601.         else
  602.         {
  603.         // if channel is allocated but sound has stopped,
  604.         //  free it
  605.         S_StopChannel(cnum);
  606.         }
  607.     }
  608.     }
  609.     // kill music if it is a single-play && finished
  610.     // if (    mus_playing
  611.     //      && !I_QrySongPlaying(mus_playing->handle)
  612.     //      && !mus_paused )
  613.     // S_StopMusic();
  614. }
  615.  
  616.  
  617. void S_SetMusicVolume(int volume)
  618. {
  619.     if (volume < 0 || volume > 127)
  620.     {
  621.     I_Error("Attempt to set music volume at %d",
  622.         volume);
  623.     }    
  624.  
  625.     I_SetMusicVolume(127);
  626.     I_SetMusicVolume(volume);
  627.     snd_MusicVolume = volume;
  628. }
  629.  
  630.  
  631.  
  632. void S_SetSfxVolume(int volume)
  633. {
  634.  
  635.     if (volume < 0 || volume > 127)
  636.     I_Error("Attempt to set sfx volume at %d", volume);
  637.  
  638.     snd_SfxVolume = volume;
  639.  
  640. }
  641.  
  642. //
  643. // Starts some music with the music id found in sounds.h.
  644. //
  645. void S_StartMusic(int m_id)
  646. {
  647.     S_ChangeMusic(m_id, false);
  648. }
  649.  
  650. void
  651. S_ChangeMusic
  652. ( int            musicnum,
  653.   int            looping )
  654. {
  655.     musicinfo_t*    music;
  656.     char        namebuf[9];
  657.  
  658.     if ( (musicnum <= mus_None)
  659.      || (musicnum >= NUMMUSIC) )
  660.     {
  661.     I_Error("Bad music number %d", musicnum);
  662.     }
  663.     else
  664.     music = &S_music[musicnum];
  665.  
  666.     if (mus_playing == music)
  667.     return;
  668.  
  669.     // shutdown old music
  670.     S_StopMusic();
  671.  
  672.     // get lumpnum if neccessary
  673.     if (!music->lumpnum)
  674.     {
  675.     sprintf(namebuf, "d_%s", music->name);
  676.     music->lumpnum = W_GetNumForName(namebuf);
  677.     }
  678.  
  679.     // load & register it
  680.     music->data = (void *) W_CacheLumpNum(music->lumpnum, PU_MUSIC);
  681.     music->handle = I_RegisterSong(music->data);
  682.  
  683.     // play it
  684.     I_PlaySong(music->handle, looping);
  685.  
  686.     mus_playing = music;
  687. }
  688.  
  689.  
  690. void S_StopMusic(void)
  691. {
  692.     if (mus_playing)
  693.     {
  694.     if (mus_paused)
  695.         I_ResumeSong(mus_playing->handle);
  696.  
  697.     I_StopSong(mus_playing->handle);
  698.     I_UnRegisterSong(mus_playing->handle);
  699.     Z_ChangeTag(mus_playing->data, PU_CACHE);
  700.     
  701.     mus_playing->data = 0;
  702.     mus_playing = 0;
  703.     }
  704. }
  705.  
  706.  
  707.  
  708.  
  709. void S_StopChannel(int cnum)
  710. {
  711.  
  712.     int        i;
  713.     channel_t*    c = &channels[cnum];
  714.  
  715.     if (c->sfxinfo)
  716.     {
  717.     // stop the sound playing
  718.     if (I_SoundIsPlaying(c->handle))
  719.     {
  720. #ifdef SAWDEBUG
  721.         if (c->sfxinfo == &S_sfx[sfx_sawful])
  722.         fprintf(stderr, "stopped\n");
  723. #endif
  724.         I_StopSound(c->handle);
  725.     }
  726.  
  727.     // check to see
  728.     //  if other channels are playing the sound
  729.     for (i=0 ; i<numChannels ; i++)
  730.     {
  731.         if (cnum != i
  732.         && c->sfxinfo == channels[i].sfxinfo)
  733.         {
  734.         break;
  735.         }
  736.     }
  737.     
  738.     // degrade usefulness of sound data
  739.     c->sfxinfo->usefulness--;
  740.  
  741.     c->sfxinfo = 0;
  742.     }
  743. }
  744.  
  745.  
  746.  
  747. //
  748. // Changes volume, stereo-separation, and pitch variables
  749. //  from the norm of a sound effect to be played.
  750. // If the sound is not audible, returns a 0.
  751. // Otherwise, modifies parameters and returns 1.
  752. //
  753. int
  754. S_AdjustSoundParams
  755. ( mobj_t*    listener,
  756.   mobj_t*    source,
  757.   int*        vol,
  758.   int*        sep,
  759.   int*        pitch )
  760. {
  761.     fixed_t    approx_dist;
  762.     fixed_t    adx;
  763.     fixed_t    ady;
  764.     angle_t    angle;
  765.  
  766.     // calculate the distance to sound origin
  767.     //  and clip it if necessary
  768.     adx = iabs(listener->x - source->x);
  769.     ady = iabs(listener->y - source->y);
  770.  
  771.     // From _GG1_ p.428. Appox. eucledian distance fast.
  772.     approx_dist = adx + ady - ((adx < ady ? adx : ady)>>1);
  773.     
  774.     if (gamemap != 8
  775.     && approx_dist > S_CLIPPING_DIST)
  776.     {
  777.     return 0;
  778.     }
  779.     
  780.     // angle of source to listener
  781.     angle = R_PointToAngle2(listener->x,
  782.                 listener->y,
  783.                 source->x,
  784.                 source->y);
  785.  
  786.     if (angle > listener->angle)
  787.     angle = angle - listener->angle;
  788.     else
  789.     angle = angle + (0xffffffff - listener->angle);
  790.  
  791.     angle >>= ANGLETOFINESHIFT;
  792.  
  793.     // stereo separation
  794.     *sep = 128 - (FixedMul(S_STEREO_SWING,finesine[angle])>>FRACBITS);
  795.  
  796.     // volume calculation
  797.     if (approx_dist < S_CLOSE_DIST)
  798.     {
  799.     *vol = snd_SfxVolume;
  800.     }
  801.     else if (gamemap == 8)
  802.     {
  803.     if (approx_dist > S_CLIPPING_DIST)
  804.         approx_dist = S_CLIPPING_DIST;
  805.  
  806.     *vol = 15+ ((snd_SfxVolume-15)
  807.             *((S_CLIPPING_DIST - approx_dist)>>FRACBITS))
  808.         / S_ATTENUATOR;
  809.     }
  810.     else
  811.     {
  812.     // distance effect
  813.     *vol = (snd_SfxVolume
  814.         * ((S_CLIPPING_DIST - approx_dist)>>FRACBITS))
  815.         / S_ATTENUATOR; 
  816.     }
  817.     
  818.     return (*vol > 0);
  819. }
  820.  
  821.  
  822.  
  823.  
  824. //
  825. // S_getChannel :
  826. //   If none available, return -1.  Otherwise channel #.
  827. //
  828. int
  829. S_getChannel
  830. ( void*        origin,
  831.   sfxinfo_t*    sfxinfo )
  832. {
  833.     // channel number to use
  834.     int        cnum;
  835.     
  836.     channel_t*    c;
  837.  
  838.     // Find an open channel
  839.     for (cnum=0 ; cnum<numChannels ; cnum++)
  840.     {
  841.     if (!channels[cnum].sfxinfo)
  842.         break;
  843.     else if (origin &&  channels[cnum].origin ==  origin)
  844.     {
  845.         S_StopChannel(cnum);
  846.         break;
  847.     }
  848.     }
  849.  
  850.     // None available
  851.     if (cnum == numChannels)
  852.     {
  853.     // Look for lower priority
  854.     for (cnum=0 ; cnum<numChannels ; cnum++)
  855.         if (channels[cnum].sfxinfo->priority >= sfxinfo->priority) break;
  856.  
  857.     if (cnum == numChannels)
  858.     {
  859.         // FUCK!  No lower priority.  Sorry, Charlie.    
  860.         return -1;
  861.     }
  862.     else
  863.     {
  864.         // Otherwise, kick out lower priority.
  865.         S_StopChannel(cnum);
  866.     }
  867.     }
  868.  
  869.     c = &channels[cnum];
  870.  
  871.     // channel is decided to be cnum.
  872.     c->sfxinfo = sfxinfo;
  873.     c->origin = origin;
  874.  
  875.     return cnum;
  876. }
  877.  
  878.  
  879.  
  880.  
  881.