| 1 |
// Copyright (C) 2006-2009 by Tor Andersson. |
| 2 |
// Copyright (C) 2017 by Chris Spiegel. |
| 3 |
// Copyright (C) 2017 by Kerry Guerrero. |
| 4 |
// |
| 5 |
// This file is part of Gargoyle. |
| 6 |
// |
| 7 |
// Gargoyle is free software; you can redistribute it and/or modify |
| 8 |
// it under the terms of the GNU General Public License as published by |
| 9 |
// the Free Software Foundation; either version 2 of the License, or |
| 10 |
// (at your option) any later version. |
| 11 |
// |
| 12 |
// Gargoyle is distributed in the hope that it will be useful, |
| 13 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
// GNU General Public License for more details. |
| 16 |
// |
| 17 |
// You should have received a copy of the GNU General Public License |
| 18 |
// along with Gargoyle; if not, write to the Free Software |
| 19 |
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 |
|
| 21 |
#import <Cocoa/Cocoa.h> |
| 22 |
|
| 23 |
#include <vector> |
| 24 |
|
| 25 |
#include "glk.h" |
| 26 |
#include "garglk.h" |
| 27 |
|
| 28 |
#import "sysmac.h" |
| 29 |
|
| 30 |
// a queue of phrases to feed to the speech synthesizer |
| 31 |
static NSMutableArray *phraseQueue = nil; |
| 32 |
static NSRange purgeRange; |
| 33 |
|
| 34 |
@interface SpeechDelegate : NSObject <NSSpeechSynthesizerDelegate> |
| 35 |
{ |
| 36 |
} |
| 37 |
- (void) speechSynthesizer: (NSSpeechSynthesizer *) sender |
| 38 |
didFinishSpeaking: (BOOL) finishSpeaking; |
| 39 |
@end |
| 40 |
|
| 41 |
@implementation SpeechDelegate |
| 42 |
{ |
| 43 |
} |
| 44 |
- (void) speechSynthesizer: (NSSpeechSynthesizer *) sender |
| 45 |
didFinishSpeaking: (BOOL) finishSpeaking |
| 46 |
{ |
| 47 |
if (finishSpeaking == NO) { |
| 48 |
// speaking stopped abruptly (see gli_tts_purge()) |
| 49 |
[phraseQueue removeObjectsInRange: purgeRange]; |
| 50 |
} else { |
| 51 |
// remove the head of the list which just completed speaking |
| 52 |
if (phraseQueue.count > 0) { |
| 53 |
[phraseQueue removeObjectAtIndex: 0]; |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
// speak the next phrase |
| 58 |
if (phraseQueue.count > 0) { |
| 59 |
[sender startSpeakingString: [phraseQueue objectAtIndex: 0]]; |
| 60 |
} |
| 61 |
} |
| 62 |
@end |
| 63 |
|
| 64 |
static std::vector<glui32> txtbuf; |
| 65 |
|
| 66 |
static NSSpeechSynthesizer *synth = nil; |
| 67 |
|
| 68 |
void gli_initialize_tts() |
| 69 |
{ |
| 70 |
if (gli_conf_speak) { |
| 71 |
// spawn speech synthesizer using the default voice |
| 72 |
synth = [NSSpeechSynthesizer new]; |
| 73 |
if (!synth) { |
| 74 |
NSLog(@"Unable to initialize TTS support!"); |
| 75 |
return; |
| 76 |
} |
| 77 |
|
| 78 |
phraseQueue = [NSMutableArray arrayWithCapacity:64]; |
| 79 |
|
| 80 |
synth.delegate = [SpeechDelegate new]; |
| 81 |
|
| 82 |
// configure optional non-default speaking voice/language |
| 83 |
if (!gli_conf_speak_language.empty()) { |
| 84 |
NSString *lang = [NSString stringWithCString: gli_conf_speak_language.c_str() |
| 85 |
encoding: NSUTF8StringEncoding]; |
| 86 |
|
| 87 |
NSArray *voices = [NSSpeechSynthesizer availableVoices]; |
| 88 |
for (NSString *voice in voices) { |
| 89 |
NSDictionary *attr = [NSSpeechSynthesizer attributesForVoice: voice]; |
| 90 |
if ([lang isEqualToString: [attr objectForKey: NSVoiceLocaleIdentifier]]) { |
| 91 |
[synth setVoice: voice]; |
| 92 |
} |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
NSLog(@"TTS configured with voice: %@", [synth voice]); |
| 97 |
} |
| 98 |
|
| 99 |
txtbuf.clear(); |
| 100 |
} |
| 101 |
|
| 102 |
static void ttsmac_add_phrase(NSString * phrase) |
| 103 |
{ |
| 104 |
// append the phrase to the queue |
| 105 |
[phraseQueue addObject: phrase]; |
| 106 |
|
| 107 |
// if the queue was empty we need to explicitly start speaking |
| 108 |
if (phraseQueue.count == 1) { |
| 109 |
[synth startSpeakingString: [phraseQueue objectAtIndex: 0]]; |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
static void ttsmac_flush() |
| 114 |
{ |
| 115 |
if (!txtbuf.empty()) { |
| 116 |
// convert codepoints in buffer into an NSString |
| 117 |
NSString *text = [[NSString alloc] initWithBytes: txtbuf.data() |
| 118 |
length: (txtbuf.size() * sizeof(glui32)) |
| 119 |
encoding: UTF32StringEncoding]; |
| 120 |
|
| 121 |
if (text) { |
| 122 |
ttsmac_add_phrase(text); |
| 123 |
[text release]; |
| 124 |
} |
| 125 |
|
| 126 |
txtbuf.clear(); |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
void gli_tts_flush() |
| 131 |
{ |
| 132 |
if (synth) { |
| 133 |
ttsmac_flush(); |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
void gli_tts_purge() |
| 138 |
{ |
| 139 |
if (synth) { |
| 140 |
purgeRange = NSMakeRange(0, phraseQueue.count); |
| 141 |
[synth stopSpeaking]; |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
void gli_tts_speak(const glui32 *buf, size_t len) |
| 146 |
{ |
| 147 |
if (!synth) { |
| 148 |
return; |
| 149 |
} |
| 150 |
|
| 151 |
for (size_t i = 0; i < len; i++) { |
| 152 |
if (buf[i] == '>' || buf[i] == '*') { |
| 153 |
continue; |
| 154 |
} |
| 155 |
|
| 156 |
txtbuf.push_back(buf[i]); |
| 157 |
|
| 158 |
// Feed the synthesizer in paragraph-sized chunks. A compromise |
| 159 |
// as doing it in smaller pieces (e.g. sentences) doesn't always |
| 160 |
// sound right when dealing with quote-delimited text. The quoted |
| 161 |
// text should be sent to the synthesizer in a single call which |
| 162 |
// requires scanning for quote pairs (including typographic |
| 163 |
// quotes) and correct handling of single-quotes and possesive |
| 164 |
// forms (at least for English). More work than I want to deal |
| 165 |
// with at this time. |
| 166 |
if (/*buf[i] == '.' || buf[i] == '!' || buf[i] == '?' || */ buf[i] == '\n') { |
| 167 |
gli_tts_flush(); |
| 168 |
} |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
void gli_free_tts() |
| 173 |
{ |
| 174 |
gli_tts_purge(); |
| 175 |
|
| 176 |
if (synth) { |
| 177 |
id delegate = synth.delegate; |
| 178 |
synth.delegate = nil; |
| 179 |
|
| 180 |
[synth release]; |
| 181 |
synth = nil; |
| 182 |
|
| 183 |
if (delegate) { |
| 184 |
[delegate release]; |
| 185 |
delegate = nil; |
| 186 |
} |
| 187 |
} |
| 188 |
} |