forked from naoufal/react-native-speech
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSpeechSynthesizer.m
More file actions
161 lines (134 loc) · 4.38 KB
/
SpeechSynthesizer.m
File metadata and controls
161 lines (134 loc) · 4.38 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#import "SpeechSynthesizer.h"
#import "RCTUtils.h"
#import "RCTLog.h"
#import "RCTEventDispatcher.h"
@implementation SpeechSynthesizer
@synthesize bridge = _bridge;
RCT_EXPORT_MODULE()
// Speak
RCT_EXPORT_METHOD(speakUtterance:(NSDictionary *)args callback:(RCTResponseSenderBlock)callback)
{
// Error if self.synthesizer was already initialized
if (self.synthesizer) {
return callback(@[RCTMakeError(@"There is a speech in progress. Use the `paused` method to know if it's paused.", nil, nil)]);
}
// Set args to variables
NSString *text = args[@"text"];
NSString *voiceIdentifier = args[@"voiceIdentifier"];
NSNumber *rate = args[@"rate"];
// Error if no text is passed
if (!text) {
RCTLogError(@"[Speech] You must specify a text to speak.");
return;
}
// Setup utterance and voice
AVSpeechUtterance *utterance = [[AVSpeechUtterance alloc] initWithString:text];
if (voiceIdentifier) {
utterance.voice = [AVSpeechSynthesisVoice voiceWithIdentifier:voiceIdentifier];
} else {
// Fallback to en-US
utterance.voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"en-US"];
}
if (rate) {
utterance.rate = [rate doubleValue];
}
self.synthesizer = [[AVSpeechSynthesizer alloc] init];
self.synthesizer.delegate = self;
// Speak
[self.synthesizer speakUtterance:utterance];
// Return that the speach has started
callback(@[[NSNull null], @true]);
}
// Stops synthesizer
RCT_EXPORT_METHOD(stopSpeakingAtBoundary)
{
if (self.synthesizer) {
[self.synthesizer stopSpeakingAtBoundary:AVSpeechBoundaryImmediate];
}
}
// Pauses synthesizer
RCT_EXPORT_METHOD(pauseSpeakingAtBoundary)
{
if (self.synthesizer) {
[self.synthesizer pauseSpeakingAtBoundary:AVSpeechBoundaryImmediate];
}
}
// Resumes synthesizer
RCT_EXPORT_METHOD(continueSpeakingAtBoundary)
{
if (self.synthesizer) {
[self.synthesizer continueSpeaking];
}
}
// Returns false if synthesizer is paued
RCT_EXPORT_METHOD(paused:(RCTResponseSenderBlock)callback)
{
if (self.synthesizer.paused) {
callback(@[@true]);
} else {
callback(@[@false]);
}
}
// Returns true if synthesizer is speaking
RCT_EXPORT_METHOD(speaking:(RCTResponseSenderBlock)callback)
{
if (self.synthesizer.speaking) {
callback(@[@true]);
} else {
callback(@[@false]);
}
}
// Returns available voices
RCT_EXPORT_METHOD(speechVoices:(RCTResponseSenderBlock)callback)
{
NSArray *speechVoices = [AVSpeechSynthesisVoice speechVoices];
NSArray *locales = [speechVoices valueForKey:@"language"];
NSMutableDictionary *voices = [[NSMutableDictionary alloc] init];
for (AVSpeechSynthesisVoice *voice in speechVoices) {
NSMutableDictionary *voiceInfo = [[NSMutableDictionary alloc] init];
voiceInfo[@"name"] = voice.name;
voiceInfo[@"language"] = voice.language;
if (voice.quality == AVSpeechSynthesisVoiceQualityEnhanced) {
voiceInfo[@"quality"] = @"enhanced";
} else {
voiceInfo[@"quality"] = @"default";
}
voices[voice.identifier] = voiceInfo;
}
callback(@[[NSNull null], voices]);
}
// Delegate
// Finished Handler
-(void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didFinishSpeechUtterance:(AVSpeechUtterance *)utterance
{
NSLog(@"Speech finished");
self.synthesizer = nil;
[self.bridge.eventDispatcher sendAppEventWithName:@"FinishSpeechUtterance" body:@{}];
}
// Started Handler
-(void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didStartSpeechUtterance:(AVSpeechUtterance *)utterance
{
NSLog(@"Speech started");
}
// Paused Handler
-(void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didPauseSpeechUtterance:(AVSpeechUtterance *)utterance
{
NSLog(@"Speech paused");
}
// Resumed Handler
-(void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didContinueSpeechUtterance:(AVSpeechUtterance *)utterance
{
NSLog(@"Speech resumed");
}
// Word Handler
-(void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer willSpeakRangeOfSpeechString:(NSRange)characterRange utterance:(AVSpeechUtterance *)utterance
{
NSLog(@"Started word");
}
// Cancelled Handler
-(void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didCancelSpeechUtterance:(AVSpeechUtterance *)utterance
{
NSLog(@"Speech cancelled");
self.synthesizer = nil;
}
@end