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

Use aar in Android Java demo. #1616

Merged
merged 5 commits into from
Dec 12, 2024
Merged
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
44 changes: 44 additions & 0 deletions android/SherpaOnnxJavaDemo/README.md
Original file line number Diff line number Diff line change
@@ -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.
13 changes: 5 additions & 8 deletions android/SherpaOnnxJavaDemo/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ android {
defaultConfig {
applicationId "com.k2fsa.sherpa.onnx"
minSdk 28
targetSdk 32
targetSdk 34
versionCode 1
versionName "1.0"

Expand All @@ -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'
}
Empty file.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -67,6 +68,16 @@ public void onCreate() {
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,
Expand All @@ -81,22 +92,21 @@ public void onCreate() {

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();

OnlineModelConfig onlineModelConfig = OnlineModelConfig.builder()
.setTransducer(onlineTransducerModelConfig)
.setTokens(modelDir + "/tokens.txt")
.setModelType("zipformer")
.build();
OnlineRecognizerConfig config = OnlineRecognizerConfig.builder()
.setOnlineModelConfig(onlineModelConfig)
.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 = 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();
Expand All @@ -110,7 +120,7 @@ private void startRecognition() {
}

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];
Expand Down Expand Up @@ -182,6 +192,7 @@ public IBinder onBind(Intent intent) {
}


@SuppressLint("ForegroundServiceType")
private void startForegroundService() {
String channelId = createNotificationChannel();

Expand Down
2 changes: 1 addition & 1 deletion android/SherpaOnnxJavaDemo/settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ dependencyResolutionManagement {
repositories {
google()
mavenCentral()
maven { url 'https://jitpack.io' }
}
}
rootProject.name = "SherpaOnnxJavaDemo"
include ':app'
include ':sherpa'
1 change: 0 additions & 1 deletion android/SherpaOnnxJavaDemo/sherpa/.gitignore

This file was deleted.

43 changes: 0 additions & 43 deletions android/SherpaOnnxJavaDemo/sherpa/build.gradle

This file was deleted.

21 changes: 0 additions & 21 deletions android/SherpaOnnxJavaDemo/sherpa/proguard-rules.pro

This file was deleted.

This file was deleted.

1 change: 0 additions & 1 deletion android/SherpaOnnxJavaDemo/sherpa/src/main/java/com

This file was deleted.

This file was deleted.

4 changes: 2 additions & 2 deletions sherpa-onnx/kotlin-api/AudioTagging.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ data class AudioTaggingModelConfig(
)

data class AudioTaggingConfig(
var model: AudioTaggingModelConfig,
var labels: String,
var model: AudioTaggingModelConfig = AudioTaggingModelConfig(),
var labels: String = "",
var topK: Int = 5,
)

Expand Down
2 changes: 1 addition & 1 deletion sherpa-onnx/kotlin-api/KeywordSpotter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import android.content.res.AssetManager

data class KeywordSpotterConfig(
var featConfig: FeatureConfig = FeatureConfig(),
var modelConfig: OnlineModelConfig,
var modelConfig: OnlineModelConfig = OnlineModelConfig(),
var maxActivePaths: Int = 4,
var keywordsFile: String = "keywords.txt",
var keywordsScore: Float = 1.5f,
Expand Down
2 changes: 1 addition & 1 deletion sherpa-onnx/kotlin-api/OfflinePunctuation.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.k2fsa.sherpa.onnx
import android.content.res.AssetManager

data class OfflinePunctuationModelConfig(
var ctTransformer: String,
var ctTransformer: String = "",
var numThreads: Int = 1,
var debug: Boolean = false,
var provider: String = "cpu",
Expand Down
4 changes: 2 additions & 2 deletions sherpa-onnx/kotlin-api/OfflineRecognizer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,14 @@ data class OfflineModelConfig(
var debug: Boolean = false,
var provider: String = "cpu",
var modelType: String = "",
var tokens: String,
var tokens: String = "",
var modelingUnit: String = "",
var bpeVocab: String = "",
)

data class OfflineRecognizerConfig(
var featConfig: FeatureConfig = FeatureConfig(),
var modelConfig: OfflineModelConfig,
var modelConfig: OfflineModelConfig = OfflineModelConfig(),
// var lmConfig: OfflineLMConfig(), // TODO(fangjun): enable it
var decodingMethod: String = "greedy_search",
var maxActivePaths: Int = 4,
Expand Down
Loading
Loading