Skip to content

A cross platform Audiobookshelf client for Android and iOS

Dart

Stop the loudness boost adding static on loud books

Barnabasea24b123ab69
diff --git a/packages/just_audio/android/src/main/java/com/ryanheise/just_audio/GainAudioProcessor.java b/packages/just_audio/android/src/main/java/com/ryanheise/just_audio/GainAudioProcessor.java
index a3ba3986..077fa92c 100644
--- a/packages/just_audio/android/src/main/java/com/ryanheise/just_audio/GainAudioProcessor.java
+++ b/packages/just_audio/android/src/main/java/com/ryanheise/just_audio/GainAudioProcessor.java
@@ -10,12 +10,23 @@ import java.nio.ByteBuffer;
* LoudnessEnhancer is a compressor that leaves narration already peaking near
* full scale almost untouched, and it lives on the audio session, so it dies
* with the activity and on phones whose effect stack is broken. This runs on
- * every sample regardless. Peaks are soft-clipped so a big boost distorts
- * gently instead of wrapping.
+ * every sample regardless. A lookahead limiter (PeakLimiter) keeps the boosted
+ * peaks under full scale. With the slider at zero the audio passes through
+ * untouched.
*/
public final class GainAudioProcessor extends BaseAudioProcessor {
+ private static final float GAIN_SMOOTH_SEC = 0.02f;
+
private static volatile float sGain = 1f;
+ private PeakLimiter limiter;
+ private float[] frameIn;
+ private float[] frameOut;
+ private boolean is16Bit;
+ private int frameBytes;
+ private float gain = 1f;
+ private float gainCoef;
+
/** Gain in millibels (100 mB = 1 dB); zero or less is unity. */
public static void setGainMb(int gainMb) {
sGain = gainMb <= 0 ? 1f : (float) Math.pow(10.0, gainMb / 2000.0);
@@ -31,33 +42,91 @@ public final class GainAudioProcessor extends BaseAudioProcessor {
return inputAudioFormat;
}
+ @Override
+ protected void onFlush() {
+ if (!isActive()) {
+ limiter = null;
+ return;
+ }
+ int channels = inputAudioFormat.channelCount;
+ int sampleRate = inputAudioFormat.sampleRate;
+ is16Bit = inputAudioFormat.encoding == C.ENCODING_PCM_16BIT;
+ frameBytes = channels * (is16Bit ? 2 : 4);
+ limiter = new PeakLimiter(sampleRate, channels);
+ frameIn = new float[channels];
+ frameOut = new float[channels];
+ gain = sGain;
+ gainCoef = (float) (1.0 - Math.exp(-1.0 / (GAIN_SMOOTH_SEC * sampleRate)));
+ }
+
+ @Override
+ protected void onReset() {
+ limiter = null;
+ }
+
@Override
public void queueInput(ByteBuffer inputBuffer) {
int remaining = inputBuffer.remaining();
if (remaining == 0) return;
- float gain = sGain;
- ByteBuffer output = replaceOutputBuffer(remaining);
- if (gain == 1f) {
+ float target = sGain;
+ // At zero the limiter stops asking for reduction and only recovers, so
+ // a hot master is not held down with the boost off.
+ boolean unity = target == 1f && gain == 1f;
+ if (limiter == null || (unity && limiter.isResting())) {
+ // Slider at zero and the limiter has let go. Anything it still
+ // holds goes out first so no audio is dropped.
+ int heldBytes = limiter == null ? 0 : limiter.held() * frameBytes;
+ ByteBuffer output = replaceOutputBuffer(heldBytes + remaining);
+ if (heldBytes > 0) drainInto(output);
output.put(inputBuffer);
- } else if (inputAudioFormat.encoding == C.ENCODING_PCM_16BIT) {
- while (inputBuffer.remaining() >= 2) {
- float sample = softClip(inputBuffer.getShort() * gain / 32768f);
- output.putShort((short) Math.round(sample * 32767f));
- }
- } else {
- while (inputBuffer.remaining() >= 4) {
- output.putFloat(softClip(inputBuffer.getFloat() * gain));
+ output.flip();
+ return;
+ }
+ ByteBuffer output = replaceOutputBuffer(remaining);
+ int channels = frameIn.length;
+ while (inputBuffer.remaining() >= frameBytes) {
+ // Ease toward the slider so dragging it never steps the level.
+ gain += (target - gain) * gainCoef;
+ if (Math.abs(target - gain) < 1e-4f) gain = target;
+ for (int c = 0; c < channels; c++) {
+ float sample = is16Bit ? inputBuffer.getShort() / 32768f : inputBuffer.getFloat();
+ frameIn[c] = sample * gain;
}
+ if (limiter.push(frameIn, frameOut, !unity)) writeFrame(output);
}
+ inputBuffer.position(inputBuffer.limit());
output.flip();
}
- // Linear up to 0.8, then eases into the ceiling. Continuous with unit
- // slope at the knee, never reaches 1.0.
- private static float softClip(float x) {
- float magnitude = Math.abs(x);
- if (magnitude <= 0.8f) return x;
- float y = 0.8f + 0.2f * (float) Math.tanh((magnitude - 0.8f) / 0.2f);
- return x < 0 ? -y : y;
+ // The limiter runs 5 ms behind, so the end of the audio is still inside
+ // it when the input stops. Same shape as media3's TrimmingAudioProcessor.
+ @Override
+ public ByteBuffer getOutput() {
+ if (super.isEnded() && limiter != null && limiter.held() > 0) {
+ ByteBuffer output = replaceOutputBuffer(limiter.held() * frameBytes);
+ drainInto(output);
+ output.flip();
+ }
+ return super.getOutput();
+ }
+
+ @Override
+ public boolean isEnded() {
+ return super.isEnded() && (limiter == null || limiter.held() == 0);
+ }
+
+ private void drainInto(ByteBuffer output) {
+ while (limiter.drain(frameOut)) writeFrame(output);
+ }
+
+ private void writeFrame(ByteBuffer output) {
+ for (float sample : frameOut) {
+ if (is16Bit) {
+ int value = Math.round(sample * 32767f);
+ output.putShort((short) Math.max(-32768, Math.min(32767, value)));
+ } else {
+ output.putFloat(Math.max(-1f, Math.min(1f, sample)));
+ }
+ }
}
}
diff --git a/packages/just_audio/android/src/main/java/com/ryanheise/just_audio/PeakLimiter.java b/packages/just_audio/android/src/main/java/com/ryanheise/just_audio/PeakLimiter.java
new file mode 100644
index 00000000..6a1e2a47
--- /dev/null
+++ b/packages/just_audio/android/src/main/java/com/ryanheise/just_audio/PeakLimiter.java
@@ -0,0 +1,152 @@
+package com.ryanheise.just_audio;
+
+import java.util.Arrays;
+
+/**
+ * Absorb patch: lookahead peak limiter behind the loudness gain. The audio is
+ * held back 5 ms so the gain can ease down before a peak comes out and recover
+ * slowly after it. Clipping the boosted samples one by one squared off every
+ * loud syllable, which came out as static on a book mastered near full scale.
+ * All channels share one gain so the stereo image stays put. No Android types
+ * in here so it compiles and tests on its own.
+ */
+final class PeakLimiter {
+ static final float CEILING = 0.891f; // -1 dBFS
+ private static final float LOOKAHEAD_SEC = 0.005f;
+ private static final float RELEASE_SEC = 0.2f;
+
+ private final int channels;
+ private final int lookahead;
+ private final double releaseCoef;
+ private final float[] delay;
+ private final float[] smoothed;
+ private final float[] minValue;
+ private final long[] minFrame;
+ private final float[] silence;
+
+ private int delayPos;
+ private int filled;
+ private int held;
+ private int minHead;
+ private int minSize;
+ private long clock;
+ // A double: in a float the last of the recovery is smaller than the
+ // rounding step near 1.0 and the gain stalls just short of it.
+ private double envelope;
+ private int smoothedPos;
+ private double smoothedSum;
+ private float lastGain;
+
+ PeakLimiter(int sampleRate, int channels) {
+ this.channels = channels;
+ lookahead = Math.max(1, Math.round(sampleRate * LOOKAHEAD_SEC));
+ releaseCoef = 1.0 - Math.exp(-1.0 / (RELEASE_SEC * sampleRate));
+ delay = new float[lookahead * channels];
+ smoothed = new float[lookahead];
+ minValue = new float[lookahead + 2];
+ minFrame = new long[lookahead + 2];
+ silence = new float[channels];
+ reset();
+ }
+
+ void reset() {
+ delayPos = 0;
+ filled = 0;
+ held = 0;
+ minHead = 0;
+ minSize = 0;
+ clock = 0;
+ envelope = 1.0;
+ Arrays.fill(smoothed, 1f);
+ smoothedPos = 0;
+ smoothedSum = lookahead;
+ lastGain = 1f;
+ }
+
+ /** Frames taken in and not yet given back. */
+ int held() {
+ return held;
+ }
+
+ /** True once the gain has recovered all the way, so stepping out of the limiter is seamless. */
+ boolean isResting() {
+ return lastGain >= 0.9999f;
+ }
+
+ /**
+ * Takes one frame and writes the frame leaving the delay line to out.
+ * Returns false while the line is still filling, when out is untouched.
+ * With limit off no new reduction is asked for and the gain just recovers.
+ */
+ boolean push(float[] frame, float[] out, boolean limit) {
+ held++;
+ boolean emitted = step(frame, out, limit);
+ if (emitted) held--;
+ return emitted;
+ }
+
+ /** Gives back one held frame at the end of the audio; false once empty. */
+ boolean drain(float[] out) {
+ while (held > 0) {
+ if (step(silence, out, true)) {
+ if (--held == 0) reset();
+ return true;
+ }
+ }
+ return false;
+ }
+
+ private boolean step(float[] frame, float[] out, boolean limit) {
+ float peak = 0f;
+ if (limit) {
+ for (int c = 0; c < channels; c++) {
+ float magnitude = Math.abs(frame[c]);
+ if (magnitude > peak) peak = magnitude;
+ }
+ }
+ float gain = advance(peak);
+ lastGain = gain;
+ boolean emit = filled == lookahead;
+ int base = delayPos * channels;
+ for (int c = 0; c < channels; c++) {
+ if (emit) out[c] = delay[base + c] * gain;
+ delay[base + c] = frame[c];
+ }
+ if (++delayPos == lookahead) delayPos = 0;
+ if (!emit) filled++;
+ return emit;
+ }
+
+ // The gain for the frame now leaving the delay line. The lowest gain any
+ // frame still in the line needs is held, released slowly, then averaged
+ // over the lookahead, so the drop is a ramp that has always finished by
+ // the time the peak that asked for it comes out.
+ private float advance(float peak) {
+ float need = peak > CEILING ? CEILING / peak : 1f;
+ int cap = minValue.length;
+ while (minSize > 0 && minValue[(minHead + minSize - 1) % cap] >= need) minSize--;
+ int tail = (minHead + minSize) % cap;
+ minValue[tail] = need;
+ minFrame[tail] = clock;
+ minSize++;
+ if (minFrame[minHead] < clock - lookahead) {
+ minHead = (minHead + 1) % cap;
+ minSize--;
+ }
+ clock++;
+ float floor = minValue[minHead];
+ envelope = floor < envelope ? floor : envelope + (floor - envelope) * releaseCoef;
+
+ float stored = (float) envelope;
+ smoothedSum += stored - smoothed[smoothedPos];
+ smoothed[smoothedPos] = stored;
+ if (++smoothedPos == lookahead) {
+ smoothedPos = 0;
+ // Start the running sum over each lap so rounding never builds up.
+ double exact = 0;
+ for (float value : smoothed) exact += value;
+ smoothedSum = exact;
+ }
+ return (float) (smoothedSum / lookahead);
+ }
+}