Skip to content
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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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!', {
Expand All @@ -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
Expand Down
12 changes: 10 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, {});
}
Expand Down
18 changes: 15 additions & 3 deletions ios/TextToSpeech/TextToSpeech.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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]);
Expand Down