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