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

VoiceType Male/Female added #69

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ document.addEventListener('deviceready', function () {
.speak({
text: 'hello, world!',
locale: 'en-GB',
rate: 0.75
rate: 0.75,
/* voiceType: Male or Female */
voiceType:'Male'
}).then(function () {
alert('success');
}, function (reason) {
Expand Down
15 changes: 15 additions & 0 deletions src/android/TTS.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import android.app.Activity;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.speech.tts.Voice;

/*
Cordova Text-to-Speech Plugin
Expand Down Expand Up @@ -160,6 +161,7 @@ private void speak(JSONArray args, CallbackContext callbackContext)
String text;
String locale;
double rate;
String voiceType;

if (params.isNull("text")) {
callbackContext.error(ERR_INVALID_OPTIONS);
Expand Down Expand Up @@ -196,6 +198,19 @@ private void speak(JSONArray args, CallbackContext callbackContext)
String[] localeArgs = locale.split("-");
tts.setLanguage(new Locale(localeArgs[0], localeArgs[1]));

voiceType = params.getString("voiceType");

if(voiceType.compareTo("Male")==0){
Voice voiceobj = new Voice("en-us-x-sfg#male_1-local",
Locale.getDefault(), 1, 1, false, null);
tts.setVoice(voiceobj);
}
else{
Voice voiceobj = new Voice("en-us-x-sfg#female_1-local",
Locale.getDefault(), 1, 1, false, null);
tts.setVoice(voiceobj);
}

if (Build.VERSION.SDK_INT >= 27) {
tts.setSpeechRate((float) rate * 0.7f);
} else {
Expand Down
24 changes: 18 additions & 6 deletions src/ios/CDVTTS.m
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ - (void)speak:(CDVInvokedUrlCommand*)command {

NSString* text = [options objectForKey:@"text"];
NSString* locale = [options objectForKey:@"locale"];
NSString* voiceType = [options objectForKey:@"voiceType"];
double rate = [[options objectForKey:@"rate"] doubleValue];
NSString* voice;

if (!locale || (id)locale == [NSNull null]) {
locale = @"en-US";
Expand All @@ -61,17 +63,27 @@ - (void)speak:(CDVInvokedUrlCommand*)command {
if (!rate) {
rate = 1.0;
}

if([voiceType isEqualToString:@"Male"]){
voice = @"com.apple.ttsbundle.siri_male_en-US_compact";
}
else{
voice = @"com.apple.ttsbundle.siri_female_en-US_compact";
}

AVSpeechUtterance* utterance = [[AVSpeechUtterance new] initWithString:text];
utterance.voice = [AVSpeechSynthesisVoice voiceWithLanguage:locale];
//utterance.voice = [AVSpeechSynthesisVoice voiceWithLanguage:locale];
utterance.voice = [AVSpeechSynthesisVoice voiceWithIdentifier:voice];
// Rate expression adjusted manually for a closer match to other platform.
utterance.rate = (AVSpeechUtteranceMinimumSpeechRate * 1.5 + AVSpeechUtteranceDefaultSpeechRate) / 2.25 * rate * rate;
//utterance.rate = (AVSpeechUtteranceMinimumSpeechRate * 1.5 + AVSpeechUtteranceDefaultSpeechRate) / 2.25 * rate * rate;
// workaround for https://github.com/vilic/cordova-plugin-tts/issues/21
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 9.0) {
utterance.rate = utterance.rate * 2;
//if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 9.0) {
//utterance.rate = utterance.rate * 2;
// see http://stackoverflow.com/questions/26097725/avspeechuterrance-speed-in-ios-8
}
utterance.pitchMultiplier = 1.2;
//}
utterance.rate = 0.45;
utterance.pitchMultiplier = 1.0;
utterance.volume = 1.0;
[synthesizer speakUtterance:utterance];
}

Expand Down