Use aar in Android Java demo. (#1616)
This commit is contained in:
44
android/SherpaOnnxJavaDemo/README.md
Normal file
44
android/SherpaOnnxJavaDemo/README.md
Normal file
@@ -0,0 +1,44 @@
|
||||
# Introduction
|
||||
|
||||
Please run the following commands to download model files before you run this Android demo:
|
||||
|
||||
```bash
|
||||
# Assume we are inside
|
||||
# /Users/fangjun/open-source/sherpa-onnx/android/SherpaOnnxJavaDemo
|
||||
|
||||
cd app/src/main/assets/
|
||||
wget https://github.com/k2-fsa/sherpa-onnx/releases/download/asr-models/sherpa-onnx-streaming-zipformer-bilingual-zh-en-2023-02-20.tar.bz2
|
||||
|
||||
tar xvf sherpa-onnx-streaming-zipformer-bilingual-zh-en-2023-02-20.tar.bz2
|
||||
rm sherpa-onnx-streaming-zipformer-bilingual-zh-en-2023-02-20.tar.bz2
|
||||
|
||||
mv sherpa-onnx-streaming-zipformer-bilingual-zh-en-2023-02-20/encoder-epoch-99-avg-1.int8.onnx ./
|
||||
mv sherpa-onnx-streaming-zipformer-bilingual-zh-en-2023-02-20/decoder-epoch-99-avg-1.onnx ./
|
||||
mv sherpa-onnx-streaming-zipformer-bilingual-zh-en-2023-02-20/joiner-epoch-99-avg-1.int8.onnx ./
|
||||
mv sherpa-onnx-streaming-zipformer-bilingual-zh-en-2023-02-20/tokens.txt ./
|
||||
|
||||
rm -rf sherpa-onnx-streaming-zipformer-bilingual-zh-en-2023-02-20/*
|
||||
|
||||
mv encoder-epoch-99-avg-1.int8.onnx ./
|
||||
mv decoder-epoch-99-avg-1.onnx ./
|
||||
mv joiner-epoch-99-avg-1.int8.onnx ./
|
||||
mv tokens.txt ./
|
||||
```
|
||||
|
||||
You should have the following directory structure:
|
||||
```
|
||||
(py38) fangjuns-MacBook-Pro:assets fangjun$ pwd
|
||||
/Users/fangjun/open-source/sherpa-onnx/android/SherpaOnnxJavaDemo/app/src/main/assets
|
||||
|
||||
(py38) fangjuns-MacBook-Pro:assets fangjun$ tree .
|
||||
.
|
||||
└── sherpa-onnx-streaming-zipformer-bilingual-zh-en-2023-02-20
|
||||
├── decoder-epoch-99-avg-1.onnx
|
||||
├── encoder-epoch-99-avg-1.int8.onnx
|
||||
├── joiner-epoch-99-avg-1.int8.onnx
|
||||
└── tokens.txt
|
||||
|
||||
1 directory, 4 files
|
||||
```
|
||||
|
||||
Remember to remove unused files to reduce the file size of the final APK.
|
||||
@@ -8,7 +8,7 @@ android {
|
||||
defaultConfig {
|
||||
applicationId "com.k2fsa.sherpa.onnx"
|
||||
minSdk 28
|
||||
targetSdk 32
|
||||
targetSdk 34
|
||||
versionCode 1
|
||||
versionName "1.0"
|
||||
|
||||
@@ -25,17 +25,14 @@ android {
|
||||
sourceCompatibility JavaVersion.VERSION_1_8
|
||||
targetCompatibility JavaVersion.VERSION_1_8
|
||||
}
|
||||
sourceSets.main{
|
||||
jniLibs.srcDirs = ['jniLibs']
|
||||
}
|
||||
}
|
||||
|
||||
dependencies {
|
||||
|
||||
implementation 'androidx.appcompat:appcompat:1.3.1'
|
||||
implementation 'com.google.android.material:material:1.3.0'
|
||||
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
|
||||
implementation 'pub.devrel:easypermissions:3.0.0'
|
||||
implementation project(path: ':sherpa')
|
||||
|
||||
}
|
||||
implementation 'androidx.core:core-ktx:1.7.0'
|
||||
// implementation files('/Users/fangjun/open-source/sherpa-onnx/android/SherpaOnnxAar/sherpa_onnx/build/outputs/aar/sherpa_onnx-release.aar')
|
||||
implementation 'com.github.k2-fsa:sherpa-onnx:master-SNAPSHOT'
|
||||
}
|
||||
|
||||
@@ -1,84 +0,0 @@
|
||||
// Copyright 2022-2023 by zhaoming
|
||||
// Copyright 2024 Xiaomi Corporation
|
||||
|
||||
package com.k2fsa.sherpa.onnx;
|
||||
|
||||
import android.content.res.AssetManager;
|
||||
|
||||
public class OnlineRecognizer {
|
||||
static {
|
||||
System.loadLibrary("sherpa-onnx-jni");
|
||||
}
|
||||
|
||||
private long ptr = 0;
|
||||
|
||||
public OnlineRecognizer(OnlineRecognizerConfig config) {
|
||||
ptr = newFromFile(config);
|
||||
}
|
||||
|
||||
public OnlineRecognizer(AssetManager assetManager, OnlineRecognizerConfig config) {
|
||||
ptr = newFromAsset(assetManager, config);
|
||||
}
|
||||
|
||||
public void decode(OnlineStream s) {
|
||||
decode(ptr, s.getPtr());
|
||||
}
|
||||
|
||||
public boolean isReady(OnlineStream s) {
|
||||
return isReady(ptr, s.getPtr());
|
||||
}
|
||||
|
||||
public boolean isEndpoint(OnlineStream s) {
|
||||
return isEndpoint(ptr, s.getPtr());
|
||||
}
|
||||
|
||||
public void reset(OnlineStream s) {
|
||||
reset(ptr, s.getPtr());
|
||||
}
|
||||
|
||||
public OnlineStream createStream() {
|
||||
long p = createStream(ptr, "");
|
||||
return new OnlineStream(p);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void finalize() throws Throwable {
|
||||
release();
|
||||
}
|
||||
|
||||
// You'd better call it manually if it is not used anymore
|
||||
public void release() {
|
||||
if (this.ptr == 0) {
|
||||
return;
|
||||
}
|
||||
delete(this.ptr);
|
||||
this.ptr = 0;
|
||||
}
|
||||
|
||||
public OnlineRecognizerResult getResult(OnlineStream s) {
|
||||
Object[] arr = getResult(ptr, s.getPtr());
|
||||
String text = (String) arr[0];
|
||||
String[] tokens = (String[]) arr[1];
|
||||
float[] timestamps = (float[]) arr[2];
|
||||
return new OnlineRecognizerResult(text, tokens, timestamps);
|
||||
}
|
||||
|
||||
|
||||
private native void delete(long ptr);
|
||||
|
||||
private native long newFromFile(OnlineRecognizerConfig config);
|
||||
|
||||
private native long newFromAsset(AssetManager assetManager, OnlineRecognizerConfig config);
|
||||
|
||||
private native long createStream(long ptr, String hotwords);
|
||||
|
||||
private native void reset(long ptr, long streamPtr);
|
||||
|
||||
private native void decode(long ptr, long streamPtr);
|
||||
|
||||
private native boolean isEndpoint(long ptr, long streamPtr);
|
||||
|
||||
private native boolean isReady(long ptr, long streamPtr);
|
||||
|
||||
private native Object[] getResult(long ptr, long streamPtr);
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.k2fsa.sherpa.onnx.service;
|
||||
|
||||
import android.Manifest;
|
||||
import android.annotation.SuppressLint;
|
||||
import android.app.Notification;
|
||||
import android.app.NotificationChannel;
|
||||
import android.app.NotificationManager;
|
||||
@@ -67,6 +68,16 @@ public class SpeechSherpaRecognitionService extends Service {
|
||||
appViewModel = Application.getInstance().getViewModel();
|
||||
int numBytes = AudioRecord.getMinBufferSize(sampleRateInHz, channelConfig, audioFormat);
|
||||
|
||||
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) {
|
||||
// TODO: Consider calling
|
||||
// ActivityCompat#requestPermissions
|
||||
// here to request the missing permissions, and then overriding
|
||||
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
|
||||
// int[] grantResults)
|
||||
// to handle the case where the user grants the permission. See the documentation
|
||||
// for ActivityCompat#requestPermissions for more details.
|
||||
return;
|
||||
}
|
||||
audioRecord = new AudioRecord(
|
||||
audioSource,
|
||||
sampleRateInHz,
|
||||
@@ -81,22 +92,21 @@ public class SpeechSherpaRecognitionService extends Service {
|
||||
|
||||
private void initializeSherpa() {
|
||||
Log.d("Current Directory", System.getProperty("user.dir"));
|
||||
String modelDir = "sherpa-onnx-streaming-zipformer-zh-14M-2023-02-23";
|
||||
String modelDir = "sherpa-onnx-streaming-zipformer-bilingual-zh-en-2023-02-20";
|
||||
initializeSherpaDir(modelDir, modelDir);
|
||||
OnlineTransducerModelConfig onlineTransducerModelConfig = OnlineTransducerModelConfig.builder()
|
||||
.setEncoder(modelDir + "/encoder-epoch-99-avg-1.int8.onnx")
|
||||
.setDecoder(modelDir + "/decoder-epoch-99-avg-1.onnx")
|
||||
.setJoiner(modelDir + "/joiner-epoch-99-avg-1.int8.onnx")
|
||||
.build();
|
||||
OnlineTransducerModelConfig onlineTransducerModelConfig = new OnlineTransducerModelConfig();
|
||||
onlineTransducerModelConfig.setEncoder(modelDir + "/encoder-epoch-99-avg-1.int8.onnx");
|
||||
onlineTransducerModelConfig.setDecoder(modelDir + "/decoder-epoch-99-avg-1.onnx");
|
||||
onlineTransducerModelConfig.setJoiner(modelDir + "/joiner-epoch-99-avg-1.int8.onnx");
|
||||
|
||||
OnlineModelConfig onlineModelConfig = OnlineModelConfig.builder()
|
||||
.setTransducer(onlineTransducerModelConfig)
|
||||
.setTokens(modelDir + "/tokens.txt")
|
||||
.setModelType("zipformer")
|
||||
.build();
|
||||
OnlineRecognizerConfig config = OnlineRecognizerConfig.builder()
|
||||
.setOnlineModelConfig(onlineModelConfig)
|
||||
.build();
|
||||
OnlineModelConfig onlineModelConfig = new OnlineModelConfig();
|
||||
onlineModelConfig.setTransducer(onlineTransducerModelConfig);
|
||||
onlineModelConfig.setTokens(modelDir + "/tokens.txt");
|
||||
onlineModelConfig.setModelType("zipformer");
|
||||
onlineModelConfig.setDebug(true);
|
||||
|
||||
OnlineRecognizerConfig config = new OnlineRecognizerConfig();
|
||||
config.setModelConfig(onlineModelConfig);
|
||||
recognizer = new OnlineRecognizer(getAssets(), config);
|
||||
|
||||
audioRecord.startRecording();
|
||||
@@ -110,7 +120,7 @@ public class SpeechSherpaRecognitionService extends Service {
|
||||
}
|
||||
|
||||
private void processSamples() {
|
||||
OnlineStream stream = recognizer.createStream();
|
||||
OnlineStream stream = recognizer.createStream("");
|
||||
double interval = 0.1;
|
||||
int bufferSize = (int) (interval * sampleRateInHz);
|
||||
short[] buffer = new short[bufferSize];
|
||||
@@ -182,6 +192,7 @@ public class SpeechSherpaRecognitionService extends Service {
|
||||
}
|
||||
|
||||
|
||||
@SuppressLint("ForegroundServiceType")
|
||||
private void startForegroundService() {
|
||||
String channelId = createNotificationChannel();
|
||||
|
||||
|
||||
@@ -10,8 +10,8 @@ dependencyResolutionManagement {
|
||||
repositories {
|
||||
google()
|
||||
mavenCentral()
|
||||
maven { url 'https://jitpack.io' }
|
||||
}
|
||||
}
|
||||
rootProject.name = "SherpaOnnxJavaDemo"
|
||||
include ':app'
|
||||
include ':sherpa'
|
||||
|
||||
1
android/SherpaOnnxJavaDemo/sherpa/.gitignore
vendored
1
android/SherpaOnnxJavaDemo/sherpa/.gitignore
vendored
@@ -1 +0,0 @@
|
||||
/build
|
||||
@@ -1,43 +0,0 @@
|
||||
plugins {
|
||||
id 'com.android.library'
|
||||
}
|
||||
|
||||
android {
|
||||
namespace 'com.k2fsa.sherpa'
|
||||
compileSdk 34
|
||||
|
||||
defaultConfig {
|
||||
minSdk 26
|
||||
targetSdk 27
|
||||
versionCode 1
|
||||
versionName "1.0"
|
||||
missingDimensionStrategy 'base', 'feature1'
|
||||
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
|
||||
}
|
||||
|
||||
buildTypes {
|
||||
release {
|
||||
minifyEnabled false
|
||||
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
|
||||
}
|
||||
}
|
||||
compileOptions {
|
||||
sourceCompatibility JavaVersion.VERSION_1_8
|
||||
targetCompatibility JavaVersion.VERSION_1_8
|
||||
}
|
||||
}
|
||||
|
||||
dependencies {
|
||||
|
||||
// implementation "androidx.appcompat"
|
||||
// implementation libs.material
|
||||
// testImplementation libs.junit
|
||||
// androidTestImplementation libs.androidx.test.ext.junit
|
||||
// androidTestImplementation libs.espresso.core
|
||||
implementation 'androidx.appcompat:appcompat:1.6.1'
|
||||
implementation 'com.google.android.material:material:1.9.0'
|
||||
testImplementation 'junit:junit:4.13.2'
|
||||
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
|
||||
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
|
||||
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
# Add project specific ProGuard rules here.
|
||||
# You can control the set of applied configuration files using the
|
||||
# proguardFiles setting in build.gradle.
|
||||
#
|
||||
# For more details, see
|
||||
# http://developer.android.com/guide/developing/tools/proguard.html
|
||||
|
||||
# If your project uses WebView with JS, uncomment the following
|
||||
# and specify the fully qualified class name to the JavaScript interface
|
||||
# class:
|
||||
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
|
||||
# public *;
|
||||
#}
|
||||
|
||||
# Uncomment this to preserve the line number information for
|
||||
# debugging stack traces.
|
||||
#-keepattributes SourceFile,LineNumberTable
|
||||
|
||||
# If you keep the line number information, uncomment this to
|
||||
# hide the original source file name.
|
||||
#-renamesourcefileattribute SourceFile
|
||||
@@ -1,9 +0,0 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
|
||||
<application
|
||||
android:allowBackup="true"
|
||||
android:label="@string/app_name"
|
||||
android:supportsRtl="true" />
|
||||
|
||||
</manifest>
|
||||
@@ -1 +0,0 @@
|
||||
../../../../../../../sherpa-onnx/sherpa-onnx/java-api/src/com
|
||||
@@ -1,3 +0,0 @@
|
||||
<resources>
|
||||
<string name="app_name">sherpa</string>
|
||||
</resources>
|
||||
Reference in New Issue
Block a user