aboutsummaryrefslogtreecommitdiff
path: root/common/text-to-speech.h
blob: cd34c287470e530da3a387c38509a6b89645a112 (plain)
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/* 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 2
 * 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, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */

#ifndef BACKENDS_TEXT_TO_SPEECH_ABSTRACT_H
#define BACKENDS_TEXT_TO_SPEECH_ABSTRACT_H

#include "common/scummsys.h"

#if defined(USE_TTS)

#include "common/array.h"
namespace Common {


/**
 * Text to speech voice class.
 */
class TTSVoice {
	friend class TextToSpeechManager;

	public:
		enum Gender {
			MALE,
			FEMALE,
			UNKNOWN_GENDER
		};

		enum Age {
			CHILD,
			ADULT,
			UNKNOWN_AGE
		};

	public:
		TTSVoice();

		TTSVoice(Gender gender, Age age, void *data, String description) ;

		TTSVoice(const TTSVoice& voice);

		~TTSVoice();

		TTSVoice& operator=(const TTSVoice& voice);

		/**
		 * Returns the gender of the used voice.
		 *
		 * @note The gender information is really platform specific, it may not be
		 * possible to find it out on some platforms. Sometimes it can be set by
		 * the user in the TTS engine configuration and so the information might be
		 * wrong
		 */
		Gender getGender() { return _gender; };

		/**
		 * Sets the voice gender, should probably be used only by the backends
		 * that are directly communicating with the TTS engine.
		 */
		void setGender(Gender gender) { _gender = gender; };

		/**
		 * Returns the age of the used voice.
		 *
		 * @note The age information is really platform specific, it may not be
		 * possible to find it out on some platforms. Sometimes it can be set by
		 * the user in the TTS engine configuration and so the information might be
		 * wrong
		 */
		Age getAge() { return _age; };

		/**
		 * Sets the voice age, should probably be used only by the backends
		 * that are directly communicating with the TTS engine.
		 */
		void setAge(Age age) { _age = age; };

		/**
		 * Returns the data about the voice, this is engine specific variable,
		 * it has close to no value for anything else then communicating
		 * directly with the TTS engine, which should probably be done only by
		 * the backends.
		 */
		void *getData() { return _data; };

		/**
		 * Sets the voice data, should probably be used only by the backends
		 * that are directly communicating with the TTS engine.
		 */
		void setData(void *data) { _data = data; };

		/**
		 * Returns the voice description. This description is really tts engine
		 * specific and might be not be available with some tts engines.
		 */
		String getDescription() { return _description; };

	protected:
		Gender _gender; ///< Gender of the voice
		Age _age; ///< Age of the voice
		void *_data; ///< Pointer to tts engine specific data about the voice
		String _description; ///< Description of the voice (gets displayed in GUI)
		int *_refCount; ///< Reference count (serves for proper feeing of _data)
};

struct TTSState {
	int _rate;
	int _pitch;
	int _volume;
	String _language;
	int _activeVoice;
	Array<TTSVoice> _availableVoices;
	TTSState *_next;
};

/**
 * The TextToSpeechManager allows speech synthesis.
 */
class TextToSpeechManager {
public:
	enum Action {
		INTERRUPT,
		INTERRUPT_NO_REPEAT,
		QUEUE,
		QUEUE_NO_REPEAT,
		DROP
	};
	/**
	 * The constructor sets the language to the translation manager language if
	 * USE_TRANSLATION is defined, or english when it isn't defined. It sets the rate,
	 * pitch and volume to their middle values.
	 */
	TextToSpeechManager();
	virtual ~TextToSpeechManager() {}

	/**
	 * Interrupts what's being said and says the given string
	 *
	 * @param str The string to say
	 * @param charset The encoding of the string. If empty this is assumed to be the
	 *        encoding used for the GUI.
	 */
	bool say(String str, String charset = "") { return say(str, INTERRUPT_NO_REPEAT, charset); }

	/**
	 * Says the given string
	 *
	 * @param str The string to say
	 * @param action What to do if another string is just being said.
	 * Possible actions are:
	 *		INTERRUPT - interrupts the current speech
	 *		INTERRUPT_NO_REPEAT - interrupts the speech (deletes the whole queue),
	 *			if the str is the same as the string currently being said,
	 *			it lets the current string finish.
	 *		QUEUE - queues the speech
	 *		QUEUE_NO_REPEAT - queues the speech only if the str is different than
	 *			the last string in the queue (or the string, that is currently
	 *			being said if the queue is empty)
	 *		DROP - does nothing if there is anything being said at the moment
	 * @param charset The encoding of the string. If empty this is assumed to be the
	 *        encoding used for the GUI.
	 */
	virtual bool say(String str, Action action, String charset = "") { return false; }

	/**
	 * Stops the speech
	 */
	virtual bool stop() { return false; }

	/**
	 * Pauses the speech
	 */
	virtual bool pause() { return false; }

	/**
	 * Resumes the speech
	 *
	 * @note On linux, the speech resumes from the begining of the last speech being
	 * said, when pause() was called. On other platforms the speech resumes from
	 * exactly where it was paused();
	 */
	virtual bool resume() { return false; }

	/**
	 * Returns true, if the TTS engine is speaking
	 */
	virtual bool isSpeaking() { return false; }

	/**
	 * Returns true, if the TTS engine is paused
	 */
	virtual bool isPaused() { return false; }

	/**
	 * Returns true, if the TTS engine is ready to speak (isn't speaking and isn't paused)
	 */
	virtual bool isReady() { return false; }

	/**
	 * Sets a voice to be used by the TTS.
	 *
	 * @param index The index of the voice inside the _ttsState->_availableVoices array
	 */
	virtual void setVoice(unsigned index) {}

	/**
	 * Returns the voice, that is used right now
	 */
	TTSVoice getVoice();

	/**
	 * Sets the speech rate
	 *
	 * @param rate Integer between -100 (slowest) and 100 (fastest), 0 is the default
	 */
	virtual void setRate(int rate) { _ttsState->_rate = rate; }

	/**
	 * Returns the current speech rate
	 */
	int getRate() { return _ttsState->_rate; }

	/**
	 * Sets the pitch
	 *
	 * @param pitch Integer between -100 (lowest) and 100 (highest), 0 is the default
	 */
	virtual void setPitch(int pitch) { _ttsState->_pitch = pitch; }

	/**
	 * Returns current speech pitch
	 */
	int getPitch() { return _ttsState->_pitch; }

	/**
	 * Sets the speech volume
	 *
	 * @param volume Volume as a percentage (0 means muted, 100 means as loud as possible)
	 */
	virtual void setVolume(unsigned volume) { _ttsState->_volume = volume; }

	/**
	 * Returns the current voice volume
	 */
	int getVolume() { return _ttsState->_volume; }

	/**
	 * Sets the speech language
	 *
	 * @param language The language identifier as defined by ISO 639-1
	 *
	 * @note After using this method, it is probably a good idea to use setVoice,
	 * because voices are usually language specific and so it is set to some platform
	 * specific default after switching languages.
	 */
	virtual void setLanguage(String language);

	/**
	 * Returns the current speech language
	 */
	String getLanguage() { return _ttsState->_language; }

	/**
	 * Returns array of available voices for the current language
	 */
	Array<TTSVoice> getVoicesArray() { return _ttsState->_availableVoices; }

	/**
	 * Returns array of indices of voices from the _availableVoices array, which
	 * have the needed gender.
	 *
	 * @param gender Gender, which indices should be returned
	 *
	 * @return Array of indices into _availableVoices
	 */
	Array<int> getVoiceIndicesByGender (TTSVoice::Gender gender);

	/**
	 * Pushes the current state of the TTS
	 */
	void pushState();

	/**
	 * Pops the TTS state
	 */
	bool popState();

	/**
	 * Frees the _data field from TTSVoice
	 */
	virtual void freeVoiceData(void *data) {}

protected:
	TTSState *_ttsState;

	void clearState();
	virtual void updateVoices() {};
};

} // End of namespace Common

#endif

#endif // BACKENDS_TEXT_TO_SPEECH_ABSTRACT_H