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 extern cvar_t snd_swapstereo;
40 void Snd_WriteLinearBlastStereo16 (void)
45 if (snd_swapstereo.value)
47 for (i=0 ; i<snd_linear_count ; i+=2)
49 val = (snd_p[i+1]*snd_vol)>>8;
50 snd_out[i ] = bound(-32768, val, 32767);
51 val = (snd_p[i ]*snd_vol)>>8;
52 snd_out[i+1] = bound(-32768, val, 32767);
57 for (i=0 ; i<snd_linear_count ; i+=2)
59 val = (snd_p[i]*snd_vol)>>8;
60 snd_out[i] = bound(-32768, val, 32767);
61 val = (snd_p[i+1]*snd_vol)>>8;
62 snd_out[i+1] = bound(-32768, val, 32767);
67 void S_TransferStereo16 (int endtime)
79 snd_vol = volume.value*256;
81 snd_p = (int *) paintbuffer;
82 lpaintedtime = paintedtime;
89 while ((hresult = pDSBuf->lpVtbl->Lock(pDSBuf, 0, gSndBufSize, &pbuf, &dwSize, &pbuf2, &dwSize2, 0)) != DS_OK)
91 if (hresult != DSERR_BUFFERLOST)
93 Con_Printf ("S_TransferStereo16: DS::Lock Sound Buffer Failed\n");
101 Con_Printf ("S_TransferStereo16: DS: couldn't restore buffer\n");
111 pbuf = (DWORD *)shm->buffer;
114 while (lpaintedtime < endtime)
116 // handle recirculating buffer issues
117 lpos = lpaintedtime & ((shm->samples>>1)-1);
119 snd_out = (short *) pbuf + (lpos<<1);
121 snd_linear_count = (shm->samples>>1) - lpos;
122 if (lpaintedtime + snd_linear_count > endtime)
123 snd_linear_count = endtime - lpaintedtime;
125 snd_linear_count <<= 1;
127 // write a linear blast of samples
128 Snd_WriteLinearBlastStereo16 ();
130 snd_p += snd_linear_count;
131 lpaintedtime += (snd_linear_count>>1);
136 pDSBuf->lpVtbl->Unlock(pDSBuf, pbuf, dwSize, NULL, 0);
140 void S_TransferPaintBuffer(int endtime)
152 DWORD dwSize,dwSize2;
157 if (shm->samplebits == 16 && shm->channels == 2)
159 S_TransferStereo16 (endtime);
163 p = (int *) paintbuffer;
164 count = (endtime - paintedtime) * shm->channels;
165 out_mask = shm->samples - 1;
166 out_idx = paintedtime * shm->channels & out_mask;
167 step = 3 - shm->channels;
168 snd_vol = volume.value*256;
175 while ((hresult = pDSBuf->lpVtbl->Lock(pDSBuf, 0, gSndBufSize, &pbuf, &dwSize, &pbuf2,&dwSize2, 0)) != DS_OK)
177 if (hresult != DSERR_BUFFERLOST)
179 Con_Printf ("S_TransferPaintBuffer: DS::Lock Sound Buffer Failed\n");
187 Con_Printf ("S_TransferPaintBuffer: DS: couldn't restore buffer\n");
197 pbuf = (DWORD *)shm->buffer;
200 if (shm->samplebits == 16)
202 short *out = (short *) pbuf;
205 val = (*p * snd_vol) >> 8;
206 out[out_idx] = bound(-32768, val, 32767);
208 out_idx = (out_idx + 1) & out_mask;
211 else if (shm->samplebits == 8)
213 unsigned char *out = (unsigned char *) pbuf;
216 val = ((*p * snd_vol) >> 16) + 128;
217 out[out_idx] = bound(0, val, 255);
219 out_idx = (out_idx + 1) & out_mask;
226 DWORD dwNewpos, dwWrite;
227 int il = paintedtime;
228 int ir = endtime - paintedtime;
232 pDSBuf->lpVtbl->Unlock(pDSBuf, pbuf, dwSize, NULL, 0);
234 pDSBuf->lpVtbl->GetCurrentPosition(pDSBuf, &dwNewpos, &dwWrite);
236 // if ((dwNewpos >= il) && (dwNewpos <= ir))
237 // Con_Printf("%d-%d p %d c\n", il, ir, dwNewpos);
244 ===============================================================================
248 ===============================================================================
251 void SND_PaintChannelFrom8 (channel_t *ch, sfxcache_t *sc, int endtime);
252 void SND_PaintChannelFrom16 (channel_t *ch, sfxcache_t *sc, int endtime);
254 void S_PaintChannels(int endtime)
262 while (paintedtime < endtime)
264 // if paintbuffer is smaller than DMA buffer
266 if (endtime - paintedtime > PAINTBUFFER_SIZE)
267 end = paintedtime + PAINTBUFFER_SIZE;
269 // clear the paint buffer
270 memset(paintbuffer, 0, (end - paintedtime) * sizeof(portable_samplepair_t));
272 // paint in the channels.
274 for (i=0; i<total_channels ; i++, ch++)
278 if (!ch->leftvol && !ch->rightvol)
280 sc = S_LoadSound (ch->sfx);
290 count = ch->end - ltime;
297 SND_PaintChannelFrom8(ch, sc, count);
299 SND_PaintChannelFrom16(ch, sc, count);
304 // if at end of loop, restart
305 if (ltime >= ch->end)
307 if (sc->loopstart >= 0)
309 ch->pos = sc->loopstart;
310 ch->end = ltime + sc->length - ch->pos;
314 // channel just stopped
323 // transfer out according to DMA format
324 S_TransferPaintBuffer(end);
329 void SND_InitScaletable (void)
333 for (i=0 ; i<32 ; i++)
334 for (j=0 ; j<256 ; j++)
335 snd_scaletable[i][j] = ((signed char)j) * i * 8;
339 void SND_PaintChannelFrom8 (channel_t *ch, sfxcache_t *sc, int count)
342 int *lscale, *rscale;
346 if (ch->leftvol > 255)
348 if (ch->rightvol > 255)
351 lscale = snd_scaletable[ch->leftvol >> 3];
352 rscale = snd_scaletable[ch->rightvol >> 3];
355 // LordHavoc: stereo sound support, and optimizations
356 sfx = (unsigned char *)sc->data + ch->pos * 2;
358 for (i=0 ; i<count ; i++)
360 paintbuffer[i].left += lscale[*sfx++];
361 paintbuffer[i].right += rscale[*sfx++];
367 sfx = (unsigned char *)sc->data + ch->pos;
369 for (i=0 ; i<count ; i++)
371 paintbuffer[i].left += lscale[*sfx];
372 paintbuffer[i].right += rscale[*sfx++];
379 void SND_PaintChannelFrom16 (channel_t *ch, sfxcache_t *sc, int count)
383 int leftvol, rightvol;
387 leftvol = ch->leftvol;
388 rightvol = ch->rightvol;
391 // LordHavoc: stereo sound support, and optimizations
392 sfx = (signed short *)sc->data + ch->pos * 2;
394 for (i=0 ; i<count ; i++)
396 paintbuffer[i].left += (*sfx++ * leftvol) >> 8;
397 paintbuffer[i].right += (*sfx++ * rightvol) >> 8;
402 sfx = (signed short *)sc->data + ch->pos;
404 for (i=0 ; i<count ; i++)
406 paintbuffer[i].left += (*sfx * leftvol) >> 8;
407 paintbuffer[i].right += (*sfx++ * rightvol) >> 8;