]> de.git.xonotic.org Git - xonotic/darkplaces.git/blob - snd_mix.c
fix warning
[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
21 #include "quakedef.h"
22 #include "snd_main.h"
23
24 extern cvar_t snd_softclip;
25
26 static portable_sampleframe_t paintbuffer[PAINTBUFFER_SIZE];
27 static portable_sampleframe_t paintbuffer_unswapped[PAINTBUFFER_SIZE];
28
29 extern speakerlayout_t snd_speakerlayout; // for querying the listeners
30
31 extern void SCR_CaptureVideo_SoundFrame(const portable_sampleframe_t *paintbuffer, size_t length);
32 static void S_CaptureAVISound(const portable_sampleframe_t *paintbuffer, size_t length)
33 {
34         size_t i;
35         unsigned int j;
36
37         if (!cls.capturevideo.active)
38                 return;
39
40         // undo whatever swapping the channel layout (swapstereo, ALSA) did
41         for(j = 0; j < snd_speakerlayout.channels; ++j)
42         {
43                 unsigned int j0 = snd_speakerlayout.listeners[j].channel_unswapped;
44                 for(i = 0; i < length; ++i)
45                         paintbuffer_unswapped[i].sample[j0] = paintbuffer[i].sample[j];
46         }
47
48         SCR_CaptureVideo_SoundFrame(paintbuffer_unswapped, length);
49 }
50
51 extern cvar_t snd_softclip;
52
53 static void S_SoftClipPaintBuffer(portable_sampleframe_t *painted_ptr, int nbframes, int width, int channels)
54 {
55         int i;
56
57         if((snd_softclip.integer == 1 && width <= 2) || snd_softclip.integer > 1)
58         {
59                 portable_sampleframe_t *p = painted_ptr;
60
61 #if 0
62 /* Soft clipping, the sound of a dream, thanks to Jon Wattes
63    post to Musicdsp.org */
64 #define SOFTCLIP(x) (x) = sin(bound(-M_PI/2, (x), M_PI/2)) * 0.25
65 #endif
66
67                 // let's do a simple limiter instead, seems to sound better
68                 static float maxvol = 0;
69                 maxvol = max(1.0f, maxvol * (1.0f - nbframes / (0.4f * snd_renderbuffer->format.speed)));
70 #define SOFTCLIP(x) if(fabs(x)>maxvol) maxvol=fabs(x); (x) /= maxvol;
71
72                 if (channels == 8)  // 7.1 surround
73                 {
74                         for (i = 0;i < nbframes;i++, p++)
75                         {
76                                 SOFTCLIP(p->sample[0]);
77                                 SOFTCLIP(p->sample[1]);
78                                 SOFTCLIP(p->sample[2]);
79                                 SOFTCLIP(p->sample[3]);
80                                 SOFTCLIP(p->sample[4]);
81                                 SOFTCLIP(p->sample[5]);
82                                 SOFTCLIP(p->sample[6]);
83                                 SOFTCLIP(p->sample[7]);
84                         }
85                 }
86                 else if (channels == 6)  // 5.1 surround
87                 {
88                         for (i = 0; i < nbframes; i++, p++)
89                         {
90                                 SOFTCLIP(p->sample[0]);
91                                 SOFTCLIP(p->sample[1]);
92                                 SOFTCLIP(p->sample[2]);
93                                 SOFTCLIP(p->sample[3]);
94                                 SOFTCLIP(p->sample[4]);
95                                 SOFTCLIP(p->sample[5]);
96                         }
97                 }
98                 else if (channels == 4)  // 4.0 surround
99                 {
100                         for (i = 0; i < nbframes; i++, p++)
101                         {
102                                 SOFTCLIP(p->sample[0]);
103                                 SOFTCLIP(p->sample[1]);
104                                 SOFTCLIP(p->sample[2]);
105                                 SOFTCLIP(p->sample[3]);
106                         }
107                 }
108                 else if (channels == 2)  // 2.0 stereo
109                 {
110                         for (i = 0; i < nbframes; i++, p++)
111                         {
112                                 SOFTCLIP(p->sample[0]);
113                                 SOFTCLIP(p->sample[1]);
114                         }
115                 }
116                 else if (channels == 1)  // 1.0 mono
117                 {
118                         for (i = 0; i < nbframes; i++, p++)
119                         {
120                                 SOFTCLIP(p->sample[0]);
121                         }
122                 }
123 #undef SOFTCLIP
124         }
125 }
126
127 static void S_ConvertPaintBuffer(portable_sampleframe_t *painted_ptr, void *rb_ptr, int nbframes, int width, int channels)
128 {
129         int i, val;
130
131         // FIXME: add 24bit and 32bit float formats
132         // FIXME: optimize with SSE intrinsics?
133         if (width == 2)  // 16bit
134         {
135                 short *snd_out = (short*)rb_ptr;
136                 if (channels == 8)  // 7.1 surround
137                 {
138                         for (i = 0;i < nbframes;i++, painted_ptr++)
139                         {
140                                 val = (int)(painted_ptr->sample[0] * 32768.0f);*snd_out++ = bound(-32768, val, 32767);
141                                 val = (int)(painted_ptr->sample[1] * 32768.0f);*snd_out++ = bound(-32768, val, 32767);
142                                 val = (int)(painted_ptr->sample[2] * 32768.0f);*snd_out++ = bound(-32768, val, 32767);
143                                 val = (int)(painted_ptr->sample[3] * 32768.0f);*snd_out++ = bound(-32768, val, 32767);
144                                 val = (int)(painted_ptr->sample[4] * 32768.0f);*snd_out++ = bound(-32768, val, 32767);
145                                 val = (int)(painted_ptr->sample[5] * 32768.0f);*snd_out++ = bound(-32768, val, 32767);
146                                 val = (int)(painted_ptr->sample[6] * 32768.0f);*snd_out++ = bound(-32768, val, 32767);
147                                 val = (int)(painted_ptr->sample[7] * 32768.0f);*snd_out++ = bound(-32768, val, 32767);
148                         }
149                 }
150                 else if (channels == 6)  // 5.1 surround
151                 {
152                         for (i = 0; i < nbframes; i++, painted_ptr++)
153                         {
154                                 val = (int)(painted_ptr->sample[0] * 32768.0f);*snd_out++ = bound(-32768, val, 32767);
155                                 val = (int)(painted_ptr->sample[1] * 32768.0f);*snd_out++ = bound(-32768, val, 32767);
156                                 val = (int)(painted_ptr->sample[2] * 32768.0f);*snd_out++ = bound(-32768, val, 32767);
157                                 val = (int)(painted_ptr->sample[3] * 32768.0f);*snd_out++ = bound(-32768, val, 32767);
158                                 val = (int)(painted_ptr->sample[4] * 32768.0f);*snd_out++ = bound(-32768, val, 32767);
159                                 val = (int)(painted_ptr->sample[5] * 32768.0f);*snd_out++ = bound(-32768, val, 32767);
160                         }
161                 }
162                 else if (channels == 4)  // 4.0 surround
163                 {
164                         for (i = 0; i < nbframes; i++, painted_ptr++)
165                         {
166                                 val = (int)(painted_ptr->sample[0] * 32768.0f);*snd_out++ = bound(-32768, val, 32767);
167                                 val = (int)(painted_ptr->sample[1] * 32768.0f);*snd_out++ = bound(-32768, val, 32767);
168                                 val = (int)(painted_ptr->sample[2] * 32768.0f);*snd_out++ = bound(-32768, val, 32767);
169                                 val = (int)(painted_ptr->sample[3] * 32768.0f);*snd_out++ = bound(-32768, val, 32767);
170                         }
171                 }
172                 else if (channels == 2)  // 2.0 stereo
173                 {
174                         for (i = 0; i < nbframes; i++, painted_ptr++)
175                         {
176                                 val = (int)(painted_ptr->sample[0] * 32768.0f);*snd_out++ = bound(-32768, val, 32767);
177                                 val = (int)(painted_ptr->sample[1] * 32768.0f);*snd_out++ = bound(-32768, val, 32767);
178                         }
179                 }
180                 else if (channels == 1)  // 1.0 mono
181                 {
182                         for (i = 0; i < nbframes; i++, painted_ptr++)
183                         {
184                                 val = (int)((painted_ptr->sample[0] + painted_ptr->sample[1]) * 16384.0f);*snd_out++ = bound(-32768, val, 32767);
185                         }
186                 }
187
188                 // noise is really really annoying
189                 if (cls.timedemo)
190                         memset(rb_ptr, 0, nbframes * channels * width);
191         }
192         else  // 8bit
193         {
194                 unsigned char *snd_out = (unsigned char*)rb_ptr;
195                 if (channels == 8)  // 7.1 surround
196                 {
197                         for (i = 0; i < nbframes; i++, painted_ptr++)
198                         {
199                                 val = (int)(painted_ptr->sample[0] * 128.0f) + 128; *snd_out++ = bound(0, val, 255);
200                                 val = (int)(painted_ptr->sample[1] * 128.0f) + 128; *snd_out++ = bound(0, val, 255);
201                                 val = (int)(painted_ptr->sample[2] * 128.0f) + 128; *snd_out++ = bound(0, val, 255);
202                                 val = (int)(painted_ptr->sample[3] * 128.0f) + 128; *snd_out++ = bound(0, val, 255);
203                                 val = (int)(painted_ptr->sample[4] * 128.0f) + 128; *snd_out++ = bound(0, val, 255);
204                                 val = (int)(painted_ptr->sample[5] * 128.0f) + 128; *snd_out++ = bound(0, val, 255);
205                                 val = (int)(painted_ptr->sample[6] * 128.0f) + 128; *snd_out++ = bound(0, val, 255);
206                                 val = (int)(painted_ptr->sample[7] * 128.0f) + 128; *snd_out++ = bound(0, val, 255);
207                         }
208                 }
209                 else if (channels == 6)  // 5.1 surround
210                 {
211                         for (i = 0; i < nbframes; i++, painted_ptr++)
212                         {
213                                 val = (int)(painted_ptr->sample[0] * 128.0f) + 128; *snd_out++ = bound(0, val, 255);
214                                 val = (int)(painted_ptr->sample[1] * 128.0f) + 128; *snd_out++ = bound(0, val, 255);
215                                 val = (int)(painted_ptr->sample[2] * 128.0f) + 128; *snd_out++ = bound(0, val, 255);
216                                 val = (int)(painted_ptr->sample[3] * 128.0f) + 128; *snd_out++ = bound(0, val, 255);
217                                 val = (int)(painted_ptr->sample[4] * 128.0f) + 128; *snd_out++ = bound(0, val, 255);
218                                 val = (int)(painted_ptr->sample[5] * 128.0f) + 128; *snd_out++ = bound(0, val, 255);
219                         }
220                 }
221                 else if (channels == 4)  // 4.0 surround
222                 {
223                         for (i = 0; i < nbframes; i++, painted_ptr++)
224                         {
225                                 val = (int)(painted_ptr->sample[0] * 128.0f) + 128; *snd_out++ = bound(0, val, 255);
226                                 val = (int)(painted_ptr->sample[1] * 128.0f) + 128; *snd_out++ = bound(0, val, 255);
227                                 val = (int)(painted_ptr->sample[2] * 128.0f) + 128; *snd_out++ = bound(0, val, 255);
228                                 val = (int)(painted_ptr->sample[3] * 128.0f) + 128; *snd_out++ = bound(0, val, 255);
229                         }
230                 }
231                 else if (channels == 2)  // 2.0 stereo
232                 {
233                         for (i = 0; i < nbframes; i++, painted_ptr++)
234                         {
235                                 val = (int)(painted_ptr->sample[0] * 128.0f) + 128; *snd_out++ = bound(0, val, 255);
236                                 val = (int)(painted_ptr->sample[1] * 128.0f) + 128; *snd_out++ = bound(0, val, 255);
237                         }
238                 }
239                 else if (channels == 1)  // 1.0 mono
240                 {
241                         for (i = 0;i < nbframes;i++, painted_ptr++)
242                         {
243                                 val = (int)((painted_ptr->sample[0] + painted_ptr->sample[1]) * 64.0f) + 128; *snd_out++ = bound(0, val, 255);
244                         }
245                 }
246
247                 // noise is really really annoying
248                 if (cls.timedemo)
249                         memset(rb_ptr, 128, nbframes * channels);
250         }
251 }
252
253
254 /*
255 ===============================================================================
256
257 CHANNEL MIXING
258
259 ===============================================================================
260 */
261
262 void S_MixToBuffer(void *stream, unsigned int bufferframes)
263 {
264         int channelindex;
265         channel_t *ch;
266         int totalmixframes;
267         unsigned char *outbytes = (unsigned char *) stream;
268         sfx_t *sfx;
269         portable_sampleframe_t *paint;
270         int wantframes;
271         int i;
272         int count;
273         int fetched;
274         int fetch;
275         int istartframe;
276         int iendframe;
277         int ilengthframes;
278         int totallength;
279         int loopstart;
280         int indexfrac;
281         int indexfracstep;
282 #define S_FETCHBUFFERSIZE 4096
283         float fetchsampleframes[S_FETCHBUFFERSIZE*2];
284         const float *fetchsampleframe;
285         float vol[SND_LISTENERS];
286         float lerp[2];
287         float sample[3];
288         double posd;
289         double speedd;
290         float maxvol;
291         qboolean looping;
292         qboolean silent;
293
294         // mix as many times as needed to fill the requested buffer
295         while (bufferframes)
296         {
297                 // limit to the size of the paint buffer
298                 totalmixframes = min(bufferframes, PAINTBUFFER_SIZE);
299
300                 // clear the paint buffer
301                 memset(paintbuffer, 0, totalmixframes * sizeof(paintbuffer[0]));
302
303                 // paint in the channels.
304                 // channels with zero volumes still advance in time but don't paint.
305                 ch = channels;
306                 for (channelindex = 0;channelindex < (int)total_channels;channelindex++, ch++)
307                 {
308                         sfx = ch->sfx;
309                         if (sfx == NULL)
310                                 continue;
311                         if (!S_LoadSound (sfx, true))
312                                 continue;
313                         if (ch->flags & CHANNELFLAG_PAUSED)
314                                 continue;
315                         if (!sfx->total_length)
316                                 continue;
317
318                         // copy the channel information to the stack for reference, otherwise the
319                         // values might change during a mix if the spatializer is updating them
320                         // (note: this still may get some old and some new values!)
321                         posd = ch->position;
322                         speedd = ch->mixspeed * sfx->format.speed / snd_renderbuffer->format.speed;
323                         for (i = 0;i < SND_LISTENERS;i++)
324                                 vol[i] = ch->volume[i];
325
326                         // check total volume level, because we can skip some code on silent sounds but other code must still run (position updates mainly)
327                         maxvol = 0;
328                         for (i = 0;i < SND_LISTENERS;i++)
329                                 if(vol[i] > maxvol)
330                                         maxvol = vol[i];
331                         switch(snd_renderbuffer->format.width)
332                         {
333                                 case 1: // 8bpp
334                                         silent = maxvol < (1.0f / (256.0f));
335                                         // so silent it has zero effect
336                                         break;
337                                 case 2: // 16bpp
338                                         silent = maxvol < (1.0f / (65536.0f));
339                                         // so silent it has zero effect
340                                         break;
341                                 default: // floating point
342                                         silent = maxvol < 1.0e-13f;
343                                         // 130 dB is difference between hearing
344                                         // threshold and a jackhammer from
345                                         // working distance.
346                                         // therefore, anyone who turns up
347                                         // volume so much they notice this
348                                         // cutoff, likely already has their
349                                         // ear-drums blown out anyway.
350                                         break;
351                         }
352
353                         // when doing prologic mixing, some channels invert one side
354                         if (ch->prologic_invert == -1)
355                                 vol[1] *= -1.0f;
356
357                         // get some sfx info in a consistent form
358                         totallength = sfx->total_length;
359                         loopstart = (int)sfx->loopstart < totallength ? (int)sfx->loopstart : ((ch->flags & CHANNELFLAG_FORCELOOP) ? 0 : totallength);
360                         looping = loopstart < totallength;
361
362                         // do the actual paint now (may skip work if silent)
363                         paint = paintbuffer;
364                         wantframes = totalmixframes;
365                         istartframe = 0;
366                         for (wantframes = totalmixframes;wantframes > 0;posd += count * speedd, wantframes -= count)
367                         {
368                                 // check if this is a delayed sound
369                                 if (posd < 0)
370                                 {
371                                         // for a delayed sound we have to eat into the delay first
372                                         count = (int)floor(-posd / speedd) + 1;
373                                         count = bound(1, count, wantframes);
374                                         // let the for loop iterator apply the skip
375                                         continue;
376                                 }
377
378                                 // compute a fetch size that won't overflow our buffer
379                                 count = wantframes;
380                                 for (;;)
381                                 {
382                                         istartframe = (int)floor(posd);
383                                         iendframe = (int)floor(posd + (count-1) * speedd);
384                                         ilengthframes = count > 1 ? (iendframe - istartframe + 2) : 2;
385                                         if (ilengthframes <= S_FETCHBUFFERSIZE)
386                                                 break;
387                                         // reduce count by 25% and try again
388                                         count -= count >> 2;
389                                 }
390
391                                 // zero whole fetch buffer for safety
392                                 // (floating point noise from uninitialized memory = HORRIBLE)
393                                 // otherwise we would only need to clear the excess
394                                 if (!silent)
395                                         memset(fetchsampleframes, 0, ilengthframes*sfx->format.channels*sizeof(fetchsampleframes[0]));
396
397                                 // if looping, do multiple fetches
398                                 fetched = 0;
399                                 for (;;)
400                                 {
401                                         fetch = min(ilengthframes - fetched, totallength - istartframe);
402                                         if (fetch > 0)
403                                         {
404                                                 if (!silent)
405                                                         sfx->fetcher->getsamplesfloat(ch, sfx, istartframe, fetch, fetchsampleframes + fetched*sfx->format.channels);
406                                                 istartframe += fetch;
407                                                 fetched += fetch;
408                                         }
409                                         if (istartframe == totallength && looping && fetched < ilengthframes)
410                                         {
411                                                 // loop and fetch some more
412                                                 posd += loopstart - totallength;
413                                                 istartframe = loopstart;
414                                         }
415                                         else
416                                         {
417                                                 break;
418                                         }
419                                 }
420
421                                 // set up our fixedpoint resampling variables (float to int conversions are expensive so do not do one per sampleframe)
422                                 fetchsampleframe = fetchsampleframes;
423                                 indexfrac = (int)floor((posd - floor(posd)) * 65536.0);
424                                 indexfracstep = (int)floor(speedd * 65536.0);
425                                 if (!silent)
426                                 {
427                                         if (sfx->format.channels == 2)
428                                         {
429                                                 // music is stereo
430 #if SND_LISTENERS != 8
431 #error the following code only supports up to 8 channels, update it
432 #endif
433                                                 if (snd_speakerlayout.channels > 2)
434                                                 {
435                                                         // surround mixing
436                                                         for (i = 0;i < count;i++, paint++)
437                                                         {
438                                                                 lerp[1] = indexfrac * (1.0f / 65536.0f);
439                                                                 lerp[0] = 1.0f - lerp[1];
440                                                                 sample[0] = fetchsampleframe[0] * lerp[0] + fetchsampleframe[2] * lerp[1];
441                                                                 sample[1] = fetchsampleframe[1] * lerp[0] + fetchsampleframe[3] * lerp[1];
442                                                                 sample[2] = (sample[0] + sample[1]) * 0.5f;
443                                                                 paint->sample[0] += sample[0] * vol[0];
444                                                                 paint->sample[1] += sample[1] * vol[1];
445                                                                 paint->sample[2] += sample[0] * vol[2];
446                                                                 paint->sample[3] += sample[1] * vol[3];
447                                                                 paint->sample[4] += sample[2] * vol[4];
448                                                                 paint->sample[5] += sample[2] * vol[5];
449                                                                 paint->sample[6] += sample[0] * vol[6];
450                                                                 paint->sample[7] += sample[1] * vol[7];
451                                                                 indexfrac += indexfracstep;
452                                                                 fetchsampleframe += 2 * (indexfrac >> 16);
453                                                                 indexfrac &= 0xFFFF;
454                                                         }
455                                                 }
456                                                 else
457                                                 {
458                                                         // stereo mixing
459                                                         for (i = 0;i < count;i++, paint++)
460                                                         {
461                                                                 lerp[1] = indexfrac * (1.0f / 65536.0f);
462                                                                 lerp[0] = 1.0f - lerp[1];
463                                                                 sample[0] = fetchsampleframe[0] * lerp[0] + fetchsampleframe[2] * lerp[1];
464                                                                 sample[1] = fetchsampleframe[1] * lerp[0] + fetchsampleframe[3] * lerp[1];
465                                                                 paint->sample[0] += sample[0] * vol[0];
466                                                                 paint->sample[1] += sample[1] * vol[1];
467                                                                 indexfrac += indexfracstep;
468                                                                 fetchsampleframe += 2 * (indexfrac >> 16);
469                                                                 indexfrac &= 0xFFFF;
470                                                         }
471                                                 }
472                                         }
473                                         else if (sfx->format.channels == 1)
474                                         {
475                                                 // most sounds are mono
476 #if SND_LISTENERS != 8
477 #error the following code only supports up to 8 channels, update it
478 #endif
479                                                 if (snd_speakerlayout.channels > 2)
480                                                 {
481                                                         // surround mixing
482                                                         for (i = 0;i < count;i++, paint++)
483                                                         {
484                                                                 lerp[1] = indexfrac * (1.0f / 65536.0f);
485                                                                 lerp[0] = 1.0f - lerp[1];
486                                                                 sample[0] = fetchsampleframe[0] * lerp[0] + fetchsampleframe[1] * lerp[1];
487                                                                 paint->sample[0] += sample[0] * vol[0];
488                                                                 paint->sample[1] += sample[0] * vol[1];
489                                                                 paint->sample[2] += sample[0] * vol[2];
490                                                                 paint->sample[3] += sample[0] * vol[3];
491                                                                 paint->sample[4] += sample[0] * vol[4];
492                                                                 paint->sample[5] += sample[0] * vol[5];
493                                                                 paint->sample[6] += sample[0] * vol[6];
494                                                                 paint->sample[7] += sample[0] * vol[7];
495                                                                 indexfrac += indexfracstep;
496                                                                 fetchsampleframe += (indexfrac >> 16);
497                                                                 indexfrac &= 0xFFFF;
498                                                         }
499                                                 }
500                                                 else
501                                                 {
502                                                         // stereo mixing
503                                                         for (i = 0;i < count;i++, paint++)
504                                                         {
505                                                                 lerp[1] = indexfrac * (1.0f / 65536.0f);
506                                                                 lerp[0] = 1.0f - lerp[1];
507                                                                 sample[0] = fetchsampleframe[0] * lerp[0] + fetchsampleframe[1] * lerp[1];
508                                                                 paint->sample[0] += sample[0] * vol[0];
509                                                                 paint->sample[1] += sample[0] * vol[1];
510                                                                 indexfrac += indexfracstep;
511                                                                 fetchsampleframe += (indexfrac >> 16);
512                                                                 indexfrac &= 0xFFFF;
513                                                         }
514                                                 }
515                                         }
516                                 }
517                         }
518                         ch->position = posd;
519                         if (!looping && istartframe == totallength)
520                                 S_StopChannel(ch - channels, false, false);
521                 }
522
523                 S_SoftClipPaintBuffer(paintbuffer, totalmixframes, snd_renderbuffer->format.width, snd_renderbuffer->format.channels);
524
525                 if (!snd_usethreadedmixing)
526                         S_CaptureAVISound(paintbuffer, totalmixframes);
527
528                 S_ConvertPaintBuffer(paintbuffer, outbytes, totalmixframes, snd_renderbuffer->format.width, snd_renderbuffer->format.channels);
529
530                 // advance the output pointer
531                 outbytes += totalmixframes * snd_renderbuffer->format.width * snd_renderbuffer->format.channels;
532                 bufferframes -= totalmixframes;
533         }
534 }