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
28 } portable_samplepair_t;
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];
35 // FIXME: it desyncs with the video too easily
36 extern cvar_t cl_avidemo;
37 static qfile_t *cl_avidemo_soundfile = NULL;
38 void S_CaptureAVISound(portable_samplepair_t *buf, size_t length)
42 qbyte out[PAINTBUFFER_SIZE * 4];
44 if (cl_avidemo.value >= 0.1f)
46 if (cl_avidemo_soundfile == NULL)
48 cl_avidemo_soundfile = FS_Open ("video/dpavi.wav", "wb", false);
50 FS_Write (cl_avidemo_soundfile, out, 44);
51 // header will be filled out when file is closed
53 FS_Seek (cl_avidemo_soundfile, 0, SEEK_END);
54 // write the sound buffer as little endian 16bit interleaved stereo
55 for(i = 0;i < length;i++)
57 n = buf[i].left >> 2; // quiet enough to prevent clipping most of the time
58 n = bound(-32768, n, 32767);
59 out[i*4+0] = n & 0xFF;
60 out[i*4+1] = (n >> 8) & 0xFF;
61 n = buf[i].right >> 2; // quiet enough to prevent clipping most of the time
62 n = bound(-32768, n, 32767);
63 out[i*4+2] = n & 0xFF;
64 out[i*4+3] = (n >> 8) & 0xFF;
66 if (FS_Write (cl_avidemo_soundfile, out, 4 * length) < 4 * length)
68 Cvar_SetValueQuick(&cl_avidemo, 0);
69 Con_Print("avi saving sound failed, out of disk space? stopping avi demo capture.\n");
72 else if (cl_avidemo_soundfile)
74 // file has not been closed yet, close it
75 FS_Seek (cl_avidemo_soundfile, 0, SEEK_END);
76 i = FS_Tell (cl_avidemo_soundfile);
78 //"RIFF", (int) unknown (chunk size), "WAVE",
79 //"fmt ", (int) 16 (chunk size), (short) format 1 (uncompressed PCM), (short) 2 channels, (int) unknown rate, (int) unknown bytes per second, (short) 4 bytes per sample (channels * bytes per channel), (short) 16 bits per channel
80 //"data", (int) unknown (chunk size)
81 memcpy (out, "RIFF****WAVEfmt \x10\x00\x00\x00\x01\x00\x02\x00********\x04\x00\x10\0data****", 44);
82 // the length of the whole RIFF chunk
85 out[5] = (n >> 8) & 0xFF;
86 out[6] = (n >> 16) & 0xFF;
87 out[7] = (n >> 24) & 0xFF;
89 n = shm->format.speed;
91 out[25] = (n >> 8) & 0xFF;
92 out[26] = (n >> 16) & 0xFF;
93 out[27] = (n >> 24) & 0xFF;
94 // bytes per second (rate * channels * bytes per channel)
95 n = shm->format.speed * 2 * 2;
97 out[29] = (n >> 8) & 0xFF;
98 out[30] = (n >> 16) & 0xFF;
99 out[31] = (n >> 24) & 0xFF;
100 // the length of the data chunk
102 out[40] = (n) & 0xFF;
103 out[41] = (n >> 8) & 0xFF;
104 out[42] = (n >> 16) & 0xFF;
105 out[43] = (n >> 24) & 0xFF;
107 FS_Seek (cl_avidemo_soundfile, 0, SEEK_SET);
108 FS_Write (cl_avidemo_soundfile, out, 44);
109 FS_Close (cl_avidemo_soundfile);
110 cl_avidemo_soundfile = NULL;
114 void S_TransferPaintBuffer(int endtime)
117 if ((pbuf = S_LockBuffer()))
123 int snd_linear_count;
125 snd_p = (int *) paintbuffer;
126 snd_vol = volume.value*256;
127 lpaintedtime = paintedtime;
128 if (shm->format.width == 2)
132 if (shm->format.channels == 2)
134 // 16bit 2 channels (stereo)
135 while (lpaintedtime < endtime)
137 // handle recirculating buffer issues
138 i = lpaintedtime & ((shm->samples >> 1) - 1);
139 snd_out = (short *) pbuf + (i << 1);
140 snd_linear_count = (shm->samples >> 1) - i;
141 if (snd_linear_count > endtime - lpaintedtime)
142 snd_linear_count = endtime - lpaintedtime;
143 snd_linear_count <<= 1;
144 if (snd_swapstereo.value)
146 for (i = 0;i < snd_linear_count;i += 2)
148 val = (snd_p[i + 1] * snd_vol) >> 8;
149 snd_out[i ] = bound(-32768, val, 32767);
150 val = (snd_p[i ] * snd_vol) >> 8;
151 snd_out[i + 1] = bound(-32768, val, 32767);
156 for (i = 0;i < snd_linear_count;i += 2)
158 val = (snd_p[i ] * snd_vol) >> 8;
159 snd_out[i ] = bound(-32768, val, 32767);
160 val = (snd_p[i + 1] * snd_vol) >> 8;
161 snd_out[i + 1] = bound(-32768, val, 32767);
164 snd_p += snd_linear_count;
165 lpaintedtime += (snd_linear_count >> 1);
170 // 16bit 1 channel (mono)
171 while (lpaintedtime < endtime)
173 // handle recirculating buffer issues
174 i = lpaintedtime & (shm->samples - 1);
175 snd_out = (short *) pbuf + i;
176 snd_linear_count = shm->samples - i;
177 if (snd_linear_count > endtime - lpaintedtime)
178 snd_linear_count = endtime - lpaintedtime;
179 for (i = 0;i < snd_linear_count;i++)
181 val = ((snd_p[i * 2 + 0] + snd_p[i * 2 + 1]) * snd_vol) >> 9;
182 snd_out[i] = bound(-32768, val, 32767);
184 snd_p += snd_linear_count << 1;
185 lpaintedtime += snd_linear_count;
192 unsigned char *snd_out;
193 if (shm->format.channels == 2)
195 // 8bit 2 channels (stereo)
196 while (lpaintedtime < endtime)
198 // handle recirculating buffer issues
199 i = lpaintedtime & ((shm->samples >> 1) - 1);
200 snd_out = (unsigned char *) pbuf + (i << 1);
201 snd_linear_count = (shm->samples >> 1) - i;
202 if (snd_linear_count > endtime - lpaintedtime)
203 snd_linear_count = endtime - lpaintedtime;
204 snd_linear_count <<= 1;
205 if (snd_swapstereo.value)
207 for (i = 0;i < snd_linear_count;i += 2)
209 val = ((snd_p[i + 1] * snd_vol) >> 16) + 128;
210 snd_out[i ] = bound(0, val, 255);
211 val = ((snd_p[i ] * snd_vol) >> 16) + 128;
212 snd_out[i + 1] = bound(0, val, 255);
217 for (i = 0;i < snd_linear_count;i += 2)
219 val = ((snd_p[i ] * snd_vol) >> 16) + 128;
220 snd_out[i ] = bound(0, val, 255);
221 val = ((snd_p[i + 1] * snd_vol) >> 16) + 128;
222 snd_out[i + 1] = bound(0, val, 255);
225 snd_p += snd_linear_count;
226 lpaintedtime += (snd_linear_count >> 1);
231 // 8bit 1 channel (mono)
232 while (lpaintedtime < endtime)
234 // handle recirculating buffer issues
235 i = lpaintedtime & (shm->samples - 1);
236 snd_out = (unsigned char *) pbuf + i;
237 snd_linear_count = shm->samples - i;
238 if (snd_linear_count > endtime - lpaintedtime)
239 snd_linear_count = endtime - lpaintedtime;
240 for (i = 0;i < snd_linear_count;i++)
242 val = (((snd_p[i * 2] + snd_p[i * 2 + 1]) * snd_vol) >> 17) + 128;
243 snd_out[i ] = bound(0, val, 255);
245 snd_p += snd_linear_count << 1;
246 lpaintedtime += snd_linear_count;
257 ===============================================================================
261 ===============================================================================
264 qboolean SND_PaintChannelFrom8 (channel_t *ch, int endtime);
265 qboolean SND_PaintChannelFrom16 (channel_t *ch, int endtime);
267 void S_PaintChannels(int endtime)
275 while (paintedtime < endtime)
277 // if paintbuffer is smaller than DMA buffer
279 if (endtime - paintedtime > PAINTBUFFER_SIZE)
280 end = paintedtime + PAINTBUFFER_SIZE;
282 // clear the paint buffer
283 memset (&paintbuffer, 0, (end - paintedtime) * sizeof (paintbuffer[0]));
285 // paint in the channels.
287 for (i=0; i<total_channels ; i++, ch++)
292 if (!ch->leftvol && !ch->rightvol)
294 if (!S_LoadSound (sfx, true))
297 // if the channel is paused
298 if (ch->flags & CHANNELFLAG_PAUSED)
302 pausedtime = end - paintedtime;
303 ch->lastptime += pausedtime;
304 ch->end += pausedtime;
308 // if the sound hasn't been painted last time, update his position
309 if (ch->lastptime < paintedtime)
311 ch->pos += paintedtime - ch->lastptime;
313 // If the sound should have ended by then
314 if ((unsigned int)ch->pos > sfx->total_length)
318 if (ch->flags & CHANNELFLAG_FORCELOOP)
322 if (sfx->loopstart >= 0)
323 loopstart = sfx->loopstart;
325 // If the sound is looped
327 ch->pos = (ch->pos - sfx->total_length) % (sfx->total_length - loopstart) + loopstart;
329 ch->pos = sfx->total_length;
330 ch->end = paintedtime + sfx->total_length - ch->pos;
341 count = ch->end - ltime;
347 if (ch->leftvol > 255)
349 if (ch->rightvol > 255)
352 if (sfx->format.width == 1)
353 stop_paint = !SND_PaintChannelFrom8(ch, count);
355 stop_paint = !SND_PaintChannelFrom16(ch, count);
360 ch->lastptime = ltime;
366 if (ltime >= ch->end)
368 // if at end of loop, restart
369 if ((sfx->loopstart >= 0 || (ch->flags & CHANNELFLAG_FORCELOOP)) && !stop_paint)
371 ch->pos = bound(0, sfx->loopstart, (int)sfx->total_length - 1);
372 ch->end = ltime + sfx->total_length - ch->pos;
374 // channel just stopped
381 S_StopChannel (ch - channels);
387 // transfer out according to DMA format
388 S_CaptureAVISound (paintbuffer, end - paintedtime);
389 S_TransferPaintBuffer(end);
394 void SND_InitScaletable (void)
398 for (i = 0;i < 32;i++)
399 for (j = 0;j < 256;j++)
400 snd_scaletable[i][j] = ((signed char)j) * i * 8;
404 qboolean SND_PaintChannelFrom8 (channel_t *ch, int count)
406 int *lscale, *rscale;
408 const sfxbuffer_t *sb;
411 lscale = snd_scaletable[ch->leftvol >> 3];
412 rscale = snd_scaletable[ch->rightvol >> 3];
414 sb = ch->sfx->fetcher->getsb (ch, ch->pos, count);
418 if (ch->sfx->format.channels == 2)
420 // LordHavoc: stereo sound support, and optimizations
421 sfx = (unsigned char *)sb->data + (ch->pos - sb->offset) * 2;
422 for (i = 0;i < count;i++)
424 paintbuffer[i].left += lscale[*sfx++];
425 paintbuffer[i].right += rscale[*sfx++];
430 sfx = (unsigned char *)sb->data + ch->pos - sb->offset;
431 for (i = 0;i < count;i++)
434 paintbuffer[i].left += lscale[n];
435 paintbuffer[i].right += rscale[n];
443 qboolean SND_PaintChannelFrom16 (channel_t *ch, int count)
445 int leftvol, rightvol;
447 const sfxbuffer_t *sb;
450 leftvol = ch->leftvol;
451 rightvol = ch->rightvol;
453 sb = ch->sfx->fetcher->getsb (ch, ch->pos, count);
457 if (ch->sfx->format.channels == 2)
459 // LordHavoc: stereo sound support, and optimizations
460 sfx = (signed short *)sb->data + (ch->pos - sb->offset) * 2;
462 for (i=0 ; i<count ; i++)
464 paintbuffer[i].left += (*sfx++ * leftvol) >> 8;
465 paintbuffer[i].right += (*sfx++ * rightvol) >> 8;
470 sfx = (signed short *)sb->data + ch->pos - sb->offset;
472 for (i=0 ; i<count ; i++)
474 paintbuffer[i].left += (*sfx * leftvol) >> 8;
475 paintbuffer[i].right += (*sfx++ * rightvol) >> 8;