forked from naoufal/react-native-speech
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpeechSynthesizer.m
164 lines (134 loc) · 4.53 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
#import "SpeechSynthesizer.h"
#import "RCTUtils.h"
#import "RCTLog.h"
#import "RCTBridge.h"
#import "SpeechEventEmitter.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) {
RCTLogInfo(@"There is a speech in progress. This means your speech request is added to a speech queue.");
}
// Set args to variables
NSString *text = args[@"text"];
NSString *voice = args[@"voice"];
NSNumber *rate = args[@"rate"];
NSNumber *beforeInterval = args[@"beforeInterval"];
NSNumber *afterInterval = args[@"afterInterval"];
NSNumber *pitch = args[@"pitch"];
NSNumber *volume = args[@"volume"];
// Error if no text is passed
if (!text) {
RCTLogWarn(@"[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 if no instance exists
AVSpeechUtterance *utterance = [[AVSpeechUtterance alloc] initWithString:text];
utterance.voice = [AVSpeechSynthesisVoice voiceWithLanguage:voiceLanguage];
if (rate) { utterance.rate = [rate doubleValue]; }
if (beforeInterval) { utterance.preUtteranceDelay = [beforeInterval doubleValue]; }
if (afterInterval) { utterance.postUtteranceDelay = [afterInterval doubleValue]; }
if (pitch) { utterance.pitchMultiplier = [pitch floatValue]; }
if (volume) { utterance.volume = [volume floatValue]; }
if (!self.synthesizer) {
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(@[[NSNull null], @true]);
} else {
callback(@[[NSNull null], @false]);
}
}
// Returns true if synthesizer is speaking
RCT_EXPORT_METHOD(speaking:(RCTResponseSenderBlock)callback)
{
if (self.synthesizer.speaking) {
callback(@[[NSNull null], @true]);
} else {
callback(@[[NSNull null], @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
{
NSString *finishString = utterance.speechString;
NSLog(@"Speech finished with - %@", finishString);
self.synthesizer = nil;
[SpeechEventEmitter application:[UIApplication sharedApplication] speechFinished:finishString];
}
// 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