2 Copyright (C) 1996-1997 Id Software, Inc.
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.
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.
13 See the GNU General Public License for more details.
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.
20 // snd_mix.c -- portable code to mix sounds for snd_dma.c
27 #define DWORD unsigned long
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;
36 void Snd_WriteLinearBlastStereo16 (void);
38 extern cvar_t snd_swapstereo;
39 void Snd_WriteLinearBlastStereo16 (void)
44 if (snd_swapstereo.value)
46 for (i=0 ; i<snd_linear_count ; i+=2)
48 val = (snd_p[i+1]*snd_vol)>>8;
51 else if (val < (short)0x8000)
52 snd_out[i] = (short)0x8000;
56 val = (snd_p[i]*snd_vol)>>8;
58 snd_out[i+1] = 0x7fff;
59 else if (val < (short)0x8000)
60 snd_out[i+1] = (short)0x8000;
67 for (i=0 ; i<snd_linear_count ; i+=2)
69 val = (snd_p[i]*snd_vol)>>8;
72 else if (val < (short)0x8000)
73 snd_out[i] = (short)0x8000;
77 val = (snd_p[i+1]*snd_vol)>>8;
79 snd_out[i+1] = 0x7fff;
80 else if (val < (short)0x8000)
81 snd_out[i+1] = (short)0x8000;
88 void S_TransferStereo16 (int endtime)
100 snd_vol = volume.value*256;
102 snd_p = (int *) paintbuffer;
103 lpaintedtime = paintedtime;
110 while ((hresult = pDSBuf->lpVtbl->Lock(pDSBuf, 0, gSndBufSize, &pbuf, &dwSize,
111 &pbuf2, &dwSize2, 0)) != DS_OK)
113 if (hresult != DSERR_BUFFERLOST)
115 Con_Printf ("S_TransferStereo16: DS::Lock Sound Buffer Failed\n");
123 Con_Printf ("S_TransferStereo16: DS: couldn't restore buffer\n");
133 pbuf = (DWORD *)shm->buffer;
136 while (lpaintedtime < endtime)
138 // handle recirculating buffer issues
139 lpos = lpaintedtime & ((shm->samples>>1)-1);
141 snd_out = (short *) pbuf + (lpos<<1);
143 snd_linear_count = (shm->samples>>1) - lpos;
144 if (lpaintedtime + snd_linear_count > endtime)
145 snd_linear_count = endtime - lpaintedtime;
147 snd_linear_count <<= 1;
149 // write a linear blast of samples
150 Snd_WriteLinearBlastStereo16 ();
152 snd_p += snd_linear_count;
153 lpaintedtime += (snd_linear_count>>1);
158 pDSBuf->lpVtbl->Unlock(pDSBuf, pbuf, dwSize, NULL, 0);
162 void S_TransferPaintBuffer(int endtime)
174 DWORD dwSize,dwSize2;
179 if (shm->samplebits == 16 && shm->channels == 2)
181 S_TransferStereo16 (endtime);
185 p = (int *) paintbuffer;
186 count = (endtime - paintedtime) * shm->channels;
187 out_mask = shm->samples - 1;
188 out_idx = paintedtime * shm->channels & out_mask;
189 step = 3 - shm->channels;
190 snd_vol = volume.value*256;
197 while ((hresult = pDSBuf->lpVtbl->Lock(pDSBuf, 0, gSndBufSize, &pbuf, &dwSize,
198 &pbuf2,&dwSize2, 0)) != DS_OK)
200 if (hresult != DSERR_BUFFERLOST)
202 Con_Printf ("S_TransferPaintBuffer: DS::Lock Sound Buffer Failed\n");
210 Con_Printf ("S_TransferPaintBuffer: DS: couldn't restore buffer\n");
220 pbuf = (DWORD *)shm->buffer;
223 if (shm->samplebits == 16)
225 short *out = (short *) pbuf;
228 val = (*p * snd_vol) >> 8;
232 else if (val < (short)0x8000)
235 out_idx = (out_idx + 1) & out_mask;
238 else if (shm->samplebits == 8)
240 unsigned char *out = (unsigned char *) pbuf;
243 val = (*p * snd_vol) >> 8;
247 else if (val < (short)0x8000)
249 out[out_idx] = (val>>8) + 128;
250 out_idx = (out_idx + 1) & out_mask;
256 DWORD dwNewpos, dwWrite;
257 int il = paintedtime;
258 int ir = endtime - paintedtime;
262 pDSBuf->lpVtbl->Unlock(pDSBuf, pbuf, dwSize, NULL, 0);
264 pDSBuf->lpVtbl->GetCurrentPosition(pDSBuf, &dwNewpos, &dwWrite);
266 // if ((dwNewpos >= il) && (dwNewpos <= ir))
267 // Con_Printf("%d-%d p %d c\n", il, ir, dwNewpos);
274 ===============================================================================
278 ===============================================================================
281 void SND_PaintChannelFrom8 (channel_t *ch, sfxcache_t *sc, int endtime);
282 void SND_PaintChannelFrom16 (channel_t *ch, sfxcache_t *sc, int endtime);
284 void S_PaintChannels(int endtime)
292 while (paintedtime < endtime)
294 // if paintbuffer is smaller than DMA buffer
296 if (endtime - paintedtime > PAINTBUFFER_SIZE)
297 end = paintedtime + PAINTBUFFER_SIZE;
299 // clear the paint buffer
300 memset(paintbuffer, 0, (end - paintedtime) * sizeof(portable_samplepair_t));
302 // paint in the channels.
304 for (i=0; i<total_channels ; i++, ch++)
308 if (!ch->leftvol && !ch->rightvol)
310 sc = S_LoadSound (ch->sfx);
319 count = ch->end - ltime;
326 SND_PaintChannelFrom8(ch, sc, count);
328 SND_PaintChannelFrom16(ch, sc, count);
333 // if at end of loop, restart
334 if (ltime >= ch->end)
336 if (sc->loopstart >= 0)
338 ch->pos = sc->loopstart;
339 ch->end = ltime + sc->length - ch->pos;
342 { // channel just stopped
351 // transfer out according to DMA format
352 S_TransferPaintBuffer(end);
357 void SND_InitScaletable (void)
361 for (i=0 ; i<32 ; i++)
362 for (j=0 ; j<256 ; j++)
363 snd_scaletable[i][j] = ((signed char)j) * i * 8;
367 void SND_PaintChannelFrom8 (channel_t *ch, sfxcache_t *sc, int count)
370 int *lscale, *rscale;
374 if (ch->leftvol > 255)
376 if (ch->rightvol > 255)
379 lscale = snd_scaletable[ch->leftvol >> 3];
380 rscale = snd_scaletable[ch->rightvol >> 3];
381 if (sc->stereo) // LordHavoc: stereo sound support, and optimizations
383 sfx = (unsigned char *)sc->data + ch->pos * 2;
385 for (i=0 ; i<count ; i++)
387 paintbuffer[i].left += lscale[*sfx++];
388 paintbuffer[i].right += rscale[*sfx++];
394 sfx = (unsigned char *)sc->data + ch->pos;
396 for (i=0 ; i<count ; i++)
398 paintbuffer[i].left += lscale[*sfx];
399 paintbuffer[i].right += rscale[*sfx++];
406 void SND_PaintChannelFrom16 (channel_t *ch, sfxcache_t *sc, int count)
410 int leftvol, rightvol;
414 leftvol = ch->leftvol;
415 rightvol = ch->rightvol;
416 if (sc->stereo) // LordHavoc: stereo sound support, and optimizations
418 sfx = (signed short *)sc->data + ch->pos * 2;
420 for (i=0 ; i<count ; i++)
422 paintbuffer[i].left += (short) ((int) (*sfx++ * leftvol) >> 8);
423 paintbuffer[i].right += (short) ((int) (*sfx++ * rightvol) >> 8);
428 sfx = (signed short *)sc->data + ch->pos;
430 for (i=0 ; i<count ; i++)
432 paintbuffer[i].left += (short) ((int) (*sfx * leftvol) >> 8);
433 paintbuffer[i].right += (short) ((int) (*sfx++ * rightvol) >> 8);