-
Notifications
You must be signed in to change notification settings - Fork 445
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add jni interface and kotlin API examples for TTS. (#381)
- Loading branch information
1 parent
b582f6c
commit 0fdb204
Showing
15 changed files
with
454 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
hs_err* | ||
main.jar | ||
vits-zh-aishell3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
// Copyright (c) 2023 Xiaomi Corporation | ||
package com.k2fsa.sherpa.onnx | ||
|
||
import android.content.res.AssetManager | ||
|
||
data class OfflineTtsVitsModelConfig( | ||
var model: String, | ||
var lexicon: String, | ||
var tokens: String, | ||
var noiseScale: Float = 0.667f, | ||
var noiseScaleW: Float = 0.8f, | ||
var lengthScale: Float = 1.0f, | ||
) | ||
|
||
data class OfflineTtsModelConfig( | ||
var vits: OfflineTtsVitsModelConfig, | ||
var numThreads: Int = 1, | ||
var debug: Boolean = false, | ||
var provider: String = "cpu", | ||
) | ||
|
||
data class OfflineTtsConfig( | ||
var model: OfflineTtsModelConfig, | ||
) | ||
|
||
class GeneratedAudio( | ||
val samples : FloatArray, | ||
val sampleRate: Int, | ||
) { | ||
fun save(filename: String) = saveImpl(filename=filename, samples=samples, sampleRate=sampleRate) | ||
|
||
private external fun saveImpl( | ||
filename: String, | ||
samples: FloatArray, | ||
sampleRate: Int | ||
): Boolean | ||
} | ||
|
||
class OfflineTts( | ||
assetManager: AssetManager? = null, | ||
var config: OfflineTtsConfig, | ||
) { | ||
private var ptr: Long | ||
|
||
init { | ||
if (assetManager != null) { | ||
ptr = new(assetManager, config) | ||
} else { | ||
ptr = newFromFile(config) | ||
} | ||
} | ||
|
||
fun generate( | ||
text: String, | ||
sid: Int = 0, | ||
speed: Float = 1.0f | ||
): GeneratedAudio { | ||
var objArray = generateImpl(ptr, text=text, sid=sid, speed=speed) | ||
return GeneratedAudio(samples=objArray[0] as FloatArray, | ||
sampleRate=objArray[1] as Int) | ||
} | ||
|
||
fun allocate(assetManager: AssetManager? = null) { | ||
if (ptr == 0L) { | ||
if (assetManager != null) { | ||
ptr = new(assetManager, config) | ||
} else { | ||
ptr = newFromFile(config) | ||
} | ||
} | ||
} | ||
|
||
fun free() { | ||
if (ptr != 0L) { | ||
delete(ptr) | ||
ptr = 0 | ||
} | ||
} | ||
|
||
protected fun finalize() { | ||
delete(ptr) | ||
} | ||
|
||
private external fun new( | ||
assetManager: AssetManager, | ||
config: OfflineTtsConfig, | ||
): Long | ||
|
||
private external fun newFromFile( | ||
config: OfflineTtsConfig, | ||
): Long | ||
|
||
private external fun delete(ptr: Long) | ||
|
||
// The returned array has two entries: | ||
// - the first entry is an 1-D float array containing audio samples. | ||
// Each sample is normalized to the range [-1, 1] | ||
// - the second entry is the sample rate | ||
external fun generateImpl( | ||
ptr: Long, | ||
text: String, | ||
sid: Int = 0, | ||
speed: Float = 1.0f | ||
): Array<Any> | ||
|
||
companion object { | ||
init { | ||
System.loadLibrary("sherpa-onnx-jni") | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.