File: speech.cpp

package info (click to toggle)
scummvm 2.9.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 450,268 kB
  • sloc: cpp: 4,297,604; asm: 28,322; python: 12,901; sh: 11,219; java: 8,477; xml: 7,843; perl: 2,633; ansic: 2,465; yacc: 1,670; javascript: 1,020; makefile: 933; lex: 578; awk: 275; objc: 82; sed: 11; php: 1
file content (211 lines) | stat: -rw-r--r-- 6,255 bytes parent folder | download | duplicates (3)
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include "glk/speech.h"
#include "common/debug.h"
#include "glk/glk.h"

#if defined(USE_TTS)
#include "common/config-manager.h"
#include "common/system.h"
#endif

namespace Glk {

SpeechManager *SpeechManager::_instance = nullptr;

SpeechManager* SpeechManager::getSpeechManagerInstance() {
#if defined(USE_TTS)
	if (!_instance)
		_instance = new SpeechManager();
	++_instance->_refCount;
	return _instance;
#else
	return nullptr;
#endif
}

void SpeechManager::releaseSpeechManagerInstance() {
	if (--_refCount == 0) {
		_instance = nullptr;
		delete this;
	}
}

void SpeechManager::syncSoundSettings() {
#if defined(USE_TTS)
	debugC(kDebugSpeech, "SpeechManager::syncSoundSettings");
	if (_instance && _instance->_ttsMan) {
		int volume = (ConfMan.getInt("speech_volume") * 100) / 256;
		if (ConfMan.hasKey("mute") && ConfMan.getBool("mute"))
			volume = 0;
		debugC(kDebugSpeech, "Set speech volume to %d", volume);
		_instance->_ttsMan->setVolume(volume);
	}
#endif
}

SpeechManager::SpeechManager() :
	_refCount(0)
#if defined(USE_TTS)
	, _ttsMan(nullptr), _lastSpeechSource(nullptr)
#endif
{
#if defined(USE_TTS)
	debugC(kDebugSpeech, "Initialize Glk::SpeechManager");
	if (_ttsMan != nullptr)
		return;
	_ttsMan = g_system->getTextToSpeechManager();
	if (_ttsMan != nullptr) {
		// Language
		_ttsMan->setLanguage(ConfMan.get("language"));
		// Enable
		_ttsMan->enable(true);
		// Volume
		int volume = (ConfMan.getInt("speech_volume") * 100) / 256;
		if (ConfMan.hasKey("mute") && ConfMan.getBool("mute"))
			volume = 0;
		_ttsMan->setVolume(volume);
		// Voice
		unsigned voice;
		if(ConfMan.hasKey("tts_voice")) {
			voice = ConfMan.getInt("tts_voice");
			if (voice >= _ttsMan->getVoicesArray().size())
				voice = _ttsMan->getDefaultVoice();
		} else
			voice = _ttsMan->getDefaultVoice();
		_ttsMan->setVoice(voice);
	} else {
		debugC(kDebugSpeech, "Text to Speech is not available");
	}
#endif
}

SpeechManager::~SpeechManager() {
#if defined(USE_TTS)
	debugC(kDebugSpeech, "Destroy Glk::SpeechManager");
#endif
}

void SpeechManager::speak(const Common::U32String &text, Speech *speechSource) {
#if defined(USE_TTS)
	if (_ttsMan != nullptr) {
		// If the previous speech is from a different source, interrupt it.
		// Otherwise queeue the speech.
		Common::TextToSpeechManager::Action speechAction = Common::TextToSpeechManager::QUEUE;
		if (speechSource != _lastSpeechSource) {
			debugC(kDebugSpeech, "Changing speack text source.");
			// Should we interrupt the text from the other source?
			// Just queueing the text seems to provide a better experience.
			//speechAction = Common::TextToSpeechManager::INTERRUPT;
			_lastSpeechSource = speechSource;
		}
		//debugC(kDebugSpeech, "Speaking: \"%s\"", text.encode().c_str());
		_ttsMan->say(text, speechAction);
	}
#endif
}

void SpeechManager::stopSpeech(Speech *speechSource) {
#if defined(USE_TTS)
	debugC(kDebugSpeech, "SpeechManager::stopSpeech()");
	// Should we only interrupt the speech if it is from the given speech source.
	// If we do that we probably want to change speak to interrupt the speech when
	// called with a different speech source as the current one.
	if (_ttsMan != nullptr)
		_ttsMan->stop();
#endif
}

Speech::Speech() : _speechManager(nullptr) {
}

Speech::~Speech() {
	if (_speechManager) {
		warning("Unbalanced calls to gli_initialize_tts and gli_free_tts");
		_speechManager->releaseSpeechManagerInstance();
	}
}

void Speech::gli_initialize_tts(void) {
	debugC(kDebugSpeech, "gli_initialize_tts");
	if (!_speechManager)
		_speechManager = SpeechManager::getSpeechManagerInstance();
}

void Speech::gli_tts_flush(void) {
	debugC(kDebugSpeech, "gli_tts_flush");
	if (_speechManager && !_speechBuffer.empty())
		_speechManager->speak(_speechBuffer, this);
	_speechBuffer.clear();
}

void Speech::gli_tts_purge(void) {
	debugC(kDebugSpeech, "gli_tts_purge");
	if (_speechManager) {
		_speechBuffer.clear();
		_speechManager->stopSpeech(this);
	}
}

void Speech::gli_tts_speak(const uint32 *buf, size_t len) {
	debugC(1, kDebugSpeech, "gli_tts_speak(const uint32 *, size_t)");
	if (_speechManager) {
		for (uint i = 0 ; i < len ; ++i, ++buf) {
			// Should we automatically flush on new lines without waiting for the call to gli_tts_flush?
			// Should we also flush on '.', '?', and '!'?
			//if (*buf == '\n') {
			//	debugC(1, kDebugSpeech, "Flushing SpeechManager buffer on new line");
			//	gli_tts_flush();
			//} else {
			_speechBuffer += *buf;
			//}
		}
		//debugC(1, kDebugSpeech, "SpeechManager buffer: %s", _speechBuffer.encode().c_str());
	}
}

void Speech::gli_tts_speak(const char *buf, size_t len) {
	debugC(1, kDebugSpeech, "gli_tts_speak(const char *, size_t)");
	if (_speechManager) {
		for (uint i = 0 ; i < len ; ++i, ++buf) {
			// Should we automatically flush on new lines without waiting for the call to gli_tts_flush?
			// Should we also flush on '.', '?', and '!'?
			//if (*buf == '\n') {
			//	debugC(1, kDebugSpeech, "Flushing SpeechManager buffer on new line");
			//	gli_tts_flush();
			//} else {
			_speechBuffer += (uint32)*buf;
			//}
		}
		//debugC(1, kDebugSpeech, "SpeechManager buffer: %s", _speechBuffer.encode().c_str());
	}
}

void Speech::gli_free_tts(void) {
	debugC(kDebugSpeech, "gli_free_tts");
	if (_speechManager) {
		_speechManager->releaseSpeechManagerInstance();
		_speechManager = nullptr;
	}
}

} // End of namespace Glk