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 // LordHavoc: was 512, expanded to 2048
31 #define PAINTBUFFER_SIZE 2048
32 portable_samplepair_t paintbuffer[PAINTBUFFER_SIZE];
33 int snd_scaletable[32][256];
34 int *snd_p, snd_linear_count, snd_vol;
37 void Snd_WriteLinearBlastStereo16 (void);
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;
49 snd_out[i ] = bound(-32768, val, 32767);
50 val = (snd_p[i ]*snd_vol)>>8;
51 snd_out[i+1] = bound(-32768, val, 32767);
56 for (i=0 ; i<snd_linear_count ; i+=2)
58 val = (snd_p[i]*snd_vol)>>8;
59 snd_out[i] = bound(-32768, val, 32767);
60 val = (snd_p[i+1]*snd_vol)>>8;
61 snd_out[i+1] = bound(-32768, val, 32767);
66 void S_TransferStereo16 (int endtime)
78 snd_vol = volume.value*256;
80 snd_p = (int *) paintbuffer;
81 lpaintedtime = paintedtime;
88 while ((hresult = pDSBuf->lpVtbl->Lock(pDSBuf, 0, gSndBufSize, &pbuf, &dwSize, &pbuf2, &dwSize2, 0)) != DS_OK)
90 if (hresult != DSERR_BUFFERLOST)
92 Con_Printf ("S_TransferStereo16: DS::Lock Sound Buffer Failed\n");
100 Con_Printf ("S_TransferStereo16: DS: couldn't restore buffer\n");
110 pbuf = (DWORD *)shm->buffer;
113 while (lpaintedtime < endtime)
115 // handle recirculating buffer issues
116 lpos = lpaintedtime & ((shm->samples>>1)-1);
118 snd_out = (short *) pbuf + (lpos<<1);
120 snd_linear_count = (shm->samples>>1) - lpos;
121 if (lpaintedtime + snd_linear_count > endtime)
122 snd_linear_count = endtime - lpaintedtime;
124 snd_linear_count <<= 1;
126 // write a linear blast of samples
127 Snd_WriteLinearBlastStereo16 ();
129 snd_p += snd_linear_count;
130 lpaintedtime += (snd_linear_count>>1);
135 pDSBuf->lpVtbl->Unlock(pDSBuf, pbuf, dwSize, NULL, 0);
139 void S_TransferPaintBuffer(int endtime)
151 DWORD dwSize,dwSize2;
156 if (shm->samplebits == 16 && shm->channels == 2)
158 S_TransferStereo16 (endtime);
162 p = (int *) paintbuffer;
163 count = (endtime - paintedtime) * shm->channels;
164 out_mask = shm->samples - 1;
165 out_idx = paintedtime * shm->channels & out_mask;
166 step = 3 - shm->channels;
167 snd_vol = volume.value*256;
174 while ((hresult = pDSBuf->lpVtbl->Lock(pDSBuf, 0, gSndBufSize, &pbuf, &dwSize, &pbuf2,&dwSize2, 0)) != DS_OK)
176 if (hresult != DSERR_BUFFERLOST)
178 Con_Printf ("S_TransferPaintBuffer: DS::Lock Sound Buffer Failed\n");
186 Con_Printf ("S_TransferPaintBuffer: DS: couldn't restore buffer\n");
196 pbuf = (DWORD *)shm->buffer;
199 if (shm->samplebits == 16)
201 short *out = (short *) pbuf;
204 val = (*p * snd_vol) >> 8;
205 out[out_idx] = bound(-32768, val, 32767);
207 out_idx = (out_idx + 1) & out_mask;
210 else if (shm->samplebits == 8)
212 unsigned char *out = (unsigned char *) pbuf;
215 val = ((*p * snd_vol) >> 16) + 128;
216 out[out_idx] = bound(0, val, 255);
218 out_idx = (out_idx + 1) & out_mask;
225 DWORD dwNewpos, dwWrite;
226 int il = paintedtime;
227 int ir = endtime - paintedtime;
231 pDSBuf->lpVtbl->Unlock(pDSBuf, pbuf, dwSize, NULL, 0);
233 pDSBuf->lpVtbl->GetCurrentPosition(pDSBuf, &dwNewpos, &dwWrite);
235 // if ((dwNewpos >= il) && (dwNewpos <= ir))
236 // Con_Printf("%d-%d p %d c\n", il, ir, dwNewpos);
243 ===============================================================================
247 ===============================================================================
250 void SND_PaintChannelFrom8 (channel_t *ch, sfxcache_t *sc, int endtime);
251 void SND_PaintChannelFrom16 (channel_t *ch, sfxcache_t *sc, int endtime);
253 void S_PaintChannels(int endtime)
261 while (paintedtime < endtime)
263 // if paintbuffer is smaller than DMA buffer
265 if (endtime - paintedtime > PAINTBUFFER_SIZE)
266 end = paintedtime + PAINTBUFFER_SIZE;
268 // clear the paint buffer
269 memset(paintbuffer, 0, (end - paintedtime) * sizeof(portable_samplepair_t));
271 // paint in the channels.
273 for (i=0; i<total_channels ; i++, ch++)
277 if (!ch->leftvol && !ch->rightvol)
279 sc = S_LoadSound (ch->sfx);
289 count = ch->end - ltime;
296 SND_PaintChannelFrom8(ch, sc, count);
298 SND_PaintChannelFrom16(ch, sc, count);
303 // if at end of loop, restart
304 if (ltime >= ch->end)
306 if (sc->loopstart >= 0)
308 ch->pos = sc->loopstart;
309 ch->end = ltime + sc->length - ch->pos;
313 // channel just stopped
322 // transfer out according to DMA format
323 S_TransferPaintBuffer(end);
328 void SND_InitScaletable (void)
332 for (i=0 ; i<32 ; i++)
333 for (j=0 ; j<256 ; j++)
334 snd_scaletable[i][j] = ((signed char)j) * i * 8;
338 void SND_PaintChannelFrom8 (channel_t *ch, sfxcache_t *sc, int count)
341 int *lscale, *rscale;
345 if (ch->leftvol > 255)
347 if (ch->rightvol > 255)
350 lscale = snd_scaletable[ch->leftvol >> 3];
351 rscale = snd_scaletable[ch->rightvol >> 3];
354 // LordHavoc: stereo sound support, and optimizations
355 sfx = (unsigned char *)sc->data + ch->pos * 2;
357 for (i=0 ; i<count ; i++)
359 paintbuffer[i].left += lscale[*sfx++];
360 paintbuffer[i].right += rscale[*sfx++];
366 sfx = (unsigned char *)sc->data + ch->pos;
368 for (i=0 ; i<count ; i++)
370 paintbuffer[i].left += lscale[*sfx];
371 paintbuffer[i].right += rscale[*sfx++];
378 void SND_PaintChannelFrom16 (channel_t *ch, sfxcache_t *sc, int count)
382 int leftvol, rightvol;
386 leftvol = ch->leftvol;
387 rightvol = ch->rightvol;
390 // LordHavoc: stereo sound support, and optimizations
391 sfx = (signed short *)sc->data + ch->pos * 2;
393 for (i=0 ; i<count ; i++)
395 paintbuffer[i].left += (*sfx++ * leftvol) >> 8;
396 paintbuffer[i].right += (*sfx++ * rightvol) >> 8;
401 sfx = (signed short *)sc->data + ch->pos;
403 for (i=0 ; i<count ; i++)
405 paintbuffer[i].left += (*sfx * leftvol) >> 8;
406 paintbuffer[i].right += (*sfx++ * rightvol) >> 8;