more tolerance to deviations in calculated plane distances compared to the stored...
[divverent/darkplaces.git] / snd_mix.c
1 /*
2 Copyright (C) 1996-1997 Id Software, Inc.
3
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.
8
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.
12
13 See the GNU General Public License for more details.
14
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.
18
19 */
20 // snd_mix.c -- portable code to mix sounds for snd_dma.c
21
22 #include "quakedef.h"
23
24 typedef struct
25 {
26         int left;
27         int right;
28 } portable_samplepair_t;
29
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];
34
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)
39 {
40         int n;
41         size_t i;
42         qbyte out[PAINTBUFFER_SIZE * 4];
43
44         if (cl_avidemo.value >= 0.1f)
45         {
46                 if (cl_avidemo_soundfile == NULL)
47                 {
48                         cl_avidemo_soundfile = FS_Open ("video/dpavi.wav", "wb", false);
49                         memset(out, 0, 44);
50                         FS_Write (cl_avidemo_soundfile, out, 44);
51                         // header will be filled out when file is closed
52                 }
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++)
56                 {
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;
65                 }
66                 if (FS_Write (cl_avidemo_soundfile, out, 4 * length) < 4 * length)
67                 {
68                         Cvar_SetValueQuick(&cl_avidemo, 0);
69                         Con_Print("avi saving sound failed, out of disk space?  stopping avi demo capture.\n");
70                 }
71         }
72         else if (cl_avidemo_soundfile)
73         {
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);
77
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
83                 n = i - 8;
84                 out[4] = (n) & 0xFF;
85                 out[5] = (n >> 8) & 0xFF;
86                 out[6] = (n >> 16) & 0xFF;
87                 out[7] = (n >> 24) & 0xFF;
88                 // rate
89                 n = shm->format.speed;
90                 out[24] = (n) & 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)
95                 n = shm->format.speed * 2 * 2;
96                 out[28] = (n) & 0xFF;
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
101                 n = i - 44;
102                 out[40] = (n) & 0xFF;
103                 out[41] = (n >> 8) & 0xFF;
104                 out[42] = (n >> 16) & 0xFF;
105                 out[43] = (n >> 24) & 0xFF;
106
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;
111         }
112 }
113
114 void S_TransferPaintBuffer(int endtime)
115 {
116         void *pbuf;
117         if ((pbuf = S_LockBuffer()))
118         {
119                 int i;
120                 int *snd_p;
121                 int snd_vol;
122                 int lpaintedtime;
123                 int snd_linear_count;
124                 int val;
125                 snd_p = (int *) paintbuffer;
126                 snd_vol = volume.value*256;
127                 lpaintedtime = paintedtime;
128                 if (shm->format.width == 2)
129                 {
130                         // 16bit
131                         short *snd_out;
132                         if (shm->format.channels == 2)
133                         {
134                                 // 16bit 2 channels (stereo)
135                                 while (lpaintedtime < endtime)
136                                 {
137                                         // handle recirculating buffer issues
138                                         i = lpaintedtime & ((shm->samples >> 1) - 1);
139                                         snd_out = (short *) pbuf + (i << 1);
140                                         snd_linear_count = (shm->samples >> 1) - i;
141                                         if (snd_linear_count > endtime - lpaintedtime)
142                                                 snd_linear_count = endtime - lpaintedtime;
143                                         snd_linear_count <<= 1;
144                                         if (snd_swapstereo.value)
145                                         {
146                                                 for (i = 0;i < snd_linear_count;i += 2)
147                                                 {
148                                                         val = (snd_p[i + 1] * snd_vol) >> 8;
149                                                         snd_out[i    ] = bound(-32768, val, 32767);
150                                                         val = (snd_p[i    ] * snd_vol) >> 8;
151                                                         snd_out[i + 1] = bound(-32768, val, 32767);
152                                                 }
153                                         }
154                                         else
155                                         {
156                                                 for (i = 0;i < snd_linear_count;i += 2)
157                                                 {
158                                                         val = (snd_p[i    ] * snd_vol) >> 8;
159                                                         snd_out[i    ] = bound(-32768, val, 32767);
160                                                         val = (snd_p[i + 1] * snd_vol) >> 8;
161                                                         snd_out[i + 1] = bound(-32768, val, 32767);
162                                                 }
163                                         }
164                                         snd_p += snd_linear_count;
165                                         lpaintedtime += (snd_linear_count >> 1);
166                                 }
167                         }
168                         else
169                         {
170                                 // 16bit 1 channel (mono)
171                                 while (lpaintedtime < endtime)
172                                 {
173                                         // handle recirculating buffer issues
174                                         i = lpaintedtime & (shm->samples - 1);
175                                         snd_out = (short *) pbuf + i;
176                                         snd_linear_count = shm->samples - i;
177                                         if (snd_linear_count > endtime - lpaintedtime)
178                                                 snd_linear_count = endtime - lpaintedtime;
179                                         for (i = 0;i < snd_linear_count;i++)
180                                         {
181                                                 val = ((snd_p[i * 2 + 0] + snd_p[i * 2 + 1]) * snd_vol) >> 9;
182                                                 snd_out[i] = bound(-32768, val, 32767);
183                                         }
184                                         snd_p += snd_linear_count << 1;
185                                         lpaintedtime += snd_linear_count;
186                                 }
187                         }
188                 }
189                 else
190                 {
191                         // 8bit
192                         unsigned char *snd_out;
193                         if (shm->format.channels == 2)
194                         {
195                                 // 8bit 2 channels (stereo)
196                                 while (lpaintedtime < endtime)
197                                 {
198                                         // handle recirculating buffer issues
199                                         i = lpaintedtime & ((shm->samples >> 1) - 1);
200                                         snd_out = (unsigned char *) pbuf + (i << 1);
201                                         snd_linear_count = (shm->samples >> 1) - i;
202                                         if (snd_linear_count > endtime - lpaintedtime)
203                                                 snd_linear_count = endtime - lpaintedtime;
204                                         snd_linear_count <<= 1;
205                                         if (snd_swapstereo.value)
206                                         {
207                                                 for (i = 0;i < snd_linear_count;i += 2)
208                                                 {
209                                                         val = ((snd_p[i + 1] * snd_vol) >> 16) + 128;
210                                                         snd_out[i    ] = bound(0, val, 255);
211                                                         val = ((snd_p[i    ] * snd_vol) >> 16) + 128;
212                                                         snd_out[i + 1] = bound(0, val, 255);
213                                                 }
214                                         }
215                                         else
216                                         {
217                                                 for (i = 0;i < snd_linear_count;i += 2)
218                                                 {
219                                                         val = ((snd_p[i    ] * snd_vol) >> 16) + 128;
220                                                         snd_out[i    ] = bound(0, val, 255);
221                                                         val = ((snd_p[i + 1] * snd_vol) >> 16) + 128;
222                                                         snd_out[i + 1] = bound(0, val, 255);
223                                                 }
224                                         }
225                                         snd_p += snd_linear_count;
226                                         lpaintedtime += (snd_linear_count >> 1);
227                                 }
228                         }
229                         else
230                         {
231                                 // 8bit 1 channel (mono)
232                                 while (lpaintedtime < endtime)
233                                 {
234                                         // handle recirculating buffer issues
235                                         i = lpaintedtime & (shm->samples - 1);
236                                         snd_out = (unsigned char *) pbuf + i;
237                                         snd_linear_count = shm->samples - i;
238                                         if (snd_linear_count > endtime - lpaintedtime)
239                                                 snd_linear_count = endtime - lpaintedtime;
240                                         for (i = 0;i < snd_linear_count;i++)
241                                         {
242                                                 val = (((snd_p[i * 2] + snd_p[i * 2 + 1]) * snd_vol) >> 17) + 128;
243                                                 snd_out[i    ] = bound(0, val, 255);
244                                         }
245                                         snd_p += snd_linear_count << 1;
246                                         lpaintedtime += snd_linear_count;
247                                 }
248                         }
249                 }
250
251                 S_UnlockBuffer();
252         }
253 }
254
255
256 /*
257 ===============================================================================
258
259 CHANNEL MIXING
260
261 ===============================================================================
262 */
263
264 qboolean SND_PaintChannelFrom8 (channel_t *ch, int endtime);
265 qboolean SND_PaintChannelFrom16 (channel_t *ch, int endtime);
266
267 void S_PaintChannels(int endtime)
268 {
269         unsigned int i;
270         int end;
271         channel_t *ch;
272         sfx_t *sfx;
273         int ltime, count;
274
275         while (paintedtime < endtime)
276         {
277                 // if paintbuffer is smaller than DMA buffer
278                 end = endtime;
279                 if (endtime - paintedtime > PAINTBUFFER_SIZE)
280                         end = paintedtime + PAINTBUFFER_SIZE;
281
282                 // clear the paint buffer
283                 memset (&paintbuffer, 0, (end - paintedtime) * sizeof (paintbuffer[0]));
284
285                 // paint in the channels.
286                 ch = channels;
287                 for (i=0; i<total_channels ; i++, ch++)
288                 {
289                         sfx = ch->sfx;
290                         if (!sfx)
291                                 continue;
292                         if (!ch->leftvol && !ch->rightvol)
293                                 continue;
294                         if (!S_LoadSound (sfx, true))
295                                 continue;
296
297                         // if the channel is paused
298                         if (ch->flags & CHANNELFLAG_PAUSED)
299                         {
300                                 size_t pausedtime;
301
302                                 pausedtime = end - paintedtime;
303                                 ch->lastptime += pausedtime;
304                                 ch->end += pausedtime;
305                                 continue;
306                         }
307
308                         // if the sound hasn't been painted last time, update his position
309                         if (ch->lastptime < paintedtime)
310                         {
311                                 ch->pos += paintedtime - ch->lastptime;
312
313                                 // If the sound should have ended by then
314                                 if ((unsigned int)ch->pos > sfx->total_length)
315                                 {
316                                         int loopstart;
317
318                                         if (ch->flags & CHANNELFLAG_FORCELOOP)
319                                                 loopstart = 0;
320                                         else
321                                                 loopstart = -1;
322                                         if (sfx->loopstart >= 0)
323                                                 loopstart = sfx->loopstart;
324
325                                         // If the sound is looped
326                                         if (loopstart >= 0)
327                                                 ch->pos = (ch->pos - sfx->total_length) % (sfx->total_length - loopstart) + loopstart;
328                                         else 
329                                                 ch->pos = sfx->total_length;
330                                         ch->end = paintedtime + sfx->total_length - ch->pos;
331                                 }
332                         }
333
334                         ltime = paintedtime;
335                         while (ltime < end)
336                         {
337                                 qboolean stop_paint;
338
339                                 // paint up to end
340                                 if (ch->end < end)
341                                         count = ch->end - ltime;
342                                 else
343                                         count = end - ltime;
344
345                                 if (count > 0)
346                                 {
347                                         if (sfx->format.width == 1)
348                                                 stop_paint = !SND_PaintChannelFrom8(ch, count);
349                                         else
350                                                 stop_paint = !SND_PaintChannelFrom16(ch, count);
351
352                                         if (!stop_paint)
353                                         {
354                                                 ltime += count;
355                                                 ch->lastptime = ltime;
356                                         }
357                                 }
358                                 else
359                                         stop_paint = false;
360
361                                 if (ltime >= ch->end)
362                                 {
363                                         // if at end of loop, restart
364                                         if ((sfx->loopstart >= 0 || (ch->flags & CHANNELFLAG_FORCELOOP)) && !stop_paint)
365                                         {
366                                                 ch->pos = bound(0, sfx->loopstart, (int)sfx->total_length - 1);
367                                                 ch->end = ltime + sfx->total_length - ch->pos;
368                                         }
369                                         // channel just stopped
370                                         else
371                                                 stop_paint = true;
372                                 }
373
374                                 if (stop_paint)
375                                 {
376                                         S_StopChannel (ch - channels);
377                                         break;
378                                 }
379                         }
380                 }
381
382                 // transfer out according to DMA format
383                 S_CaptureAVISound (paintbuffer, end - paintedtime);
384                 S_TransferPaintBuffer(end);
385                 paintedtime = end;
386         }
387 }
388
389 void SND_InitScaletable (void)
390 {
391         int i, j;
392
393         for (i = 0;i < 32;i++)
394                 for (j = 0;j < 256;j++)
395                         snd_scaletable[i][j] = ((signed char)j) * i * 8;
396 }
397
398
399 qboolean SND_PaintChannelFrom8 (channel_t *ch, int count)
400 {
401         int *lscale, *rscale;
402         unsigned char *sfx;
403         const sfxbuffer_t *sb;
404         int i, n;
405
406         if (ch->leftvol > 255)
407                 ch->leftvol = 255;
408         if (ch->rightvol > 255)
409                 ch->rightvol = 255;
410
411         lscale = snd_scaletable[ch->leftvol >> 3];
412         rscale = snd_scaletable[ch->rightvol >> 3];
413
414         sb = ch->sfx->fetcher->getsb (ch, ch->pos, count);
415         if (sb == NULL)
416                 return false;
417
418         if (ch->sfx->format.channels == 2)
419         {
420                 // LordHavoc: stereo sound support, and optimizations
421                 sfx = (unsigned char *)sb->data + (ch->pos - sb->offset) * 2;
422                 for (i = 0;i < count;i++)
423                 {
424                         paintbuffer[i].left += lscale[*sfx++];
425                         paintbuffer[i].right += rscale[*sfx++];
426                 }
427         }
428         else
429         {
430                 sfx = (unsigned char *)sb->data + ch->pos - sb->offset;
431                 for (i = 0;i < count;i++)
432                 {
433                         n = *sfx++;
434                         paintbuffer[i].left += lscale[n];
435                         paintbuffer[i].right += rscale[n];
436                 }
437
438         }
439         ch->pos += count;
440         return true;
441 }
442
443 qboolean SND_PaintChannelFrom16 (channel_t *ch, int count)
444 {
445         int leftvol, rightvol;
446         signed short *sfx;
447         const sfxbuffer_t *sb;
448         int i;
449
450         leftvol = ch->leftvol;
451         rightvol = ch->rightvol;
452
453         sb = ch->sfx->fetcher->getsb (ch, ch->pos, count);
454         if (sb == NULL)
455                 return false;
456
457         if (ch->sfx->format.channels == 2)
458         {
459                 // LordHavoc: stereo sound support, and optimizations
460                 sfx = (signed short *)sb->data + (ch->pos - sb->offset) * 2;
461
462                 for (i=0 ; i<count ; i++)
463                 {
464                         paintbuffer[i].left += (*sfx++ * leftvol) >> 8;
465                         paintbuffer[i].right += (*sfx++ * rightvol) >> 8;
466                 }
467         }
468         else
469         {
470                 sfx = (signed short *)sb->data + ch->pos - sb->offset;
471
472                 for (i=0 ; i<count ; i++)
473                 {
474                         paintbuffer[i].left += (*sfx * leftvol) >> 8;
475                         paintbuffer[i].right += (*sfx++ * rightvol) >> 8;
476                 }
477         }
478
479         ch->pos += count;
480         return true;
481 }
482