Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Android implementation #2

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

C++ version of [openWakeWord](https://github.com/dscripka/openWakeWord).

# Linux

## Build

1. Download a release of the [onnxruntime](https://github.com/microsoft/onnxruntime) and extract to `lib/<arch>` where `<arch>` is `uname -m`.
Expand All @@ -18,3 +20,34 @@ arecord -r 16000 -c 1 -f S16_LE -t raw - | \
```

You can add multiple `--model <path>` arguments. See `--help` for more options.

# Android

- openWakeWord-cpp path: `android/app/src/main/cpp/openWakeWord-cpp`
- [onnxruntime-android](https://mvnrepository.com/artifact/com.microsoft.onnxruntime/onnxruntime-android) path: `android/app/src/main/cpp/onnxruntime-android`
- models path moved from android/app/src/main/cpp/openWakeWord-cpp/models to: `android/app/src/main/assets/models`

android/app/build.gradle:
```gradle
android {
defaultConfig {
minSdkVersion 27
ndk {
ldLibs "log"
}
}
externalNativeBuild {
cmake {
path "src/main/cpp/openWakeWord-cpp/src/android/CMakeLists.txt"
}
}
```

Example of Android service is in `openWakeWord-cpp/src/android/OpenWakeWordServiceExample.java`. There C++ part is defaulted end after waking, and started after manualy calling the service (intend) again.
- Extras with `end` property end service.
- Extras with `stop` property end cpp subprocess.
- Extras with `keyword` property start service, or only cpp subprocess, and set wake word model path. Default is `models/alexa_v0.1.onnx`.
- Optional extras `sensitivity` as string value. Default is `0.5`.
- Optional extras `closeServiceAfterWakeWordActivation` property end android service after waking (cpp subprocess is end by hardcode). Default is `false`.
- Don't forget to create first `NotificationChannel` in MainActivity.
- Android destroy service automaticly after same time, that's why you must set `Worker`, which will call this service each 16 minutes.
48 changes: 48 additions & 0 deletions src/android/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# https://mvnrepository.com/artifact/com.microsoft.onnxruntime/onnxruntime-android/1.16.3

cmake_minimum_required(VERSION 3.13)

project(openWakeWord C CXX)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

string(APPEND CMAKE_CXX_FLAGS " -Wall -Wextra -Wl,-rpath,'$ORIGIN'")
string(APPEND CMAKE_C_FLAGS " -Wall -Wextra")


set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
set(THREADS_PREFER_PTHREAD_FLAG TRUE)

find_package(Threads REQUIRED)


add_library(onnxruntime SHARED IMPORTED)
set_target_properties(
onnxruntime
PROPERTIES
IMPORTED_LOCATION ${CMAKE_CURRENT_LIST_DIR}/../../../onnxruntime-android/jni/${ANDROID_ABI}/libonnxruntime.so
INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_CURRENT_LIST_DIR}/../../../onnxruntime-android/headers
)


find_library(log-lib log)
find_library(android-lib android)


add_library(
openWakeWord
SHARED
${CMAKE_CURRENT_LIST_DIR}/../main.cpp
)

target_link_libraries(
openWakeWord
PRIVATE
Threads::Threads
onnxruntime
aaudio
log
${log-lib}
${android-lib}
)
Loading