diff --git a/README.md b/README.md index 661efcc..803472e 100644 --- a/README.md +++ b/README.md @@ -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) { diff --git a/src/android/TTS.java b/src/android/TTS.java index 21c7a63..14b5e99 100644 --- a/src/android/TTS.java +++ b/src/android/TTS.java @@ -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 @@ -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); @@ -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 { diff --git a/src/ios/CDVTTS.m b/src/ios/CDVTTS.m index d1001eb..8ce884d 100644 --- a/src/ios/CDVTTS.m +++ b/src/ios/CDVTTS.m @@ -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"; @@ -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]; }