]> git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - snd_mix.c
round the pitchchange setting to the nearest value
[xonotic/darkplaces.git] / snd_mix.c
index e01ca0ee7d5b5b705ae17653bc79f7967cd353cc..67898912149e19dcd3d0cd48314245569a7da96c 100644 (file)
--- a/snd_mix.c
+++ b/snd_mix.c
@@ -201,8 +201,8 @@ void S_MixToBuffer(void *stream, unsigned int bufferframes)
        int loopstart;
        int indexfrac;
        int indexfracstep;
-       const int fetchsampleframesmax = 1024;
-       float fetchsampleframes[1024*2];
+#define S_FETCHBUFFERSIZE 4096
+       float fetchsampleframes[S_FETCHBUFFERSIZE*2];
        const float *fetchsampleframe;
        float vol[SND_LISTENERS];
        float lerp[2];
@@ -246,6 +246,7 @@ void S_MixToBuffer(void *stream, unsigned int bufferframes)
                                vol[i] = ch->volume[i];
 
                        // check total volume level, because we can skip some code on silent sounds but other code must still run (position updates mainly)
+                       sum = 0;
                        for (i = 0;i < SND_LISTENERS;i++)
                                sum += vol[i]*vol[i];
                        silent = sum < 0.001f;
@@ -262,33 +263,30 @@ void S_MixToBuffer(void *stream, unsigned int bufferframes)
                        // do the actual paint now (may skip work if silent)
                        paint = paintbuffer;
                        wantframes = totalmixframes;
-                       while (wantframes > 0)
+                       istartframe = 0;
+                       for (wantframes = totalmixframes;wantframes > 0;posd += count * speedd, wantframes -= count)
                        {
-                               // mix full output length (if possible)
-                               count = wantframes;
+                               // check if this is a delayed sound
                                if (posd < 0)
                                {
                                        // for a delayed sound we have to eat into the delay first
-                                       count = (int)-posd;
-                                       if (count > wantframes)
-                                               count = wantframes;
-                                       posd += count;
-                                       wantframes -= count;
+                                       count = (int)floor(-posd / speedd) + 1;
+                                       count = bound(1, count, wantframes);
+                                       // let the for loop iterator apply the skip
                                        continue;
                                }
 
-                               // get fetch size
-                               istartframe = (int)floor(posd);
-                               iendframe = (int)floor(posd + count * speedd);
-                               ilengthframes = iendframe + 2 - istartframe;
-                               // don't overflow fetch buffer
-                               while (ilengthframes > fetchsampleframesmax)
+                               // compute a fetch size that won't overflow our buffer
+                               count = wantframes;
+                               for (;;)
                                {
-                                       count /= 2;
-                                       iendframe = (int)floor(posd + count * speedd);
-                                       ilengthframes = iendframe + 2 - istartframe;
-                                       if (count < 2)
-                                               ilengthframes = 2;
+                                       istartframe = (int)floor(posd);
+                                       iendframe = (int)floor(posd + (count-1) * speedd);
+                                       ilengthframes = count > 1 ? (iendframe - istartframe + 2) : 2;
+                                       if (ilengthframes <= S_FETCHBUFFERSIZE)
+                                               break;
+                                       // reduce count by 25% and try again
+                                       count -= count >> 2;
                                }
 
                                // zero whole fetch buffer for safety
@@ -301,7 +299,7 @@ void S_MixToBuffer(void *stream, unsigned int bufferframes)
                                fetched = 0;
                                for (;;)
                                {
-                                       fetch = min(ilengthframes, totallength - istartframe);
+                                       fetch = min(ilengthframes - fetched, totallength - istartframe);
                                        if (fetch > 0)
                                        {
                                                if (!silent)
@@ -316,7 +314,9 @@ void S_MixToBuffer(void *stream, unsigned int bufferframes)
                                                istartframe = loopstart;
                                        }
                                        else
+                                       {
                                                break;
+                                       }
                                }
 
                                // set up our fixedpoint resampling variables (float to int conversions are expensive so do not do one per sampleframe)
@@ -415,8 +415,6 @@ void S_MixToBuffer(void *stream, unsigned int bufferframes)
                                                }
                                        }
                                }
-                               posd += count * speedd;
-                               wantframes -= count;
                        }
                        ch->position = posd;
                        if (!looping && istartframe == totallength)