Skip to content

Commit

Permalink
Replace Android system TTS engine (#508)
Browse files Browse the repository at this point in the history
  • Loading branch information
csukuangfj authored Dec 31, 2023
1 parent e475e75 commit d7e10bb
Show file tree
Hide file tree
Showing 57 changed files with 1,488 additions and 0 deletions.
5 changes: 5 additions & 0 deletions android/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,8 @@ for usage.

- [SherpaOnnxVadAsr](./SherpaOnnxVadAsr) It uses a VAD with a non-streaming
ASR model.

- [SherpaOnnxTts](./SherpaOnnxTts) It is for standalone text-to-speech.

- [SherpaOnnxTtsEngine](./SherpaOnnxTtsEngine) It is for text-to-speech engine;
you can use it to replace the system TTS engine.
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ class OfflineTts(

fun sampleRate() = getSampleRate(ptr)

fun numSpeakers() = getNumSpeakers(ptr)

fun generate(
text: String,
sid: Int = 0,
Expand Down Expand Up @@ -113,6 +115,7 @@ class OfflineTts(

private external fun delete(ptr: Long)
private external fun getSampleRate(ptr: Long): Int
private external fun getNumSpeakers(ptr: Long): Int

// The returned array has two entries:
// - the first entry is an 1-D float array containing audio samples.
Expand Down
15 changes: 15 additions & 0 deletions android/SherpaOnnxTtsEngine/.gitignore
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions android/SherpaOnnxTtsEngine/app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
71 changes: 71 additions & 0 deletions android/SherpaOnnxTtsEngine/app/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
plugins {
id("com.android.application")
id("org.jetbrains.kotlin.android")
}

android {
namespace = "com.k2fsa.sherpa.onnx.tts.engine"
compileSdk = 34

defaultConfig {
applicationId = "com.k2fsa.sherpa.onnx.tts.engine"
minSdk = 21
targetSdk = 34
versionCode = 1
versionName = "1.0"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables {
useSupportLibrary = true
}
}

buildTypes {
release {
isMinifyEnabled = 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"
}
buildFeatures {
compose = true
}
composeOptions {
kotlinCompilerExtensionVersion = "1.5.1"
}
packaging {
resources {
excludes += "/META-INF/{AL2.0,LGPL2.1}"
}
}
}

dependencies {

implementation("androidx.core:core-ktx:1.12.0")
implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.6.2")
implementation("androidx.activity:activity-compose:1.8.2")
implementation(platform("androidx.compose:compose-bom:2023.08.00"))
implementation("androidx.compose.ui:ui")
implementation("androidx.compose.ui:ui-graphics")
implementation("androidx.compose.ui:ui-tooling-preview")
implementation("androidx.compose.material3:material3")
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")
androidTestImplementation(platform("androidx.compose:compose-bom:2023.08.00"))
androidTestImplementation("androidx.compose.ui:ui-test-junit4")
debugImplementation("androidx.compose.ui:ui-tooling")
debugImplementation("androidx.compose.ui:ui-test-manifest")
}
21 changes: 21 additions & 0 deletions android/SherpaOnnxTtsEngine/app/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.k2fsa.sherpa.onnx.tts.engine

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.tts.engine", appContext.packageName)
}
}
78 changes: 78 additions & 0 deletions android/SherpaOnnxTtsEngine/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.SherpaOnnxTtsEngine"
tools:targetApi="31">
<activity
android:name=".GetSampleText"
android:exported="true"
android:theme="@android:style/Theme.Translucent.NoTitleBar">
<intent-filter>
<action android:name="android.speech.tts.engine.GET_SAMPLE_TEXT" />

<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".CheckVoiceData"
android:exported="true">
<intent-filter>
<action android:name="android.speech.tts.engine.CHECK_TTS_DATA" />

<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".InstallVoiceData"
android:exported="true">
<intent-filter>
<action android:name="android.speech.tts.engine.INSTALL_TTS_DATA" />

<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>

<service
android:name=".TtsService"
android:enabled="true"
android:exported="true"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.TTS_SERVICE" />

<category android:name="android.intent.category.DEFAULT" />
</intent-filter>

<meta-data
android:name="android.speech.tts"
android:resource="@xml/tts_engine" />
</service>

<activity
android:name=".MainActivity"
android:exported="true"
android:label="@string/app_name"
android:theme="@style/Theme.SherpaOnnxTtsEngine">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.speech.tts.engine.CONFIGURE_ENGINE" />

<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>

</manifest>
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.k2fsa.sherpa.onnx.tts.engine

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.speech.tts.TextToSpeech

class CheckVoiceData : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val intent = Intent().apply {
putStringArrayListExtra(TextToSpeech.Engine.EXTRA_AVAILABLE_VOICES, arrayListOf(TtsEngine.lang))
putStringArrayListExtra(TextToSpeech.Engine.EXTRA_UNAVAILABLE_VOICES, arrayListOf())
}
setResult(TextToSpeech.Engine.CHECK_VOICE_DATA_PASS, intent)
finish()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.k2fsa.sherpa.onnx.tts.engine

import android.app.Activity
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.speech.tts.TextToSpeech

class GetSampleText : Activity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
var result = TextToSpeech.LANG_AVAILABLE
var text: String = ""
when(TtsEngine.lang) {
"eng" -> {
text = "This is a text-to-speech engine with next generation Kaldi"
}
"zho", "cmn" -> {
text = "使用新一代 Kaldi 进行语音合成"
}
else -> {
result = TextToSpeech.LANG_NOT_SUPPORTED
}
}

val intent = Intent().apply{
if(result == TextToSpeech.LANG_AVAILABLE) {
putExtra(TextToSpeech.Engine.EXTRA_SAMPLE_TEXT, text)
} else {
putExtra("sampleText", text)
}
}

setResult(result, intent)
finish()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.k2fsa.sherpa.onnx.tts.engine

import android.app.Activity
import android.os.Bundle
import android.view.Window

class InstallVoiceData : Activity() {
override fun onCreate(savedInstanceState: Bundle?) {
requestWindowFeature(Window.FEATURE_NO_TITLE)
super.onCreate(savedInstanceState)
}
}
Loading

0 comments on commit d7e10bb

Please sign in to comment.