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
29 } portable_samplepair_t;
31 // LordHavoc: was 512, expanded to 2048
32 #define PAINTBUFFER_SIZE 2048
33 portable_samplepair_t paintbuffer[PAINTBUFFER_SIZE];
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 // TODO: rewrite this function
115 void S_TransferPaintBuffer(int endtime)
118 if ((pbuf = S_LockBuffer()))
123 int snd_linear_count;
125 snd_p = (int *) paintbuffer;
126 lpaintedtime = paintedtime;
127 if (shm->format.width == 2)
131 if (shm->format.channels == 2)
133 // 16bit 2 channels (stereo)
134 while (lpaintedtime < endtime)
136 // handle recirculating buffer issues
137 i = lpaintedtime & ((shm->samples >> 1) - 1);
138 snd_out = (short *) pbuf + (i << 1);
139 snd_linear_count = (shm->samples >> 1) - i;
140 if (snd_linear_count > endtime - lpaintedtime)
141 snd_linear_count = endtime - lpaintedtime;
142 snd_linear_count <<= 1;
143 if (snd_swapstereo.value)
145 for (i = 0;i < snd_linear_count;i += 2)
147 snd_out[i ] = bound(-32768, snd_p[i + 1], 32767);
148 snd_out[i + 1] = bound(-32768, snd_p[i ], 32767);
153 for (i = 0;i < snd_linear_count;i += 2)
155 snd_out[i ] = bound(-32768, snd_p[i ], 32767);
156 snd_out[i + 1] = bound(-32768, snd_p[i + 1], 32767);
159 snd_p += snd_linear_count;
160 lpaintedtime += (snd_linear_count >> 1);
165 // 16bit 1 channel (mono)
166 while (lpaintedtime < endtime)
168 // handle recirculating buffer issues
169 i = lpaintedtime & (shm->samples - 1);
170 snd_out = (short *) pbuf + i;
171 snd_linear_count = shm->samples - i;
172 if (snd_linear_count > endtime - lpaintedtime)
173 snd_linear_count = endtime - lpaintedtime;
174 for (i = 0;i < snd_linear_count;i++)
176 val = (snd_p[i * 2 + 0] + snd_p[i * 2 + 1]) >> 1;
177 snd_out[i] = bound(-32768, val, 32767);
179 snd_p += snd_linear_count << 1;
180 lpaintedtime += snd_linear_count;
187 unsigned char *snd_out;
188 if (shm->format.channels == 2)
190 // 8bit 2 channels (stereo)
191 while (lpaintedtime < endtime)
193 // handle recirculating buffer issues
194 i = lpaintedtime & ((shm->samples >> 1) - 1);
195 snd_out = (unsigned char *) pbuf + (i << 1);
196 snd_linear_count = (shm->samples >> 1) - i;
197 if (snd_linear_count > endtime - lpaintedtime)
198 snd_linear_count = endtime - lpaintedtime;
199 snd_linear_count <<= 1;
200 if (snd_swapstereo.value)
202 for (i = 0;i < snd_linear_count;i += 2)
204 val = (snd_p[i + 1] >> 8) + 128;
205 snd_out[i ] = bound(0, val, 255);
206 val = (snd_p[i ] >> 8) + 128;
207 snd_out[i + 1] = bound(0, val, 255);
212 for (i = 0;i < snd_linear_count;i += 2)
214 val = (snd_p[i ] >> 8) + 128;
215 snd_out[i ] = bound(0, val, 255);
216 val = (snd_p[i + 1] >> 8) + 128;
217 snd_out[i + 1] = bound(0, val, 255);
220 snd_p += snd_linear_count;
221 lpaintedtime += (snd_linear_count >> 1);
226 // 8bit 1 channel (mono)
227 while (lpaintedtime < endtime)
229 // handle recirculating buffer issues
230 i = lpaintedtime & (shm->samples - 1);
231 snd_out = (unsigned char *) pbuf + i;
232 snd_linear_count = shm->samples - i;
233 if (snd_linear_count > endtime - lpaintedtime)
234 snd_linear_count = endtime - lpaintedtime;
235 for (i = 0;i < snd_linear_count;i++)
237 val = ((snd_p[i * 2] + snd_p[i * 2 + 1]) >> 9) + 128;
238 snd_out[i ] = bound(0, val, 255);
240 snd_p += snd_linear_count << 1;
241 lpaintedtime += snd_linear_count;
252 ===============================================================================
256 ===============================================================================
259 qboolean SND_PaintChannelFrom8 (channel_t *ch, int endtime);
260 qboolean SND_PaintChannelFrom16 (channel_t *ch, int endtime);
262 void S_PaintChannels(int endtime)
270 while (paintedtime < endtime)
272 // if paintbuffer is smaller than DMA buffer
274 if (endtime - paintedtime > PAINTBUFFER_SIZE)
275 end = paintedtime + PAINTBUFFER_SIZE;
277 // clear the paint buffer
278 memset (&paintbuffer, 0, (end - paintedtime) * sizeof (paintbuffer[0]));
280 // paint in the channels.
282 for (i=0; i<total_channels ; i++, ch++)
287 if (!ch->leftvol && !ch->rightvol)
289 if (!S_LoadSound (sfx, true))
292 // if the channel is paused
293 if (ch->flags & CHANNELFLAG_PAUSED)
297 pausedtime = end - paintedtime;
298 ch->lastptime += pausedtime;
299 ch->end += pausedtime;
303 // if the sound hasn't been painted last time, update his position
304 if (ch->lastptime < paintedtime)
306 ch->pos += paintedtime - ch->lastptime;
308 // If the sound should have ended by then
309 if ((unsigned int)ch->pos > sfx->total_length)
313 if (ch->flags & CHANNELFLAG_FORCELOOP)
317 if (sfx->loopstart >= 0)
318 loopstart = sfx->loopstart;
320 // If the sound is looped
322 ch->pos = (ch->pos - sfx->total_length) % (sfx->total_length - loopstart) + loopstart;
324 ch->pos = sfx->total_length;
325 ch->end = paintedtime + sfx->total_length - ch->pos;
336 count = ch->end - ltime;
342 if (ch->leftvol > 255)
344 if (ch->rightvol > 255)
347 if (sfx->format.width == 1)
348 stop_paint = !SND_PaintChannelFrom8 (ch, count);
350 stop_paint = !SND_PaintChannelFrom16 (ch, count);
355 ch->lastptime = ltime;
361 if (ltime >= ch->end)
363 // if at end of loop, restart
364 if ((sfx->loopstart >= 0 || (ch->flags & CHANNELFLAG_FORCELOOP)) && !stop_paint)
366 ch->pos = bound(0, sfx->loopstart, (int)sfx->total_length - 1);
367 ch->end = ltime + sfx->total_length - ch->pos;
369 // channel just stopped
376 S_StopChannel (ch - channels);
382 // transfer out according to DMA format
383 S_CaptureAVISound (paintbuffer, end - paintedtime);
384 S_TransferPaintBuffer(end);
390 // TODO: Try to merge SND_PaintChannelFrom8 and SND_PaintChannelFrom16
391 qboolean SND_PaintChannelFrom8 (channel_t *ch, int count)
393 int snd_vol, leftvol, rightvol;
394 const signed char *sfx;
395 const sfxbuffer_t *sb;
398 // If this channel manages its own volume
399 if (ch->flags & CHANNELFLAG_FULLVOLUME)
402 snd_vol = volume.value * 256;
404 leftvol = ch->leftvol * snd_vol;
405 rightvol = ch->rightvol * snd_vol;
407 sb = ch->sfx->fetcher->getsb (ch, ch->pos, count);
411 // Stereo sound support
412 if (ch->sfx->format.channels == 2)
414 sfx = sb->data + (ch->pos - sb->offset) * 2;
415 for (i = 0;i < count;i++)
417 paintbuffer[i].left += (*sfx++ * leftvol) >> 8;
418 paintbuffer[i].right += (*sfx++ * rightvol) >> 8;
423 sfx = sb->data + ch->pos - sb->offset;
424 for (i = 0;i < count;i++)
426 paintbuffer[i].left += (*sfx * leftvol) >> 8;
427 paintbuffer[i].right += (*sfx++ * rightvol) >> 8;
435 qboolean SND_PaintChannelFrom16 (channel_t *ch, int count)
437 int snd_vol, leftvol, rightvol;
439 const sfxbuffer_t *sb;
442 // If this channel manages its own volume
443 if (ch->flags & CHANNELFLAG_FULLVOLUME)
446 snd_vol = volume.value * 256;
448 leftvol = ch->leftvol * snd_vol;
449 rightvol = ch->rightvol * snd_vol;
451 sb = ch->sfx->fetcher->getsb (ch, ch->pos, count);
455 // Stereo sound support
456 if (ch->sfx->format.channels == 2)
458 sfx = (signed short *)sb->data + (ch->pos - sb->offset) * 2;
460 for (i=0 ; i<count ; i++)
462 paintbuffer[i].left += (*sfx++ * leftvol) >> 16;
463 paintbuffer[i].right += (*sfx++ * rightvol) >> 16;
468 sfx = (signed short *)sb->data + ch->pos - sb->offset;
470 for (i=0 ; i<count ; i++)
472 paintbuffer[i].left += (*sfx * leftvol) >> 16;
473 paintbuffer[i].right += (*sfx++ * rightvol) >> 16;