Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*
Cordova Text-to-Speech Plugin
https://github.com/vilic/cordova-plugin-tts
by VILIC VANE
https://github.com/vilic
MIT License
*/
#import <Cordova/CDV.h>
#import "CDVTTS.h"
@implementation CDVTTS
- (void)pluginInitialize {
synthesizer = [AVSpeechSynthesizer new];
synthesizer.delegate = self;
}
- (void)speechSynthesizer:(AVSpeechSynthesizer*)synthesizer didFinishSpeechUtterance:(AVSpeechUtterance*)utterance {
CDVPluginResult* result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
if (lastCallbackId) {
[self.commandDelegate sendPluginResult:result callbackId:lastCallbackId];
lastCallbackId = nil;
} else {
[self.commandDelegate sendPluginResult:result callbackId:callbackId];
callbackId = nil;
}
[[AVAudioSession sharedInstance] setActive:NO withOptions:0 error:nil];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient
withOptions: 0 error: nil];
[[AVAudioSession sharedInstance] setActive:YES withOptions: 0 error:nil];
}
- (void)speak:(CDVInvokedUrlCommand*)command {
[[AVAudioSession sharedInstance] setActive:NO withOptions:0 error:nil];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback
withOptions:AVAudioSessionCategoryOptionDuckOthers error:nil];
if (callbackId) {
lastCallbackId = callbackId;
}
callbackId = command.callbackId;
[synthesizer stopSpeakingAtBoundary:AVSpeechBoundaryImmediate];
NSDictionary* options = [command.arguments objectAtIndex:0];
NSString* text = [options objectForKey:@"text"];
NSString* locale = [options objectForKey:@"locale"];
double rate = [[options objectForKey:@"rate"] doubleValue];
if (!locale || (id)locale == [NSNull null]) {
locale = @"en-US";
}
if (!rate) {
rate = 1.0;
}
AVSpeechUtterance* utterance = [[AVSpeechUtterance new] initWithString:text];
utterance.voice = [AVSpeechSynthesisVoice voiceWithLanguage:locale];
// Rate expression adjusted manually for a closer match to other platform.
utterance.rate = (AVSpeechUtteranceMinimumSpeechRate * 1.5 + AVSpeechUtteranceDefaultSpeechRate) / 2.5 * rate * rate;
utterance.pitchMultiplier = 1.2;
[synthesizer speakUtterance:utterance];
}
@end