forked from naoufal/react-native-speech
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSpeechSynthesizer.m
214 lines (168 loc) · 5.36 KB
/
SpeechSynthesizer.m
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#import "SpeechSynthesizer.h"
#import "RCTUtils.h"
#import "RCTLog.h"
#import <React/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 *voice = args[@"voice"];
NSNumber *rate = args[@"rate"];
// Error if no text is passed
if (!text) {
RCTLogError(@"[Speech] You must specify a text to speak.");
return;
}
// Set default voice
NSString *voiceLanguage;
// Set voice if provided
if (voice) {
voiceLanguage = voice;
// Fallback to en-US
} else {
voiceLanguage = @"en-US";
}
// Setup utterance and voice
AVSpeechUtterance *utterance = [[AVSpeechUtterance alloc] initWithString:text];
utterance.voice = [AVSpeechSynthesisVoice voiceWithLanguage:voiceLanguage];
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]);
}
// SpeakWithFinish
RCT_EXPORT_METHOD(speakUtteranceWithFinish:(NSDictionary *)args callback:(RCTResponseSenderBlock)callback)
{
// Error if self.synthesizer was already initialized
if (self.synthesizer || self.cb) {
return callback(@[RCTMakeError(@"There is a speech in progress. Use the `paused` method to know if it's paused.", nil, nil)]);
}
self.cb = callback;
// Set args to variables
NSString *text = args[@"text"];
NSString *voice = args[@"voice"];
NSNumber *rate = args[@"rate"];
// Error if no text is passed
if (!text) {
RCTLogError(@"[Speech] You must specify a text to speak.");
return;
}
// Set default voice
NSString *voiceLanguage;
// Set voice if provided
if (voice) {
voiceLanguage = voice;
// Fallback to en-US
} else {
voiceLanguage = @"en-US";
}
// Setup utterance and voice
AVSpeechUtterance *utterance = [[AVSpeechUtterance alloc] initWithString:text];
utterance.voice = [AVSpeechSynthesisVoice voiceWithLanguage:voiceLanguage];
if (rate) {
utterance.rate = [rate doubleValue];
}
self.synthesizer = [[AVSpeechSynthesizer alloc] init];
self.synthesizer.delegate = self;
// Speak
[self.synthesizer speakUtterance:utterance];
}
// Stops synthesizer
RCT_EXPORT_METHOD(stopSpeakingAtBoundary)
{
if (self.synthesizer) {
[self.synthesizer stopSpeakingAtBoundary:AVSpeechBoundaryImmediate];
self.synthesizer = nil;
}
}
// 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"];
callback(@[[NSNull null], locales]);
}
// Delegate
// Finished Handler
-(void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didFinishSpeechUtterance:(AVSpeechUtterance *)utterance
{
NSLog(@"Speech finished");
self.synthesizer = nil;
[self.bridge.eventDispatcher sendAppEventWithName:@"FinishSpeechUtterance" body:@{}];
if (self.cb) {
// Return that the speach has started
self.cb(@[[NSNull null], @true]);
self.cb = nil;
}
}
// 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");
}
@end