home *** CD-ROM | disk | FTP | other *** search
/ Enigma Amiga Life 113 / EnigmaAmiga113CD.iso / software / sviluppo / quakeworld_src / client / snd_mix.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-06-17  |  8.4 KB  |  404 lines

  1. /*
  2. Copyright (C) 1996-1997 Id Software, Inc.
  3.  
  4. This program is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU General Public License
  6. as published by the Free Software Foundation; either version 2
  7. of the License, or (at your option) any later version.
  8.  
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
  12.  
  13. See the GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  18.  
  19. */
  20. // snd_mix.c -- portable code to mix sounds for snd_dma.c
  21.  
  22. #include "quakedef.h"
  23.  
  24. #ifdef _WIN32
  25. #include "winquake.h"
  26. #else
  27. #define DWORD unsigned long
  28. #endif
  29.  
  30. #define PAINTBUFFER_SIZE  512
  31. portable_samplepair_t paintbuffer[PAINTBUFFER_SIZE];
  32. int   snd_scaletable[32][256];
  33. int   *snd_p, snd_linear_count, snd_vol;
  34. short *snd_out;
  35.  
  36. #ifdef AMIGA
  37. extern void S_TransferPaintBuffer(int);
  38. #else
  39.  
  40. void Snd_WriteLinearBlastStereo16 (void);
  41.  
  42. #if !id386
  43. void Snd_WriteLinearBlastStereo16 (void)
  44. {
  45.   int   i;
  46.   int   val;
  47.  
  48.   for (i=0 ; i<snd_linear_count ; i+=2)
  49.   {
  50.     val = (snd_p[i]*snd_vol)>>8;
  51.     if (val > 0x7fff)
  52.       snd_out[i] = 0x7fff;
  53.     else if (val < (short)0x8000)
  54.       snd_out[i] = (short)0x8000;
  55.     else
  56.       snd_out[i] = val;
  57.  
  58.     val = (snd_p[i+1]*snd_vol)>>8;
  59.     if (val > 0x7fff)
  60.       snd_out[i+1] = 0x7fff;
  61.     else if (val < (short)0x8000)
  62.       snd_out[i+1] = (short)0x8000;
  63.     else
  64.       snd_out[i+1] = val;
  65.   }
  66. }
  67. #endif
  68.  
  69. void S_TransferStereo16 (int endtime)
  70. {
  71.   int   lpos;
  72.   int   lpaintedtime;
  73.   DWORD *pbuf;
  74. #ifdef _WIN32
  75.   int   reps;
  76.   DWORD dwSize,dwSize2;
  77.   DWORD *pbuf2;
  78.   HRESULT hresult;
  79. #endif
  80.   
  81.   snd_vol = volume.value*256;
  82.  
  83.   snd_p = (int *) paintbuffer;
  84.   lpaintedtime = paintedtime;
  85.  
  86. #ifdef _WIN32
  87.   if (pDSBuf)
  88.   {
  89.     reps = 0;
  90.  
  91.     while ((hresult = pDSBuf->lpVtbl->Lock(pDSBuf, 0, gSndBufSize, &pbuf, &dwSize, 
  92.                      &pbuf2, &dwSize2, 0)) != DS_OK)
  93.     {
  94.       if (hresult != DSERR_BUFFERLOST)
  95.       {
  96.         Con_Printf ("S_TransferStereo16: DS::Lock Sound Buffer Failed\n");
  97.         S_Shutdown ();
  98.         S_Startup ();
  99.         return;
  100.       }
  101.  
  102.       if (++reps > 10000)
  103.       {
  104.         Con_Printf ("S_TransferStereo16: DS: couldn't restore buffer\n");
  105.         S_Shutdown ();
  106.         S_Startup ();
  107.         return;
  108.       }
  109.     }
  110.   }
  111.   else
  112. #endif
  113.   {
  114.     pbuf = (DWORD *)shm->buffer;
  115.   }
  116.  
  117.   while (lpaintedtime < endtime)
  118.   {
  119.   // handle recirculating buffer issues
  120.     lpos = lpaintedtime & ((shm->samples>>1)-1);
  121.  
  122.     snd_out = (short *) pbuf + (lpos<<1);
  123.  
  124.     snd_linear_count = (shm->samples>>1) - lpos;
  125.     if (lpaintedtime + snd_linear_count > endtime)
  126.       snd_linear_count = endtime - lpaintedtime;
  127.  
  128.     snd_linear_count <<= 1;
  129.  
  130.   // write a linear blast of samples
  131.     Snd_WriteLinearBlastStereo16 ();
  132.  
  133.     snd_p += snd_linear_count;
  134.     lpaintedtime += (snd_linear_count>>1);
  135.   }
  136.  
  137. #ifdef _WIN32
  138.   if (pDSBuf)
  139.     pDSBuf->lpVtbl->Unlock(pDSBuf, pbuf, dwSize, NULL, 0);
  140. #endif
  141. }
  142.  
  143. void S_TransferPaintBuffer(int endtime)
  144. {
  145.   int   out_idx;
  146.   int   count;
  147.   int   out_mask;
  148.   int   *p;
  149.   int   step;
  150.   int   val;
  151.   int   snd_vol;
  152.   DWORD *pbuf;
  153. #ifdef _WIN32
  154.   int   reps;
  155.   DWORD dwSize,dwSize2;
  156.   DWORD *pbuf2;
  157.   HRESULT hresult;
  158. #endif
  159.  
  160.   if (shm->samplebits == 16 && shm->channels == 2)
  161.   {
  162.     S_TransferStereo16 (endtime);
  163.     return;
  164.   }
  165.   
  166.   p = (int *) paintbuffer;
  167.   count = (endtime - paintedtime) * shm->channels;
  168.   out_mask = shm->samples - 1; 
  169.   out_idx = paintedtime * shm->channels & out_mask;
  170.   step = 3 - shm->channels;
  171.   snd_vol = volume.value*256;
  172.  
  173. #ifdef _WIN32
  174.   if (pDSBuf)
  175.   {
  176.     reps = 0;
  177.  
  178.     while ((hresult = pDSBuf->lpVtbl->Lock(pDSBuf, 0, gSndBufSize, &pbuf, &dwSize, 
  179.                      &pbuf2,&dwSize2, 0)) != DS_OK)
  180.     {
  181.       if (hresult != DSERR_BUFFERLOST)
  182.       {
  183.         Con_Printf ("S_TransferPaintBuffer: DS::Lock Sound Buffer Failed\n");
  184.         S_Shutdown ();
  185.         S_Startup ();
  186.         return;
  187.       }
  188.  
  189.       if (++reps > 10000)
  190.       {
  191.         Con_Printf ("S_TransferPaintBuffer: DS: couldn't restore buffer\n");
  192.         S_Shutdown ();
  193.         S_Startup ();
  194.         return;
  195.       }
  196.     }
  197.   }
  198.   else
  199. #endif
  200.   {
  201.     pbuf = (DWORD *)shm->buffer;
  202.   }
  203.  
  204.   if (shm->samplebits == 16)
  205.   {
  206.     short *out = (short *) pbuf;
  207.     while (count--)
  208.     {
  209.       val = (*p * snd_vol) >> 8;
  210.       p+= step;
  211.       if (val > 0x7fff)
  212.         val = 0x7fff;
  213.       else if (val < (short)0x8000)
  214.         val = (short)0x8000;
  215.       out[out_idx] = val;
  216.       out_idx = (out_idx + 1) & out_mask;
  217.     }
  218.   }
  219.   else if (shm->samplebits == 8)
  220.   {
  221.     unsigned char *out = (unsigned char *) pbuf;
  222.     while (count--)
  223.     {
  224.       val = (*p * snd_vol) >> 8;
  225.       p+= step;
  226.       if (val > 0x7fff)
  227.         val = 0x7fff;
  228.       else if (val < (short)0x8000)
  229.         val = (short)0x8000;
  230.       out[out_idx] = (val>>8) + 128;
  231.       out_idx = (out_idx + 1) & out_mask;
  232.     }
  233.   }
  234.  
  235. #ifdef _WIN32
  236.   if (pDSBuf) {
  237.     DWORD dwNewpos, dwWrite;
  238.     int il = paintedtime;
  239.     int ir = endtime - paintedtime;
  240.     
  241.     ir += il;
  242.  
  243.     pDSBuf->lpVtbl->Unlock(pDSBuf, pbuf, dwSize, NULL, 0);
  244.  
  245.     pDSBuf->lpVtbl->GetCurrentPosition(pDSBuf, &dwNewpos, &dwWrite);
  246.  
  247. //    if ((dwNewpos >= il) && (dwNewpos <= ir))
  248. //      Con_Printf("%d-%d p %d c\n", il, ir, dwNewpos);
  249.   }
  250. #endif
  251. }
  252. #endif /* AMIGA */
  253.  
  254.  
  255. /*
  256. ===============================================================================
  257.  
  258. CHANNEL MIXING
  259.  
  260. ===============================================================================
  261. */
  262.  
  263. void SND_PaintChannelFrom8 (channel_t *ch, sfxcache_t *sc, int endtime);
  264. void SND_PaintChannelFrom16 (channel_t *ch, sfxcache_t *sc, int endtime);
  265.  
  266. void S_PaintChannels(int endtime)
  267. {
  268.   int   i;
  269.   int   end;
  270.   channel_t *ch;
  271.   sfxcache_t  *sc;
  272.   int   ltime, count;
  273.  
  274.   while (paintedtime < endtime)
  275.   {
  276.   // if paintbuffer is smaller than DMA buffer
  277.     end = endtime;
  278.     if (endtime - paintedtime > PAINTBUFFER_SIZE)
  279.       end = paintedtime + PAINTBUFFER_SIZE;
  280.  
  281.   // clear the paint buffer
  282.     Q_memset(paintbuffer, 0, (end - paintedtime) * sizeof(portable_samplepair_t));
  283.  
  284.   // paint in the channels.
  285.     ch = channels;
  286.     for (i=0; i<total_channels ; i++, ch++)
  287.     {
  288.       if (!ch->sfx)
  289.         continue;
  290.       if (!ch->leftvol && !ch->rightvol)
  291.         continue;
  292.       sc = S_LoadSound (ch->sfx);
  293.       if (!sc)
  294.         continue;
  295.  
  296.       ltime = paintedtime;
  297.  
  298.       while (ltime < end)
  299.       { // paint up to end
  300.         if (ch->end < end)
  301.           count = ch->end - ltime;
  302.         else
  303.           count = end - ltime;
  304.  
  305.         if (count > 0)
  306.         { 
  307.           if (sc->width == 1)
  308.             SND_PaintChannelFrom8(ch, sc, count);
  309.           else
  310.             SND_PaintChannelFrom16(ch, sc, count);
  311.   
  312.           ltime += count;
  313.         }
  314.  
  315.       // if at end of loop, restart
  316.         if (ltime >= ch->end)
  317.         {
  318.           if (sc->loopstart >= 0)
  319.           {
  320.             ch->pos = sc->loopstart;
  321.             ch->end = ltime + sc->length - ch->pos;
  322.           }
  323.           else        
  324.           { // channel just stopped
  325.             ch->sfx = NULL;
  326.             break;
  327.           }
  328.         }
  329.       }
  330.                                 
  331.     }
  332.  
  333.   // transfer out according to DMA format
  334.     S_TransferPaintBuffer(end);
  335.     paintedtime = end;
  336.   }
  337. }
  338.  
  339. void SND_InitScaletable (void)
  340. {
  341.   int   i, j;
  342.   
  343.   for (i=0 ; i<32 ; i++)
  344.     for (j=0 ; j<256 ; j++)
  345.       snd_scaletable[i][j] = ((signed char)j) * i * 8;
  346. }
  347.  
  348.  
  349. #if !id386
  350.  
  351. void SND_PaintChannelFrom8 (channel_t *ch, sfxcache_t *sc, int count)
  352. {
  353.   int   data;
  354.   int   *lscale, *rscale;
  355.   unsigned char *sfx;
  356.   int   i;
  357.  
  358.   if (ch->leftvol > 255)
  359.     ch->leftvol = 255;
  360.   if (ch->rightvol > 255)
  361.     ch->rightvol = 255;
  362.     
  363.   lscale = snd_scaletable[ch->leftvol >> 3];
  364.   rscale = snd_scaletable[ch->rightvol >> 3];
  365.   sfx = (signed char *)sc->data + ch->pos;
  366.  
  367.   for (i=0 ; i<count ; i++)
  368.   {
  369.     data = sfx[i];
  370.     paintbuffer[i].left += lscale[data];
  371.     paintbuffer[i].right += rscale[data];
  372.   }
  373.   
  374.   ch->pos += count;
  375. }
  376.  
  377. #endif  // !id386
  378.  
  379.  
  380. void SND_PaintChannelFrom16 (channel_t *ch, sfxcache_t *sc, int count)
  381. {
  382.   int data;
  383.   int left, right;
  384.   int leftvol, rightvol;
  385.   signed short *sfx;
  386.   int i;
  387.  
  388.   leftvol = ch->leftvol;
  389.   rightvol = ch->rightvol;
  390.   sfx = (signed short *)sc->data + ch->pos;
  391.  
  392.   for (i=0 ; i<count ; i++)
  393.   {
  394.     data = sfx[i];
  395.     left = (data * leftvol) >> 8;
  396.     right = (data * rightvol) >> 8;
  397.     paintbuffer[i].left += left;
  398.     paintbuffer[i].right += right;
  399.   }
  400.  
  401.   ch->pos += count;
  402. }
  403.  
  404.