From 717aa203d025478224c3781d6de7a9e70cdd2289 Mon Sep 17 00:00:00 2001 From: Liviu Date: Thu, 22 Jul 2021 16:03:54 +0200 Subject: [PATCH 1/3] Add: iosParams and audioOutput flag for iOS --- index.d.ts | 12 ++++++++++-- index.js | 2 +- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/index.d.ts b/index.d.ts index 3073395..cf2fb19 100644 --- a/index.d.ts +++ b/index.d.ts @@ -76,11 +76,19 @@ export type AndroidOptions = { KEY_PARAM_PAN: number; }; +export type IosOptions = { + /** Parameter to specify which voice to use, check voices() for available values. */ + iosVoiceId: string; + /** Parameter to specify which speech rate this line should be spoken with. Will override default rate if set for this utterance. */ + rate: number; + /** Parameter key to specify the audio hardware output of the TTS. */ + audioOutput: "speaker" | "earpiece"; +} + export type Options = | string | { - iosVoiceId: string; - rate: number; + iosParams: IosOptions; androidParams: AndroidOptions; }; diff --git a/index.js b/index.js index 6da7573..9b1a58a 100644 --- a/index.js +++ b/index.js @@ -80,7 +80,7 @@ class Tts extends NativeEventEmitter { // compatibility with old-style voiceId argument passing if (typeof options === 'string') { if (Platform.OS === 'ios') { - return TextToSpeech.speak(utterance, { iosVoiceId: options }); + return TextToSpeech.speak(utterance, options.iosParams || {}); } else { return TextToSpeech.speak(utterance, {}); } From 6191d622e209c4c40683ced0dda1d5783074ec03 Mon Sep 17 00:00:00 2001 From: Liviu Date: Thu, 22 Jul 2021 16:04:45 +0200 Subject: [PATCH 2/3] Add: audio output switch based on requested flag. --- ios/TextToSpeech/TextToSpeech.m | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/ios/TextToSpeech/TextToSpeech.m b/ios/TextToSpeech/TextToSpeech.m index a258146..5e7f376 100644 --- a/ios/TextToSpeech/TextToSpeech.m +++ b/ios/TextToSpeech/TextToSpeech.m @@ -52,17 +52,19 @@ + (BOOL)requiresMainQueueSetup reject(@"no_text", @"No text to speak", nil); return; } + + NSDictionary *iosParams = [params valueForKey:@"iosParams"]; AVSpeechUtterance *utterance = [[AVSpeechUtterance alloc] initWithString:text]; - NSString* voice = [params valueForKey:@"iosVoiceId"]; + NSString* voice = [iosParams valueForKey:@"iosVoiceId"]; if (voice) { utterance.voice = [AVSpeechSynthesisVoice voiceWithIdentifier:voice]; } else if (_defaultVoice) { utterance.voice = _defaultVoice; } - float rate = [[params valueForKey:@"rate"] floatValue]; + float rate = [[iosParams valueForKey:@"rate"] floatValue]; if (rate) { if(rate > AVSpeechUtteranceMinimumSpeechRate && rate < AVSpeechUtteranceMaximumSpeechRate) { utterance.rate = rate; @@ -79,10 +81,20 @@ + (BOOL)requiresMainQueueSetup } if([_ignoreSilentSwitch isEqualToString:@"ignore"]) { - [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil]; + [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:nil]; } else if([_ignoreSilentSwitch isEqualToString:@"obey"]) { [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:nil]; } + + NSString *audioOutput = [iosParams valueForKey:@"audioOutput"]; + if (audioOutput) { + if([audioOutput isEqualToString:@"speaker"]) { + [[AVAudioSession sharedInstance] overrideOutputAudioPort:(AVAudioSessionPortOverrideSpeaker) error:nil]; + } else if([audioOutput isEqualToString:@"earpiece"]) { + [[AVAudioSession sharedInstance] overrideOutputAudioPort:(AVAudioSessionPortOverrideNone) error:nil]; + } + } + [self.synthesizer speakUtterance:utterance]; resolve([NSNumber numberWithUnsignedLong:utterance.hash]); From c93ae4097ee1c070779d74d69289993ce34b884f Mon Sep 17 00:00:00 2001 From: Liviu Date: Thu, 22 Jul 2021 16:09:47 +0200 Subject: [PATCH 3/3] Mod: update README to reflect the new change. --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index 972f516..a27f361 100644 --- a/README.md +++ b/README.md @@ -56,6 +56,7 @@ Additionally, speak() allows to pass platform-specific options. Tts.speak('Hello, world!', { iosVoiceId: 'com.apple.ttsbundle.Moira-compact', rate: 0.5, + audioOutput: 'speaker', }); // Android Tts.speak('Hello, world!', { @@ -80,6 +81,8 @@ The supported options for IOS are: - `iosVoiceId` which voice to use, check [voices()](#list-voices) for available values - `rate` which speech rate this line should be spoken with. Will override [default rate](#set-default-speech-rate) if set for this utterance. +- `audioOutput` you can use either `earpiece` or `speaker`. Default one is `speaker`. To enable the earpiece you need to set `Tts.setIgnoreSilentSwitch("ignore");` + Stop speaking and flush the TTS queue. ```js