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

Add Ability To Select TTS Voice #749

Open
wants to merge 1 commit 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
28 changes: 28 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 Down Expand Up @@ -67,6 +68,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 speechVoice = intent.getStringExtra("voice");
final float speechPitch = intent.getFloatExtra("pitch", 1.0f);

// STREAM_MUSIC is the default audio stream for TTS, see:
Expand Down Expand Up @@ -121,6 +123,23 @@ public void writeResult(PrintWriter out) {
return;
}

if ("LIST_AVAILABLE".equals(speechVoice)) {
try (JsonWriter writer = new JsonWriter(out)) {
writer.setIndent(" ");
Voice defaultVoice = mTts.getVoice();
writer.beginArray();
for (Voice voice : mTts.getVoices()) {
writer.beginObject();
writer.name("name").value(voice.getName());
writer.name("default").value(defaultVoice.equals(voice.getName()));
writer.endObject();
}
writer.endArray();
}
out.println();
return;
}

if ("LIST_AVAILABLE".equals(speechEngine)) {
try (JsonWriter writer = new JsonWriter(out)) {
writer.setIndent(" ");
Expand Down Expand Up @@ -172,6 +191,15 @@ public void onDone(String utteranceId) {
}
}

if (speechVoice != null) {
for (Voice voice : mTts.getVoices()) {
if (voice.getName().equals(speechVoice)) {
mTts.setVoice(voice);
break;
}
}
}

mTts.setPitch(speechPitch);
mTts.setSpeechRate(intent.getFloatExtra("rate", 1.0f));

Expand Down