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 sprintf (filename, "%s/dpavi.wav", com_gamedir);
44 cl_avidemo_soundfile = fopen(filename, "wb");
46 fwrite(out, 1, 44, cl_avidemo_soundfile);
47 // header will be filled out when file is closed
49 fseek(cl_avidemo_soundfile, 0, SEEK_END);
50 // write the sound buffer as little endian 16bit interleaved stereo
51 for(i = 0;i < length;i++)
53 n = buf[i].left >> 2; // quiet enough to prevent clipping most of the time
54 n = bound(-32768, n, 32767);
55 out[i*4+0] = n & 0xFF;
56 out[i*4+1] = (n >> 8) & 0xFF;
57 n = buf[i].right >> 2; // quiet enough to prevent clipping most of the time
58 n = bound(-32768, n, 32767);
59 out[i*4+2] = n & 0xFF;
60 out[i*4+3] = (n >> 8) & 0xFF;
62 if (fwrite(out, 4, length, cl_avidemo_soundfile) < length)
64 Cvar_SetValueQuick(&cl_avidemo, 0);
65 Con_Printf("avi saving sound failed, out of disk space? stopping avi demo capture.\n");
68 else if (cl_avidemo_soundfile)
70 // file has not been closed yet, close it
71 fseek(cl_avidemo_soundfile, 0, SEEK_END);
72 i = ftell(cl_avidemo_soundfile);
74 //"RIFF", (int) unknown (chunk size), "WAVE",
75 //"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
76 //"data", (int) unknown (chunk size)
77 memcpy(out, "RIFF****WAVEfmt \x10\x00\x00\x00\x01\x00\x02\x00********\x04\x00\x10\x00data****", 44);
78 // the length of the whole RIFF chunk
81 out[5] = (n >> 8) & 0xFF;
82 out[6] = (n >> 16) & 0xFF;
83 out[7] = (n >> 24) & 0xFF;
87 out[25] = (n >> 8) & 0xFF;
88 out[26] = (n >> 16) & 0xFF;
89 out[27] = (n >> 24) & 0xFF;
90 // bytes per second (rate * channels * bytes per channel)
93 out[29] = (n >> 8) & 0xFF;
94 out[30] = (n >> 16) & 0xFF;
95 out[31] = (n >> 24) & 0xFF;
96 // the length of the data chunk
99 out[41] = (n >> 8) & 0xFF;
100 out[42] = (n >> 16) & 0xFF;
101 out[43] = (n >> 24) & 0xFF;
103 fseek(cl_avidemo_soundfile, 0, SEEK_SET);
104 fwrite(out, 1, 44, cl_avidemo_soundfile);
105 fclose(cl_avidemo_soundfile);
106 cl_avidemo_soundfile = NULL;
111 void S_TransferPaintBuffer(int endtime)
114 if ((pbuf = S_LockBuffer()))
120 int snd_linear_count;
122 snd_p = (int *) paintbuffer;
123 snd_vol = volume.value*256;
124 lpaintedtime = paintedtime;
125 if (shm->samplebits == 16)
129 if (shm->channels == 2)
131 // 16bit 2 channels (stereo)
132 while (lpaintedtime < endtime)
134 // handle recirculating buffer issues
135 i = lpaintedtime & ((shm->samples >> 1) - 1);
136 snd_out = (short *) pbuf + (i << 1);
137 snd_linear_count = (shm->samples >> 1) - i;
138 if (snd_linear_count > endtime - lpaintedtime)
139 snd_linear_count = endtime - lpaintedtime;
140 snd_linear_count <<= 1;
141 if (snd_swapstereo.value)
143 for (i = 0;i < snd_linear_count;i += 2)
145 val = (snd_p[i + 1] * snd_vol) >> 8;
146 snd_out[i ] = bound(-32768, val, 32767);
147 val = (snd_p[i ] * snd_vol) >> 8;
148 snd_out[i + 1] = bound(-32768, val, 32767);
153 for (i = 0;i < snd_linear_count;i += 2)
155 val = (snd_p[i ] * snd_vol) >> 8;
156 snd_out[i ] = bound(-32768, val, 32767);
157 val = (snd_p[i + 1] * snd_vol) >> 8;
158 snd_out[i + 1] = bound(-32768, val, 32767);
161 snd_p += snd_linear_count;
162 lpaintedtime += (snd_linear_count >> 1);
167 // 16bit 1 channel (mono)
168 while (lpaintedtime < endtime)
170 // handle recirculating buffer issues
171 i = lpaintedtime & (shm->samples - 1);
172 snd_out = (short *) pbuf + i;
173 snd_linear_count = shm->samples - i;
174 if (snd_linear_count > endtime - lpaintedtime)
175 snd_linear_count = endtime - lpaintedtime;
176 for (i = 0;i < snd_linear_count;i++)
178 val = ((snd_p[i * 2 + 0] + snd_p[i * 2 + 1]) * snd_vol) >> 9;
179 snd_out[i] = bound(-32768, val, 32767);
181 snd_p += snd_linear_count << 1;
182 lpaintedtime += snd_linear_count;
189 unsigned char *snd_out;
190 if (shm->channels == 2)
192 // 8bit 2 channels (stereo)
193 while (lpaintedtime < endtime)
195 // handle recirculating buffer issues
196 i = lpaintedtime & ((shm->samples >> 1) - 1);
197 snd_out = (unsigned char *) pbuf + (i << 1);
198 snd_linear_count = (shm->samples >> 1) - i;
199 if (snd_linear_count > endtime - lpaintedtime)
200 snd_linear_count = endtime - lpaintedtime;
201 snd_linear_count <<= 1;
202 if (snd_swapstereo.value)
204 for (i = 0;i < snd_linear_count;i += 2)
206 val = ((snd_p[i + 1] * snd_vol) >> 16) + 128;
207 snd_out[i ] = bound(0, val, 255);
208 val = ((snd_p[i ] * snd_vol) >> 16) + 128;
209 snd_out[i + 1] = bound(0, val, 255);
214 for (i = 0;i < snd_linear_count;i += 2)
216 val = ((snd_p[i ] * snd_vol) >> 16) + 128;
217 snd_out[i ] = bound(0, val, 255);
218 val = ((snd_p[i + 1] * snd_vol) >> 16) + 128;
219 snd_out[i + 1] = bound(0, val, 255);
222 snd_p += snd_linear_count;
223 lpaintedtime += (snd_linear_count >> 1);
228 // 8bit 1 channel (mono)
229 while (lpaintedtime < endtime)
231 // handle recirculating buffer issues
232 i = lpaintedtime & (shm->samples - 1);
233 snd_out = (unsigned char *) pbuf + i;
234 snd_linear_count = shm->samples - i;
235 if (snd_linear_count > endtime - lpaintedtime)
236 snd_linear_count = endtime - lpaintedtime;
237 for (i = 0;i < snd_linear_count;i++)
239 val = (((snd_p[i * 2] + snd_p[i * 2 + 1]) * snd_vol) >> 17) + 128;
240 snd_out[i ] = bound(0, val, 255);
242 snd_p += snd_linear_count << 1;
243 lpaintedtime += snd_linear_count;
254 ===============================================================================
258 ===============================================================================
261 void SND_PaintChannelFrom8 (channel_t *ch, sfxcache_t *sc, int endtime);
262 void SND_PaintChannelFrom16 (channel_t *ch, sfxcache_t *sc, int endtime);
264 void S_PaintChannels(int endtime)
272 while (paintedtime < endtime)
274 // if paintbuffer is smaller than DMA buffer
276 if (endtime - paintedtime > PAINTBUFFER_SIZE)
277 end = paintedtime + PAINTBUFFER_SIZE;
279 // clear the paint buffer, filling it with data from rawsamples (music/video/whatever)
280 S_RawSamples_Dequeue(&paintbuffer->left, end - paintedtime);
282 // paint in the channels.
284 for (i=0; i<total_channels ; i++, ch++)
288 if (!ch->leftvol && !ch->rightvol)
290 sc = S_LoadSound (ch->sfx, true);
300 count = ch->end - ltime;
307 SND_PaintChannelFrom8(ch, sc, count);
309 SND_PaintChannelFrom16(ch, sc, count);
314 // if at end of loop, restart
315 if (ltime >= ch->end)
317 if (sc->loopstart >= 0)
319 ch->pos = sc->loopstart;
320 ch->end = ltime + sc->length - ch->pos;
324 // channel just stopped
332 // transfer out according to DMA format
333 //S_CaptureAVISound(paintbuffer, end - paintedtime);
334 S_TransferPaintBuffer(end);
339 void SND_InitScaletable (void)
343 for (i = 0;i < 32;i++)
344 for (j = 0;j < 256;j++)
345 snd_scaletable[i][j] = ((signed char)j) * i * 8;
349 void SND_PaintChannelFrom8 (channel_t *ch, sfxcache_t *sc, int count)
351 int *lscale, *rscale;
355 if (ch->leftvol > 255)
357 if (ch->rightvol > 255)
360 lscale = snd_scaletable[ch->leftvol >> 3];
361 rscale = snd_scaletable[ch->rightvol >> 3];
364 // LordHavoc: stereo sound support, and optimizations
365 sfx = (unsigned char *)sc->data + ch->pos * 2;
366 for (i = 0;i < count;i++)
368 paintbuffer[i].left += lscale[*sfx++];
369 paintbuffer[i].right += rscale[*sfx++];
374 sfx = (unsigned char *)sc->data + ch->pos;
375 for (i = 0;i < count;i++)
378 paintbuffer[i].left += lscale[n];
379 paintbuffer[i].right += rscale[n];
386 void SND_PaintChannelFrom16 (channel_t *ch, sfxcache_t *sc, int count)
388 int leftvol, rightvol;
392 leftvol = ch->leftvol;
393 rightvol = ch->rightvol;
396 // LordHavoc: stereo sound support, and optimizations
397 sfx = (signed short *)sc->data + ch->pos * 2;
399 for (i=0 ; i<count ; i++)
401 paintbuffer[i].left += (*sfx++ * leftvol) >> 8;
402 paintbuffer[i].right += (*sfx++ * rightvol) >> 8;
407 sfx = (signed short *)sc->data + ch->pos;
409 for (i=0 ; i<count ; i++)
411 paintbuffer[i].left += (*sfx * leftvol) >> 8;
412 paintbuffer[i].right += (*sfx++ * rightvol) >> 8;