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 // LordHavoc: was 512, expanded to 2048
29 #define PAINTBUFFER_SIZE 2048
30 portable_samplepair_t paintbuffer[PAINTBUFFER_SIZE];
31 int snd_scaletable[32][256];
34 // LordHavoc: disabled this because it desyncs with the video too easily
35 extern cvar_t cl_avidemo;
36 static FILE *cl_avidemo_soundfile = NULL;
37 void S_CaptureAVISound(portable_samplepair_t *buf, int length)
40 qbyte out[PAINTBUFFER_SIZE * 4];
41 char filename[MAX_OSPATH];
43 if (cl_avidemo.value >= 0.1f)
45 if (cl_avidemo_soundfile == NULL)
47 sprintf (filename, "%s/dpavi.wav", com_gamedir);
48 cl_avidemo_soundfile = fopen(filename, "wb");
50 fwrite(out, 1, 44, cl_avidemo_soundfile);
51 // header will be filled out when file is closed
53 fseek(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 (fwrite(out, 4, length, cl_avidemo_soundfile) < length)
68 Cvar_SetValueQuick(&cl_avidemo, 0);
69 Con_Printf("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 fseek(cl_avidemo_soundfile, 0, SEEK_END);
76 i = ftell(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\x00data****", 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;
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)
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 fseek(cl_avidemo_soundfile, 0, SEEK_SET);
108 fwrite(out, 1, 44, cl_avidemo_soundfile);
109 fclose(cl_avidemo_soundfile);
110 cl_avidemo_soundfile = NULL;
115 void S_TransferPaintBuffer(int endtime)
118 if ((pbuf = S_LockBuffer()))
124 int snd_linear_count;
126 snd_p = (int *) paintbuffer;
127 snd_vol = volume.value*256;
128 lpaintedtime = paintedtime;
129 if (shm->samplebits == 16)
133 if (shm->channels == 2)
135 // 16bit 2 channels (stereo)
136 while (lpaintedtime < endtime)
138 // handle recirculating buffer issues
139 i = lpaintedtime & ((shm->samples >> 1) - 1);
140 snd_out = (short *) pbuf + (i << 1);
141 snd_linear_count = (shm->samples >> 1) - i;
142 if (snd_linear_count > endtime - lpaintedtime)
143 snd_linear_count = endtime - lpaintedtime;
144 snd_linear_count <<= 1;
145 if (snd_swapstereo.value)
147 for (i = 0;i < snd_linear_count;i += 2)
149 val = (snd_p[i + 1] * snd_vol) >> 8;
150 snd_out[i ] = bound(-32768, val, 32767);
151 val = (snd_p[i ] * snd_vol) >> 8;
152 snd_out[i + 1] = bound(-32768, val, 32767);
157 for (i = 0;i < snd_linear_count;i += 2)
159 val = (snd_p[i ] * snd_vol) >> 8;
160 snd_out[i ] = bound(-32768, val, 32767);
161 val = (snd_p[i + 1] * snd_vol) >> 8;
162 snd_out[i + 1] = bound(-32768, val, 32767);
165 snd_p += snd_linear_count;
166 lpaintedtime += (snd_linear_count >> 1);
171 // 16bit 1 channel (mono)
172 while (lpaintedtime < endtime)
174 // handle recirculating buffer issues
175 i = lpaintedtime & (shm->samples - 1);
176 snd_out = (short *) pbuf + i;
177 snd_linear_count = shm->samples - i;
178 if (snd_linear_count > endtime - lpaintedtime)
179 snd_linear_count = endtime - lpaintedtime;
180 for (i = 0;i < snd_linear_count;i++)
182 val = ((snd_p[i * 2 + 0] + snd_p[i * 2 + 1]) * snd_vol) >> 9;
183 snd_out[i] = bound(-32768, val, 32767);
185 snd_p += snd_linear_count << 1;
186 lpaintedtime += snd_linear_count;
193 unsigned char *snd_out;
194 if (shm->channels == 2)
196 // 8bit 2 channels (stereo)
197 while (lpaintedtime < endtime)
199 // handle recirculating buffer issues
200 i = lpaintedtime & ((shm->samples >> 1) - 1);
201 snd_out = (unsigned char *) pbuf + (i << 1);
202 snd_linear_count = (shm->samples >> 1) - i;
203 if (snd_linear_count > endtime - lpaintedtime)
204 snd_linear_count = endtime - lpaintedtime;
205 snd_linear_count <<= 1;
206 if (snd_swapstereo.value)
208 for (i = 0;i < snd_linear_count;i += 2)
210 val = ((snd_p[i + 1] * snd_vol) >> 16) + 128;
211 snd_out[i ] = bound(0, val, 255);
212 val = ((snd_p[i ] * snd_vol) >> 16) + 128;
213 snd_out[i + 1] = bound(0, val, 255);
218 for (i = 0;i < snd_linear_count;i += 2)
220 val = ((snd_p[i ] * snd_vol) >> 16) + 128;
221 snd_out[i ] = bound(0, val, 255);
222 val = ((snd_p[i + 1] * snd_vol) >> 16) + 128;
223 snd_out[i + 1] = bound(0, val, 255);
226 snd_p += snd_linear_count;
227 lpaintedtime += (snd_linear_count >> 1);
232 // 8bit 1 channel (mono)
233 while (lpaintedtime < endtime)
235 // handle recirculating buffer issues
236 i = lpaintedtime & (shm->samples - 1);
237 snd_out = (unsigned char *) pbuf + i;
238 snd_linear_count = shm->samples - i;
239 if (snd_linear_count > endtime - lpaintedtime)
240 snd_linear_count = endtime - lpaintedtime;
241 for (i = 0;i < snd_linear_count;i++)
243 val = (((snd_p[i * 2] + snd_p[i * 2 + 1]) * snd_vol) >> 17) + 128;
244 snd_out[i ] = bound(0, val, 255);
246 snd_p += snd_linear_count << 1;
247 lpaintedtime += snd_linear_count;
258 ===============================================================================
262 ===============================================================================
265 void SND_PaintChannelFrom8 (channel_t *ch, sfxcache_t *sc, int endtime);
266 void SND_PaintChannelFrom16 (channel_t *ch, sfxcache_t *sc, int endtime);
268 void S_PaintChannels(int endtime)
276 while (paintedtime < endtime)
278 // if paintbuffer is smaller than DMA buffer
280 if (endtime - paintedtime > PAINTBUFFER_SIZE)
281 end = paintedtime + PAINTBUFFER_SIZE;
283 // clear the paint buffer, filling it with data from rawsamples (music/video/whatever)
284 S_RawSamples_Dequeue(&paintbuffer->left, end - paintedtime);
286 // paint in the channels.
288 for (i=0; i<total_channels ; i++, ch++)
292 if (!ch->leftvol && !ch->rightvol)
294 sc = S_LoadSound (ch->sfx, true);
304 count = ch->end - ltime;
311 SND_PaintChannelFrom8(ch, sc, count);
313 SND_PaintChannelFrom16(ch, sc, count);
318 // if at end of loop, restart
319 if (ltime >= ch->end)
321 if (sc->loopstart >= 0)
323 ch->pos = sc->loopstart;
324 ch->end = ltime + sc->length - ch->pos;
328 // channel just stopped
336 // transfer out according to DMA format
337 //S_CaptureAVISound(paintbuffer, end - paintedtime);
338 S_TransferPaintBuffer(end);
343 void SND_InitScaletable (void)
347 for (i = 0;i < 32;i++)
348 for (j = 0;j < 256;j++)
349 snd_scaletable[i][j] = ((signed char)j) * i * 8;
353 void SND_PaintChannelFrom8 (channel_t *ch, sfxcache_t *sc, int count)
355 int *lscale, *rscale;
359 if (ch->leftvol > 255)
361 if (ch->rightvol > 255)
364 lscale = snd_scaletable[ch->leftvol >> 3];
365 rscale = snd_scaletable[ch->rightvol >> 3];
368 // LordHavoc: stereo sound support, and optimizations
369 sfx = (unsigned char *)sc->data + ch->pos * 2;
370 for (i = 0;i < count;i++)
372 paintbuffer[i].left += lscale[*sfx++];
373 paintbuffer[i].right += rscale[*sfx++];
378 sfx = (unsigned char *)sc->data + ch->pos;
379 for (i = 0;i < count;i++)
382 paintbuffer[i].left += lscale[n];
383 paintbuffer[i].right += rscale[n];
390 void SND_PaintChannelFrom16 (channel_t *ch, sfxcache_t *sc, int count)
392 int leftvol, rightvol;
396 leftvol = ch->leftvol;
397 rightvol = ch->rightvol;
400 // LordHavoc: stereo sound support, and optimizations
401 sfx = (signed short *)sc->data + ch->pos * 2;
403 for (i=0 ; i<count ; i++)
405 paintbuffer[i].left += (*sfx++ * leftvol) >> 8;
406 paintbuffer[i].right += (*sfx++ * rightvol) >> 8;
411 sfx = (signed short *)sc->data + ch->pos;
413 for (i=0 ; i<count ; i++)
415 paintbuffer[i].left += (*sfx * leftvol) >> 8;
416 paintbuffer[i].right += (*sfx++ * rightvol) >> 8;