live2d model
This commit is contained in:
60
app/src/main/java/com/digitalperson/util/FileHelper.kt
Normal file
60
app/src/main/java/com/digitalperson/util/FileHelper.kt
Normal file
@@ -0,0 +1,60 @@
|
||||
package com.digitalperson.util
|
||||
|
||||
import android.content.Context
|
||||
import android.util.Log
|
||||
import com.digitalperson.config.AppConfig
|
||||
import java.io.File
|
||||
import java.io.FileOutputStream
|
||||
|
||||
object FileHelper {
|
||||
private const val TAG = AppConfig.TAG
|
||||
|
||||
fun assetExists(context: Context, path: String): Boolean {
|
||||
return try {
|
||||
context.assets.open(path).close()
|
||||
true
|
||||
} catch (_: Throwable) {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
fun copyAssetsToInternal(context: Context, assetDir: String, targetDir: File, files: Array<String>): File {
|
||||
if (!targetDir.exists()) targetDir.mkdirs()
|
||||
|
||||
for (name in files) {
|
||||
val assetPath = "$assetDir/$name"
|
||||
val outFile = File(targetDir, name)
|
||||
if (outFile.exists() && outFile.length() > 0) continue
|
||||
try {
|
||||
context.assets.open(assetPath).use { input ->
|
||||
FileOutputStream(outFile).use { output ->
|
||||
input.copyTo(output)
|
||||
}
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
Log.e(TAG, "Failed to copy asset $assetPath: ${e.message}")
|
||||
}
|
||||
}
|
||||
return targetDir
|
||||
}
|
||||
|
||||
fun copySenseVoiceAssets(context: Context): File {
|
||||
val outDir = File(context.filesDir, AppConfig.Asr.MODEL_DIR)
|
||||
val files = arrayOf(
|
||||
"am.mvn",
|
||||
"chn_jpn_yue_eng_ko_spectok.bpe.model",
|
||||
"embedding.npy",
|
||||
"sense-voice-encoder.rknn"
|
||||
)
|
||||
return copyAssetsToInternal(context, AppConfig.Asr.MODEL_DIR, outDir, files)
|
||||
}
|
||||
|
||||
fun ensureDir(dir: File): File {
|
||||
if (!dir.exists()) dir.mkdirs()
|
||||
return dir
|
||||
}
|
||||
|
||||
fun getAsrAudioDir(context: Context): File {
|
||||
return ensureDir(File(context.filesDir, "asr_audio"))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user