|
| 1 | +from os.path import basename, dirname, exists, join |
| 2 | +import zipfile |
| 3 | + |
| 4 | +from pythonforandroid.logger import info |
| 5 | +from pythonforandroid.recipe import PythonRecipe, current_directory, shprint |
| 6 | +from pythonforandroid.util import BuildInterruptingException, ensure_dir |
| 7 | + |
| 8 | + |
| 9 | +class VoskRecipe(PythonRecipe): |
| 10 | + version = "0.3.45" |
| 11 | + url = "https://github.com/alphacep/vosk-api/archive/refs/tags/v{version}.tar.gz" |
| 12 | + site_packages_name = "vosk" |
| 13 | + depends = ["cffi"] |
| 14 | + python_depends = ["requests", "tqdm", "srt", "websockets"] |
| 15 | + hostpython_prerequisites = [ |
| 16 | + "setuptools", |
| 17 | + "wheel", |
| 18 | + "cffi", |
| 19 | + "requests", |
| 20 | + "tqdm", |
| 21 | + "srt", |
| 22 | + "websockets", |
| 23 | + ] |
| 24 | + call_hostpython_via_targetpython = False |
| 25 | + |
| 26 | + android_aar_url = ( |
| 27 | + "https://repo.maven.apache.org/maven2/com/alphacephei/" |
| 28 | + "vosk-android/{version}/vosk-android-{version}.aar" |
| 29 | + ) |
| 30 | + android_aar_abis = { |
| 31 | + "arm64-v8a": "arm64-v8a", |
| 32 | + "armeabi-v7a": "armeabi-v7a", |
| 33 | + "x86": "x86", |
| 34 | + "x86_64": "x86_64", |
| 35 | + } |
| 36 | + |
| 37 | + def build_arch(self, arch): |
| 38 | + self.install_hostpython_prerequisites() |
| 39 | + |
| 40 | + env = self.get_recipe_env(arch) |
| 41 | + python_dir = join(self.get_build_dir(arch.arch), "python") |
| 42 | + install_dir = self.ctx.get_python_install_dir(arch.arch) |
| 43 | + |
| 44 | + info("Installing Vosk Python bindings into site-packages") |
| 45 | + with current_directory(python_dir): |
| 46 | + shprint( |
| 47 | + self._host_recipe.pip, |
| 48 | + "install", |
| 49 | + ".", |
| 50 | + "--compile", |
| 51 | + "--no-deps", |
| 52 | + "--target", |
| 53 | + install_dir, |
| 54 | + _env=env, |
| 55 | + ) |
| 56 | + |
| 57 | + self.install_android_libvosk(arch) |
| 58 | + |
| 59 | + def install_android_libvosk(self, arch): |
| 60 | + aar_abi = self.android_aar_abis.get(arch.arch) |
| 61 | + if aar_abi is None: |
| 62 | + supported = ", ".join(sorted(self.android_aar_abis)) |
| 63 | + raise BuildInterruptingException( |
| 64 | + f"Vosk does not provide an Android library for {arch.arch}. " |
| 65 | + f"Supported ABIs: {supported}" |
| 66 | + ) |
| 67 | + |
| 68 | + aar_path = self.download_android_aar() |
| 69 | + aar_lib_path = f"jni/{aar_abi}/libvosk.so" |
| 70 | + target_path = join( |
| 71 | + self.ctx.get_python_install_dir(arch.arch), |
| 72 | + self.site_packages_name, |
| 73 | + "libvosk.so", |
| 74 | + ) |
| 75 | + ensure_dir(dirname(target_path)) |
| 76 | + |
| 77 | + info(f"Installing Android {arch.arch} libvosk.so") |
| 78 | + try: |
| 79 | + with zipfile.ZipFile(aar_path) as aar: |
| 80 | + with aar.open(aar_lib_path) as source, open(target_path, "wb") as target: |
| 81 | + target.write(source.read()) |
| 82 | + except KeyError as exc: |
| 83 | + raise BuildInterruptingException( |
| 84 | + f"{basename(aar_path)} does not contain {aar_lib_path}" |
| 85 | + ) from exc |
| 86 | + |
| 87 | + def download_android_aar(self): |
| 88 | + aar_dir = join(self.ctx.packages_path, "vosk-android") |
| 89 | + ensure_dir(aar_dir) |
| 90 | + |
| 91 | + aar_name = f"vosk-android-{self.version}.aar" |
| 92 | + aar_path = join(aar_dir, aar_name) |
| 93 | + if not exists(aar_path): |
| 94 | + self.download_file( |
| 95 | + self.android_aar_url.format(version=self.version), |
| 96 | + aar_name, |
| 97 | + cwd=aar_dir, |
| 98 | + ) |
| 99 | + return aar_path |
| 100 | + |
| 101 | + |
| 102 | +recipe = VoskRecipe() |
0 commit comments