fixed name of dpavi.wav file to work correctly with FS_Open (this is in commented...
[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_Printf("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->samplebits == 16)
125                 {
126                         // 16bit
127                         short *snd_out;
128                         if (shm->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->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 void SND_PaintChannelFrom8 (channel_t *ch, sfxcache_t *sc, int endtime);
261 void SND_PaintChannelFrom16 (channel_t *ch, sfxcache_t *sc, int endtime);
262
263 void S_PaintChannels(int endtime)
264 {
265         int i;
266         int end;
267         channel_t *ch;
268         sfxcache_t *sc;
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                         if (!ch->sfx)
286                                 continue;
287                         if (!ch->leftvol && !ch->rightvol)
288                                 continue;
289                         sc = S_LoadSound (ch->sfx, true);
290                         if (!sc)
291                                 continue;
292
293                         ltime = paintedtime;
294
295                         while (ltime < end)
296                         {
297                                 // paint up to end
298                                 if (ch->end < end)
299                                         count = ch->end - ltime;
300                                 else
301                                         count = end - ltime;
302
303                                 if (count > 0)
304                                 {
305                                         if (sc->width == 1)
306                                                 SND_PaintChannelFrom8(ch, sc, count);
307                                         else
308                                                 SND_PaintChannelFrom16(ch, sc, count);
309
310                                         ltime += count;
311                                 }
312
313                                 // if at end of loop, restart
314                                 if (ltime >= ch->end)
315                                 {
316                                         if (sc->loopstart >= 0)
317                                         {
318                                                 ch->pos = sc->loopstart;
319                                                 ch->end = ltime + sc->length - ch->pos;
320                                         }
321                                         else
322                                         {
323                                                 // channel just stopped
324                                                 ch->sfx = NULL;
325                                                 break;
326                                         }
327                                 }
328                         }
329                 }
330
331                 // transfer out according to DMA format
332                 //S_CaptureAVISound(paintbuffer, end - paintedtime);
333                 S_TransferPaintBuffer(end);
334                 paintedtime = end;
335         }
336 }
337
338 void SND_InitScaletable (void)
339 {
340         int i, j;
341
342         for (i = 0;i < 32;i++)
343                 for (j = 0;j < 256;j++)
344                         snd_scaletable[i][j] = ((signed char)j) * i * 8;
345 }
346
347
348 void SND_PaintChannelFrom8 (channel_t *ch, sfxcache_t *sc, int count)
349 {
350         int *lscale, *rscale;
351         unsigned char *sfx;
352         int i, n;
353
354         if (ch->leftvol > 255)
355                 ch->leftvol = 255;
356         if (ch->rightvol > 255)
357                 ch->rightvol = 255;
358
359         lscale = snd_scaletable[ch->leftvol >> 3];
360         rscale = snd_scaletable[ch->rightvol >> 3];
361         if (sc->stereo)
362         {
363                 // LordHavoc: stereo sound support, and optimizations
364                 sfx = (unsigned char *)sc->data + ch->pos * 2;
365                 for (i = 0;i < count;i++)
366                 {
367                         paintbuffer[i].left += lscale[*sfx++];
368                         paintbuffer[i].right += rscale[*sfx++];
369                 }
370         }
371         else
372         {
373                 sfx = (unsigned char *)sc->data + ch->pos;
374                 for (i = 0;i < count;i++)
375                 {
376                         n = *sfx++;
377                         paintbuffer[i].left += lscale[n];
378                         paintbuffer[i].right += rscale[n];
379                 }
380
381         }
382         ch->pos += count;
383 }
384
385 void SND_PaintChannelFrom16 (channel_t *ch, sfxcache_t *sc, int count)
386 {
387         int leftvol, rightvol;
388         signed short *sfx;
389         int i;
390
391         leftvol = ch->leftvol;
392         rightvol = ch->rightvol;
393         if (sc->stereo)
394         {
395                 // LordHavoc: stereo sound support, and optimizations
396                 sfx = (signed short *)sc->data + ch->pos * 2;
397
398                 for (i=0 ; i<count ; i++)
399                 {
400                         paintbuffer[i].left += (*sfx++ * leftvol) >> 8;
401                         paintbuffer[i].right += (*sfx++ * rightvol) >> 8;
402                 }
403         }
404         else
405         {
406                 sfx = (signed short *)sc->data + ch->pos;
407
408                 for (i=0 ; i<count ; i++)
409                 {
410                         paintbuffer[i].left += (*sfx * leftvol) >> 8;
411                         paintbuffer[i].right += (*sfx++ * rightvol) >> 8;
412                 }
413         }
414
415         ch->pos += count;
416 }
417