1
1
package com.k2fsa.sherpa.onnx.tts.engine
2
2
3
- import android.app.Application
3
+ import android.content.Context
4
4
import android.content.res.AssetManager
5
5
import android.util.Log
6
6
import androidx.compose.runtime.MutableState
@@ -21,7 +21,6 @@ object TtsEngine {
21
21
var lang: String? = null
22
22
23
23
24
-
25
24
val speedState: MutableState <Float > = mutableStateOf(1.0F )
26
25
val speakerIdState: MutableState <Int > = mutableStateOf(0 )
27
26
@@ -44,19 +43,7 @@ object TtsEngine {
44
43
private var dataDir: String? = null
45
44
private var assets: AssetManager ? = null
46
45
47
- private var application: Application ? = null
48
-
49
- fun createTts (application : Application ) {
50
- Log .i(TAG , " Init Next-gen Kaldi TTS" )
51
- if (tts == null ) {
52
- this .application = application
53
- initTts()
54
- }
55
- }
56
-
57
- private fun initTts () {
58
- assets = application?.assets
59
-
46
+ init {
60
47
// The purpose of such a design is to make the CI test easier
61
48
// Please see
62
49
// https://github.com/k2-fsa/sherpa-onnx/blob/master/scripts/apk/generate-tts-apk-script.py
@@ -89,9 +76,21 @@ object TtsEngine {
89
76
// ruleFsts = "vits-zh-aishell3/rule.fst"
90
77
// lexicon = "lexicon.txt"
91
78
// lang = "zho"
79
+ }
80
+
81
+
82
+ fun createTts (context : Context ) {
83
+ Log .i(TAG , " Init Next-gen Kaldi TTS" )
84
+ if (tts == null ) {
85
+ initTts(context)
86
+ }
87
+ }
88
+
89
+ private fun initTts (context : Context ) {
90
+ assets = context.assets
92
91
93
92
if (dataDir != null ) {
94
- val newDir = copyDataDir(modelDir!! )
93
+ val newDir = copyDataDir(context, modelDir!! )
95
94
modelDir = newDir + " /" + modelDir
96
95
dataDir = newDir + " /" + dataDir
97
96
assets = null
@@ -107,39 +106,39 @@ object TtsEngine {
107
106
}
108
107
109
108
110
- private fun copyDataDir (dataDir : String ): String {
109
+ private fun copyDataDir (context : Context , dataDir : String ): String {
111
110
println (" data dir is $dataDir " )
112
- copyAssets(dataDir)
111
+ copyAssets(context, dataDir)
113
112
114
- val newDataDir = application !! .getExternalFilesDir(null )!! .absolutePath
113
+ val newDataDir = context .getExternalFilesDir(null )!! .absolutePath
115
114
println (" newDataDir: $newDataDir " )
116
115
return newDataDir
117
116
}
118
117
119
- private fun copyAssets (path : String ) {
118
+ private fun copyAssets (context : Context , path : String ) {
120
119
val assets: Array <String >?
121
120
try {
122
- assets = application !! .assets.list(path)
121
+ assets = context .assets.list(path)
123
122
if (assets!! .isEmpty()) {
124
- copyFile(path)
123
+ copyFile(context, path)
125
124
} else {
126
- val fullPath = " ${application !! .getExternalFilesDir(null )} /$path "
125
+ val fullPath = " ${context .getExternalFilesDir(null )} /$path "
127
126
val dir = File (fullPath)
128
127
dir.mkdirs()
129
128
for (asset in assets.iterator()) {
130
129
val p: String = if (path == " " ) " " else path + " /"
131
- copyAssets(p + asset)
130
+ copyAssets(context, p + asset)
132
131
}
133
132
}
134
133
} catch (ex: IOException ) {
135
134
Log .e(TAG , " Failed to copy $path . ${ex.toString()} " )
136
135
}
137
136
}
138
137
139
- private fun copyFile (filename : String ) {
138
+ private fun copyFile (context : Context , filename : String ) {
140
139
try {
141
- val istream = application !! .assets.open(filename)
142
- val newFilename = application !! .getExternalFilesDir(null ).toString() + " /" + filename
140
+ val istream = context .assets.open(filename)
141
+ val newFilename = context .getExternalFilesDir(null ).toString() + " /" + filename
143
142
val ostream = FileOutputStream (newFilename)
144
143
// Log.i(TAG, "Copying $filename to $newFilename")
145
144
val buffer = ByteArray (1024 )
0 commit comments