2 Copyright (C) 1996-1997 Id Software, Inc.
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.
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.
13 See the GNU General Public License for more details.
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.
24 extern cvar_t snd_softclip;
26 static portable_sampleframe_t paintbuffer[PAINTBUFFER_SIZE];
27 static portable_sampleframe_t paintbuffer_unswapped[PAINTBUFFER_SIZE];
29 extern speakerlayout_t snd_speakerlayout; // for querying the listeners
31 #ifdef CONFIG_VIDEO_CAPTURE
32 static void S_CaptureAVISound(const portable_sampleframe_t *sampleframes, size_t length)
37 if (!cls.capturevideo.active)
40 // undo whatever swapping the channel layout (swapstereo, ALSA) did
41 for(j = 0; j < snd_speakerlayout.channels; ++j)
43 unsigned int j0 = snd_speakerlayout.listeners[j].channel_unswapped;
44 for(i = 0; i < length; ++i)
45 paintbuffer_unswapped[i].sample[j0] = sampleframes[i].sample[j];
48 SCR_CaptureVideo_SoundFrame(paintbuffer_unswapped, length);
52 extern cvar_t snd_softclip;
54 static void S_SoftClipPaintBuffer(portable_sampleframe_t *painted_ptr, int nbframes, int width, int nchannels)
58 if((snd_softclip.integer == 1 && width <= 2) || snd_softclip.integer > 1)
60 portable_sampleframe_t *p = painted_ptr;
63 /* Soft clipping, the sound of a dream, thanks to Jon Wattes
64 post to Musicdsp.org */
65 #define SOFTCLIP(x) (x) = sin(bound(-M_PI/2, (x), M_PI/2)) * 0.25
68 // let's do a simple limiter instead, seems to sound better
69 static float maxvol = 0;
70 maxvol = max(1.0f, maxvol * (1.0f - nbframes / (0.4f * snd_renderbuffer->format.speed)));
71 #define SOFTCLIP(x) if(fabs(x)>maxvol) maxvol=fabs(x); (x) /= maxvol;
73 if (nchannels == 8) // 7.1 surround
75 for (i = 0;i < nbframes;i++, p++)
77 SOFTCLIP(p->sample[0]);
78 SOFTCLIP(p->sample[1]);
79 SOFTCLIP(p->sample[2]);
80 SOFTCLIP(p->sample[3]);
81 SOFTCLIP(p->sample[4]);
82 SOFTCLIP(p->sample[5]);
83 SOFTCLIP(p->sample[6]);
84 SOFTCLIP(p->sample[7]);
87 else if (nchannels == 6) // 5.1 surround
89 for (i = 0; i < nbframes; i++, p++)
91 SOFTCLIP(p->sample[0]);
92 SOFTCLIP(p->sample[1]);
93 SOFTCLIP(p->sample[2]);
94 SOFTCLIP(p->sample[3]);
95 SOFTCLIP(p->sample[4]);
96 SOFTCLIP(p->sample[5]);
99 else if (nchannels == 4) // 4.0 surround
101 for (i = 0; i < nbframes; i++, p++)
103 SOFTCLIP(p->sample[0]);
104 SOFTCLIP(p->sample[1]);
105 SOFTCLIP(p->sample[2]);
106 SOFTCLIP(p->sample[3]);
109 else if (nchannels == 2) // 2.0 stereo
111 for (i = 0; i < nbframes; i++, p++)
113 SOFTCLIP(p->sample[0]);
114 SOFTCLIP(p->sample[1]);
117 else if (nchannels == 1) // 1.0 mono
119 for (i = 0; i < nbframes; i++, p++)
121 SOFTCLIP(p->sample[0]);
128 static void S_ConvertPaintBuffer(portable_sampleframe_t *painted_ptr, void *rb_ptr, int nbframes, int width, int nchannels)
132 if (width == 4) // 32bit float
134 float *snd_out = (float*)rb_ptr;
135 if (nchannels == 8) // 7.1 surround
137 for (i = 0; i < nbframes; i++, painted_ptr++)
139 *snd_out++ = painted_ptr->sample[0];
140 *snd_out++ = painted_ptr->sample[1];
141 *snd_out++ = painted_ptr->sample[2];
142 *snd_out++ = painted_ptr->sample[3];
143 *snd_out++ = painted_ptr->sample[4];
144 *snd_out++ = painted_ptr->sample[5];
145 *snd_out++ = painted_ptr->sample[6];
146 *snd_out++ = painted_ptr->sample[7];
149 else if (nchannels == 6) // 5.1 surround
151 for (i = 0; i < nbframes; i++, painted_ptr++)
153 *snd_out++ = painted_ptr->sample[0];
154 *snd_out++ = painted_ptr->sample[1];
155 *snd_out++ = painted_ptr->sample[2];
156 *snd_out++ = painted_ptr->sample[3];
157 *snd_out++ = painted_ptr->sample[4];
158 *snd_out++ = painted_ptr->sample[5];
161 else if (nchannels == 4) // 4.0 surround
163 for (i = 0; i < nbframes; i++, painted_ptr++)
165 *snd_out++ = painted_ptr->sample[0];
166 *snd_out++ = painted_ptr->sample[1];
167 *snd_out++ = painted_ptr->sample[2];
168 *snd_out++ = painted_ptr->sample[3];
171 else if (nchannels == 2) // 2.0 stereo
173 for (i = 0; i < nbframes; i++, painted_ptr++)
175 *snd_out++ = painted_ptr->sample[0];
176 *snd_out++ = painted_ptr->sample[1];
179 else if (nchannels == 1) // 1.0 mono
181 for (i = 0; i < nbframes; i++, painted_ptr++)
183 *snd_out++ = painted_ptr->sample[0];
187 // noise is really really annoying
189 memset(rb_ptr, 0, nbframes * nchannels * width);
191 else if (width == 2) // 16bit
193 short *snd_out = (short*)rb_ptr;
194 if (nchannels == 8) // 7.1 surround
196 for (i = 0;i < nbframes;i++, painted_ptr++)
198 val = (int)(painted_ptr->sample[0] * 32768.0f);*snd_out++ = bound(-32768, val, 32767);
199 val = (int)(painted_ptr->sample[1] * 32768.0f);*snd_out++ = bound(-32768, val, 32767);
200 val = (int)(painted_ptr->sample[2] * 32768.0f);*snd_out++ = bound(-32768, val, 32767);
201 val = (int)(painted_ptr->sample[3] * 32768.0f);*snd_out++ = bound(-32768, val, 32767);
202 val = (int)(painted_ptr->sample[4] * 32768.0f);*snd_out++ = bound(-32768, val, 32767);
203 val = (int)(painted_ptr->sample[5] * 32768.0f);*snd_out++ = bound(-32768, val, 32767);
204 val = (int)(painted_ptr->sample[6] * 32768.0f);*snd_out++ = bound(-32768, val, 32767);
205 val = (int)(painted_ptr->sample[7] * 32768.0f);*snd_out++ = bound(-32768, val, 32767);
208 else if (nchannels == 6) // 5.1 surround
210 for (i = 0; i < nbframes; i++, painted_ptr++)
212 val = (int)(painted_ptr->sample[0] * 32768.0f);*snd_out++ = bound(-32768, val, 32767);
213 val = (int)(painted_ptr->sample[1] * 32768.0f);*snd_out++ = bound(-32768, val, 32767);
214 val = (int)(painted_ptr->sample[2] * 32768.0f);*snd_out++ = bound(-32768, val, 32767);
215 val = (int)(painted_ptr->sample[3] * 32768.0f);*snd_out++ = bound(-32768, val, 32767);
216 val = (int)(painted_ptr->sample[4] * 32768.0f);*snd_out++ = bound(-32768, val, 32767);
217 val = (int)(painted_ptr->sample[5] * 32768.0f);*snd_out++ = bound(-32768, val, 32767);
220 else if (nchannels == 4) // 4.0 surround
222 for (i = 0; i < nbframes; i++, painted_ptr++)
224 val = (int)(painted_ptr->sample[0] * 32768.0f);*snd_out++ = bound(-32768, val, 32767);
225 val = (int)(painted_ptr->sample[1] * 32768.0f);*snd_out++ = bound(-32768, val, 32767);
226 val = (int)(painted_ptr->sample[2] * 32768.0f);*snd_out++ = bound(-32768, val, 32767);
227 val = (int)(painted_ptr->sample[3] * 32768.0f);*snd_out++ = bound(-32768, val, 32767);
230 else if (nchannels == 2) // 2.0 stereo
232 for (i = 0; i < nbframes; i++, painted_ptr++)
234 val = (int)(painted_ptr->sample[0] * 32768.0f);*snd_out++ = bound(-32768, val, 32767);
235 val = (int)(painted_ptr->sample[1] * 32768.0f);*snd_out++ = bound(-32768, val, 32767);
238 else if (nchannels == 1) // 1.0 mono
240 for (i = 0; i < nbframes; i++, painted_ptr++)
242 val = (int)((painted_ptr->sample[0] + painted_ptr->sample[1]) * 16384.0f);*snd_out++ = bound(-32768, val, 32767);
246 // noise is really really annoying
248 memset(rb_ptr, 0, nbframes * nchannels * width);
252 unsigned char *snd_out = (unsigned char*)rb_ptr;
253 if (nchannels == 8) // 7.1 surround
255 for (i = 0; i < nbframes; i++, painted_ptr++)
257 val = (int)(painted_ptr->sample[0] * 128.0f) + 128; *snd_out++ = bound(0, val, 255);
258 val = (int)(painted_ptr->sample[1] * 128.0f) + 128; *snd_out++ = bound(0, val, 255);
259 val = (int)(painted_ptr->sample[2] * 128.0f) + 128; *snd_out++ = bound(0, val, 255);
260 val = (int)(painted_ptr->sample[3] * 128.0f) + 128; *snd_out++ = bound(0, val, 255);
261 val = (int)(painted_ptr->sample[4] * 128.0f) + 128; *snd_out++ = bound(0, val, 255);
262 val = (int)(painted_ptr->sample[5] * 128.0f) + 128; *snd_out++ = bound(0, val, 255);
263 val = (int)(painted_ptr->sample[6] * 128.0f) + 128; *snd_out++ = bound(0, val, 255);
264 val = (int)(painted_ptr->sample[7] * 128.0f) + 128; *snd_out++ = bound(0, val, 255);
267 else if (nchannels == 6) // 5.1 surround
269 for (i = 0; i < nbframes; i++, painted_ptr++)
271 val = (int)(painted_ptr->sample[0] * 128.0f) + 128; *snd_out++ = bound(0, val, 255);
272 val = (int)(painted_ptr->sample[1] * 128.0f) + 128; *snd_out++ = bound(0, val, 255);
273 val = (int)(painted_ptr->sample[2] * 128.0f) + 128; *snd_out++ = bound(0, val, 255);
274 val = (int)(painted_ptr->sample[3] * 128.0f) + 128; *snd_out++ = bound(0, val, 255);
275 val = (int)(painted_ptr->sample[4] * 128.0f) + 128; *snd_out++ = bound(0, val, 255);
276 val = (int)(painted_ptr->sample[5] * 128.0f) + 128; *snd_out++ = bound(0, val, 255);
279 else if (nchannels == 4) // 4.0 surround
281 for (i = 0; i < nbframes; i++, painted_ptr++)
283 val = (int)(painted_ptr->sample[0] * 128.0f) + 128; *snd_out++ = bound(0, val, 255);
284 val = (int)(painted_ptr->sample[1] * 128.0f) + 128; *snd_out++ = bound(0, val, 255);
285 val = (int)(painted_ptr->sample[2] * 128.0f) + 128; *snd_out++ = bound(0, val, 255);
286 val = (int)(painted_ptr->sample[3] * 128.0f) + 128; *snd_out++ = bound(0, val, 255);
289 else if (nchannels == 2) // 2.0 stereo
291 for (i = 0; i < nbframes; i++, painted_ptr++)
293 val = (int)(painted_ptr->sample[0] * 128.0f) + 128; *snd_out++ = bound(0, val, 255);
294 val = (int)(painted_ptr->sample[1] * 128.0f) + 128; *snd_out++ = bound(0, val, 255);
297 else if (nchannels == 1) // 1.0 mono
299 for (i = 0;i < nbframes;i++, painted_ptr++)
301 val = (int)((painted_ptr->sample[0] + painted_ptr->sample[1]) * 64.0f) + 128; *snd_out++ = bound(0, val, 255);
305 // noise is really really annoying
307 memset(rb_ptr, 128, nbframes * nchannels);
313 ===============================================================================
317 ===============================================================================
320 void S_MixToBuffer(void *stream, unsigned int bufferframes)
325 unsigned char *outbytes = (unsigned char *) stream;
327 portable_sampleframe_t *paint;
340 #define S_FETCHBUFFERSIZE 4096
341 float fetchsampleframes[S_FETCHBUFFERSIZE*2];
342 const float *fetchsampleframe;
343 float vol[SND_LISTENERS];
352 // mix as many times as needed to fill the requested buffer
355 // limit to the size of the paint buffer
356 totalmixframes = min(bufferframes, PAINTBUFFER_SIZE);
358 // clear the paint buffer
359 memset(paintbuffer, 0, totalmixframes * sizeof(paintbuffer[0]));
361 // paint in the channels.
362 // channels with zero volumes still advance in time but don't paint.
363 ch = channels; // cppcheck complains here but it is wrong, channels is a channel_t[MAX_CHANNELS] and not an int
364 for (channelindex = 0;channelindex < (int)total_channels;channelindex++, ch++)
369 if (!S_LoadSound (sfx, true))
371 if (ch->flags & CHANNELFLAG_PAUSED)
373 if (!sfx->total_length)
376 // copy the channel information to the stack for reference, otherwise the
377 // values might change during a mix if the spatializer is updating them
378 // (note: this still may get some old and some new values!)
380 speedd = ch->mixspeed * sfx->format.speed / snd_renderbuffer->format.speed;
381 for (i = 0;i < SND_LISTENERS;i++)
382 vol[i] = ch->volume[i];
384 // check total volume level, because we can skip some code on silent sounds but other code must still run (position updates mainly)
386 for (i = 0;i < SND_LISTENERS;i++)
389 switch(snd_renderbuffer->format.width)
392 silent = maxvol < (1.0f / (256.0f));
393 // so silent it has zero effect
396 silent = maxvol < (1.0f / (65536.0f));
397 // so silent it has zero effect
399 default: // floating point
400 silent = maxvol < 1.0e-13f;
401 // 130 dB is difference between hearing
402 // threshold and a jackhammer from
404 // therefore, anyone who turns up
405 // volume so much they notice this
406 // cutoff, likely already has their
407 // ear-drums blown out anyway.
411 // when doing prologic mixing, some channels invert one side
412 if (ch->prologic_invert == -1)
415 // get some sfx info in a consistent form
416 totallength = sfx->total_length;
417 loopstart = (int)sfx->loopstart < totallength ? (int)sfx->loopstart : ((ch->flags & CHANNELFLAG_FORCELOOP) ? 0 : totallength);
418 looping = loopstart < totallength;
420 // do the actual paint now (may skip work if silent)
423 for (wantframes = totalmixframes;wantframes > 0;posd += count * speedd, wantframes -= count)
425 // check if this is a delayed sound
428 // for a delayed sound we have to eat into the delay first
429 count = (int)floor(-posd / speedd) + 1;
430 count = bound(1, count, wantframes);
431 // let the for loop iterator apply the skip
435 // compute a fetch size that won't overflow our buffer
439 istartframe = (int)floor(posd);
440 iendframe = (int)floor(posd + (count-1) * speedd);
441 ilengthframes = count > 1 ? (iendframe - istartframe + 2) : 2;
442 if (ilengthframes <= S_FETCHBUFFERSIZE)
444 // reduce count by 25% and try again
448 // zero whole fetch buffer for safety
449 // (floating point noise from uninitialized memory = HORRIBLE)
450 // otherwise we would only need to clear the excess
452 memset(fetchsampleframes, 0, ilengthframes*sfx->format.channels*sizeof(fetchsampleframes[0]));
454 // if looping, do multiple fetches
458 fetch = min(ilengthframes - fetched, totallength - istartframe);
462 sfx->fetcher->getsamplesfloat(ch, sfx, istartframe, fetch, fetchsampleframes + fetched*sfx->format.channels);
463 istartframe += fetch;
466 if (istartframe == totallength && looping && fetched < ilengthframes)
468 // loop and fetch some more
469 posd += loopstart - totallength;
470 istartframe = loopstart;
478 // set up our fixedpoint resampling variables (float to int conversions are expensive so do not do one per sampleframe)
479 fetchsampleframe = fetchsampleframes;
480 indexfrac = (int)floor((posd - floor(posd)) * 65536.0);
481 indexfracstep = (int)floor(speedd * 65536.0);
484 if (sfx->format.channels == 2)
487 #if SND_LISTENERS != 8
488 #error the following code only supports up to 8 channels, update it
490 if (snd_speakerlayout.channels > 2)
493 for (i = 0;i < count;i++, paint++)
495 lerp[1] = indexfrac * (1.0f / 65536.0f);
496 lerp[0] = 1.0f - lerp[1];
497 sample[0] = fetchsampleframe[0] * lerp[0] + fetchsampleframe[2] * lerp[1];
498 sample[1] = fetchsampleframe[1] * lerp[0] + fetchsampleframe[3] * lerp[1];
499 sample[2] = (sample[0] + sample[1]) * 0.5f;
500 paint->sample[0] += sample[0] * vol[0];
501 paint->sample[1] += sample[1] * vol[1];
502 paint->sample[2] += sample[0] * vol[2];
503 paint->sample[3] += sample[1] * vol[3];
504 paint->sample[4] += sample[2] * vol[4];
505 paint->sample[5] += sample[2] * vol[5];
506 paint->sample[6] += sample[0] * vol[6];
507 paint->sample[7] += sample[1] * vol[7];
508 indexfrac += indexfracstep;
509 fetchsampleframe += 2 * (indexfrac >> 16);
516 for (i = 0;i < count;i++, paint++)
518 lerp[1] = indexfrac * (1.0f / 65536.0f);
519 lerp[0] = 1.0f - lerp[1];
520 sample[0] = fetchsampleframe[0] * lerp[0] + fetchsampleframe[2] * lerp[1];
521 sample[1] = fetchsampleframe[1] * lerp[0] + fetchsampleframe[3] * lerp[1];
522 paint->sample[0] += sample[0] * vol[0];
523 paint->sample[1] += sample[1] * vol[1];
524 indexfrac += indexfracstep;
525 fetchsampleframe += 2 * (indexfrac >> 16);
530 else if (sfx->format.channels == 1)
532 // most sounds are mono
533 #if SND_LISTENERS != 8
534 #error the following code only supports up to 8 channels, update it
536 if (snd_speakerlayout.channels > 2)
539 for (i = 0;i < count;i++, paint++)
541 lerp[1] = indexfrac * (1.0f / 65536.0f);
542 lerp[0] = 1.0f - lerp[1];
543 sample[0] = fetchsampleframe[0] * lerp[0] + fetchsampleframe[1] * lerp[1];
544 paint->sample[0] += sample[0] * vol[0];
545 paint->sample[1] += sample[0] * vol[1];
546 paint->sample[2] += sample[0] * vol[2];
547 paint->sample[3] += sample[0] * vol[3];
548 paint->sample[4] += sample[0] * vol[4];
549 paint->sample[5] += sample[0] * vol[5];
550 paint->sample[6] += sample[0] * vol[6];
551 paint->sample[7] += sample[0] * vol[7];
552 indexfrac += indexfracstep;
553 fetchsampleframe += (indexfrac >> 16);
560 for (i = 0;i < count;i++, paint++)
562 lerp[1] = indexfrac * (1.0f / 65536.0f);
563 lerp[0] = 1.0f - lerp[1];
564 sample[0] = fetchsampleframe[0] * lerp[0] + fetchsampleframe[1] * lerp[1];
565 paint->sample[0] += sample[0] * vol[0];
566 paint->sample[1] += sample[0] * vol[1];
567 indexfrac += indexfracstep;
568 fetchsampleframe += (indexfrac >> 16);
576 if (!looping && istartframe == totallength)
577 S_StopChannel(ch - channels, false, false);
580 S_SoftClipPaintBuffer(paintbuffer, totalmixframes, snd_renderbuffer->format.width, snd_renderbuffer->format.channels);
582 #ifdef CONFIG_VIDEO_CAPTURE
583 if (!snd_usethreadedmixing)
584 S_CaptureAVISound(paintbuffer, totalmixframes);
587 S_ConvertPaintBuffer(paintbuffer, outbytes, totalmixframes, snd_renderbuffer->format.width, snd_renderbuffer->format.channels);
589 // advance the output pointer
590 outbytes += totalmixframes * snd_renderbuffer->format.width * snd_renderbuffer->format.channels;
591 bufferframes -= totalmixframes;