| 1 |
/**************************************************************************/ |
| 2 |
/* tts_apple_embedded.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_apple_embedded.h" |
| 32 |
|
| 33 |
@implementation GDTTTS |
| 34 |
|
| 35 |
- (id)init { |
| 36 |
self = [super init]; |
| 37 |
self->speaking = false; |
| 38 |
self->av_synth = [[AVSpeechSynthesizer alloc] init]; |
| 39 |
[self->av_synth setDelegate:self]; |
| 40 |
print_verbose("Text-to-Speech: AVSpeechSynthesizer initialized."); |
| 41 |
return self; |
| 42 |
} |
| 43 |
|
| 44 |
- (void)speechSynthesizer:(AVSpeechSynthesizer *)av_synth willSpeakRangeOfSpeechString:(NSRange)characterRange utterance:(AVSpeechUtterance *)utterance { |
| 45 |
NSString *string = [utterance speechString]; |
| 46 |
|
| 47 |
// Convert from UTF-16 to UTF-32 position. |
| 48 |
int pos = 0; |
| 49 |
for (NSUInteger i = 0; i < MIN(characterRange.location, string.length); i++) { |
| 50 |
unichar c = [string characterAtIndex:i]; |
| 51 |
if ((c & 0xfffffc00) == 0xd800) { |
| 52 |
i++; |
| 53 |
} |
| 54 |
pos++; |
| 55 |
} |
| 56 |
|
| 57 |
DisplayServer::get_singleton()->tts_post_utterance_event(DisplayServer::TTS_UTTERANCE_BOUNDARY, ids[utterance], pos); |
| 58 |
} |
| 59 |
|
| 60 |
- (void)speechSynthesizer:(AVSpeechSynthesizer *)av_synth didCancelSpeechUtterance:(AVSpeechUtterance *)utterance { |
| 61 |
DisplayServer::get_singleton()->tts_post_utterance_event(DisplayServer::TTS_UTTERANCE_CANCELED, ids[utterance]); |
| 62 |
ids.erase(utterance); |
| 63 |
speaking = false; |
| 64 |
[self update]; |
| 65 |
} |
| 66 |
|
| 67 |
- (void)speechSynthesizer:(AVSpeechSynthesizer *)av_synth didFinishSpeechUtterance:(AVSpeechUtterance *)utterance { |
| 68 |
DisplayServer::get_singleton()->tts_post_utterance_event(DisplayServer::TTS_UTTERANCE_ENDED, ids[utterance]); |
| 69 |
ids.erase(utterance); |
| 70 |
speaking = false; |
| 71 |
[self update]; |
| 72 |
} |
| 73 |
|
| 74 |
- (void)update { |
| 75 |
if (!speaking && queue.size() > 0) { |
| 76 |
DisplayServer::TTSUtterance &message = queue.front()->get(); |
| 77 |
|
| 78 |
AVSpeechUtterance *new_utterance = [[AVSpeechUtterance alloc] initWithString:[NSString stringWithUTF8String:message.text.utf8().get_data()]]; |
| 79 |
[new_utterance setVoice:[AVSpeechSynthesisVoice voiceWithIdentifier:[NSString stringWithUTF8String:message.voice.utf8().get_data()]]]; |
| 80 |
if (message.rate > 1.f) { |
| 81 |
[new_utterance setRate:Math::remap(message.rate, 1.f, 10.f, AVSpeechUtteranceDefaultSpeechRate, AVSpeechUtteranceMaximumSpeechRate)]; |
| 82 |
} else if (message.rate < 1.f) { |
| 83 |
[new_utterance setRate:Math::remap(message.rate, 0.1f, 1.f, AVSpeechUtteranceMinimumSpeechRate, AVSpeechUtteranceDefaultSpeechRate)]; |
| 84 |
} |
| 85 |
[new_utterance setPitchMultiplier:message.pitch]; |
| 86 |
[new_utterance setVolume:(Math::remap(message.volume, 0.f, 100.f, 0.f, 1.f))]; |
| 87 |
|
| 88 |
ids[new_utterance] = message.id; |
| 89 |
[av_synth speakUtterance:new_utterance]; |
| 90 |
DisplayServer::get_singleton()->tts_post_utterance_event(DisplayServer::TTS_UTTERANCE_STARTED, message.id); |
| 91 |
|
| 92 |
queue.pop_front(); |
| 93 |
speaking = true; |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
- (void)pauseSpeaking { |
| 98 |
[av_synth pauseSpeakingAtBoundary:AVSpeechBoundaryImmediate]; |
| 99 |
} |
| 100 |
|
| 101 |
- (void)resumeSpeaking { |
| 102 |
[av_synth continueSpeaking]; |
| 103 |
} |
| 104 |
|
| 105 |
- (void)stopSpeaking { |
| 106 |
for (DisplayServer::TTSUtterance &message : queue) { |
| 107 |
DisplayServer::get_singleton()->tts_post_utterance_event(DisplayServer::TTS_UTTERANCE_CANCELED, message.id); |
| 108 |
} |
| 109 |
queue.clear(); |
| 110 |
[av_synth stopSpeakingAtBoundary:AVSpeechBoundaryImmediate]; |
| 111 |
speaking = false; |
| 112 |
} |
| 113 |
|
| 114 |
- (bool)isSpeaking { |
| 115 |
return speaking || (queue.size() > 0); |
| 116 |
} |
| 117 |
|
| 118 |
- (bool)isPaused { |
| 119 |
return [av_synth isPaused]; |
| 120 |
} |
| 121 |
|
| 122 |
- (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 { |
| 123 |
if (interrupt) { |
| 124 |
[self stopSpeaking]; |
| 125 |
} |
| 126 |
|
| 127 |
if (text.is_empty()) { |
| 128 |
DisplayServer::get_singleton()->tts_post_utterance_event(DisplayServer::TTS_UTTERANCE_CANCELED, utterance_id); |
| 129 |
return; |
| 130 |
} |
| 131 |
|
| 132 |
DisplayServer::TTSUtterance message; |
| 133 |
message.text = text; |
| 134 |
message.voice = voice; |
| 135 |
message.volume = CLAMP(volume, 0, 100); |
| 136 |
message.pitch = CLAMP(pitch, 0.f, 2.f); |
| 137 |
message.rate = CLAMP(rate, 0.1f, 10.f); |
| 138 |
message.id = utterance_id; |
| 139 |
queue.push_back(message); |
| 140 |
|
| 141 |
if ([self isPaused]) { |
| 142 |
[self resumeSpeaking]; |
| 143 |
} else { |
| 144 |
[self update]; |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
- (Array)getVoices { |
| 149 |
Array list; |
| 150 |
for (AVSpeechSynthesisVoice *voice in [AVSpeechSynthesisVoice speechVoices]) { |
| 151 |
NSString *voiceIdentifierString = [voice identifier]; |
| 152 |
NSString *voiceLocaleIdentifier = [voice language]; |
| 153 |
NSString *voiceName = [voice name]; |
| 154 |
Dictionary voice_d; |
| 155 |
voice_d["name"] = String::utf8([voiceName UTF8String]); |
| 156 |
voice_d["id"] = String::utf8([voiceIdentifierString UTF8String]); |
| 157 |
voice_d["language"] = String::utf8([voiceLocaleIdentifier UTF8String]); |
| 158 |
list.push_back(voice_d); |
| 159 |
} |
| 160 |
return list; |
| 161 |
} |
| 162 |
|
| 163 |
@end |