Skip to content

A cross platform Audiobookshelf client for Android and iOS

Dart

Stop transcription crashing phones with older processors like the Boox Palma

Barnabas095629002263
diff --git a/.gitignore b/.gitignore
index 3b78b558..030ded68 100644
--- a/.gitignore
+++ b/.gitignore
@@ -81,6 +81,8 @@ packages/audio_service/example/pubspec.lock
# Native build intermediates Gradle writes inside vendored plugins
packages/*/android/.cxx/
+packages/*/android_compat/.cxx/
+packages/*/android_compat/build/
packages/whisper_ggml_plus/pubspec.lock
# Kotlin build session caches
diff --git a/android/app/build.gradle.kts b/android/app/build.gradle.kts
index 90112c3c..0ee8502f 100644
--- a/android/app/build.gradle.kts
+++ b/android/app/build.gradle.kts
@@ -147,6 +147,11 @@ dependencies {
// SAF document moves for custom download folders (MainActivity.moveBookToSaf).
implementation("androidx.documentfile:documentfile:1.0.1")
+ // Baseline armv8-a whisper engine for CPUs without dotprod/fp16 (Boox
+ // Palma and friends); the plugin's Dart side picks it over the fast lib
+ // at runtime.
+ implementation(project(":whisper_ggml_plus_compat"))
+
// Google Play Services — Chromecast + Wearable Data Layer (pushes ABS
// session credentials to the paired Wear OS app so the watch can sign in
// without typing on the watch keyboard). Scoped to github + playstore so
diff --git a/android/settings.gradle.kts b/android/settings.gradle.kts
index ca7fe065..af7142bf 100644
--- a/android/settings.gradle.kts
+++ b/android/settings.gradle.kts
@@ -24,3 +24,9 @@ plugins {
}
include(":app")
+
+// Fallback whisper engine for CPUs that can't run the fast arm64 build
+// (see packages/whisper_ggml_plus/android_compat).
+include(":whisper_ggml_plus_compat")
+project(":whisper_ggml_plus_compat").projectDir =
+ settingsDir.resolve("../packages/whisper_ggml_plus/android_compat")
diff --git a/lib/services/transcription_service.dart b/lib/services/transcription_service.dart
index cec68fde..63351620 100644
--- a/lib/services/transcription_service.dart
+++ b/lib/services/transcription_service.dart
@@ -370,6 +370,7 @@ class TranscriptionService {
debugPrint('[Transcribe] window=${window.toStringAsFixed(1)}s '
'model=${info.fileName} lang=$lang extract=${extractMs}ms '
'whisper=${watch.elapsedMilliseconds - extractMs}ms '
+ 'engine=${Whisper.usesCompatEngine ? 'compat' : 'fast'} '
'chars=${text.length}');
if (text.isEmpty) throw TranscriptionException(TranscriptionError.empty);
// Hand the extracted clip to the caller (for review playback). Ownership
@@ -494,7 +495,8 @@ class TranscriptionService {
debugPrint('[Transcribe] segments window=${window.toStringAsFixed(1)}s '
'model=${info.fileName} lang=$lang extract=${extractMs}ms '
'whisper=${watch.elapsedMilliseconds - extractMs}ms '
- 'threads=${_threads()} segments=${segments.length}');
+ 'threads=${_threads()} engine=${Whisper.usesCompatEngine ? 'compat' : 'fast'} '
+ 'segments=${segments.length}');
if (segments.isEmpty) throw TranscriptionException(TranscriptionError.empty);
return segments;
} finally {
diff --git a/packages/whisper_ggml_plus/android/src/whisper/CMakeLists.txt b/packages/whisper_ggml_plus/android/src/whisper/CMakeLists.txt
index c5e888d2..4d7ecfe6 100644
--- a/packages/whisper_ggml_plus/android/src/whisper/CMakeLists.txt
+++ b/packages/whisper_ggml_plus/android/src/whisper/CMakeLists.txt
@@ -25,8 +25,12 @@ set(WHISPER_OPENVINO OFF CACHE BOOL "" FORCE)
# Without an arch baseline ggml compiles plain armv8-a and loses the dotprod
# and fp16 kernels that quantized whisper spends nearly all its time in.
-# armv8.2-a+dotprod+fp16 needs Cortex-A75/A55 (2018) or newer.
-if (ANDROID_ABI STREQUAL "arm64-v8a")
+# armv8.2-a+dotprod+fp16 needs Cortex-A75/A55 (2018) or newer; cores older
+# than that SIGILL on it, so ../../../android_compat builds this same file a
+# second time with WHISPER_COMPAT on, producing a plain armv8-a
+# libwhisper_compat.so that the Dart side picks at runtime instead.
+option(WHISPER_COMPAT "Baseline armv8-a build for CPUs without dotprod/fp16" OFF)
+if (ANDROID_ABI STREQUAL "arm64-v8a" AND NOT WHISPER_COMPAT)
set(GGML_CPU_ARM_ARCH "armv8.2-a+dotprod+fp16" CACHE STRING "" FORCE)
endif()
@@ -59,7 +63,13 @@ if (ANDROID)
)
endif()
+if (WHISPER_COMPAT)
+ set(WHISPER_OUTPUT_NAME "whisper_compat")
+else()
+ set(WHISPER_OUTPUT_NAME "whisper")
+endif()
+
set_target_properties(whisper_flutter PROPERTIES
PUBLIC_HEADER src/whisper.h
- OUTPUT_NAME "whisper"
+ OUTPUT_NAME "${WHISPER_OUTPUT_NAME}"
)
diff --git a/packages/whisper_ggml_plus/android_compat/build.gradle b/packages/whisper_ggml_plus/android_compat/build.gradle
new file mode 100644
index 00000000..2b31471f
--- /dev/null
+++ b/packages/whisper_ggml_plus/android_compat/build.gradle
@@ -0,0 +1,51 @@
+group 'com.devac.whisper_flutter'
+version '1.0'
+
+buildscript {
+ repositories {
+ google()
+ mavenCentral()
+ }
+
+ dependencies {
+ classpath 'com.android.tools.build:gradle:8.4.2'
+ }
+}
+
+apply plugin: 'com.android.library'
+
+// Second build of ../android/src/whisper with WHISPER_COMPAT on: a plain
+// armv8-a libwhisper_compat.so for CPUs without dotprod/fp16 (Boox Palma and
+// other pre-2018 cores), which SIGILL on the fast lib. arm64 only - every
+// other ABI's libwhisper.so is already a baseline build.
+android {
+ namespace "com.devac.whisper_ggml_plus.compat"
+
+ compileSdk 34
+
+ externalNativeBuild {
+ cmake {
+ path "../android/src/whisper/CMakeLists.txt"
+ version "3.22.1"
+ }
+ }
+
+ compileOptions {
+ sourceCompatibility JavaVersion.VERSION_17
+ targetCompatibility JavaVersion.VERSION_17
+ }
+
+ defaultConfig {
+ minSdkVersion 21
+ ndk {
+ abiFilters("arm64-v8a")
+ }
+ externalNativeBuild {
+ cmake {
+ cFlags "-O3 -s -flto=thin -ffunction-sections -fdata-sections -fvisibility=hidden -fvisibility-inlines-hidden"
+ cppFlags "-O3 -s -flto=thin -ffunction-sections -fdata-sections -fvisibility=hidden -fvisibility-inlines-hidden"
+ arguments "-Wl,--gc-sections,--exclude-libs,ALL", "-DANDROID_STL=c++_static", "-DCMAKE_BUILD_TYPE=Release", "-DWHISPER_COMPAT=ON"
+ }
+ }
+ }
+}
diff --git a/packages/whisper_ggml_plus/android_compat/src/main/AndroidManifest.xml b/packages/whisper_ggml_plus/android_compat/src/main/AndroidManifest.xml
new file mode 100644
index 00000000..94cbbcfc
--- /dev/null
+++ b/packages/whisper_ggml_plus/android_compat/src/main/AndroidManifest.xml
@@ -0,0 +1 @@
+<manifest xmlns:android="http://schemas.android.com/apk/res/android" />
diff --git a/packages/whisper_ggml_plus/lib/src/whisper.dart b/packages/whisper_ggml_plus/lib/src/whisper.dart
index b20f6fa7..ab82431f 100644
--- a/packages/whisper_ggml_plus/lib/src/whisper.dart
+++ b/packages/whisper_ggml_plus/lib/src/whisper.dart
@@ -37,8 +37,38 @@ class Whisper {
/// override of model storage path
final String? modelDir;
+ /// The arm64 libwhisper.so is compiled for armv8.2-a+dotprod+fp16, which
+ /// Cortex-A73-class and older cores (Boox Palma, SD835-era phones) cannot
+ /// execute - the first call SIGILLs. Those CPUs get the baseline
+ /// libwhisper_compat.so instead. Requiring the extensions on every core
+ /// keeps mixed-cluster devices on the safe side.
+ static final bool usesCompatEngine = _detectCompatEngine();
+
+ static bool _detectCompatEngine() {
+ if (!Platform.isAndroid) return false;
+ try {
+ final features = File('/proc/cpuinfo')
+ .readAsLinesSync()
+ .where((line) => line.startsWith('Features'));
+ if (features.isEmpty) return false;
+ return !features.every(
+ (line) => line.contains('asimddp') && line.contains('asimdhp'),
+ );
+ } catch (_) {
+ return false;
+ }
+ }
+
DynamicLibrary _openLib() {
if (Platform.isAndroid) {
+ if (usesCompatEngine) {
+ try {
+ return DynamicLibrary.open('libwhisper_compat.so');
+ } catch (_) {
+ // Only packaged for arm64-v8a; every other ABI's libwhisper.so is
+ // already a baseline build.
+ }
+ }
return DynamicLibrary.open('libwhisper.so');
} else if (Platform.isWindows) {
return DynamicLibrary.open('whisper_ggml_plus.dll');