Fixed the sounds not being advanced in time while not being listened. This is a long...
[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 // LordHavoc: was 512, expanded to 2048
25 #define PAINTBUFFER_SIZE 2048
26 portable_samplepair_t paintbuffer[PAINTBUFFER_SIZE];
27 int snd_scaletable[32][256];
28
29 /*
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)
34 {
35         int i, n;
36         qbyte out[PAINTBUFFER_SIZE * 4];
37         char filename[MAX_OSPATH];
38
39         if (cl_avidemo.value >= 0.1f)
40         {
41                 if (cl_avidemo_soundfile == NULL)
42                 {
43                         cl_avidemo_soundfile = FS_Open ("dpavi.wav", "wb", false);
44                         memset(out, 0, 44);
45                         fwrite(out, 1, 44, cl_avidemo_soundfile);
46                         // header will be filled out when file is closed
47                 }
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++)
51                 {
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;
60                 }
61                 if (fwrite(out, 4, length, cl_avidemo_soundfile) < length)
62                 {
63                         Cvar_SetValueQuick(&cl_avidemo, 0);
64                         Con_Print("avi saving sound failed, out of disk space?  stopping avi demo capture.\n");
65                 }
66         }
67         else if (cl_avidemo_soundfile)
68         {
69                 // file has not been closed yet, close it
70                 fseek(cl_avidemo_soundfile, 0, SEEK_END);
71                 i = ftell(cl_avidemo_soundfile);
72
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
78                 n = i - 8;
79                 out[4] = (n) & 0xFF;
80                 out[5] = (n >> 8) & 0xFF;
81                 out[6] = (n >> 16) & 0xFF;
82                 out[7] = (n >> 24) & 0xFF;
83                 // rate
84                 n = shm->speed;
85                 out[24] = (n) & 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)
90                 n = shm->speed * 4;
91                 out[28] = (n) & 0xFF;
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
96                 n = i - 44;
97                 out[40] = (n) & 0xFF;
98                 out[41] = (n >> 8) & 0xFF;
99                 out[42] = (n >> 16) & 0xFF;
100                 out[43] = (n >> 24) & 0xFF;
101
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;
106         }
107 }
108 */
109
110 void S_TransferPaintBuffer(int endtime)
111 {
112         void *pbuf;
113         if ((pbuf = S_LockBuffer()))
114         {
115                 int i;
116                 int *snd_p;
117                 int snd_vol;
118                 int lpaintedtime;
119                 int snd_linear_count;
120                 int val;
121                 snd_p = (int *) paintbuffer;
122                 snd_vol = volume.value*256;
123                 lpaintedtime = paintedtime;
124                 if (shm->format.width == 2)
125                 {
126                         // 16bit
127                         short *snd_out;
128                         if (shm->format.channels == 2)
129                         {
130                                 // 16bit 2 channels (stereo)
131                                 while (lpaintedtime < endtime)
132                                 {
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)
141                                         {
142                                                 for (i = 0;i < snd_linear_count;i += 2)
143                                                 {
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);
148                                                 }
149                                         }
150                                         else
151                                         {
152                                                 for (i = 0;i < snd_linear_count;i += 2)
153                                                 {
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);
158                                                 }
159                                         }
160                                         snd_p += snd_linear_count;
161                                         lpaintedtime += (snd_linear_count >> 1);
162                                 }
163                         }
164                         else
165                         {
166                                 // 16bit 1 channel (mono)
167                                 while (lpaintedtime < endtime)
168                                 {
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++)
176                                         {
177                                                 val = ((snd_p[i * 2 + 0] + snd_p[i * 2 + 1]) * snd_vol) >> 9;
178                                                 snd_out[i] = bound(-32768, val, 32767);
179                                         }
180                                         snd_p += snd_linear_count << 1;
181                                         lpaintedtime += snd_linear_count;
182                                 }
183                         }
184                 }
185                 else
186                 {
187                         // 8bit
188                         unsigned char *snd_out;
189                         if (shm->format.channels == 2)
190                         {
191                                 // 8bit 2 channels (stereo)
192                                 while (lpaintedtime < endtime)
193                                 {
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)
202                                         {
203                                                 for (i = 0;i < snd_linear_count;i += 2)
204                                                 {
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);
209                                                 }
210                                         }
211                                         else
212                                         {
213                                                 for (i = 0;i < snd_linear_count;i += 2)
214                                                 {
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);
219                                                 }
220                                         }
221                                         snd_p += snd_linear_count;
222                                         lpaintedtime += (snd_linear_count >> 1);
223                                 }
224                         }
225                         else
226                         {
227                                 // 8bit 1 channel (mono)
228                                 while (lpaintedtime < endtime)
229                                 {
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++)
237                                         {
238                                                 val = (((snd_p[i * 2] + snd_p[i * 2 + 1]) * snd_vol) >> 17) + 128;
239                                                 snd_out[i    ] = bound(0, val, 255);
240                                         }
241                                         snd_p += snd_linear_count << 1;
242                                         lpaintedtime += snd_linear_count;
243                                 }
244                         }
245                 }
246
247                 S_UnlockBuffer();
248         }
249 }
250
251
252 /*
253 ===============================================================================
254
255 CHANNEL MIXING
256
257 ===============================================================================
258 */
259
260 qboolean SND_PaintChannelFrom8 (channel_t *ch, int endtime);
261 qboolean SND_PaintChannelFrom16 (channel_t *ch, int endtime);
262
263 void S_PaintChannels(int endtime)
264 {
265         unsigned int i;
266         int end;
267         channel_t *ch;
268         sfx_t *sfx;
269         int ltime, count;
270
271         while (paintedtime < endtime)
272         {
273                 // if paintbuffer is smaller than DMA buffer
274                 end = endtime;
275                 if (endtime - paintedtime > PAINTBUFFER_SIZE)
276                         end = paintedtime + PAINTBUFFER_SIZE;
277
278                 // clear the paint buffer, filling it with data from rawsamples (music/video/whatever)
279                 S_RawSamples_Dequeue(&paintbuffer->left, end - paintedtime);
280
281                 // paint in the channels.
282                 ch = channels;
283                 for (i=0; i<total_channels ; i++, ch++)
284                 {
285                         sfx = ch->sfx;
286                         if (!sfx)
287                                 continue;
288                         if (!ch->leftvol && !ch->rightvol)
289                                 continue;
290                         if (!S_LoadSound (sfx, true))
291                                 continue;
292
293                         // if the sound hasn't been painted last time, update his position
294                         if (ch->lastptime < paintedtime)
295                         {
296                                 ch->pos += paintedtime - ch->lastptime;
297
298                                 // If the sound should have ended by then
299                                 if ((unsigned int)ch->pos > sfx->total_length)
300                                 {
301                                         int loopstart;
302
303                                         if (ch->forceloop)
304                                                 loopstart = 0;
305                                         else
306                                                 loopstart = -1;
307                                         if (sfx->loopstart >= 0)
308                                                 loopstart = sfx->loopstart;
309
310                                         // If the sound is looped
311                                         if (loopstart >= 0)
312                                                 ch->pos = (ch->pos - sfx->total_length) % (sfx->total_length - loopstart) + loopstart;
313                                         else 
314                                                 ch->pos = sfx->total_length;
315                                         ch->end = paintedtime + sfx->total_length - ch->pos;
316                                 }
317                         }
318
319                         ltime = paintedtime;
320                         while (ltime < end)
321                         {
322                                 qboolean stop_paint;
323
324                                 // paint up to end
325                                 if (ch->end < end)
326                                         count = ch->end - ltime;
327                                 else
328                                         count = end - ltime;
329
330                                 if (count > 0)
331                                 {
332                                         if (sfx->format.width == 1)
333                                                 stop_paint = !SND_PaintChannelFrom8(ch, count);
334                                         else
335                                                 stop_paint = !SND_PaintChannelFrom16(ch, count);
336
337                                         if (!stop_paint)
338                                         {
339                                                 ltime += count;
340                                                 ch->lastptime = ltime;
341                                         }
342                                 }
343                                 else
344                                         stop_paint = false;
345
346                                 if (ltime >= ch->end)
347                                 {
348                                         // if at end of loop, restart
349                                         if ((sfx->loopstart >= 0 || ch->forceloop) && !stop_paint)
350                                         {
351                                                 ch->pos = bound(0, sfx->loopstart, (int)sfx->total_length - 1);
352                                                 ch->end = ltime + sfx->total_length - ch->pos;
353                                         }
354                                         // channel just stopped
355                                         else
356                                                 stop_paint = true;
357                                 }
358
359                                 if (stop_paint)
360                                 {
361                                         if (ch->sfx->fetcher->end != NULL)
362                                                 ch->sfx->fetcher->end (ch);
363                                         ch->sfx = NULL;
364                                         break;
365                                 }
366                         }
367                 }
368
369                 // transfer out according to DMA format
370                 //S_CaptureAVISound(paintbuffer, end - paintedtime);
371                 S_TransferPaintBuffer(end);
372                 paintedtime = end;
373         }
374 }
375
376 void SND_InitScaletable (void)
377 {
378         int i, j;
379
380         for (i = 0;i < 32;i++)
381                 for (j = 0;j < 256;j++)
382                         snd_scaletable[i][j] = ((signed char)j) * i * 8;
383 }
384
385
386 qboolean SND_PaintChannelFrom8 (channel_t *ch, int count)
387 {
388         int *lscale, *rscale;
389         unsigned char *sfx;
390         const sfxbuffer_t *sb;
391         int i, n;
392
393         if (ch->leftvol > 255)
394                 ch->leftvol = 255;
395         if (ch->rightvol > 255)
396                 ch->rightvol = 255;
397
398         lscale = snd_scaletable[ch->leftvol >> 3];
399         rscale = snd_scaletable[ch->rightvol >> 3];
400
401         sb = ch->sfx->fetcher->getsb (ch, ch->pos, count);
402         if (sb == NULL)
403                 return false;
404
405         if (ch->sfx->format.channels == 2)
406         {
407                 // LordHavoc: stereo sound support, and optimizations
408                 sfx = (unsigned char *)sb->data + (ch->pos - sb->offset) * 2;
409                 for (i = 0;i < count;i++)
410                 {
411                         paintbuffer[i].left += lscale[*sfx++];
412                         paintbuffer[i].right += rscale[*sfx++];
413                 }
414         }
415         else
416         {
417                 sfx = (unsigned char *)sb->data + ch->pos - sb->offset;
418                 for (i = 0;i < count;i++)
419                 {
420                         n = *sfx++;
421                         paintbuffer[i].left += lscale[n];
422                         paintbuffer[i].right += rscale[n];
423                 }
424
425         }
426         ch->pos += count;
427         return true;
428 }
429
430 qboolean SND_PaintChannelFrom16 (channel_t *ch, int count)
431 {
432         int leftvol, rightvol;
433         signed short *sfx;
434         const sfxbuffer_t *sb;
435         int i;
436
437         leftvol = ch->leftvol;
438         rightvol = ch->rightvol;
439
440         sb = ch->sfx->fetcher->getsb (ch, ch->pos, count);
441         if (sb == NULL)
442                 return false;
443
444         if (ch->sfx->format.channels == 2)
445         {
446                 // LordHavoc: stereo sound support, and optimizations
447                 sfx = (signed short *)sb->data + (ch->pos - sb->offset) * 2;
448
449                 for (i=0 ; i<count ; i++)
450                 {
451                         paintbuffer[i].left += (*sfx++ * leftvol) >> 8;
452                         paintbuffer[i].right += (*sfx++ * rightvol) >> 8;
453                 }
454         }
455         else
456         {
457                 sfx = (signed short *)sb->data + ch->pos - sb->offset;
458
459                 for (i=0 ; i<count ; i++)
460                 {
461                         paintbuffer[i].left += (*sfx * leftvol) >> 8;
462                         paintbuffer[i].right += (*sfx++ * rightvol) >> 8;
463                 }
464         }
465
466         ch->pos += count;
467         return true;
468 }
469