diff --git a/.github/scripts/test-kws.sh b/.github/scripts/test-kws.sh new file mode 100755 index 0000000000..710a193fce --- /dev/null +++ b/.github/scripts/test-kws.sh @@ -0,0 +1,68 @@ +#!/usr/bin/env bash + +set -e + +log() { + # This function is from espnet + local fname=${BASH_SOURCE[1]##*/} + echo -e "$(date '+%Y-%m-%d %H:%M:%S') (${fname}:${BASH_LINENO[0]}:${FUNCNAME[1]}) $*" +} + +echo "EXE is $EXE" +echo "PATH: $PATH" + +which $EXE + +log "------------------------------------------------------------" +log "Run Chinese keyword spotting (Wenetspeech)" +log "------------------------------------------------------------" + +repo_url=https://www.modelscope.cn/pkufool/sherpa-onnx-kws-zipformer-wenetspeech-3.3M-2024-01-01.git +log "Start testing ${repo_url}" +repo=sherpa-onnx-kws-zipformer-wenetspeech-3.3M-2024-01-01 +log "Download pretrained model and test-data from $repo_url" + +GIT_LFS_SKIP_SMUDGE=1 git clone $repo_url +pushd $repo +git lfs pull --include "*.onnx" +ls -lh *.onnx +popd + +time $EXE \ + --tokens=$repo/tokens.txt \ + --encoder=$repo/encoder-epoch-12-avg-2-chunk-16-left-64.onnx \ + --decoder=$repo/decoder-epoch-12-avg-2-chunk-16-left-64.onnx \ + --joiner=$repo/joiner-epoch-12-avg-2-chunk-16-left-64.onnx \ + --keywords-file=$repo/test_wavs/test_keywords.txt \ + --max-active-paths=4 \ + --num-threads=4 \ + $repo/test_wavs/3.wav $repo/test_wavs/4.wav $repo/test_wavs/5.wav $repo/test_wavs/6.wav + +rm -rf $repo + +log "------------------------------------------------------------" +log "Run English keyword spotting (Gigaspeech)" +log "------------------------------------------------------------" + +repo_url=https://www.modelscope.cn/pkufool/sherpa-onnx-kws-zipformer-gigaspeech-3.3M-2024-01-01.git +log "Start testing ${repo_url}" +repo=sherpa-onnx-kws-zipformer-gigaspeech-3.3M-2024-01-01 +log "Download pretrained model and test-data from $repo_url" + +GIT_LFS_SKIP_SMUDGE=1 git clone $repo_url +pushd $repo +git lfs pull --include "*.onnx" +ls -lh *.onnx +popd + +time $EXE \ + --tokens=$repo/tokens.txt \ + --encoder=$repo/encoder-epoch-12-avg-2-chunk-16-left-64.onnx \ + --decoder=$repo/decoder-epoch-12-avg-2-chunk-16-left-64.onnx \ + --joiner=$repo/joiner-epoch-12-avg-2-chunk-16-left-64.onnx \ + --keywords-file=$repo/test_wavs/test_keywords.txt \ + --max-active-paths=4 \ + --num-threads=4 \ + $repo/test_wavs/0.wav $repo/test_wavs/1.wav + +rm -rf $repo diff --git a/.github/workflows/apk-kws.yaml b/.github/workflows/apk-kws.yaml new file mode 100644 index 0000000000..b920e20268 --- /dev/null +++ b/.github/workflows/apk-kws.yaml @@ -0,0 +1,67 @@ +name: apk-kws + +on: + push: + branches: + - apk-kws + tags: + - '*' + + workflow_dispatch: + +concurrency: + group: apk-kws-${{ github.ref }} + cancel-in-progress: true + +permissions: + contents: write + +jobs: + apk: + runs-on: ${{ matrix.os }} + strategy: + fail-fast: false + matrix: + os: [ubuntu-latest] + + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: ccache + uses: hendrikmuhs/ccache-action@v1.2 + with: + key: ${{ matrix.os }}-android + + - name: Display NDK HOME + shell: bash + run: | + echo "ANDROID_NDK_LATEST_HOME: ${ANDROID_NDK_LATEST_HOME}" + ls -lh ${ANDROID_NDK_LATEST_HOME} + + - name: build APK + shell: bash + run: | + export CMAKE_CXX_COMPILER_LAUNCHER=ccache + export PATH="/usr/lib/ccache:/usr/local/opt/ccache/libexec:$PATH" + cmake --version + + export ANDROID_NDK=$ANDROID_NDK_LATEST_HOME + ./build-kws-apk.sh + + - name: Display APK + shell: bash + run: | + ls -lh ./apks/ + + - uses: actions/upload-artifact@v3 + with: + path: ./apks/*.apk + + - name: Release APK + uses: svenstaro/upload-release-action@v2 + with: + file_glob: true + file: apks/*.apk + overwrite: true diff --git a/.github/workflows/linux.yaml b/.github/workflows/linux.yaml index 3c1c167501..8c2c6749a7 100644 --- a/.github/workflows/linux.yaml +++ b/.github/workflows/linux.yaml @@ -107,6 +107,14 @@ jobs: name: release-static path: build/bin/* + - name: Test transducer kws + shell: bash + run: | + export PATH=$PWD/build/bin:$PATH + export EXE=sherpa-onnx-keyword-spotter + + .github/scripts/test-kws.sh + - name: Test online CTC shell: bash run: | diff --git a/.github/workflows/macos.yaml b/.github/workflows/macos.yaml index b73b0a5043..7c3b9940ab 100644 --- a/.github/workflows/macos.yaml +++ b/.github/workflows/macos.yaml @@ -98,6 +98,14 @@ jobs: otool -L build/bin/sherpa-onnx otool -l build/bin/sherpa-onnx + - name: Test transducer kws + shell: bash + run: | + export PATH=$PWD/build/bin:$PATH + export EXE=sherpa-onnx-keyword-spotter + + .github/scripts/test-kws.sh + - name: Test online CTC shell: bash run: | @@ -106,7 +114,6 @@ jobs: .github/scripts/test-online-ctc.sh - - name: Test offline TTS shell: bash run: | diff --git a/.github/workflows/run-python-test.yaml b/.github/workflows/run-python-test.yaml index 351c38ebff..be59076f6f 100644 --- a/.github/workflows/run-python-test.yaml +++ b/.github/workflows/run-python-test.yaml @@ -62,7 +62,7 @@ jobs: - name: Install Python dependencies shell: bash run: | - python3 -m pip install --upgrade pip numpy sentencepiece==0.1.96 soundfile + python3 -m pip install --upgrade pip numpy pypinyin sentencepiece==0.1.96 soundfile - name: Install sherpa-onnx shell: bash diff --git a/.github/workflows/test-python-offline-websocket-server.yaml b/.github/workflows/test-python-offline-websocket-server.yaml index a8415ea072..58e36e5db3 100644 --- a/.github/workflows/test-python-offline-websocket-server.yaml +++ b/.github/workflows/test-python-offline-websocket-server.yaml @@ -45,7 +45,7 @@ jobs: - name: Install Python dependencies shell: bash run: | - python3 -m pip install --upgrade pip numpy sentencepiece + python3 -m pip install --upgrade pip numpy pypinyin sentencepiece - name: Install sherpa-onnx shell: bash diff --git a/.github/workflows/test-python-online-websocket-server.yaml b/.github/workflows/test-python-online-websocket-server.yaml index e32366990d..60fbbdf8dc 100644 --- a/.github/workflows/test-python-online-websocket-server.yaml +++ b/.github/workflows/test-python-online-websocket-server.yaml @@ -45,7 +45,7 @@ jobs: - name: Install Python dependencies shell: bash run: | - python3 -m pip install --upgrade pip numpy sentencepiece + python3 -m pip install --upgrade pip numpy pypinyin sentencepiece - name: Install sherpa-onnx shell: bash diff --git a/android/SherpaOnnxKws/.gitignore b/android/SherpaOnnxKws/.gitignore new file mode 100644 index 0000000000..aa724b7707 --- /dev/null +++ b/android/SherpaOnnxKws/.gitignore @@ -0,0 +1,15 @@ +*.iml +.gradle +/local.properties +/.idea/caches +/.idea/libraries +/.idea/modules.xml +/.idea/workspace.xml +/.idea/navEditor.xml +/.idea/assetWizardSettings.xml +.DS_Store +/build +/captures +.externalNativeBuild +.cxx +local.properties diff --git a/android/SherpaOnnxKws/app/.gitignore b/android/SherpaOnnxKws/app/.gitignore new file mode 100644 index 0000000000..42afabfd2a --- /dev/null +++ b/android/SherpaOnnxKws/app/.gitignore @@ -0,0 +1 @@ +/build \ No newline at end of file diff --git a/android/SherpaOnnxKws/app/build.gradle b/android/SherpaOnnxKws/app/build.gradle new file mode 100644 index 0000000000..d64be8079c --- /dev/null +++ b/android/SherpaOnnxKws/app/build.gradle @@ -0,0 +1,44 @@ +plugins { + id 'com.android.application' + id 'org.jetbrains.kotlin.android' +} + +android { + namespace 'com.k2fsa.sherpa.onnx' + compileSdk 32 + + defaultConfig { + applicationId "com.k2fsa.sherpa.onnx" + minSdk 21 + targetSdk 32 + versionCode 1 + versionName "1.0" + + 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 + } + kotlinOptions { + jvmTarget = '1.8' + } +} + +dependencies { + + implementation 'androidx.core:core-ktx:1.7.0' + implementation 'androidx.appcompat:appcompat:1.5.1' + implementation 'com.google.android.material:material:1.7.0' + implementation 'androidx.constraintlayout:constraintlayout:2.1.4' + testImplementation 'junit:junit:4.13.2' + androidTestImplementation 'androidx.test.ext:junit:1.1.4' + androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.0' +} \ No newline at end of file diff --git a/android/SherpaOnnxKws/app/proguard-rules.pro b/android/SherpaOnnxKws/app/proguard-rules.pro new file mode 100644 index 0000000000..481bb43481 --- /dev/null +++ b/android/SherpaOnnxKws/app/proguard-rules.pro @@ -0,0 +1,21 @@ +# 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 \ No newline at end of file diff --git a/android/SherpaOnnxKws/app/src/androidTest/java/com/k2fsa/sherpa/onnx/ExampleInstrumentedTest.kt b/android/SherpaOnnxKws/app/src/androidTest/java/com/k2fsa/sherpa/onnx/ExampleInstrumentedTest.kt new file mode 100644 index 0000000000..1833832023 --- /dev/null +++ b/android/SherpaOnnxKws/app/src/androidTest/java/com/k2fsa/sherpa/onnx/ExampleInstrumentedTest.kt @@ -0,0 +1,24 @@ +package com.k2fsa.sherpa.onnx + +import androidx.test.platform.app.InstrumentationRegistry +import androidx.test.ext.junit.runners.AndroidJUnit4 + +import org.junit.Test +import org.junit.runner.RunWith + +import org.junit.Assert.* + +/** + * Instrumented test, which will execute on an Android device. + * + * See [testing documentation](http://d.android.com/tools/testing). + */ +@RunWith(AndroidJUnit4::class) +class ExampleInstrumentedTest { + @Test + fun useAppContext() { + // Context of the app under test. + val appContext = InstrumentationRegistry.getInstrumentation().targetContext + assertEquals("com.k2fsa.sherpa.onnx", appContext.packageName) + } +} \ No newline at end of file diff --git a/android/SherpaOnnxKws/app/src/main/AndroidManifest.xml b/android/SherpaOnnxKws/app/src/main/AndroidManifest.xml new file mode 100644 index 0000000000..935fb0e958 --- /dev/null +++ b/android/SherpaOnnxKws/app/src/main/AndroidManifest.xml @@ -0,0 +1,32 @@ + + + + + + + + + + + + + + + + + + diff --git a/android/SherpaOnnxKws/app/src/main/assets/.gitkeep b/android/SherpaOnnxKws/app/src/main/assets/.gitkeep new file mode 100644 index 0000000000..e69de29bb2 diff --git a/android/SherpaOnnxKws/app/src/main/java/com/k2fsa/sherpa/onnx/MainActivity.kt b/android/SherpaOnnxKws/app/src/main/java/com/k2fsa/sherpa/onnx/MainActivity.kt new file mode 100644 index 0000000000..83c8abe311 --- /dev/null +++ b/android/SherpaOnnxKws/app/src/main/java/com/k2fsa/sherpa/onnx/MainActivity.kt @@ -0,0 +1,207 @@ +package com.k2fsa.sherpa.onnx + +import android.Manifest +import android.content.pm.PackageManager +import android.media.AudioFormat +import android.media.AudioRecord +import android.media.MediaRecorder +import android.os.Bundle +import android.text.method.ScrollingMovementMethod +import android.util.Log +import android.widget.Button +import android.widget.EditText +import android.widget.TextView +import android.widget.Toast +import androidx.appcompat.app.AppCompatActivity +import androidx.core.app.ActivityCompat +import com.k2fsa.sherpa.onnx.* +import kotlin.concurrent.thread + +private const val TAG = "sherpa-onnx" +private const val REQUEST_RECORD_AUDIO_PERMISSION = 200 + +class MainActivity : AppCompatActivity() { + private val permissions: Array = arrayOf(Manifest.permission.RECORD_AUDIO) + + private lateinit var model: SherpaOnnxKws + private var audioRecord: AudioRecord? = null + private lateinit var recordButton: Button + private lateinit var textView: TextView + private lateinit var inputText: EditText + private var recordingThread: Thread? = null + + private val audioSource = MediaRecorder.AudioSource.MIC + private val sampleRateInHz = 16000 + private val channelConfig = AudioFormat.CHANNEL_IN_MONO + + // Note: We don't use AudioFormat.ENCODING_PCM_FLOAT + // since the AudioRecord.read(float[]) needs API level >= 23 + // but we are targeting API level >= 21 + private val audioFormat = AudioFormat.ENCODING_PCM_16BIT + private var idx: Int = 0 + private var lastText: String = "" + + @Volatile + private var isRecording: Boolean = false + + override fun onRequestPermissionsResult( + requestCode: Int, permissions: Array, grantResults: IntArray + ) { + super.onRequestPermissionsResult(requestCode, permissions, grantResults) + val permissionToRecordAccepted = if (requestCode == REQUEST_RECORD_AUDIO_PERMISSION) { + grantResults[0] == PackageManager.PERMISSION_GRANTED + } else { + false + } + + if (!permissionToRecordAccepted) { + Log.e(TAG, "Audio record is disallowed") + finish() + } + + Log.i(TAG, "Audio record is permitted") + } + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + setContentView(R.layout.activity_main) + + ActivityCompat.requestPermissions(this, permissions, REQUEST_RECORD_AUDIO_PERMISSION) + + Log.i(TAG, "Start to initialize model") + initModel() + Log.i(TAG, "Finished initializing model") + + recordButton = findViewById(R.id.record_button) + recordButton.setOnClickListener { onclick() } + + textView = findViewById(R.id.my_text) + textView.movementMethod = ScrollingMovementMethod() + + inputText = findViewById(R.id.input_text) + } + + private fun onclick() { + if (!isRecording) { + var keywords = inputText.text.toString() + + Log.i(TAG, keywords) + keywords = keywords.replace("\n", "/") + // If keywords is an empty string, it just resets the decoding stream + // always returns true in this case. + // If keywords is not empty, it will create a new decoding stream with + // the given keywords appended to the default keywords. + // Return false if errors occured when adding keywords, true otherwise. + val status = model.reset(keywords) + if (!status) { + Log.i(TAG, "Failed to reset with keywords.") + Toast.makeText(this, "Failed to set keywords.", Toast.LENGTH_LONG).show(); + return + } + + val ret = initMicrophone() + if (!ret) { + Log.e(TAG, "Failed to initialize microphone") + return + } + Log.i(TAG, "state: ${audioRecord?.state}") + audioRecord!!.startRecording() + recordButton.setText(R.string.stop) + isRecording = true + textView.text = "" + lastText = "" + idx = 0 + + recordingThread = thread(true) { + processSamples() + } + Log.i(TAG, "Started recording") + } else { + isRecording = false + audioRecord!!.stop() + audioRecord!!.release() + audioRecord = null + recordButton.setText(R.string.start) + Log.i(TAG, "Stopped recording") + } + } + + private fun processSamples() { + Log.i(TAG, "processing samples") + + val interval = 0.1 // i.e., 100 ms + val bufferSize = (interval * sampleRateInHz).toInt() // in samples + val buffer = ShortArray(bufferSize) + + while (isRecording) { + val ret = audioRecord?.read(buffer, 0, buffer.size) + if (ret != null && ret > 0) { + val samples = FloatArray(ret) { buffer[it] / 32768.0f } + model.acceptWaveform(samples, sampleRate=sampleRateInHz) + while (model.isReady()) { + model.decode() + } + + val text = model.keyword + + var textToDisplay = lastText; + + if(text.isNotBlank()) { + if (lastText.isBlank()) { + textToDisplay = "${idx}: ${text}" + } else { + textToDisplay = "${idx}: ${text}\n${lastText}" + } + lastText = "${idx}: ${text}\n${lastText}" + idx += 1 + } + + runOnUiThread { + textView.text = textToDisplay + } + } + } + } + + private fun initMicrophone(): Boolean { + if (ActivityCompat.checkSelfPermission( + this, Manifest.permission.RECORD_AUDIO + ) != PackageManager.PERMISSION_GRANTED + ) { + ActivityCompat.requestPermissions(this, permissions, REQUEST_RECORD_AUDIO_PERMISSION) + return false + } + + val numBytes = AudioRecord.getMinBufferSize(sampleRateInHz, channelConfig, audioFormat) + Log.i( + TAG, "buffer size in milliseconds: ${numBytes * 1000.0f / sampleRateInHz}" + ) + + audioRecord = AudioRecord( + audioSource, + sampleRateInHz, + channelConfig, + audioFormat, + numBytes * 2 // a sample has two bytes as we are using 16-bit PCM + ) + return true + } + + private fun initModel() { + // Please change getModelConfig() to add new models + // See https://k2-fsa.github.io/sherpa/onnx/kws/pretrained_models/index.html + // for a list of available models + val type = 0 + Log.i(TAG, "Select model type ${type}") + val config = KeywordSpotterConfig( + featConfig = getFeatureConfig(sampleRate = sampleRateInHz, featureDim = 80), + modelConfig = getModelConfig(type = type)!!, + keywordsFile = getKeywordsFile(type = type)!!, + ) + + model = SherpaOnnxKws( + assetManager = application.assets, + config = config, + ) + } +} diff --git a/android/SherpaOnnxKws/app/src/main/java/com/k2fsa/sherpa/onnx/SherpaOnnx.kt b/android/SherpaOnnxKws/app/src/main/java/com/k2fsa/sherpa/onnx/SherpaOnnx.kt new file mode 100644 index 0000000000..d40692665a --- /dev/null +++ b/android/SherpaOnnxKws/app/src/main/java/com/k2fsa/sherpa/onnx/SherpaOnnx.kt @@ -0,0 +1,162 @@ +// Copyright (c) 2024 Xiaomi Corporation +package com.k2fsa.sherpa.onnx + +import android.content.res.AssetManager + +data class OnlineTransducerModelConfig( + var encoder: String = "", + var decoder: String = "", + var joiner: String = "", +) + +data class OnlineModelConfig( + var transducer: OnlineTransducerModelConfig = OnlineTransducerModelConfig(), + var tokens: String, + var numThreads: Int = 1, + var debug: Boolean = false, + var provider: String = "cpu", + var modelType: String = "", +) + +data class FeatureConfig( + var sampleRate: Int = 16000, + var featureDim: Int = 80, +) + +data class KeywordSpotterConfig( + var featConfig: FeatureConfig = FeatureConfig(), + var modelConfig: OnlineModelConfig, + var maxActivePaths: Int = 4, + var keywordsFile: String = "keywords.txt", + var keywordsScore: Float = 1.5f, + var keywordsThreshold: Float = 0.25f, + var numTrailingBlanks: Int = 2, +) + +class SherpaOnnxKws( + assetManager: AssetManager? = null, + var config: KeywordSpotterConfig, +) { + private val ptr: Long + + init { + if (assetManager != null) { + ptr = new(assetManager, config) + } else { + ptr = newFromFile(config) + } + } + + protected fun finalize() { + delete(ptr) + } + + fun acceptWaveform(samples: FloatArray, sampleRate: Int) = + acceptWaveform(ptr, samples, sampleRate) + + fun inputFinished() = inputFinished(ptr) + fun decode() = decode(ptr) + fun isReady(): Boolean = isReady(ptr) + fun reset(keywords: String): Boolean = reset(ptr, keywords) + + val keyword: String + get() = getKeyword(ptr) + + private external fun delete(ptr: Long) + + private external fun new( + assetManager: AssetManager, + config: KeywordSpotterConfig, + ): Long + + private external fun newFromFile( + config: KeywordSpotterConfig, + ): Long + + private external fun acceptWaveform(ptr: Long, samples: FloatArray, sampleRate: Int) + private external fun inputFinished(ptr: Long) + private external fun getKeyword(ptr: Long): String + private external fun reset(ptr: Long, keywords: String): Boolean + private external fun decode(ptr: Long) + private external fun isReady(ptr: Long): Boolean + + companion object { + init { + System.loadLibrary("sherpa-onnx-jni") + } + } +} + +fun getFeatureConfig(sampleRate: Int, featureDim: Int): FeatureConfig { + return FeatureConfig(sampleRate = sampleRate, featureDim = featureDim) +} + +/* +Please see +https://k2-fsa.github.io/sherpa/onnx/kws/pretrained_models/index.html +for a list of pre-trained models. + +We only add a few here. Please change the following code +to add your own. (It should be straightforward to add a new model +by following the code) + +@param type +0 - sherpa-onnx-kws-zipformer-wenetspeech-3.3M-2024-01-01 (Chinese) + https://www.modelscope.cn/models/pkufool/sherpa-onnx-kws-zipformer-wenetspeech-3.3M-2024-01-01/summary + +1 - sherpa-onnx-kws-zipformer-gigaspeech-3.3M-2024-01-01 (English) + https://www.modelscope.cn/models/pkufool/sherpa-onnx-kws-zipformer-gigaspeech-3.3M-2024-01-01/summary + + */ +fun getModelConfig(type: Int): OnlineModelConfig? { + when (type) { + 0 -> { + val modelDir = "sherpa-onnx-kws-zipformer-wenetspeech-3.3M-2024-01-01" + return OnlineModelConfig( + transducer = OnlineTransducerModelConfig( + encoder = "$modelDir/encoder-epoch-12-avg-2-chunk-16-left-64.onnx", + decoder = "$modelDir/decoder-epoch-12-avg-2-chunk-16-left-64.onnx", + joiner = "$modelDir/joiner-epoch-12-avg-2-chunk-16-left-64.onnx", + ), + tokens = "$modelDir/tokens.txt", + modelType = "zipformer2", + ) + } + + 1 -> { + val modelDir = "sherpa-onnx-kws-zipformer-gigaspeech-3.3M-2024-01-01" + return OnlineModelConfig( + transducer = OnlineTransducerModelConfig( + encoder = "$modelDir/encoder-epoch-12-avg-2-chunk-16-left-64.onnx", + decoder = "$modelDir/decoder-epoch-12-avg-2-chunk-16-left-64.onnx", + joiner = "$modelDir/joiner-epoch-12-avg-2-chunk-16-left-64.onnx", + ), + tokens = "$modelDir/tokens.txt", + modelType = "zipformer2", + ) + } + + } + return null; +} + +/* + * Get the default keywords for each model. + * Caution: The types and modelDir should be the same as those in getModelConfig + * function above. + */ +fun getKeywordsFile(type: Int) : String { + when (type) { + 0 -> { + val modelDir = "sherpa-onnx-kws-zipformer-wenetspeech-3.3M-2024-01-01" + return "$modelDir/keywords.txt" + } + + 1 -> { + val modelDir = "sherpa-onnx-kws-zipformer-gigaspeech-3.3M-2024-01-01" + return "$modelDir/keywords.txt" + } + + } + return ""; +} diff --git a/android/SherpaOnnxKws/app/src/main/java/com/k2fsa/sherpa/onnx/WaveReader.kt b/android/SherpaOnnxKws/app/src/main/java/com/k2fsa/sherpa/onnx/WaveReader.kt new file mode 100644 index 0000000000..dca3998404 --- /dev/null +++ b/android/SherpaOnnxKws/app/src/main/java/com/k2fsa/sherpa/onnx/WaveReader.kt @@ -0,0 +1,29 @@ +// Copyright (c) 2023 Xiaomi Corporation +package com.k2fsa.sherpa.onnx + +import android.content.res.AssetManager + +class WaveReader { + companion object { + // Read a mono wave file asset + // The returned array has two entries: + // - the first entry contains an 1-D float array + // - the second entry is the sample rate + external fun readWaveFromAsset( + assetManager: AssetManager, + filename: String, + ): Array + + // Read a mono wave file from disk + // The returned array has two entries: + // - the first entry contains an 1-D float array + // - the second entry is the sample rate + external fun readWaveFromFile( + filename: String, + ): Array + + init { + System.loadLibrary("sherpa-onnx-jni") + } + } +} diff --git a/android/SherpaOnnxKws/app/src/main/jniLibs/.gitignore b/android/SherpaOnnxKws/app/src/main/jniLibs/.gitignore new file mode 100644 index 0000000000..949c039f10 --- /dev/null +++ b/android/SherpaOnnxKws/app/src/main/jniLibs/.gitignore @@ -0,0 +1,4 @@ +*.so +*.txt +*.onnx +*.wav diff --git a/android/SherpaOnnxKws/app/src/main/jniLibs/arm64-v8a/.gitkeep b/android/SherpaOnnxKws/app/src/main/jniLibs/arm64-v8a/.gitkeep new file mode 100644 index 0000000000..e69de29bb2 diff --git a/android/SherpaOnnxKws/app/src/main/jniLibs/armeabi-v7a/.gitkeep b/android/SherpaOnnxKws/app/src/main/jniLibs/armeabi-v7a/.gitkeep new file mode 100644 index 0000000000..e69de29bb2 diff --git a/android/SherpaOnnxKws/app/src/main/jniLibs/x86/.gitkeep b/android/SherpaOnnxKws/app/src/main/jniLibs/x86/.gitkeep new file mode 100644 index 0000000000..e69de29bb2 diff --git a/android/SherpaOnnxKws/app/src/main/jniLibs/x86_64/.gitkeep b/android/SherpaOnnxKws/app/src/main/jniLibs/x86_64/.gitkeep new file mode 100644 index 0000000000..e69de29bb2 diff --git a/android/SherpaOnnxKws/app/src/main/res/drawable-v24/ic_launcher_foreground.xml b/android/SherpaOnnxKws/app/src/main/res/drawable-v24/ic_launcher_foreground.xml new file mode 100644 index 0000000000..2b068d1146 --- /dev/null +++ b/android/SherpaOnnxKws/app/src/main/res/drawable-v24/ic_launcher_foreground.xml @@ -0,0 +1,30 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/android/SherpaOnnxKws/app/src/main/res/drawable/ic_launcher_background.xml b/android/SherpaOnnxKws/app/src/main/res/drawable/ic_launcher_background.xml new file mode 100644 index 0000000000..07d5da9cbf --- /dev/null +++ b/android/SherpaOnnxKws/app/src/main/res/drawable/ic_launcher_background.xml @@ -0,0 +1,170 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/android/SherpaOnnxKws/app/src/main/res/layout/activity_main.xml b/android/SherpaOnnxKws/app/src/main/res/layout/activity_main.xml new file mode 100644 index 0000000000..fe5442809b --- /dev/null +++ b/android/SherpaOnnxKws/app/src/main/res/layout/activity_main.xml @@ -0,0 +1,46 @@ + + + + + + + + + +