| 1 |
/**************************************************************************/ |
| 2 |
/* tts_macos.mm */ |
| 3 |
/**************************************************************************/ |
| 4 |
/* This file is part of: */ |
| 5 |
/* GODOT ENGINE */ |
| 6 |
/* https://godotengine.org */ |
| 7 |
/**************************************************************************/ |
| 8 |
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 |
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 |
/* */ |
| 11 |
/* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 |
/* a copy of this software and associated documentation files (the */ |
| 13 |
/* "Software"), to deal in the Software without restriction, including */ |
| 14 |
/* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 |
/* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 |
/* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 |
/* the following conditions: */ |
| 18 |
/* */ |
| 19 |
/* The above copyright notice and this permission notice shall be */ |
| 20 |
/* included in all copies or substantial portions of the Software. */ |
| 21 |
/* */ |
| 22 |
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 |
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 |
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 |
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 |
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 |
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 |
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 |
/**************************************************************************/ |
| 30 |
|
| 31 |
#import "tts_macos.h" |
| 32 |
|
| 33 |
@implementation TTS_MacOS |
| 34 |
|
| 35 |
- (id)init { |
| 36 |
self = [super init]; |
| 37 |
self->speaking = false; |
| 38 |
self->have_utterance = false; |
| 39 |
self->last_utterance = -1; |
| 40 |
self->paused = false; |
| 41 |
if (@available(macOS 10.14, *)) { |
| 42 |
self->synth = [[AVSpeechSynthesizer alloc] init]; |
| 43 |
[self->synth setDelegate:self]; |
| 44 |
print_verbose("Text-to-Speech: AVSpeechSynthesizer initialized."); |
| 45 |
} else { |
| 46 |
self->synth = [[NSSpeechSynthesizer alloc] init]; |
| 47 |
[self->synth setDelegate:self]; |
| 48 |
print_verbose("Text-to-Speech: NSSpeechSynthesizer initialized."); |
| 49 |
} |
| 50 |
return self; |
| 51 |
} |
| 52 |
|
| 53 |
// AVSpeechSynthesizer callback (macOS 10.14+) |
| 54 |
|
| 55 |
- (void)speechSynthesizer:(AVSpeechSynthesizer *)av_synth willSpeakRangeOfSpeechString:(NSRange)characterRange utterance:(AVSpeechUtterance *)utterance API_AVAILABLE(macosx(10.14)) { |
| 56 |
NSString *string = [utterance speechString]; |
| 57 |
|
| 58 |
// Convert from UTF-16 to UTF-32 position. |
| 59 |
int pos = 0; |
| 60 |
for (NSUInteger i = 0; i < MIN(characterRange.location, string.length); i++) { |
| 61 |
unichar c = [string characterAtIndex:i]; |
| 62 |
if ((c & 0xfffffc00) == 0xd800) { |
| 63 |
i++; |
| 64 |
} |
| 65 |
pos++; |
| 66 |
} |
| 67 |
|
| 68 |
DisplayServer::get_singleton()->tts_post_utterance_event(DisplayServer::TTS_UTTERANCE_BOUNDARY, ids[utterance], pos); |
| 69 |
} |
| 70 |
|
| 71 |
// AVSpeechSynthesizer callback (macOS 10.14+) |
| 72 |
|
| 73 |
- (void)speechSynthesizer:(AVSpeechSynthesizer *)av_synth didCancelSpeechUtterance:(AVSpeechUtterance *)utterance API_AVAILABLE(macosx(10.14)) { |
| 74 |
DisplayServer::get_singleton()->tts_post_utterance_event(DisplayServer::TTS_UTTERANCE_CANCELED, ids[utterance]); |
| 75 |
ids.erase(utterance); |
| 76 |
speaking = false; |
| 77 |
[self update]; |
| 78 |
} |
| 79 |
|
| 80 |
// AVSpeechSynthesizer callback (macOS 10.14+) |
| 81 |
|
| 82 |
- (void)speechSynthesizer:(AVSpeechSynthesizer *)av_synth didFinishSpeechUtterance:(AVSpeechUtterance *)utterance API_AVAILABLE(macosx(10.14)) { |
| 83 |
DisplayServer::get_singleton()->tts_post_utterance_event(DisplayServer::TTS_UTTERANCE_ENDED, ids[utterance]); |
| 84 |
ids.erase(utterance); |
| 85 |
speaking = false; |
| 86 |
[self update]; |
| 87 |
} |
| 88 |
|
| 89 |
// NSSpeechSynthesizer callback (macOS 10.4+) |
| 90 |
|
| 91 |
- (void)speechSynthesizer:(NSSpeechSynthesizer *)ns_synth willSpeakWord:(NSRange)characterRange ofString:(NSString *)string { |
| 92 |
if (!paused && have_utterance) { |
| 93 |
// Convert from UTF-16 to UTF-32 position. |
| 94 |
int pos = 0; |
| 95 |
for (NSUInteger i = 0; i < MIN(characterRange.location, string.length); i++) { |
| 96 |
unichar c = [string characterAtIndex:i]; |
| 97 |
if ((c & 0xfffffc00) == 0xd800) { |
| 98 |
i++; |
| 99 |
} |
| 100 |
pos++; |
| 101 |
} |
| 102 |
|
| 103 |
DisplayServer::get_singleton()->tts_post_utterance_event(DisplayServer::TTS_UTTERANCE_BOUNDARY, last_utterance, pos); |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
- (void)speechSynthesizer:(NSSpeechSynthesizer *)ns_synth didFinishSpeaking:(BOOL)success { |
| 108 |
if (!paused && have_utterance) { |
| 109 |
if (success) { |
| 110 |
DisplayServer::get_singleton()->tts_post_utterance_event(DisplayServer::TTS_UTTERANCE_ENDED, last_utterance); |
| 111 |
} else { |
| 112 |
DisplayServer::get_singleton()->tts_post_utterance_event(DisplayServer::TTS_UTTERANCE_CANCELED, last_utterance); |
| 113 |
} |
| 114 |
have_utterance = false; |
| 115 |
} |
| 116 |
speaking = false; |
| 117 |
[self update]; |
| 118 |
} |
| 119 |
|
| 120 |
- (void)update { |
| 121 |
if (!speaking && queue.size() > 0) { |
| 122 |
DisplayServer::TTSUtterance &message = queue.front()->get(); |
| 123 |
|
| 124 |
if (@available(macOS 10.14, *)) { |
| 125 |
AVSpeechSynthesizer *av_synth = synth; |
| 126 |
AVSpeechUtterance *new_utterance = [[AVSpeechUtterance alloc] initWithString:[NSString stringWithUTF8String:message.text.utf8().get_data()]]; |
| 127 |
[new_utterance setVoice:[AVSpeechSynthesisVoice voiceWithIdentifier:[NSString stringWithUTF8String:message.voice.utf8().get_data()]]]; |
| 128 |
if (message.rate > 1.f) { |
| 129 |
[new_utterance setRate:Math::remap(message.rate, 1.f, 10.f, AVSpeechUtteranceDefaultSpeechRate, AVSpeechUtteranceMaximumSpeechRate)]; |
| 130 |
} else if (message.rate < 1.f) { |
| 131 |
[new_utterance setRate:Math::remap(message.rate, 0.1f, 1.f, AVSpeechUtteranceMinimumSpeechRate, AVSpeechUtteranceDefaultSpeechRate)]; |
| 132 |
} |
| 133 |
[new_utterance setPitchMultiplier:message.pitch]; |
| 134 |
[new_utterance setVolume:(Math::remap(message.volume, 0.f, 100.f, 0.f, 1.f))]; |
| 135 |
|
| 136 |
ids[new_utterance] = message.id; |
| 137 |
[av_synth speakUtterance:new_utterance]; |
| 138 |
} else { |
| 139 |
NSSpeechSynthesizer *ns_synth = synth; |
| 140 |
[ns_synth setObject:nil forProperty:NSSpeechResetProperty error:nil]; |
| 141 |
[ns_synth setVoice:[NSString stringWithUTF8String:message.voice.utf8().get_data()]]; |
| 142 |
int base_pitch = [[ns_synth objectForProperty:NSSpeechPitchBaseProperty error:nil] intValue]; |
| 143 |
[ns_synth setObject:[NSNumber numberWithInt:(base_pitch * (message.pitch / 2.f + 0.5f))] forProperty:NSSpeechPitchBaseProperty error:nullptr]; |
| 144 |
[ns_synth setVolume:(Math::remap(message.volume, 0.f, 100.f, 0.f, 1.f))]; |
| 145 |
[ns_synth setRate:(message.rate * 200)]; |
| 146 |
|
| 147 |
last_utterance = message.id; |
| 148 |
have_utterance = true; |
| 149 |
[ns_synth startSpeakingString:[NSString stringWithUTF8String:message.text.utf8().get_data()]]; |
| 150 |
} |
| 151 |
DisplayServer::get_singleton()->tts_post_utterance_event(DisplayServer::TTS_UTTERANCE_STARTED, message.id); |
| 152 |
queue.pop_front(); |
| 153 |
|
| 154 |
speaking = true; |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
- (void)pauseSpeaking { |
| 159 |
if (@available(macOS 10.14, *)) { |
| 160 |
AVSpeechSynthesizer *av_synth = synth; |
| 161 |
[av_synth pauseSpeakingAtBoundary:AVSpeechBoundaryImmediate]; |
| 162 |
} else { |
| 163 |
NSSpeechSynthesizer *ns_synth = synth; |
| 164 |
[ns_synth pauseSpeakingAtBoundary:NSSpeechImmediateBoundary]; |
| 165 |
} |
| 166 |
paused = true; |
| 167 |
} |
| 168 |
|
| 169 |
- (void)resumeSpeaking { |
| 170 |
if (@available(macOS 10.14, *)) { |
| 171 |
AVSpeechSynthesizer *av_synth = synth; |
| 172 |
[av_synth continueSpeaking]; |
| 173 |
} else { |
| 174 |
NSSpeechSynthesizer *ns_synth = synth; |
| 175 |
[ns_synth continueSpeaking]; |
| 176 |
} |
| 177 |
paused = false; |
| 178 |
} |
| 179 |
|
| 180 |
- (void)stopSpeaking { |
| 181 |
for (DisplayServer::TTSUtterance &message : queue) { |
| 182 |
DisplayServer::get_singleton()->tts_post_utterance_event(DisplayServer::TTS_UTTERANCE_CANCELED, message.id); |
| 183 |
} |
| 184 |
queue.clear(); |
| 185 |
if (@available(macOS 10.14, *)) { |
| 186 |
AVSpeechSynthesizer *av_synth = synth; |
| 187 |
[av_synth stopSpeakingAtBoundary:AVSpeechBoundaryImmediate]; |
| 188 |
} else { |
| 189 |
NSSpeechSynthesizer *ns_synth = synth; |
| 190 |
if (have_utterance) { |
| 191 |
DisplayServer::get_singleton()->tts_post_utterance_event(DisplayServer::TTS_UTTERANCE_CANCELED, last_utterance); |
| 192 |
} |
| 193 |
[ns_synth stopSpeaking]; |
| 194 |
} |
| 195 |
have_utterance = false; |
| 196 |
speaking = false; |
| 197 |
paused = false; |
| 198 |
} |
| 199 |
|
| 200 |
- (bool)isSpeaking { |
| 201 |
return speaking || (queue.size() > 0); |
| 202 |
} |
| 203 |
|
| 204 |
- (bool)isPaused { |
| 205 |
if (@available(macOS 10.14, *)) { |
| 206 |
AVSpeechSynthesizer *av_synth = synth; |
| 207 |
return [av_synth isPaused]; |
| 208 |
} else { |
| 209 |
return paused; |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
- (void)speak:(const String &)text voice:(const String &)voice volume:(int)volume pitch:(float)pitch rate:(float)rate utterance_id:(int64_t)utterance_id interrupt:(bool)interrupt { |
| 214 |
if (interrupt) { |
| 215 |
[self stopSpeaking]; |
| 216 |
} |
| 217 |
|
| 218 |
if (text.is_empty()) { |
| 219 |
DisplayServer::get_singleton()->tts_post_utterance_event(DisplayServer::TTS_UTTERANCE_CANCELED, utterance_id); |
| 220 |
return; |
| 221 |
} |
| 222 |
|
| 223 |
DisplayServer::TTSUtterance message; |
| 224 |
message.text = text; |
| 225 |
message.voice = voice; |
| 226 |
message.volume = CLAMP(volume, 0, 100); |
| 227 |
message.pitch = CLAMP(pitch, 0.f, 2.f); |
| 228 |
message.rate = CLAMP(rate, 0.1f, 10.f); |
| 229 |
message.id = utterance_id; |
| 230 |
queue.push_back(message); |
| 231 |
|
| 232 |
if ([self isPaused]) { |
| 233 |
[self resumeSpeaking]; |
| 234 |
} else { |
| 235 |
[self update]; |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
- (Array)getVoices { |
| 240 |
Array list; |
| 241 |
if (@available(macOS 10.14, *)) { |
| 242 |
for (AVSpeechSynthesisVoice *voice in [AVSpeechSynthesisVoice speechVoices]) { |
| 243 |
NSString *voiceIdentifierString = [voice identifier]; |
| 244 |
NSString *voiceLocaleIdentifier = [voice language]; |
| 245 |
NSString *voiceName = [voice name]; |
| 246 |
Dictionary voice_d; |
| 247 |
voice_d["name"] = String::utf8([voiceName UTF8String]); |
| 248 |
voice_d["id"] = String::utf8([voiceIdentifierString UTF8String]); |
| 249 |
voice_d["language"] = String::utf8([voiceLocaleIdentifier UTF8String]); |
| 250 |
list.push_back(voice_d); |
| 251 |
} |
| 252 |
} else { |
| 253 |
for (NSString *voiceIdentifierString in [NSSpeechSynthesizer availableVoices]) { |
| 254 |
NSString *voiceLocaleIdentifier = [[NSSpeechSynthesizer attributesForVoice:voiceIdentifierString] objectForKey:NSVoiceLocaleIdentifier]; |
| 255 |
NSString *voiceName = [[NSSpeechSynthesizer attributesForVoice:voiceIdentifierString] objectForKey:NSVoiceName]; |
| 256 |
Dictionary voice_d; |
| 257 |
voice_d["name"] = String([voiceName UTF8String]); |
| 258 |
voice_d["id"] = String([voiceIdentifierString UTF8String]); |
| 259 |
voice_d["language"] = String([voiceLocaleIdentifier UTF8String]); |
| 260 |
list.push_back(voice_d); |
| 261 |
} |
| 262 |
} |
| 263 |
return list; |
| 264 |
} |
| 265 |
|
| 266 |
@end |