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];
36 // LordHavoc: disabled this because it desyncs with the video too easily
37 extern cvar_t cl_avidemo;
38 static FILE *cl_avidemo_soundfile = NULL;
39 void S_CaptureAVISound(portable_samplepair_t *buf, int length)
42 qbyte out[PAINTBUFFER_SIZE * 4];
43 char filename[MAX_OSPATH];
45 if (cl_avidemo.value >= 0.1f)
47 if (cl_avidemo_soundfile == NULL)
49 cl_avidemo_soundfile = FS_Open ("dpavi.wav", "wb", false);
51 fwrite(out, 1, 44, cl_avidemo_soundfile);
52 // header will be filled out when file is closed
54 fseek(cl_avidemo_soundfile, 0, SEEK_END);
55 // write the sound buffer as little endian 16bit interleaved stereo
56 for(i = 0;i < length;i++)
58 n = buf[i].left >> 2; // quiet enough to prevent clipping most of the time
59 n = bound(-32768, n, 32767);
60 out[i*4+0] = n & 0xFF;
61 out[i*4+1] = (n >> 8) & 0xFF;
62 n = buf[i].right >> 2; // quiet enough to prevent clipping most of the time
63 n = bound(-32768, n, 32767);
64 out[i*4+2] = n & 0xFF;
65 out[i*4+3] = (n >> 8) & 0xFF;
67 if (fwrite(out, 4, length, cl_avidemo_soundfile) < length)
69 Cvar_SetValueQuick(&cl_avidemo, 0);
70 Con_Print("avi saving sound failed, out of disk space? stopping avi demo capture.\n");
73 else if (cl_avidemo_soundfile)
75 // file has not been closed yet, close it
76 fseek(cl_avidemo_soundfile, 0, SEEK_END);
77 i = ftell(cl_avidemo_soundfile);
79 //"RIFF", (int) unknown (chunk size), "WAVE",
80 //"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
81 //"data", (int) unknown (chunk size)
82 memcpy(out, "RIFF****WAVEfmt \x10\x00\x00\x00\x01\x00\x02\x00********\x04\x00\x10\x00data****", 44);
83 // the length of the whole RIFF chunk
86 out[5] = (n >> 8) & 0xFF;
87 out[6] = (n >> 16) & 0xFF;
88 out[7] = (n >> 24) & 0xFF;
92 out[25] = (n >> 8) & 0xFF;
93 out[26] = (n >> 16) & 0xFF;
94 out[27] = (n >> 24) & 0xFF;
95 // bytes per second (rate * channels * bytes per channel)
98 out[29] = (n >> 8) & 0xFF;
99 out[30] = (n >> 16) & 0xFF;
100 out[31] = (n >> 24) & 0xFF;
101 // the length of the data chunk
103 out[40] = (n) & 0xFF;
104 out[41] = (n >> 8) & 0xFF;
105 out[42] = (n >> 16) & 0xFF;
106 out[43] = (n >> 24) & 0xFF;
108 fseek(cl_avidemo_soundfile, 0, SEEK_SET);
109 fwrite(out, 1, 44, cl_avidemo_soundfile);
110 fclose(cl_avidemo_soundfile);
111 cl_avidemo_soundfile = NULL;
116 void S_TransferPaintBuffer(int endtime)
119 if ((pbuf = S_LockBuffer()))
125 int snd_linear_count;
127 snd_p = (int *) paintbuffer;
128 snd_vol = volume.value*256;
129 lpaintedtime = paintedtime;
130 if (shm->format.width == 2)
134 if (shm->format.channels == 2)
136 // 16bit 2 channels (stereo)
137 while (lpaintedtime < endtime)
139 // handle recirculating buffer issues
140 i = lpaintedtime & ((shm->samples >> 1) - 1);
141 snd_out = (short *) pbuf + (i << 1);
142 snd_linear_count = (shm->samples >> 1) - i;
143 if (snd_linear_count > endtime - lpaintedtime)
144 snd_linear_count = endtime - lpaintedtime;
145 snd_linear_count <<= 1;
146 if (snd_swapstereo.value)
148 for (i = 0;i < snd_linear_count;i += 2)
150 val = (snd_p[i + 1] * snd_vol) >> 8;
151 snd_out[i ] = bound(-32768, val, 32767);
152 val = (snd_p[i ] * snd_vol) >> 8;
153 snd_out[i + 1] = bound(-32768, val, 32767);
158 for (i = 0;i < snd_linear_count;i += 2)
160 val = (snd_p[i ] * snd_vol) >> 8;
161 snd_out[i ] = bound(-32768, val, 32767);
162 val = (snd_p[i + 1] * snd_vol) >> 8;
163 snd_out[i + 1] = bound(-32768, val, 32767);
166 snd_p += snd_linear_count;
167 lpaintedtime += (snd_linear_count >> 1);
172 // 16bit 1 channel (mono)
173 while (lpaintedtime < endtime)
175 // handle recirculating buffer issues
176 i = lpaintedtime & (shm->samples - 1);
177 snd_out = (short *) pbuf + i;
178 snd_linear_count = shm->samples - i;
179 if (snd_linear_count > endtime - lpaintedtime)
180 snd_linear_count = endtime - lpaintedtime;
181 for (i = 0;i < snd_linear_count;i++)
183 val = ((snd_p[i * 2 + 0] + snd_p[i * 2 + 1]) * snd_vol) >> 9;
184 snd_out[i] = bound(-32768, val, 32767);
186 snd_p += snd_linear_count << 1;
187 lpaintedtime += snd_linear_count;
194 unsigned char *snd_out;
195 if (shm->format.channels == 2)
197 // 8bit 2 channels (stereo)
198 while (lpaintedtime < endtime)
200 // handle recirculating buffer issues
201 i = lpaintedtime & ((shm->samples >> 1) - 1);
202 snd_out = (unsigned char *) pbuf + (i << 1);
203 snd_linear_count = (shm->samples >> 1) - i;
204 if (snd_linear_count > endtime - lpaintedtime)
205 snd_linear_count = endtime - lpaintedtime;
206 snd_linear_count <<= 1;
207 if (snd_swapstereo.value)
209 for (i = 0;i < snd_linear_count;i += 2)
211 val = ((snd_p[i + 1] * snd_vol) >> 16) + 128;
212 snd_out[i ] = bound(0, val, 255);
213 val = ((snd_p[i ] * snd_vol) >> 16) + 128;
214 snd_out[i + 1] = bound(0, val, 255);
219 for (i = 0;i < snd_linear_count;i += 2)
221 val = ((snd_p[i ] * snd_vol) >> 16) + 128;
222 snd_out[i ] = bound(0, val, 255);
223 val = ((snd_p[i + 1] * snd_vol) >> 16) + 128;
224 snd_out[i + 1] = bound(0, val, 255);
227 snd_p += snd_linear_count;
228 lpaintedtime += (snd_linear_count >> 1);
233 // 8bit 1 channel (mono)
234 while (lpaintedtime < endtime)
236 // handle recirculating buffer issues
237 i = lpaintedtime & (shm->samples - 1);
238 snd_out = (unsigned char *) pbuf + i;
239 snd_linear_count = shm->samples - i;
240 if (snd_linear_count > endtime - lpaintedtime)
241 snd_linear_count = endtime - lpaintedtime;
242 for (i = 0;i < snd_linear_count;i++)
244 val = (((snd_p[i * 2] + snd_p[i * 2 + 1]) * snd_vol) >> 17) + 128;
245 snd_out[i ] = bound(0, val, 255);
247 snd_p += snd_linear_count << 1;
248 lpaintedtime += snd_linear_count;
259 ===============================================================================
263 ===============================================================================
266 qboolean SND_PaintChannelFrom8 (channel_t *ch, int endtime);
267 qboolean SND_PaintChannelFrom16 (channel_t *ch, int endtime);
269 void S_PaintChannels(int endtime)
277 while (paintedtime < endtime)
279 // if paintbuffer is smaller than DMA buffer
281 if (endtime - paintedtime > PAINTBUFFER_SIZE)
282 end = paintedtime + PAINTBUFFER_SIZE;
284 // clear the paint buffer, filling it with data from rawsamples (music/video/whatever)
285 S_RawSamples_Dequeue(&paintbuffer->left, end - paintedtime);
287 // paint in the channels.
289 for (i=0; i<total_channels ; i++, ch++)
294 if (!ch->leftvol && !ch->rightvol)
296 if (!S_LoadSound (sfx, true))
299 // if the channel is paused
300 if (ch->flags & CHANNELFLAG_PAUSED)
304 pausedtime = end - paintedtime;
305 ch->lastptime += pausedtime;
306 ch->end += pausedtime;
310 // if the sound hasn't been painted last time, update his position
311 if (ch->lastptime < paintedtime)
313 ch->pos += paintedtime - ch->lastptime;
315 // If the sound should have ended by then
316 if ((unsigned int)ch->pos > sfx->total_length)
320 if (ch->flags & CHANNELFLAG_FORCELOOP)
324 if (sfx->loopstart >= 0)
325 loopstart = sfx->loopstart;
327 // If the sound is looped
329 ch->pos = (ch->pos - sfx->total_length) % (sfx->total_length - loopstart) + loopstart;
331 ch->pos = sfx->total_length;
332 ch->end = paintedtime + sfx->total_length - ch->pos;
343 count = ch->end - ltime;
349 if (sfx->format.width == 1)
350 stop_paint = !SND_PaintChannelFrom8(ch, count);
352 stop_paint = !SND_PaintChannelFrom16(ch, count);
357 ch->lastptime = ltime;
363 if (ltime >= ch->end)
365 // if at end of loop, restart
366 if ((sfx->loopstart >= 0 || (ch->flags & CHANNELFLAG_FORCELOOP)) && !stop_paint)
368 ch->pos = bound(0, sfx->loopstart, (int)sfx->total_length - 1);
369 ch->end = ltime + sfx->total_length - ch->pos;
371 // channel just stopped
378 S_StopChannel (ch - channels);
384 // transfer out according to DMA format
385 //S_CaptureAVISound(paintbuffer, end - paintedtime);
386 S_TransferPaintBuffer(end);
391 void SND_InitScaletable (void)
395 for (i = 0;i < 32;i++)
396 for (j = 0;j < 256;j++)
397 snd_scaletable[i][j] = ((signed char)j) * i * 8;
401 qboolean SND_PaintChannelFrom8 (channel_t *ch, int count)
403 int *lscale, *rscale;
405 const sfxbuffer_t *sb;
408 if (ch->leftvol > 255)
410 if (ch->rightvol > 255)
413 lscale = snd_scaletable[ch->leftvol >> 3];
414 rscale = snd_scaletable[ch->rightvol >> 3];
416 sb = ch->sfx->fetcher->getsb (ch, ch->pos, count);
420 if (ch->sfx->format.channels == 2)
422 // LordHavoc: stereo sound support, and optimizations
423 sfx = (unsigned char *)sb->data + (ch->pos - sb->offset) * 2;
424 for (i = 0;i < count;i++)
426 paintbuffer[i].left += lscale[*sfx++];
427 paintbuffer[i].right += rscale[*sfx++];
432 sfx = (unsigned char *)sb->data + ch->pos - sb->offset;
433 for (i = 0;i < count;i++)
436 paintbuffer[i].left += lscale[n];
437 paintbuffer[i].right += rscale[n];
445 qboolean SND_PaintChannelFrom16 (channel_t *ch, int count)
447 int leftvol, rightvol;
449 const sfxbuffer_t *sb;
452 leftvol = ch->leftvol;
453 rightvol = ch->rightvol;
455 sb = ch->sfx->fetcher->getsb (ch, ch->pos, count);
459 if (ch->sfx->format.channels == 2)
461 // LordHavoc: stereo sound support, and optimizations
462 sfx = (signed short *)sb->data + (ch->pos - sb->offset) * 2;
464 for (i=0 ; i<count ; i++)
466 paintbuffer[i].left += (*sfx++ * leftvol) >> 8;
467 paintbuffer[i].right += (*sfx++ * rightvol) >> 8;
472 sfx = (signed short *)sb->data + ch->pos - sb->offset;
474 for (i=0 ; i<count ; i++)
476 paintbuffer[i].left += (*sfx * leftvol) >> 8;
477 paintbuffer[i].right += (*sfx++ * rightvol) >> 8;