]> icculus.org git repositories - divverent/darkplaces.git/blob - snd_mix.c
don't use 3d textures on drivers that don't explicitly advertise it as an extension
[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                         sprintf (filename, "%s/dpavi.wav", com_gamedir);
44                         cl_avidemo_soundfile = fopen(filename, "wb");
45                         memset(out, 0, 44);
46                         fwrite(out, 1, 44, cl_avidemo_soundfile);
47                         // header will be filled out when file is closed
48                 }
49                 fseek(cl_avidemo_soundfile, 0, SEEK_END);
50                 // write the sound buffer as little endian 16bit interleaved stereo
51                 for(i = 0;i < length;i++)
52                 {
53                         n = buf[i].left >> 2; // quiet enough to prevent clipping most of the time
54                         n = bound(-32768, n, 32767);
55                         out[i*4+0] = n & 0xFF;
56                         out[i*4+1] = (n >> 8) & 0xFF;
57                         n = buf[i].right >> 2; // quiet enough to prevent clipping most of the time
58                         n = bound(-32768, n, 32767);
59                         out[i*4+2] = n & 0xFF;
60                         out[i*4+3] = (n >> 8) & 0xFF;
61                 }
62                 if (fwrite(out, 4, length, cl_avidemo_soundfile) < length)
63                 {
64                         Cvar_SetValueQuick(&cl_avidemo, 0);
65                         Con_Printf("avi saving sound failed, out of disk space?  stopping avi demo capture.\n");
66                 }
67         }
68         else if (cl_avidemo_soundfile)
69         {
70                 // file has not been closed yet, close it
71                 fseek(cl_avidemo_soundfile, 0, SEEK_END);
72                 i = ftell(cl_avidemo_soundfile);
73
74                 //"RIFF", (int) unknown (chunk size), "WAVE",
75                 //"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
76                 //"data", (int) unknown (chunk size)
77                 memcpy(out, "RIFF****WAVEfmt \x10\x00\x00\x00\x01\x00\x02\x00********\x04\x00\x10\x00data****", 44);
78                 // the length of the whole RIFF chunk
79                 n = i - 8;
80                 out[4] = (n) & 0xFF;
81                 out[5] = (n >> 8) & 0xFF;
82                 out[6] = (n >> 16) & 0xFF;
83                 out[7] = (n >> 24) & 0xFF;
84                 // rate
85                 n = shm->speed;
86                 out[24] = (n) & 0xFF;
87                 out[25] = (n >> 8) & 0xFF;
88                 out[26] = (n >> 16) & 0xFF;
89                 out[27] = (n >> 24) & 0xFF;
90                 // bytes per second (rate * channels * bytes per channel)
91                 n = shm->speed * 4;
92                 out[28] = (n) & 0xFF;
93                 out[29] = (n >> 8) & 0xFF;
94                 out[30] = (n >> 16) & 0xFF;
95                 out[31] = (n >> 24) & 0xFF;
96                 // the length of the data chunk
97                 n = i - 44;
98                 out[40] = (n) & 0xFF;
99                 out[41] = (n >> 8) & 0xFF;
100                 out[42] = (n >> 16) & 0xFF;
101                 out[43] = (n >> 24) & 0xFF;
102
103                 fseek(cl_avidemo_soundfile, 0, SEEK_SET);
104                 fwrite(out, 1, 44, cl_avidemo_soundfile);
105                 fclose(cl_avidemo_soundfile);
106                 cl_avidemo_soundfile = NULL;
107         }
108 }
109 */
110
111 void S_TransferPaintBuffer(int endtime)
112 {
113         void *pbuf;
114         if ((pbuf = S_LockBuffer()))
115         {
116                 int i;
117                 int *snd_p;
118                 int snd_vol;
119                 int lpaintedtime;
120                 int snd_linear_count;
121                 int val;
122                 snd_p = (int *) paintbuffer;
123                 snd_vol = volume.value*256;
124                 lpaintedtime = paintedtime;
125                 if (shm->samplebits == 16)
126                 {
127                         // 16bit
128                         short *snd_out;
129                         if (shm->channels == 2)
130                         {
131                                 // 16bit 2 channels (stereo)
132                                 while (lpaintedtime < endtime)
133                                 {
134                                         // handle recirculating buffer issues
135                                         i = lpaintedtime & ((shm->samples >> 1) - 1);
136                                         snd_out = (short *) pbuf + (i << 1);
137                                         snd_linear_count = (shm->samples >> 1) - i;
138                                         if (snd_linear_count > endtime - lpaintedtime)
139                                                 snd_linear_count = endtime - lpaintedtime;
140                                         snd_linear_count <<= 1;
141                                         if (snd_swapstereo.value)
142                                         {
143                                                 for (i = 0;i < snd_linear_count;i += 2)
144                                                 {
145                                                         val = (snd_p[i + 1] * snd_vol) >> 8;
146                                                         snd_out[i    ] = bound(-32768, val, 32767);
147                                                         val = (snd_p[i    ] * snd_vol) >> 8;
148                                                         snd_out[i + 1] = bound(-32768, val, 32767);
149                                                 }
150                                         }
151                                         else
152                                         {
153                                                 for (i = 0;i < snd_linear_count;i += 2)
154                                                 {
155                                                         val = (snd_p[i    ] * snd_vol) >> 8;
156                                                         snd_out[i    ] = bound(-32768, val, 32767);
157                                                         val = (snd_p[i + 1] * snd_vol) >> 8;
158                                                         snd_out[i + 1] = bound(-32768, val, 32767);
159                                                 }
160                                         }
161                                         snd_p += snd_linear_count;
162                                         lpaintedtime += (snd_linear_count >> 1);
163                                 }
164                         }
165                         else
166                         {
167                                 // 16bit 1 channel (mono)
168                                 while (lpaintedtime < endtime)
169                                 {
170                                         // handle recirculating buffer issues
171                                         i = lpaintedtime & (shm->samples - 1);
172                                         snd_out = (short *) pbuf + i;
173                                         snd_linear_count = shm->samples - i;
174                                         if (snd_linear_count > endtime - lpaintedtime)
175                                                 snd_linear_count = endtime - lpaintedtime;
176                                         for (i = 0;i < snd_linear_count;i++)
177                                         {
178                                                 val = ((snd_p[i * 2 + 0] + snd_p[i * 2 + 1]) * snd_vol) >> 9;
179                                                 snd_out[i] = bound(-32768, val, 32767);
180                                         }
181                                         snd_p += snd_linear_count << 1;
182                                         lpaintedtime += snd_linear_count;
183                                 }
184                         }
185                 }
186                 else
187                 {
188                         // 8bit
189                         unsigned char *snd_out;
190                         if (shm->channels == 2)
191                         {
192                                 // 8bit 2 channels (stereo)
193                                 while (lpaintedtime < endtime)
194                                 {
195                                         // handle recirculating buffer issues
196                                         i = lpaintedtime & ((shm->samples >> 1) - 1);
197                                         snd_out = (unsigned char *) pbuf + (i << 1);
198                                         snd_linear_count = (shm->samples >> 1) - i;
199                                         if (snd_linear_count > endtime - lpaintedtime)
200                                                 snd_linear_count = endtime - lpaintedtime;
201                                         snd_linear_count <<= 1;
202                                         if (snd_swapstereo.value)
203                                         {
204                                                 for (i = 0;i < snd_linear_count;i += 2)
205                                                 {
206                                                         val = ((snd_p[i + 1] * snd_vol) >> 16) + 128;
207                                                         snd_out[i    ] = bound(0, val, 255);
208                                                         val = ((snd_p[i    ] * snd_vol) >> 16) + 128;
209                                                         snd_out[i + 1] = bound(0, val, 255);
210                                                 }
211                                         }
212                                         else
213                                         {
214                                                 for (i = 0;i < snd_linear_count;i += 2)
215                                                 {
216                                                         val = ((snd_p[i    ] * snd_vol) >> 16) + 128;
217                                                         snd_out[i    ] = bound(0, val, 255);
218                                                         val = ((snd_p[i + 1] * snd_vol) >> 16) + 128;
219                                                         snd_out[i + 1] = bound(0, val, 255);
220                                                 }
221                                         }
222                                         snd_p += snd_linear_count;
223                                         lpaintedtime += (snd_linear_count >> 1);
224                                 }
225                         }
226                         else
227                         {
228                                 // 8bit 1 channel (mono)
229                                 while (lpaintedtime < endtime)
230                                 {
231                                         // handle recirculating buffer issues
232                                         i = lpaintedtime & (shm->samples - 1);
233                                         snd_out = (unsigned char *) pbuf + i;
234                                         snd_linear_count = shm->samples - i;
235                                         if (snd_linear_count > endtime - lpaintedtime)
236                                                 snd_linear_count = endtime - lpaintedtime;
237                                         for (i = 0;i < snd_linear_count;i++)
238                                         {
239                                                 val = (((snd_p[i * 2] + snd_p[i * 2 + 1]) * snd_vol) >> 17) + 128;
240                                                 snd_out[i    ] = bound(0, val, 255);
241                                         }
242                                         snd_p += snd_linear_count << 1;
243                                         lpaintedtime += snd_linear_count;
244                                 }
245                         }
246                 }
247
248                 S_UnlockBuffer();
249         }
250 }
251
252
253 /*
254 ===============================================================================
255
256 CHANNEL MIXING
257
258 ===============================================================================
259 */
260
261 void SND_PaintChannelFrom8 (channel_t *ch, sfxcache_t *sc, int endtime);
262 void SND_PaintChannelFrom16 (channel_t *ch, sfxcache_t *sc, int endtime);
263
264 void S_PaintChannels(int endtime)
265 {
266         int i;
267         int end;
268         channel_t *ch;
269         sfxcache_t *sc;
270         int ltime, count;
271
272         while (paintedtime < endtime)
273         {
274                 // if paintbuffer is smaller than DMA buffer
275                 end = endtime;
276                 if (endtime - paintedtime > PAINTBUFFER_SIZE)
277                         end = paintedtime + PAINTBUFFER_SIZE;
278
279                 // clear the paint buffer, filling it with data from rawsamples (music/video/whatever)
280                 S_RawSamples_Dequeue(&paintbuffer->left, end - paintedtime);
281
282                 // paint in the channels.
283                 ch = channels;
284                 for (i=0; i<total_channels ; i++, ch++)
285                 {
286                         if (!ch->sfx)
287                                 continue;
288                         if (!ch->leftvol && !ch->rightvol)
289                                 continue;
290                         sc = S_LoadSound (ch->sfx, true);
291                         if (!sc)
292                                 continue;
293
294                         ltime = paintedtime;
295
296                         while (ltime < end)
297                         {
298                                 // paint up to end
299                                 if (ch->end < end)
300                                         count = ch->end - ltime;
301                                 else
302                                         count = end - ltime;
303
304                                 if (count > 0)
305                                 {
306                                         if (sc->width == 1)
307                                                 SND_PaintChannelFrom8(ch, sc, count);
308                                         else
309                                                 SND_PaintChannelFrom16(ch, sc, count);
310
311                                         ltime += count;
312                                 }
313
314                                 // if at end of loop, restart
315                                 if (ltime >= ch->end)
316                                 {
317                                         if (sc->loopstart >= 0)
318                                         {
319                                                 ch->pos = sc->loopstart;
320                                                 ch->end = ltime + sc->length - ch->pos;
321                                         }
322                                         else
323                                         {
324                                                 // channel just stopped
325                                                 ch->sfx = NULL;
326                                                 break;
327                                         }
328                                 }
329                         }
330                 }
331
332                 // transfer out according to DMA format
333                 //S_CaptureAVISound(paintbuffer, end - paintedtime);
334                 S_TransferPaintBuffer(end);
335                 paintedtime = end;
336         }
337 }
338
339 void SND_InitScaletable (void)
340 {
341         int i, j;
342
343         for (i = 0;i < 32;i++)
344                 for (j = 0;j < 256;j++)
345                         snd_scaletable[i][j] = ((signed char)j) * i * 8;
346 }
347
348
349 void SND_PaintChannelFrom8 (channel_t *ch, sfxcache_t *sc, int count)
350 {
351         int *lscale, *rscale;
352         unsigned char *sfx;
353         int i, n;
354
355         if (ch->leftvol > 255)
356                 ch->leftvol = 255;
357         if (ch->rightvol > 255)
358                 ch->rightvol = 255;
359
360         lscale = snd_scaletable[ch->leftvol >> 3];
361         rscale = snd_scaletable[ch->rightvol >> 3];
362         if (sc->stereo)
363         {
364                 // LordHavoc: stereo sound support, and optimizations
365                 sfx = (unsigned char *)sc->data + ch->pos * 2;
366                 for (i = 0;i < count;i++)
367                 {
368                         paintbuffer[i].left += lscale[*sfx++];
369                         paintbuffer[i].right += rscale[*sfx++];
370                 }
371         }
372         else
373         {
374                 sfx = (unsigned char *)sc->data + ch->pos;
375                 for (i = 0;i < count;i++)
376                 {
377                         n = *sfx++;
378                         paintbuffer[i].left += lscale[n];
379                         paintbuffer[i].right += rscale[n];
380                 }
381
382         }
383         ch->pos += count;
384 }
385
386 void SND_PaintChannelFrom16 (channel_t *ch, sfxcache_t *sc, int count)
387 {
388         int leftvol, rightvol;
389         signed short *sfx;
390         int i;
391
392         leftvol = ch->leftvol;
393         rightvol = ch->rightvol;
394         if (sc->stereo)
395         {
396                 // LordHavoc: stereo sound support, and optimizations
397                 sfx = (signed short *)sc->data + ch->pos * 2;
398
399                 for (i=0 ; i<count ; i++)
400                 {
401                         paintbuffer[i].left += (*sfx++ * leftvol) >> 8;
402                         paintbuffer[i].right += (*sfx++ * rightvol) >> 8;
403                 }
404         }
405         else
406         {
407                 sfx = (signed short *)sc->data + ch->pos;
408
409                 for (i=0 ; i<count ; i++)
410                 {
411                         paintbuffer[i].left += (*sfx * leftvol) >> 8;
412                         paintbuffer[i].right += (*sfx++ * rightvol) >> 8;
413                 }
414         }
415
416         ch->pos += count;
417 }
418