Fixed a couple of comments, and modified slightly the developer printing in ResampleSfx
[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
21
22 #include "quakedef.h"
23 #include "snd_main.h"
24
25 typedef struct
26 {
27         int left;
28         int right;
29 } portable_samplepair_t;
30
31 // LordHavoc: was 512, expanded to 2048
32 #define PAINTBUFFER_SIZE 2048
33 portable_samplepair_t paintbuffer[PAINTBUFFER_SIZE];
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 // TODO: rewrite this function
115 void S_TransferPaintBuffer(int endtime)
116 {
117         void *pbuf;
118         if ((pbuf = S_LockBuffer()))
119         {
120                 int i;
121                 int *snd_p;
122                 int lpaintedtime;
123                 int snd_linear_count;
124                 int val;
125                 snd_p = (int *) paintbuffer;
126                 lpaintedtime = paintedtime;
127                 if (shm->format.width == 2)
128                 {
129                         // 16bit
130                         short *snd_out;
131                         if (shm->format.channels == 2)
132                         {
133                                 // 16bit 2 channels (stereo)
134                                 while (lpaintedtime < endtime)
135                                 {
136                                         // handle recirculating buffer issues
137                                         i = lpaintedtime & ((shm->samples >> 1) - 1);
138                                         snd_out = (short *) pbuf + (i << 1);
139                                         snd_linear_count = (shm->samples >> 1) - i;
140                                         if (snd_linear_count > endtime - lpaintedtime)
141                                                 snd_linear_count = endtime - lpaintedtime;
142                                         snd_linear_count <<= 1;
143                                         if (snd_swapstereo.value)
144                                         {
145                                                 for (i = 0;i < snd_linear_count;i += 2)
146                                                 {
147                                                         snd_out[i    ] = bound(-32768, snd_p[i + 1], 32767);
148                                                         snd_out[i + 1] = bound(-32768, snd_p[i    ], 32767);
149                                                 }
150                                         }
151                                         else
152                                         {
153                                                 for (i = 0;i < snd_linear_count;i += 2)
154                                                 {
155                                                         snd_out[i    ] = bound(-32768, snd_p[i    ], 32767);
156                                                         snd_out[i + 1] = bound(-32768, snd_p[i + 1], 32767);
157                                                 }
158                                         }
159                                         snd_p += snd_linear_count;
160                                         lpaintedtime += (snd_linear_count >> 1);
161                                 }
162                         }
163                         else
164                         {
165                                 // 16bit 1 channel (mono)
166                                 while (lpaintedtime < endtime)
167                                 {
168                                         // handle recirculating buffer issues
169                                         i = lpaintedtime & (shm->samples - 1);
170                                         snd_out = (short *) pbuf + i;
171                                         snd_linear_count = shm->samples - i;
172                                         if (snd_linear_count > endtime - lpaintedtime)
173                                                 snd_linear_count = endtime - lpaintedtime;
174                                         for (i = 0;i < snd_linear_count;i++)
175                                         {
176                                                 val = (snd_p[i * 2 + 0] + snd_p[i * 2 + 1]) >> 1;
177                                                 snd_out[i] = bound(-32768, val, 32767);
178                                         }
179                                         snd_p += snd_linear_count << 1;
180                                         lpaintedtime += snd_linear_count;
181                                 }
182                         }
183                 }
184                 else
185                 {
186                         // 8bit
187                         unsigned char *snd_out;
188                         if (shm->format.channels == 2)
189                         {
190                                 // 8bit 2 channels (stereo)
191                                 while (lpaintedtime < endtime)
192                                 {
193                                         // handle recirculating buffer issues
194                                         i = lpaintedtime & ((shm->samples >> 1) - 1);
195                                         snd_out = (unsigned char *) pbuf + (i << 1);
196                                         snd_linear_count = (shm->samples >> 1) - i;
197                                         if (snd_linear_count > endtime - lpaintedtime)
198                                                 snd_linear_count = endtime - lpaintedtime;
199                                         snd_linear_count <<= 1;
200                                         if (snd_swapstereo.value)
201                                         {
202                                                 for (i = 0;i < snd_linear_count;i += 2)
203                                                 {
204                                                         val = (snd_p[i + 1] >> 8) + 128;
205                                                         snd_out[i    ] = bound(0, val, 255);
206                                                         val = (snd_p[i    ] >> 8) + 128;
207                                                         snd_out[i + 1] = bound(0, val, 255);
208                                                 }
209                                         }
210                                         else
211                                         {
212                                                 for (i = 0;i < snd_linear_count;i += 2)
213                                                 {
214                                                         val = (snd_p[i    ] >> 8) + 128;
215                                                         snd_out[i    ] = bound(0, val, 255);
216                                                         val = (snd_p[i + 1] >> 8) + 128;
217                                                         snd_out[i + 1] = bound(0, val, 255);
218                                                 }
219                                         }
220                                         snd_p += snd_linear_count;
221                                         lpaintedtime += (snd_linear_count >> 1);
222                                 }
223                         }
224                         else
225                         {
226                                 // 8bit 1 channel (mono)
227                                 while (lpaintedtime < endtime)
228                                 {
229                                         // handle recirculating buffer issues
230                                         i = lpaintedtime & (shm->samples - 1);
231                                         snd_out = (unsigned char *) pbuf + i;
232                                         snd_linear_count = shm->samples - i;
233                                         if (snd_linear_count > endtime - lpaintedtime)
234                                                 snd_linear_count = endtime - lpaintedtime;
235                                         for (i = 0;i < snd_linear_count;i++)
236                                         {
237                                                 val = ((snd_p[i * 2] + snd_p[i * 2 + 1]) >> 9) + 128;
238                                                 snd_out[i    ] = bound(0, val, 255);
239                                         }
240                                         snd_p += snd_linear_count << 1;
241                                         lpaintedtime += snd_linear_count;
242                                 }
243                         }
244                 }
245
246                 S_UnlockBuffer();
247         }
248 }
249
250
251 /*
252 ===============================================================================
253
254 CHANNEL MIXING
255
256 ===============================================================================
257 */
258
259 qboolean SND_PaintChannelFrom8 (channel_t *ch, int endtime);
260 qboolean SND_PaintChannelFrom16 (channel_t *ch, int endtime);
261
262 void S_PaintChannels(int endtime)
263 {
264         unsigned int i;
265         int end;
266         channel_t *ch;
267         sfx_t *sfx;
268         int ltime, count;
269
270         while (paintedtime < endtime)
271         {
272                 // if paintbuffer is smaller than DMA buffer
273                 end = endtime;
274                 if (endtime - paintedtime > PAINTBUFFER_SIZE)
275                         end = paintedtime + PAINTBUFFER_SIZE;
276
277                 // clear the paint buffer
278                 memset (&paintbuffer, 0, (end - paintedtime) * sizeof (paintbuffer[0]));
279
280                 // paint in the channels.
281                 ch = channels;
282                 for (i=0; i<total_channels ; i++, ch++)
283                 {
284                         sfx = ch->sfx;
285                         if (!sfx)
286                                 continue;
287                         if (!ch->leftvol && !ch->rightvol)
288                                 continue;
289                         if (!S_LoadSound (sfx, true))
290                                 continue;
291
292                         // if the channel is paused
293                         if (ch->flags & CHANNELFLAG_PAUSED)
294                         {
295                                 size_t pausedtime;
296
297                                 pausedtime = end - paintedtime;
298                                 ch->lastptime += pausedtime;
299                                 ch->end += pausedtime;
300                                 continue;
301                         }
302
303                         // if the sound hasn't been painted last time, update his position
304                         if (ch->lastptime < paintedtime)
305                         {
306                                 ch->pos += paintedtime - ch->lastptime;
307
308                                 // If the sound should have ended by then
309                                 if ((unsigned int)ch->pos > sfx->total_length)
310                                 {
311                                         int loopstart;
312
313                                         if (ch->flags & CHANNELFLAG_FORCELOOP)
314                                                 loopstart = 0;
315                                         else
316                                                 loopstart = -1;
317                                         if (sfx->loopstart >= 0)
318                                                 loopstart = sfx->loopstart;
319
320                                         // If the sound is looped
321                                         if (loopstart >= 0)
322                                                 ch->pos = (ch->pos - sfx->total_length) % (sfx->total_length - loopstart) + loopstart;
323                                         else
324                                                 ch->pos = sfx->total_length;
325                                         ch->end = paintedtime + sfx->total_length - ch->pos;
326                                 }
327                         }
328
329                         ltime = paintedtime;
330                         while (ltime < end)
331                         {
332                                 qboolean stop_paint;
333
334                                 // paint up to end
335                                 if (ch->end < end)
336                                         count = ch->end - ltime;
337                                 else
338                                         count = end - ltime;
339
340                                 if (count > 0)
341                                 {
342                                         if (ch->leftvol > 255)
343                                                 ch->leftvol = 255;
344                                         if (ch->rightvol > 255)
345                                                 ch->rightvol = 255;
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
390 // TODO: Try to merge SND_PaintChannelFrom8 and SND_PaintChannelFrom16
391 qboolean SND_PaintChannelFrom8 (channel_t *ch, int count)
392 {
393         int snd_vol, leftvol, rightvol;
394         const signed char *sfx;
395         const sfxbuffer_t *sb;
396         int i;
397
398         // If this channel manages its own volume
399         if (ch->flags & CHANNELFLAG_FULLVOLUME)
400                 snd_vol = 256;
401         else
402                 snd_vol = volume.value * 256;
403
404         leftvol = ch->leftvol * snd_vol;
405         rightvol = ch->rightvol * snd_vol;
406
407         sb = ch->sfx->fetcher->getsb (ch, ch->pos, count);
408         if (sb == NULL)
409                 return false;
410
411         // Stereo sound support
412         if (ch->sfx->format.channels == 2)
413         {
414                 sfx = sb->data + (ch->pos - sb->offset) * 2;
415                 for (i = 0;i < count;i++)
416                 {
417                         paintbuffer[i].left += (*sfx++ * leftvol) >> 8;
418                         paintbuffer[i].right += (*sfx++ * rightvol) >> 8;
419                 }
420         }
421         else
422         {
423                 sfx = sb->data + ch->pos - sb->offset;
424                 for (i = 0;i < count;i++)
425                 {
426                         paintbuffer[i].left += (*sfx * leftvol) >> 8;
427                         paintbuffer[i].right += (*sfx++ * rightvol) >> 8;
428                 }
429
430         }
431         ch->pos += count;
432         return true;
433 }
434
435 qboolean SND_PaintChannelFrom16 (channel_t *ch, int count)
436 {
437         int snd_vol, leftvol, rightvol;
438         signed short *sfx;
439         const sfxbuffer_t *sb;
440         int i;
441
442         // If this channel manages its own volume
443         if (ch->flags & CHANNELFLAG_FULLVOLUME)
444                 snd_vol = 256;
445         else
446                 snd_vol = volume.value * 256;
447
448         leftvol = ch->leftvol * snd_vol;
449         rightvol = ch->rightvol * snd_vol;
450
451         sb = ch->sfx->fetcher->getsb (ch, ch->pos, count);
452         if (sb == NULL)
453                 return false;
454
455         // Stereo sound support
456         if (ch->sfx->format.channels == 2)
457         {
458                 sfx = (signed short *)sb->data + (ch->pos - sb->offset) * 2;
459
460                 for (i=0 ; i<count ; i++)
461                 {
462                         paintbuffer[i].left += (*sfx++ * leftvol) >> 16;
463                         paintbuffer[i].right += (*sfx++ * rightvol) >> 16;
464                 }
465         }
466         else
467         {
468                 sfx = (signed short *)sb->data + ch->pos - sb->offset;
469
470                 for (i=0 ; i<count ; i++)
471                 {
472                         paintbuffer[i].left += (*sfx * leftvol) >> 16;
473                         paintbuffer[i].right += (*sfx++ * rightvol) >> 16;
474                 }
475         }
476
477         ch->pos += count;
478         return true;
479 }
480