Skip to content
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
41 changes: 41 additions & 0 deletions app/src/main/java/com/termux/api/apis/TextToSpeechAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import android.speech.tts.TextToSpeech.Engine;
import android.speech.tts.TextToSpeech.EngineInfo;
import android.speech.tts.UtteranceProgressListener;
import android.speech.tts.Voice;
import android.util.JsonWriter;

import com.termux.api.util.ResultReturner;
Expand All @@ -22,6 +23,7 @@
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.Set;

public class TextToSpeechAPI {

Expand Down Expand Up @@ -64,6 +66,7 @@ protected void onHandleIntent(final Intent intent) {
Logger.logDebug(LOG_TAG, "onHandleIntent:\n" + IntentUtils.getIntentString(intent));

final String speechLanguage = intent.getStringExtra("language");
final String speechVoice = intent.getStringExtra("voice");
final String speechRegion = intent.getStringExtra("region");
final String speechVariant = intent.getStringExtra("variant");
final String speechEngine = intent.getStringExtra("engine");
Expand Down Expand Up @@ -139,6 +142,28 @@ public void writeResult(PrintWriter out) {
return;
}

Set<Voice> availableVoices = mTts.getVoices();

if ("LIST_AVAILABLE".equals(speechVoice)) {
try (JsonWriter writer = new JsonWriter(out)) {
writer.setIndent(" ");
String defaultVoiceName = mTts.getDefaultVoice().getName();
writer.beginArray();
for (Voice info : availableVoices) {
writer.beginObject();
writer.name("name").value(info.getName());
writer.name("locale").value(info.getLocale().getLanguage() + "-" + info.getLocale().getCountry());
writer.name("requiresNetworkConnection").value(info.isNetworkConnectionRequired());
writer.name("installed").value(!info.getFeatures().contains("notInstalled"));
writer.name("default").value(defaultVoiceName.equals(info.getName()));
writer.endObject();
}
writer.endArray();
}
out.println();
return;
}

final AtomicInteger ttsDoneUtterancesCount = new AtomicInteger();

mTts.setOnUtteranceProgressListener(new UtteranceProgressListener() {
Expand Down Expand Up @@ -177,6 +202,22 @@ public void onDone(String utteranceId) {

String utteranceId = "utterance_id";
Bundle params = new Bundle();
if (speechVoice != null) {
for (Voice voice : availableVoices) {
if (speechVoice.equals(voice.getName())) {
int setVoiceResult = mTts.setVoice(voice);
if (setVoiceResult == TextToSpeech.LANG_NOT_SUPPORTED) {
Logger.logError(LOG_TAG, "tts.setVoice('" + speechVoice +"') returned " + setVoiceResult);
}
break;
}
}
} else if (speechLanguage != null) {
int setLanguageResult = mTts.setLanguage(getLocale(speechLanguage, speechRegion, speechVariant));
if (setLanguageResult != TextToSpeech.LANG_AVAILABLE) {
Logger.logError(LOG_TAG, "tts.setLanguage('" + speechLanguage + "') returned " + setLanguageResult);
}
}
params.putInt(Engine.KEY_PARAM_STREAM, streamToUse);
params.putString(Engine.KEY_PARAM_UTTERANCE_ID, utteranceId);

Expand Down