A cross platform Audiobookshelf client for Android and iOS
Dart
Run the equalizer and bass boost inside the player on Android so they work on every phone
Barnabas226dd6a2d14a
diff --git a/android/app/src/main/kotlin/com/barnabas/absorb/MainActivity.kt b/android/app/src/main/kotlin/com/barnabas/absorb/MainActivity.ktindex 58e8ded6..2ed4bb6c 100644--- a/android/app/src/main/kotlin/com/barnabas/absorb/MainActivity.kt+++ b/android/app/src/main/kotlin/com/barnabas/absorb/MainActivity.kt@@ -9,8 +9,6 @@ import android.provider.MediaStoreimport androidx.documentfile.provider.DocumentFileimport java.io.Fileimport java.io.FileInputStream-import android.media.audiofx.BassBoost-import android.media.audiofx.Equalizerimport android.media.audiofx.Virtualizerimport android.os.Buildimport android.os.Bundle@@ -25,6 +23,7 @@ import android.view.WindowManagerimport com.ryanheise.audioservice.AudioServiceimport com.ryanheise.audioservice.AudioServiceActivityimport com.ryanheise.audioservice.AudioServicePlugin+import com.ryanheise.just_audio.EqControllerimport com.ryanheise.just_audio.GainControllerimport com.ryanheise.just_audio.MonoControllerimport es.antonborri.home_widget.HomeWidgetLaunchIntent@@ -42,19 +41,14 @@ class MainActivity : AudioServiceActivity() {private var volumeKeysChannel: MethodChannel? = nullprivate var watchVolumeKeys = false- private var equalizer: Equalizer? = null- private var bassBoost: BassBoost? = null+ // The band EQ, bass boost and loudness run inside the player's audio sink+ // (EqAudioProcessor, GainAudioProcessor), not as session effects, so they+ // survive the activity and work on phones whose effect stack is broken.+ // Only the virtualizer is still a session effect.private var virtualizer: Virtualizer? = nullprivate var currentSessionId: Int = 0private var eqEnabled: Boolean = false- // Loudness is sample gain in the player's audio sink (GainAudioProcessor),- // not a session effect, so it survives the activity and effect teardown.private var eqLoudnessGainMb: Int = 0- // Some devices (e.g. older Samsung on Android 9) have a broken audio-effect- // HAL that fails to initialize. Constructing AudioEffects against it during- // playback can crash the process natively, which Kotlin can't catch. Once- // init proves the engine is unavailable, skip attaching native effects.- private var effectsAvailable: Boolean = true// Android brings a task back from a dead process by recreating its root// activity with the intent that first created it. When that was the@@ -389,51 +383,26 @@ class MainActivity : AudioServiceActivity() {}private fun handleInit(result: MethodChannel.Result) {- try {- val tempEq = Equalizer(0, 0)- val numBands = tempEq.numberOfBands.toInt()- val frequencies = mutableListOf<Int>()- for (i in 0 until numBands) {- frequencies.add(tempEq.getCenterFreq(i.toShort()) / 1000)- }- val bandRange = tempEq.bandLevelRange- val minLevel = bandRange[0] / 100.0- val maxLevel = bandRange[1] / 100.0- tempEq.release()-- effectsAvailable = true- Log.d(TAG, "init: $numBands bands, frequencies=$frequencies, range=[$minLevel, $maxLevel]dB")- result.success(mapOf(- "bands" to numBands,- "frequencies" to frequencies,- "minLevel" to minLevel,- "maxLevel" to maxLevel- ))- } catch (e: Exception) {- effectsAvailable = false- Log.d(TAG, "EQ probe deferred until playback: ${e.message}")- result.success(mapOf(- "bands" to 5,- "frequencies" to listOf(60, 230, 910, 3600, 14000),- "minLevel" to -15.0,- "maxLevel" to 15.0,- "deferred" to true- ))- }+ // The bands live in the player's audio sink and are the same on every+ // phone, so there is nothing to probe.+ result.success(mapOf(+ "bands" to EqController.BANDS,+ "frequencies" to EqController.BAND_HZ.toList(),+ "minLevel" to -15.0,+ "maxLevel" to 15.0+ ))}private fun handleAttachSession(sessionId: Int, result: MethodChannel.Result) {try {- Log.d(TAG, "attachSession: $sessionId (previous: $currentSessionId, haveEffects=${equalizer != null})")+ Log.d(TAG, "attachSession: $sessionId (previous: $currentSessionId, haveVirtualizer=${virtualizer != null})")// ExoPlayer reuses one audio session id across books. Re-attaching to- // the SAME live session must reuse the effects already bound to it —- // building a second Equalizer/BassBoost/etc. on that session without- // releasing the old ones leaks native effects every book switch and- // can cost our instance control of the engine, so EQ silently stops- // affecting the sound until the process restarts. Only tear down and- // rebuild when the session actually changed. Dart re-pushes the band /- // enabled / effect values right after this call in either case.- if (sessionId != 0 && sessionId == currentSessionId && equalizer != null) {+ // the SAME live session must reuse the effect already bound to it:+ // building a second Virtualizer on that session without releasing+ // the old one leaks native effects every book switch. Only tear down+ // and rebuild when the session actually changed. Dart re-pushes the+ // effect values right after this call in either case.+ if (sessionId != 0 && sessionId == currentSessionId && virtualizer != null) {result.success(true)return}@@ -445,29 +414,6 @@ class MainActivity : AudioServiceActivity() {return}- // init() probes Equalizer(0, 0) on the global output mix, which some- // devices/HAL states reject with a catchable Error -3 right after a- // process start (e.g. just after an app update) — which used to leave- // effectsAvailable=false and EQ silent until a force-restart. A real- // per-session effect usually still works, so build it here regardless- // of the probe result and let this construction decide availability.- // (A device that hard-crashes on construction would have crashed at- // init already, so reaching this point means construction is catch-safe.)- try {- equalizer = Equalizer(0, sessionId).apply { enabled = false }- effectsAvailable = true- } catch (e: Exception) {- effectsAvailable = false- equalizer = null- Log.w(TAG, "attachSession: Equalizer unavailable on session $sessionId: ${e.message}")- result.success(true)- return- }- bassBoost = try {- BassBoost(0, sessionId).apply { enabled = false }- } catch (e: Exception) {- Log.w(TAG, "BassBoost not supported: ${e.message}"); null- }virtualizer = try {Virtualizer(0, sessionId).apply { enabled = false }} catch (e: Exception) {@@ -487,7 +433,7 @@ class MainActivity : AudioServiceActivity() {// loudness are independent and track their own values, so they// keep working with the equalizer off.eqEnabled = enabled- equalizer?.enabled = enabled+ EqController.setEnabled(enabled)result.success(true)} catch (e: Exception) {result.error("EQ_ERROR", e.message, null)@@ -496,7 +442,7 @@ class MainActivity : AudioServiceActivity() {private fun handleSetBand(band: Int, level: Int, result: MethodChannel.Result) {try {- equalizer?.setBandLevel(band.toShort(), level.toShort())+ EqController.setBandMb(band, level)result.success(true)} catch (e: Exception) {result.error("EQ_ERROR", e.message, null)@@ -505,10 +451,7 @@ class MainActivity : AudioServiceActivity() {private fun handleSetBassBoost(strength: Int, result: MethodChannel.Result) {try {- val s = strength.toShort().coerceIn(0, 1000)- bassBoost?.setStrength(s)- // Independent of the band-EQ master: on when there's something to do.- bassBoost?.enabled = s > 0+ EqController.setBassStrength(strength)result.success(true)} catch (e: Exception) {result.error("EQ_ERROR", e.message, null)@@ -537,13 +480,8 @@ class MainActivity : AudioServiceActivity() {}private fun releaseEffects() {- try { equalizer?.release() } catch (_: Exception) {}- try { bassBoost?.release() } catch (_: Exception) {}try { virtualizer?.release() } catch (_: Exception) {}- equalizer = null- bassBoost = nullvirtualizer = null- eqEnabled = false}private fun isBluetoothAudioConnected(): Boolean {diff --git a/packages/just_audio/android/src/main/java/com/ryanheise/just_audio/AudioPlayer.java b/packages/just_audio/android/src/main/java/com/ryanheise/just_audio/AudioPlayer.javaindex fc9d9b04..814ed5af 100644--- a/packages/just_audio/android/src/main/java/com/ryanheise/just_audio/AudioPlayer.java+++ b/packages/just_audio/android/src/main/java/com/ryanheise/just_audio/AudioPlayer.java@@ -958,11 +958,11 @@ public class AudioPlayer implements MethodCallHandler, Player.Listener, MetadataContext context,boolean enableFloatOutput,boolean enableAudioTrackPlaybackParams) {- Log.i(TAG, "PATCH ACTIVE: buildAudioSink with SonicAudioProcessor + MonoProcessor + GainAudioProcessor, AudioTrack speed DISABLED");+ Log.i(TAG, "PATCH ACTIVE: buildAudioSink with SonicAudioProcessor + MonoProcessor + EqAudioProcessor + GainAudioProcessor, AudioTrack speed DISABLED");return new DefaultAudioSink.Builder(context).setEnableFloatOutput(enableFloatOutput).setEnableAudioTrackPlaybackParams(false)- .setAudioProcessors(new androidx.media3.common.audio.AudioProcessor[]{sonicAudioProcessor, monoProcessor, new GainAudioProcessor()})+ .setAudioProcessors(new androidx.media3.common.audio.AudioProcessor[]{sonicAudioProcessor, monoProcessor, new EqAudioProcessor(), new GainAudioProcessor()}).build();}};diff --git a/packages/just_audio/android/src/main/java/com/ryanheise/just_audio/EqAudioProcessor.java b/packages/just_audio/android/src/main/java/com/ryanheise/just_audio/EqAudioProcessor.javanew file mode 100644index 00000000..308645cd--- /dev/null+++ b/packages/just_audio/android/src/main/java/com/ryanheise/just_audio/EqAudioProcessor.java@@ -0,0 +1,181 @@+package com.ryanheise.just_audio;++import androidx.media3.common.C;+import androidx.media3.common.audio.BaseAudioProcessor;+import java.nio.ByteBuffer;++/**+ * Absorb patch: the five-band equalizer and the bass boost inside the+ * ExoPlayer audio sink, the same biquads the iOS tap runs. Android's session+ * Equalizer and BassBoost die with the activity and on phones whose effect+ * stack is broken; this runs on every sample regardless. The bands sit where+ * the platform effect reports them on most phones, so saved presets carry+ * over. With the bands off and no bass boost the audio passes through+ * untouched.+ */+public final class EqAudioProcessor extends BaseAudioProcessor {+ public static final int BANDS = 5;+ public static final int[] BAND_HZ = {60, 230, 910, 3600, 14000};+ private static final float BAND_Q = 1.4f;+ private static final float BASS_HZ = 120f;+ private static final float BASS_MAX_DB = 12f;+ private static final int BASS = BANDS;++ private static volatile boolean sEnabled;+ private static final int[] sBandMb = new int[BANDS];+ private static volatile int sBassStrength;+ private static volatile int sVersion;++ public static void setEnabled(boolean enabled) {+ sEnabled = enabled;+ sVersion++;+ }++ /** One band's gain in millibels (100 mB = 1 dB). */+ public static void setBandMb(int band, int mb) {+ if (band < 0 || band >= BANDS) return;+ sBandMb[band] = Math.max(-1500, Math.min(1500, mb));+ sVersion++;+ }++ /** Bass boost strength 0-1000, mapped to a low shelf of 0-12 dB at 120 Hz. */+ public static void setBassStrength(int strength) {+ sBassStrength = Math.max(0, Math.min(1000, strength));+ sVersion++;+ }++ private final float[][] coef = new float[BANDS + 1][5];+ private float[][][] delay;+ private int version = -1;+ private boolean bandsOn;+ private boolean bassOn;+ private boolean is16Bit;+ private int channels;+ private int sampleRate;+ private int frameBytes;++ @Override+ public AudioFormat onConfigure(AudioFormat inputAudioFormat)+ throws UnhandledAudioFormatException {+ int encoding = inputAudioFormat.encoding;+ if (encoding != C.ENCODING_PCM_16BIT && encoding != C.ENCODING_PCM_FLOAT) {+ throw new UnhandledAudioFormatException(inputAudioFormat);+ }+ return inputAudioFormat;+ }++ @Override+ protected void onFlush() {+ if (!isActive()) return;+ channels = inputAudioFormat.channelCount;+ sampleRate = inputAudioFormat.sampleRate;+ is16Bit = inputAudioFormat.encoding == C.ENCODING_PCM_16BIT;+ frameBytes = channels * (is16Bit ? 2 : 4);+ delay = new float[BANDS + 1][channels][4];+ version = -1;+ }++ @Override+ public void queueInput(ByteBuffer inputBuffer) {+ int remaining = inputBuffer.remaining();+ if (remaining == 0) return;+ if (version != sVersion) rebuild();+ ByteBuffer output = replaceOutputBuffer(remaining);+ if (!bandsOn && !bassOn) {+ output.put(inputBuffer);+ output.flip();+ return;+ }+ while (inputBuffer.remaining() >= frameBytes) {+ for (int c = 0; c < channels; c++) {+ float sample = is16Bit ? inputBuffer.getShort() / 32768f : inputBuffer.getFloat();+ if (bandsOn) {+ for (int band = 0; band < BANDS; band++) {+ sample = biquad(coef[band], delay[band][c], sample);+ }+ }+ if (bassOn) sample = biquad(coef[BASS], delay[BASS][c], sample);+ if (is16Bit) {+ int value = Math.round(sample * 32767f);+ output.putShort((short) Math.max(-32768, Math.min(32767, value)));+ } else {+ output.putFloat(sample);+ }+ }+ }+ inputBuffer.position(inputBuffer.limit());+ output.flip();+ }++ private static float biquad(float[] c, float[] d, float x) {+ float y = c[0] * x + c[1] * d[0] + c[2] * d[1] - c[3] * d[2] - c[4] * d[3];+ d[1] = d[0];+ d[0] = x;+ d[3] = d[2];+ d[2] = y;+ return y;+ }++ private void rebuild() {+ version = sVersion;+ // A peaking biquad at or above Nyquist puts its poles outside the unit+ // circle and the output turns to NaN, so such a band is left flat.+ // Low-bitrate AAC decodes at 22050 Hz, where the 14 kHz band trips it.+ float maxHz = sampleRate * 0.5f * 0.95f;+ boolean any = false;+ for (int band = 0; band < BANDS; band++) {+ float db = sEnabled ? sBandMb[band] / 100f : 0f;+ if (db != 0f) any = true;+ if (db == 0f || BAND_HZ[band] >= maxHz) {+ unity(coef[band]);+ } else {+ peaking(coef[band], BAND_HZ[band], db, BAND_Q, sampleRate);+ }+ }+ bandsOn = sEnabled && any;+ float bassDb = sBassStrength / 1000f * BASS_MAX_DB;+ bassOn = bassDb > 0f && BASS_HZ < maxHz;+ if (bassOn) {+ lowShelf(coef[BASS], BASS_HZ, bassDb, sampleRate);+ } else {+ unity(coef[BASS]);+ }+ }++ private static void unity(float[] c) {+ c[0] = 1f;+ c[1] = 0f;+ c[2] = 0f;+ c[3] = 0f;+ c[4] = 0f;+ }++ private static void peaking(float[] c, float f0, float dbGain, float q, float fs) {+ double a = Math.pow(10.0, dbGain / 40.0);+ double w0 = 2.0 * Math.PI * f0 / fs;+ double sinw = Math.sin(w0);+ double cosw = Math.cos(w0);+ double alpha = sinw / (2.0 * q);+ double a0 = 1.0 + alpha / a;+ c[0] = (float) ((1.0 + alpha * a) / a0);+ c[1] = (float) ((-2.0 * cosw) / a0);+ c[2] = (float) ((1.0 - alpha * a) / a0);+ c[3] = (float) ((-2.0 * cosw) / a0);+ c[4] = (float) ((1.0 - alpha / a) / a0);+ }++ private static void lowShelf(float[] c, float f0, float dbGain, float fs) {+ double a = Math.pow(10.0, dbGain / 40.0);+ double w0 = 2.0 * Math.PI * f0 / fs;+ double sinw = Math.sin(w0);+ double cosw = Math.cos(w0);+ double alpha = sinw / 2.0 * Math.sqrt((a + 1.0 / a) * (1.0 / 0.7071 - 1.0) + 2.0);+ double sqrtA2alpha = 2.0 * Math.sqrt(a) * alpha;+ double a0 = (a + 1.0) + (a - 1.0) * cosw + sqrtA2alpha;+ c[0] = (float) ((a * ((a + 1.0) - (a - 1.0) * cosw + sqrtA2alpha)) / a0);+ c[1] = (float) ((2.0 * a * ((a - 1.0) - (a + 1.0) * cosw)) / a0);+ c[2] = (float) ((a * ((a + 1.0) - (a - 1.0) * cosw - sqrtA2alpha)) / a0);+ c[3] = (float) ((-2.0 * ((a - 1.0) + (a + 1.0) * cosw)) / a0);+ c[4] = (float) (((a + 1.0) + (a - 1.0) * cosw - sqrtA2alpha) / a0);+ }+}diff --git a/packages/just_audio/android/src/main/java/com/ryanheise/just_audio/EqController.java b/packages/just_audio/android/src/main/java/com/ryanheise/just_audio/EqController.javanew file mode 100644index 00000000..9e3587e9--- /dev/null+++ b/packages/just_audio/android/src/main/java/com/ryanheise/just_audio/EqController.java@@ -0,0 +1,22 @@+package com.ryanheise.just_audio;++/**+ * Static bridge so MainActivity can drive the in-sink equalizer without+ * seeing media3 types on its classpath (same reason GainController exists).+ */+public class EqController {+ public static final int BANDS = EqAudioProcessor.BANDS;+ public static final int[] BAND_HZ = EqAudioProcessor.BAND_HZ;++ public static void setEnabled(boolean enabled) {+ EqAudioProcessor.setEnabled(enabled);+ }++ public static void setBandMb(int band, int mb) {+ EqAudioProcessor.setBandMb(band, mb);+ }++ public static void setBassStrength(int strength) {+ EqAudioProcessor.setBassStrength(strength);+ }+}