strncpy -> {strlcpy,memcpy}. Replaced the "forceloop" boolean in "channel_t" by a...
[xonotic/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 sound hasn't been painted last time, update his position
300                         if (ch->lastptime < paintedtime)
301                         {
302                                 ch->pos += paintedtime - ch->lastptime;
303
304                                 // If the sound should have ended by then
305                                 if ((unsigned int)ch->pos > sfx->total_length)
306                                 {
307                                         int loopstart;
308
309                                         if (ch->flags & CHANNELFLAG_FORCELOOP)
310                                                 loopstart = 0;
311                                         else
312                                                 loopstart = -1;
313                                         if (sfx->loopstart >= 0)
314                                                 loopstart = sfx->loopstart;
315
316                                         // If the sound is looped
317                                         if (loopstart >= 0)
318                                                 ch->pos = (ch->pos - sfx->total_length) % (sfx->total_length - loopstart) + loopstart;
319                                         else 
320                                                 ch->pos = sfx->total_length;
321                                         ch->end = paintedtime + sfx->total_length - ch->pos;
322                                 }
323                         }
324
325                         ltime = paintedtime;
326                         while (ltime < end)
327                         {
328                                 qboolean stop_paint;
329
330                                 // paint up to end
331                                 if (ch->end < end)
332                                         count = ch->end - ltime;
333                                 else
334                                         count = end - ltime;
335
336                                 if (count > 0)
337                                 {
338                                         if (sfx->format.width == 1)
339                                                 stop_paint = !SND_PaintChannelFrom8(ch, count);
340                                         else
341                                                 stop_paint = !SND_PaintChannelFrom16(ch, count);
342
343                                         if (!stop_paint)
344                                         {
345                                                 ltime += count;
346                                                 ch->lastptime = ltime;
347                                         }
348                                 }
349                                 else
350                                         stop_paint = false;
351
352                                 if (ltime >= ch->end)
353                                 {
354                                         // if at end of loop, restart
355                                         if ((sfx->loopstart >= 0 || (ch->flags & CHANNELFLAG_FORCELOOP)) && !stop_paint)
356                                         {
357                                                 ch->pos = bound(0, sfx->loopstart, (int)sfx->total_length - 1);
358                                                 ch->end = ltime + sfx->total_length - ch->pos;
359                                         }
360                                         // channel just stopped
361                                         else
362                                                 stop_paint = true;
363                                 }
364
365                                 if (stop_paint)
366                                 {
367                                         if (ch->sfx->fetcher->end != NULL)
368                                                 ch->sfx->fetcher->end (ch);
369                                         ch->sfx = NULL;
370                                         break;
371                                 }
372                         }
373                 }
374
375                 // transfer out according to DMA format
376                 //S_CaptureAVISound(paintbuffer, end - paintedtime);
377                 S_TransferPaintBuffer(end);
378                 paintedtime = end;
379         }
380 }
381
382 void SND_InitScaletable (void)
383 {
384         int i, j;
385
386         for (i = 0;i < 32;i++)
387                 for (j = 0;j < 256;j++)
388                         snd_scaletable[i][j] = ((signed char)j) * i * 8;
389 }
390
391
392 qboolean SND_PaintChannelFrom8 (channel_t *ch, int count)
393 {
394         int *lscale, *rscale;
395         unsigned char *sfx;
396         const sfxbuffer_t *sb;
397         int i, n;
398
399         if (ch->leftvol > 255)
400                 ch->leftvol = 255;
401         if (ch->rightvol > 255)
402                 ch->rightvol = 255;
403
404         lscale = snd_scaletable[ch->leftvol >> 3];
405         rscale = snd_scaletable[ch->rightvol >> 3];
406
407         sb = ch->sfx->fetcher->getsb (ch, ch->pos, count);
408         if (sb == NULL)
409                 return false;
410
411         if (ch->sfx->format.channels == 2)
412         {
413                 // LordHavoc: stereo sound support, and optimizations
414                 sfx = (unsigned char *)sb->data + (ch->pos - sb->offset) * 2;
415                 for (i = 0;i < count;i++)
416                 {
417                         paintbuffer[i].left += lscale[*sfx++];
418                         paintbuffer[i].right += rscale[*sfx++];
419                 }
420         }
421         else
422         {
423                 sfx = (unsigned char *)sb->data + ch->pos - sb->offset;
424                 for (i = 0;i < count;i++)
425                 {
426                         n = *sfx++;
427                         paintbuffer[i].left += lscale[n];
428                         paintbuffer[i].right += rscale[n];
429                 }
430
431         }
432         ch->pos += count;
433         return true;
434 }
435
436 qboolean SND_PaintChannelFrom16 (channel_t *ch, int count)
437 {
438         int leftvol, rightvol;
439         signed short *sfx;
440         const sfxbuffer_t *sb;
441         int i;
442
443         leftvol = ch->leftvol;
444         rightvol = ch->rightvol;
445
446         sb = ch->sfx->fetcher->getsb (ch, ch->pos, count);
447         if (sb == NULL)
448                 return false;
449
450         if (ch->sfx->format.channels == 2)
451         {
452                 // LordHavoc: stereo sound support, and optimizations
453                 sfx = (signed short *)sb->data + (ch->pos - sb->offset) * 2;
454
455                 for (i=0 ; i<count ; i++)
456                 {
457                         paintbuffer[i].left += (*sfx++ * leftvol) >> 8;
458                         paintbuffer[i].right += (*sfx++ * rightvol) >> 8;
459                 }
460         }
461         else
462         {
463                 sfx = (signed short *)sb->data + ch->pos - sb->offset;
464
465                 for (i=0 ; i<count ; i++)
466                 {
467                         paintbuffer[i].left += (*sfx * leftvol) >> 8;
468                         paintbuffer[i].right += (*sfx++ * rightvol) >> 8;
469                 }
470         }
471
472         ch->pos += count;
473         return true;
474 }
475