Skip to content

Commit

Permalink
fix: different behavior with blank text (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
robingenz authored Mar 11, 2021
1 parent 65857f5 commit 527a51f
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 56 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,4 @@ jobs:
- name: Install dependencies
run: npm ci
- name: Run Lint script
run: npm run lint
run: npm run prettier -- --check
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

public class Constant {

public static final String ERROR_TEXT_MISSING = "Text is missing";
public static final String ERROR_TTS_NOT_INITIALIZED = "TextToSpeech is not initialized";
public static final String ERROR_UTTERANCE = "Failed to read text";
public static final String ERROR_UNSUPPORTED_LOCALE = "Unsupported locale";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.getcapacitor.community.tts;

import static com.getcapacitor.community.tts.Constant.ERROR_TEXT_MISSING;
import static com.getcapacitor.community.tts.Constant.ERROR_TTS_NOT_INITIALIZED;
import static com.getcapacitor.community.tts.Constant.ERROR_UNSUPPORTED_LOCALE;
import static com.getcapacitor.community.tts.Constant.ERROR_UTTERANCE;
Expand Down Expand Up @@ -81,56 +80,22 @@ public void load() {
@PluginMethod
public void speak(final PluginCall call) {
try {
String text;
String locale;
double speechRate;
double volume;
double pitchRate;

if (!call.hasOption("text") || !isStringValid(call.getString("text"))) {
call.error(ERROR_TEXT_MISSING);
String text = call.getString("text", "");
String locale = call.getString("locale", "en-US");
float speechRate = call.getFloat("rate", 1.0f);
float pitchRate = call.getFloat("pitch", 1.0f);
double volume = call.getDouble("volume", 1.0);

if (!supportedLocales.contains(Locale.forLanguageTag((locale)))) {
call.error(ERROR_UNSUPPORTED_LOCALE);
return;
} else {
text = call.getString("text");
}

if (!call.hasOption("text") || !isStringValid(call.getString("locale"))) {
locale = "en-US";
} else {
locale = call.getString("locale");
if (!supportedLocales.contains(Locale.forLanguageTag((locale)))) {
call.error(ERROR_UNSUPPORTED_LOCALE);
return;
}
}

if (!call.hasOption("rate") || !isStringValid(call.getString("rate"))) {
speechRate = 1.0;
} else {
speechRate = call.getFloat("rate");
}

if (!call.hasOption("pitch") || !isStringValid(call.getString("pitch"))) {
pitchRate = 1.0;
} else {
pitchRate = call.getFloat("pitch");
}

if (!call.hasOption("volume") || !isStringValid(call.getString("volume"))) {
volume = 1.0;
} else {
volume = call.getFloat("volume");
}

if (tts == null) {
if (tts == null || !ttsInitialized) {
call.error(ERROR_TTS_NOT_INITIALIZED);
return;
}

if (!ttsInitialized) {
call.error(ERROR_TTS_NOT_INITIALIZED);
}

tts.stop();

tts.setOnUtteranceProgressListener(
Expand Down Expand Up @@ -160,16 +125,16 @@ public void onError(String utteranceId) {
ttsParams.putSerializable(android.speech.tts.TextToSpeech.Engine.KEY_PARAM_VOLUME, volume);

tts.setLanguage(new Locale(locale));
tts.setSpeechRate((float) speechRate);
tts.setPitch((float) pitchRate);
tts.setSpeechRate(speechRate);
tts.setPitch(pitchRate);
tts.speak(text, android.speech.tts.TextToSpeech.QUEUE_FLUSH, ttsParams, call.getCallbackId());
} else {
HashMap<String, String> ttsParams = new HashMap<>();
ttsParams.put(android.speech.tts.TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, call.getCallbackId());
ttsParams.put(android.speech.tts.TextToSpeech.Engine.KEY_PARAM_VOLUME, Double.toString(volume));

tts.setLanguage(new Locale(locale));
tts.setPitch((float) pitchRate);
tts.setPitch(pitchRate);
tts.speak(text, android.speech.tts.TextToSpeech.QUEUE_FLUSH, ttsParams);
}
} catch (Exception ex) {
Expand Down Expand Up @@ -266,10 +231,6 @@ public void getSupportedVoices(PluginCall call) {
}
}

private boolean isStringValid(String value) {
return (value != null && !value.isEmpty() && !value.equals("null"));
}

private JSObject convertVoiceToJSObject(Voice voice) {
Locale locale = voice.getLocale();
JSObject obj = new JSObject();
Expand Down
3 changes: 0 additions & 3 deletions src/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@ export class TextToSpeechWeb extends WebPlugin implements TextToSpeechPlugin {
if (!this.speechSynthesis) {
this.throwUnsupportedError();
}
if (!options.text) {
throw new Error('Text option was not provided.');
}
const speechSynthesis = this.speechSynthesis;
if (this.currentlyActive) {
return;
Expand Down

0 comments on commit 527a51f

Please sign in to comment.