]> git.xonotic.org Git - xonotic/darkplaces.git/blob - snd_mix.c
6bb065894c898ebc17ada67d17f4b75043bec8b6
[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 // 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                         ltime = paintedtime;
294
295                         while (ltime < end)
296                         {
297                                 qboolean stop_paint;
298
299                                 // paint up to end
300                                 if (ch->end < end)
301                                         count = ch->end - ltime;
302                                 else
303                                         count = end - ltime;
304
305                                 if (count > 0)
306                                 {
307                                         if (sfx->format.width == 1)
308                                                 stop_paint = !SND_PaintChannelFrom8(ch, count);
309                                         else
310                                                 stop_paint = !SND_PaintChannelFrom16(ch, count);
311
312                                         ltime += count;
313                                 }
314                                 else
315                                         stop_paint = false;
316
317                                 if (ltime >= ch->end)
318                                 {
319                                         // if at end of loop, restart
320                                         if ((sfx->loopstart >= 0 || ch->forceloop) && !stop_paint)
321                                         {
322                                                 ch->pos = bound(0, sfx->loopstart, (int)sfx->total_length - 1);
323                                                 ch->end = ltime + sfx->total_length - ch->pos;
324                                         }
325                                         // channel just stopped
326                                         else
327                                                 stop_paint = true;
328                                 }
329
330                                 if (stop_paint)
331                                 {
332                                         if (ch->sfx->fetcher->end != NULL)
333                                                 ch->sfx->fetcher->end (ch);
334                                         ch->sfx = NULL;
335                                         break;
336                                 }
337                         }
338                 }
339
340                 // transfer out according to DMA format
341                 //S_CaptureAVISound(paintbuffer, end - paintedtime);
342                 S_TransferPaintBuffer(end);
343                 paintedtime = end;
344         }
345 }
346
347 void SND_InitScaletable (void)
348 {
349         int i, j;
350
351         for (i = 0;i < 32;i++)
352                 for (j = 0;j < 256;j++)
353                         snd_scaletable[i][j] = ((signed char)j) * i * 8;
354 }
355
356
357 qboolean SND_PaintChannelFrom8 (channel_t *ch, int count)
358 {
359         int *lscale, *rscale;
360         unsigned char *sfx;
361         const sfxbuffer_t *sb;
362         int i, n;
363
364         if (ch->leftvol > 255)
365                 ch->leftvol = 255;
366         if (ch->rightvol > 255)
367                 ch->rightvol = 255;
368
369         lscale = snd_scaletable[ch->leftvol >> 3];
370         rscale = snd_scaletable[ch->rightvol >> 3];
371
372         sb = ch->sfx->fetcher->getsb (ch, ch->pos, count);
373         if (sb == NULL)
374                 return false;
375
376         if (ch->sfx->format.channels == 2)
377         {
378                 // LordHavoc: stereo sound support, and optimizations
379                 sfx = (unsigned char *)sb->data + (ch->pos - sb->offset) * 2;
380                 for (i = 0;i < count;i++)
381                 {
382                         paintbuffer[i].left += lscale[*sfx++];
383                         paintbuffer[i].right += rscale[*sfx++];
384                 }
385         }
386         else
387         {
388                 sfx = (unsigned char *)sb->data + ch->pos - sb->offset;
389                 for (i = 0;i < count;i++)
390                 {
391                         n = *sfx++;
392                         paintbuffer[i].left += lscale[n];
393                         paintbuffer[i].right += rscale[n];
394                 }
395
396         }
397         ch->pos += count;
398         return true;
399 }
400
401 qboolean SND_PaintChannelFrom16 (channel_t *ch, int count)
402 {
403         int leftvol, rightvol;
404         signed short *sfx;
405         const sfxbuffer_t *sb;
406         int i;
407
408         leftvol = ch->leftvol;
409         rightvol = ch->rightvol;
410
411         sb = ch->sfx->fetcher->getsb (ch, ch->pos, count);
412         if (sb == NULL)
413                 return false;
414
415         if (ch->sfx->format.channels == 2)
416         {
417                 // LordHavoc: stereo sound support, and optimizations
418                 sfx = (signed short *)sb->data + (ch->pos - sb->offset) * 2;
419
420                 for (i=0 ; i<count ; i++)
421                 {
422                         paintbuffer[i].left += (*sfx++ * leftvol) >> 8;
423                         paintbuffer[i].right += (*sfx++ * rightvol) >> 8;
424                 }
425         }
426         else
427         {
428                 sfx = (signed short *)sb->data + ch->pos - sb->offset;
429
430                 for (i=0 ; i<count ; i++)
431                 {
432                         paintbuffer[i].left += (*sfx * leftvol) >> 8;
433                         paintbuffer[i].right += (*sfx++ * rightvol) >> 8;
434                 }
435         }
436
437         ch->pos += count;
438         return true;
439 }
440