Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TextToSpeechAPI: Synthesize speech output to WAV file #555

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion app/src/main/java/com/termux/api/apis/TextToSpeechAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import com.termux.shared.logger.Logger;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Locale;
Expand All @@ -37,6 +38,9 @@ public static class TextToSpeechService extends IntentService {
TextToSpeech mTts;
final CountDownLatch mTtsLatch = new CountDownLatch(1);

// file we're recording to
protected static File file;

private static final String LOG_TAG = "TextToSpeechService";

public TextToSpeechService() {
Expand Down Expand Up @@ -67,6 +71,7 @@ protected void onHandleIntent(final Intent intent) {
final String speechRegion = intent.getStringExtra("region");
final String speechVariant = intent.getStringExtra("variant");
final String speechEngine = intent.getStringExtra("engine");
final String speechFile = intent.getStringExtra("file");
final float speechPitch = intent.getFloatExtra("pitch", 1.0f);

// STREAM_MUSIC is the default audio stream for TTS, see:
Expand Down Expand Up @@ -97,6 +102,14 @@ protected void onHandleIntent(final Intent intent) {
}
final int streamToUse = streamToUseInt;

if (speechFile != null) {
file = new File(speechFile);
if (!file.canWrite()) {
Logger.logError(LOG_TAG, "Can't write to file: " + file.getName());
return;
}
}

mTts = new TextToSpeech(this, status -> {
if (status == TextToSpeech.SUCCESS) {
mTtsLatch.countDown();
Expand Down Expand Up @@ -187,7 +200,12 @@ public void onDone(String utteranceId) {
while ((line = reader.readLine()) != null) {
if (!line.isEmpty()) {
submittedUtterances++;
mTts.speak(line, TextToSpeech.QUEUE_ADD, params, utteranceId);
if (speechFile != null) {
mTts.synthesizeToFile(line, params, file, utteranceId);
break;
} else {
mTts.speak(line, TextToSpeech.QUEUE_ADD, params, utteranceId);
}
}
}
}
Expand Down