From 9dd0e03568295b1faef64d3cbf3916a36b04b547 Mon Sep 17 00:00:00 2001 From: Fangjun Kuang Date: Sat, 22 Jun 2024 18:18:36 +0800 Subject: [PATCH] Enable to stop TTS generation (#1041) --- CMakeLists.txt | 2 +- .../com/k2fsa/sherpa/onnx/MainActivity.kt | 35 ++++++++-- .../main/java/com/k2fsa/sherpa/onnx/Tts.kt | 4 +- .../app/src/main/res/layout/activity_main.xml | 12 ++++ .../app/src/main/res/values/strings.xml | 1 + .../sherpa/onnx/tts/engine/TtsService.kt | 7 +- .../non-streaming-asr/pubspec.yaml | 2 +- dart-api-examples/streaming-asr/pubspec.yaml | 2 +- dart-api-examples/tts/bin/piper.dart | 4 ++ dart-api-examples/tts/pubspec.yaml | 2 +- dart-api-examples/vad/pubspec.yaml | 2 +- dotnet-examples/offline-tts-play/Program.cs | 4 ++ kotlin-api-examples/test_tts.kt | 42 +++++++++++- .../NonStreamingTextToSpeech.rc | Bin 12926 -> 13026 bytes .../NonStreamingTextToSpeechDlg.cpp | 62 ++++++++++++------ .../NonStreamingTextToSpeechDlg.h | 3 + .../NonStreamingTextToSpeech/Resource.h | 3 +- nodejs-addon-examples/package.json | 2 +- python-api-examples/offline-tts-play.py | 7 ++ scripts/dotnet/OfflineTts.cs | 6 +- sherpa-onnx/c-api/c-api.cc | 10 +-- sherpa-onnx/c-api/c-api.h | 15 +++-- sherpa-onnx/csrc/offline-tts-vits-impl.h | 10 +-- sherpa-onnx/csrc/offline-tts.h | 4 +- .../csrc/sherpa-onnx-offline-tts-play-alsa.cc | 11 +++- .../csrc/sherpa-onnx-offline-tts-play.cc | 10 ++- sherpa-onnx/csrc/sherpa-onnx-offline-tts.cc | 3 +- sherpa-onnx/flutter/CHANGELOG.md | 4 ++ .../flutter/lib/src/sherpa_onnx_bindings.dart | 2 +- sherpa-onnx/flutter/lib/src/tts.dart | 6 +- sherpa-onnx/jni/offline-tts.cc | 34 +++++++++- sherpa-onnx/python/csrc/offline-tts.cc | 6 +- 32 files changed, 248 insertions(+), 69 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index d474c6aa9..ec529bab6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,7 +8,7 @@ project(sherpa-onnx) # ./nodejs-addon-examples # ./dart-api-examples/ # ./sherpa-onnx/flutter/CHANGELOG.md -set(SHERPA_ONNX_VERSION "1.10.0") +set(SHERPA_ONNX_VERSION "1.10.1") # Disable warning about # diff --git a/android/SherpaOnnxTts/app/src/main/java/com/k2fsa/sherpa/onnx/MainActivity.kt b/android/SherpaOnnxTts/app/src/main/java/com/k2fsa/sherpa/onnx/MainActivity.kt index f44bef8eb..b95ad7d78 100644 --- a/android/SherpaOnnxTts/app/src/main/java/com/k2fsa/sherpa/onnx/MainActivity.kt +++ b/android/SherpaOnnxTts/app/src/main/java/com/k2fsa/sherpa/onnx/MainActivity.kt @@ -26,6 +26,9 @@ class MainActivity : AppCompatActivity() { private lateinit var speed: EditText private lateinit var generate: Button private lateinit var play: Button + private lateinit var stop: Button + private var stopped: Boolean = false + private var mediaPlayer: MediaPlayer? = null // see // https://developer.android.com/reference/kotlin/android/media/AudioTrack @@ -49,9 +52,11 @@ class MainActivity : AppCompatActivity() { generate = findViewById(R.id.generate) play = findViewById(R.id.play) + stop = findViewById(R.id.stop) generate.setOnClickListener { onClickGenerate() } play.setOnClickListener { onClickPlay() } + stop.setOnClickListener { onClickStop() } sid.setText("0") speed.setText("1.0") @@ -70,7 +75,7 @@ class MainActivity : AppCompatActivity() { AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_FLOAT ) - Log.i(TAG, "sampleRate: ${sampleRate}, buffLength: ${bufLength}") + Log.i(TAG, "sampleRate: $sampleRate, buffLength: $bufLength") val attr = AudioAttributes.Builder().setContentType(AudioAttributes.CONTENT_TYPE_SPEECH) .setUsage(AudioAttributes.USAGE_MEDIA) @@ -90,8 +95,14 @@ class MainActivity : AppCompatActivity() { } // this function is called from C++ - private fun callback(samples: FloatArray) { - track.write(samples, 0, samples.size, AudioTrack.WRITE_BLOCKING) + private fun callback(samples: FloatArray): Int { + if (!stopped) { + track.write(samples, 0, samples.size, AudioTrack.WRITE_BLOCKING) + return 1 + } else { + track.stop() + return 0 + } } private fun onClickGenerate() { @@ -127,6 +138,8 @@ class MainActivity : AppCompatActivity() { track.play() play.isEnabled = false + generate.isEnabled = false + stopped = false Thread { val audio = tts.generateWithCallback( text = textStr, @@ -140,6 +153,7 @@ class MainActivity : AppCompatActivity() { if (ok) { runOnUiThread { play.isEnabled = true + generate.isEnabled = true track.stop() } } @@ -148,11 +162,22 @@ class MainActivity : AppCompatActivity() { private fun onClickPlay() { val filename = application.filesDir.absolutePath + "/generated.wav" - val mediaPlayer = MediaPlayer.create( + mediaPlayer?.stop() + mediaPlayer = MediaPlayer.create( applicationContext, Uri.fromFile(File(filename)) ) - mediaPlayer.start() + mediaPlayer?.start() + } + + private fun onClickStop() { + stopped = true + play.isEnabled = true + generate.isEnabled = true + track.pause() + track.flush() + mediaPlayer?.stop() + mediaPlayer = null } private fun initTts() { diff --git a/android/SherpaOnnxTts/app/src/main/java/com/k2fsa/sherpa/onnx/Tts.kt b/android/SherpaOnnxTts/app/src/main/java/com/k2fsa/sherpa/onnx/Tts.kt index b25869d07..4f9c4b6f6 100644 --- a/android/SherpaOnnxTts/app/src/main/java/com/k2fsa/sherpa/onnx/Tts.kt +++ b/android/SherpaOnnxTts/app/src/main/java/com/k2fsa/sherpa/onnx/Tts.kt @@ -76,7 +76,7 @@ class OfflineTts( text: String, sid: Int = 0, speed: Float = 1.0f, - callback: (samples: FloatArray) -> Unit + callback: (samples: FloatArray) -> Int ): GeneratedAudio { val objArray = generateWithCallbackImpl( ptr, @@ -146,7 +146,7 @@ class OfflineTts( text: String, sid: Int = 0, speed: Float = 1.0f, - callback: (samples: FloatArray) -> Unit + callback: (samples: FloatArray) -> Int ): Array companion object { diff --git a/android/SherpaOnnxTts/app/src/main/res/layout/activity_main.xml b/android/SherpaOnnxTts/app/src/main/res/layout/activity_main.xml index 3547de872..c66022a8c 100644 --- a/android/SherpaOnnxTts/app/src/main/res/layout/activity_main.xml +++ b/android/SherpaOnnxTts/app/src/main/res/layout/activity_main.xml @@ -84,4 +84,16 @@ app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toBottomOf="@id/generate" /> + +