- int outrate = inrate * delta / -(CLOCK_FREQ * 10);
- if (16 * abs(outrate) < limit)
- outrate = inrate; /* favor native rate to avoid resampling */
- else if (outrate > limit)
+ int adj = sys->rate * (delta + change) / (CLOCK_FREQ * ADJUST_FACTOR);
+
+ /* This avoids too fast rate variation. They sound ugly as hell and they
+ * make the algorithm unstable (e.g. oscillation around inrate). */
+ if (adj > +ADJUST_MAX)
+ adj = +ADJUST_MAX;
+ if (adj < -ADJUST_MAX)
+ adj = -ADJUST_MAX;
+
+ if (abs(adj) < (inrate >> 10))
+ adj = 0; /* favor native rate to avoid resampling */
+
+ /* This keeps the effective rate within specified range
+ * (+/-AOUT_MAX_RESAMPLING% - see <vlc_aout.h>) of the nominal rate. */
+ unsigned outrate = inrate - adj;
+ const int limit = inrate * AOUT_MAX_RESAMPLING / 100;
+
+ if (outrate > inrate + limit)