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
24 // LordHavoc: was 512, expanded to 2048
25 #define PAINTBUFFER_SIZE 2048
26 portable_samplepair_t paintbuffer[PAINTBUFFER_SIZE];
27 int snd_scaletable[32][256];
30 // LordHavoc: disabled this because it desyncs with the video too easily
31 extern cvar_t cl_avidemo;
32 static FILE *cl_avidemo_soundfile = NULL;
33 void S_CaptureAVISound(portable_samplepair_t *buf, int length)
36 qbyte out[PAINTBUFFER_SIZE * 4];
37 char filename[MAX_OSPATH];
39 if (cl_avidemo.value >= 0.1f)
41 if (cl_avidemo_soundfile == NULL)
43 cl_avidemo_soundfile = FS_Open ("dpavi.wav", "wb", false);
45 fwrite(out, 1, 44, cl_avidemo_soundfile);
46 // header will be filled out when file is closed
48 fseek(cl_avidemo_soundfile, 0, SEEK_END);
49 // write the sound buffer as little endian 16bit interleaved stereo
50 for(i = 0;i < length;i++)
52 n = buf[i].left >> 2; // quiet enough to prevent clipping most of the time
53 n = bound(-32768, n, 32767);
54 out[i*4+0] = n & 0xFF;
55 out[i*4+1] = (n >> 8) & 0xFF;
56 n = buf[i].right >> 2; // quiet enough to prevent clipping most of the time
57 n = bound(-32768, n, 32767);
58 out[i*4+2] = n & 0xFF;
59 out[i*4+3] = (n >> 8) & 0xFF;
61 if (fwrite(out, 4, length, cl_avidemo_soundfile) < length)
63 Cvar_SetValueQuick(&cl_avidemo, 0);
64 Con_Print("avi saving sound failed, out of disk space? stopping avi demo capture.\n");
67 else if (cl_avidemo_soundfile)
69 // file has not been closed yet, close it
70 fseek(cl_avidemo_soundfile, 0, SEEK_END);
71 i = ftell(cl_avidemo_soundfile);
73 //"RIFF", (int) unknown (chunk size), "WAVE",
74 //"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
75 //"data", (int) unknown (chunk size)
76 memcpy(out, "RIFF****WAVEfmt \x10\x00\x00\x00\x01\x00\x02\x00********\x04\x00\x10\x00data****", 44);
77 // the length of the whole RIFF chunk
80 out[5] = (n >> 8) & 0xFF;
81 out[6] = (n >> 16) & 0xFF;
82 out[7] = (n >> 24) & 0xFF;
86 out[25] = (n >> 8) & 0xFF;
87 out[26] = (n >> 16) & 0xFF;
88 out[27] = (n >> 24) & 0xFF;
89 // bytes per second (rate * channels * bytes per channel)
92 out[29] = (n >> 8) & 0xFF;
93 out[30] = (n >> 16) & 0xFF;
94 out[31] = (n >> 24) & 0xFF;
95 // the length of the data chunk
98 out[41] = (n >> 8) & 0xFF;
99 out[42] = (n >> 16) & 0xFF;
100 out[43] = (n >> 24) & 0xFF;
102 fseek(cl_avidemo_soundfile, 0, SEEK_SET);
103 fwrite(out, 1, 44, cl_avidemo_soundfile);
104 fclose(cl_avidemo_soundfile);
105 cl_avidemo_soundfile = NULL;
110 void S_TransferPaintBuffer(int endtime)
113 if ((pbuf = S_LockBuffer()))
119 int snd_linear_count;
121 snd_p = (int *) paintbuffer;
122 snd_vol = volume.value*256;
123 lpaintedtime = paintedtime;
124 if (shm->format.width == 2)
128 if (shm->format.channels == 2)
130 // 16bit 2 channels (stereo)
131 while (lpaintedtime < endtime)
133 // handle recirculating buffer issues
134 i = lpaintedtime & ((shm->samples >> 1) - 1);
135 snd_out = (short *) pbuf + (i << 1);
136 snd_linear_count = (shm->samples >> 1) - i;
137 if (snd_linear_count > endtime - lpaintedtime)
138 snd_linear_count = endtime - lpaintedtime;
139 snd_linear_count <<= 1;
140 if (snd_swapstereo.value)
142 for (i = 0;i < snd_linear_count;i += 2)
144 val = (snd_p[i + 1] * snd_vol) >> 8;
145 snd_out[i ] = bound(-32768, val, 32767);
146 val = (snd_p[i ] * snd_vol) >> 8;
147 snd_out[i + 1] = bound(-32768, val, 32767);
152 for (i = 0;i < snd_linear_count;i += 2)
154 val = (snd_p[i ] * snd_vol) >> 8;
155 snd_out[i ] = bound(-32768, val, 32767);
156 val = (snd_p[i + 1] * snd_vol) >> 8;
157 snd_out[i + 1] = bound(-32768, val, 32767);
160 snd_p += snd_linear_count;
161 lpaintedtime += (snd_linear_count >> 1);
166 // 16bit 1 channel (mono)
167 while (lpaintedtime < endtime)
169 // handle recirculating buffer issues
170 i = lpaintedtime & (shm->samples - 1);
171 snd_out = (short *) pbuf + i;
172 snd_linear_count = shm->samples - i;
173 if (snd_linear_count > endtime - lpaintedtime)
174 snd_linear_count = endtime - lpaintedtime;
175 for (i = 0;i < snd_linear_count;i++)
177 val = ((snd_p[i * 2 + 0] + snd_p[i * 2 + 1]) * snd_vol) >> 9;
178 snd_out[i] = bound(-32768, val, 32767);
180 snd_p += snd_linear_count << 1;
181 lpaintedtime += snd_linear_count;
188 unsigned char *snd_out;
189 if (shm->format.channels == 2)
191 // 8bit 2 channels (stereo)
192 while (lpaintedtime < endtime)
194 // handle recirculating buffer issues
195 i = lpaintedtime & ((shm->samples >> 1) - 1);
196 snd_out = (unsigned char *) pbuf + (i << 1);
197 snd_linear_count = (shm->samples >> 1) - i;
198 if (snd_linear_count > endtime - lpaintedtime)
199 snd_linear_count = endtime - lpaintedtime;
200 snd_linear_count <<= 1;
201 if (snd_swapstereo.value)
203 for (i = 0;i < snd_linear_count;i += 2)
205 val = ((snd_p[i + 1] * snd_vol) >> 16) + 128;
206 snd_out[i ] = bound(0, val, 255);
207 val = ((snd_p[i ] * snd_vol) >> 16) + 128;
208 snd_out[i + 1] = bound(0, val, 255);
213 for (i = 0;i < snd_linear_count;i += 2)
215 val = ((snd_p[i ] * snd_vol) >> 16) + 128;
216 snd_out[i ] = bound(0, val, 255);
217 val = ((snd_p[i + 1] * snd_vol) >> 16) + 128;
218 snd_out[i + 1] = bound(0, val, 255);
221 snd_p += snd_linear_count;
222 lpaintedtime += (snd_linear_count >> 1);
227 // 8bit 1 channel (mono)
228 while (lpaintedtime < endtime)
230 // handle recirculating buffer issues
231 i = lpaintedtime & (shm->samples - 1);
232 snd_out = (unsigned char *) pbuf + i;
233 snd_linear_count = shm->samples - i;
234 if (snd_linear_count > endtime - lpaintedtime)
235 snd_linear_count = endtime - lpaintedtime;
236 for (i = 0;i < snd_linear_count;i++)
238 val = (((snd_p[i * 2] + snd_p[i * 2 + 1]) * snd_vol) >> 17) + 128;
239 snd_out[i ] = bound(0, val, 255);
241 snd_p += snd_linear_count << 1;
242 lpaintedtime += snd_linear_count;
253 ===============================================================================
257 ===============================================================================
260 qboolean SND_PaintChannelFrom8 (channel_t *ch, int endtime);
261 qboolean SND_PaintChannelFrom16 (channel_t *ch, int endtime);
263 void S_PaintChannels(int endtime)
271 while (paintedtime < endtime)
273 // if paintbuffer is smaller than DMA buffer
275 if (endtime - paintedtime > PAINTBUFFER_SIZE)
276 end = paintedtime + PAINTBUFFER_SIZE;
278 // clear the paint buffer, filling it with data from rawsamples (music/video/whatever)
279 S_RawSamples_Dequeue(&paintbuffer->left, end - paintedtime);
281 // paint in the channels.
283 for (i=0; i<total_channels ; i++, ch++)
288 if (!ch->leftvol && !ch->rightvol)
290 if (!S_LoadSound (sfx, true))
301 count = ch->end - ltime;
307 if (sfx->format.width == 1)
308 stop_paint = !SND_PaintChannelFrom8(ch, count);
310 stop_paint = !SND_PaintChannelFrom16(ch, count);
317 if (ltime >= ch->end)
319 // if at end of loop, restart
320 if ((sfx->loopstart >= 0 || ch->forceloop) && !stop_paint)
322 ch->pos = bound(0, sfx->loopstart, (int)sfx->total_length - 1);
323 ch->end = ltime + sfx->total_length - ch->pos;
325 // channel just stopped
332 if (ch->sfx->fetcher->end != NULL)
333 ch->sfx->fetcher->end (ch);
340 // transfer out according to DMA format
341 //S_CaptureAVISound(paintbuffer, end - paintedtime);
342 S_TransferPaintBuffer(end);
347 void SND_InitScaletable (void)
351 for (i = 0;i < 32;i++)
352 for (j = 0;j < 256;j++)
353 snd_scaletable[i][j] = ((signed char)j) * i * 8;
357 qboolean SND_PaintChannelFrom8 (channel_t *ch, int count)
359 int *lscale, *rscale;
361 const sfxbuffer_t *sb;
364 if (ch->leftvol > 255)
366 if (ch->rightvol > 255)
369 lscale = snd_scaletable[ch->leftvol >> 3];
370 rscale = snd_scaletable[ch->rightvol >> 3];
372 sb = ch->sfx->fetcher->getsb (ch, ch->pos, count);
376 if (ch->sfx->format.channels == 2)
378 // LordHavoc: stereo sound support, and optimizations
379 sfx = (unsigned char *)sb->data + (ch->pos - sb->offset) * 2;
380 for (i = 0;i < count;i++)
382 paintbuffer[i].left += lscale[*sfx++];
383 paintbuffer[i].right += rscale[*sfx++];
388 sfx = (unsigned char *)sb->data + ch->pos - sb->offset;
389 for (i = 0;i < count;i++)
392 paintbuffer[i].left += lscale[n];
393 paintbuffer[i].right += rscale[n];
401 qboolean SND_PaintChannelFrom16 (channel_t *ch, int count)
403 int leftvol, rightvol;
405 const sfxbuffer_t *sb;
408 leftvol = ch->leftvol;
409 rightvol = ch->rightvol;
411 sb = ch->sfx->fetcher->getsb (ch, ch->pos, count);
415 if (ch->sfx->format.channels == 2)
417 // LordHavoc: stereo sound support, and optimizations
418 sfx = (signed short *)sb->data + (ch->pos - sb->offset) * 2;
420 for (i=0 ; i<count ; i++)
422 paintbuffer[i].left += (*sfx++ * leftvol) >> 8;
423 paintbuffer[i].right += (*sfx++ * rightvol) >> 8;
428 sfx = (signed short *)sb->data + ch->pos - sb->offset;
430 for (i=0 ; i<count ; i++)
432 paintbuffer[i].left += (*sfx * leftvol) >> 8;
433 paintbuffer[i].right += (*sfx++ * rightvol) >> 8;