tts_ios.mm 6.4 KB

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