2019-07-10 10:49:29 +02:00
|
|
|
/* 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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Disable symbol overrides so that we can use system headers.
|
|
|
|
#define FORBIDDEN_SYMBOL_ALLOW_ALL
|
|
|
|
|
|
|
|
#include "backends/text-to-speech/linux/linux-text-to-speech.h"
|
|
|
|
|
|
|
|
#if defined(USE_LINUX_TTS)
|
2019-07-11 00:14:28 +02:00
|
|
|
#include <speech-dispatcher/libspeechd.h>
|
2019-07-18 13:34:22 +02:00
|
|
|
#include "backends/platform/sdl/sdl-sys.h"
|
2019-07-11 00:14:28 +02:00
|
|
|
|
2019-07-10 10:49:29 +02:00
|
|
|
#include "common/translation.h"
|
2019-07-11 00:14:28 +02:00
|
|
|
#include "common/system.h"
|
2019-07-14 21:48:26 +02:00
|
|
|
#include "common/ustr.h"
|
|
|
|
#include "common/config-manager.h"
|
2019-08-02 17:03:17 +02:00
|
|
|
|
2019-07-11 00:14:28 +02:00
|
|
|
SPDConnection *_connection;
|
|
|
|
|
|
|
|
void speech_begin_callback(size_t msg_id, size_t client_id, SPDNotificationType state){
|
|
|
|
LinuxTextToSpeechManager *manager =
|
|
|
|
static_cast<LinuxTextToSpeechManager *> (g_system->getTextToSpeechManager());
|
2019-08-02 17:03:17 +02:00
|
|
|
manager->updateState(LinuxTextToSpeechManager::SPEECH_BEGUN);
|
2019-07-11 00:14:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void speech_end_callback(size_t msg_id, size_t client_id, SPDNotificationType state){
|
|
|
|
LinuxTextToSpeechManager *manager =
|
|
|
|
static_cast<LinuxTextToSpeechManager *> (g_system->getTextToSpeechManager());
|
2019-08-02 17:03:17 +02:00
|
|
|
manager->updateState(LinuxTextToSpeechManager::SPEECH_ENDED);
|
2019-07-11 00:14:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void speech_cancel_callback(size_t msg_id, size_t client_id, SPDNotificationType state){
|
|
|
|
LinuxTextToSpeechManager *manager =
|
|
|
|
static_cast<LinuxTextToSpeechManager *> (g_system->getTextToSpeechManager());
|
2019-08-02 17:03:17 +02:00
|
|
|
manager->updateState(LinuxTextToSpeechManager::SPEECH_CANCELED);
|
2019-07-11 00:14:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void speech_resume_callback(size_t msg_id, size_t client_id, SPDNotificationType state){
|
|
|
|
LinuxTextToSpeechManager *manager =
|
|
|
|
static_cast<LinuxTextToSpeechManager *> (g_system->getTextToSpeechManager());
|
2019-08-02 17:03:17 +02:00
|
|
|
manager->updateState(LinuxTextToSpeechManager::SPEECH_RESUMED);
|
2019-07-11 00:14:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void speech_pause_callback(size_t msg_id, size_t client_id, SPDNotificationType state){
|
|
|
|
LinuxTextToSpeechManager *manager =
|
|
|
|
static_cast<LinuxTextToSpeechManager *> (g_system->getTextToSpeechManager());
|
2019-08-02 17:03:17 +02:00
|
|
|
manager->updateState(LinuxTextToSpeechManager::SPEECH_PAUSED);
|
2019-07-11 00:14:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
LinuxTextToSpeechManager::LinuxTextToSpeechManager()
|
|
|
|
: _speechState(READY) {
|
2019-07-14 21:01:37 +02:00
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
|
|
|
void LinuxTextToSpeechManager::init() {
|
2019-07-11 00:14:28 +02:00
|
|
|
_connection = spd_open("ScummVM", "main", NULL, SPD_MODE_THREADED);
|
|
|
|
if (_connection == 0) {
|
2019-07-11 23:38:06 +02:00
|
|
|
_speechState = BROKEN;
|
2019-07-14 19:34:29 +02:00
|
|
|
warning("Couldn't initialize text to speech through speech-dispatcher");
|
2019-07-11 00:14:28 +02:00
|
|
|
return;
|
|
|
|
}
|
2019-08-02 01:50:59 +02:00
|
|
|
_lastSaid = "";
|
2019-07-10 10:49:29 +02:00
|
|
|
|
2019-07-11 00:14:28 +02:00
|
|
|
_connection->callback_begin = speech_begin_callback;
|
|
|
|
spd_set_notification_on(_connection, SPD_BEGIN);
|
|
|
|
_connection->callback_end = speech_end_callback;
|
|
|
|
spd_set_notification_on(_connection, SPD_END);
|
|
|
|
_connection->callback_cancel = speech_cancel_callback;
|
|
|
|
spd_set_notification_on(_connection, SPD_CANCEL);
|
|
|
|
_connection->callback_resume = speech_resume_callback;
|
|
|
|
spd_set_notification_on(_connection, SPD_RESUME);
|
|
|
|
_connection->callback_pause = speech_pause_callback;
|
|
|
|
spd_set_notification_on(_connection, SPD_PAUSE);
|
|
|
|
|
|
|
|
updateVoices();
|
2019-07-12 00:46:42 +02:00
|
|
|
_ttsState->_activeVoice = 0;
|
2019-07-17 23:51:58 +02:00
|
|
|
#ifdef USE_TRANSLATION
|
|
|
|
setLanguage(TransMan.getCurrentLanguage());
|
|
|
|
#else
|
|
|
|
setLanguage("en");
|
|
|
|
#endif
|
2019-08-02 17:03:17 +02:00
|
|
|
_speechQueue.clear();
|
2019-07-10 10:49:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
LinuxTextToSpeechManager::~LinuxTextToSpeechManager() {
|
2019-07-11 23:38:06 +02:00
|
|
|
if (_connection != 0)
|
|
|
|
spd_close(_connection);
|
2019-07-11 00:14:28 +02:00
|
|
|
}
|
|
|
|
|
2019-08-02 17:03:17 +02:00
|
|
|
void LinuxTextToSpeechManager::updateState(LinuxTextToSpeechManager::SpeechEvent event) {
|
|
|
|
if (_speechState == BROKEN)
|
|
|
|
return;
|
|
|
|
switch(event) {
|
|
|
|
case SPEECH_ENDED:
|
|
|
|
_speechQueue.pop_front();
|
|
|
|
if (_speechQueue.size() == 0)
|
|
|
|
_speechState = READY;
|
|
|
|
break;
|
|
|
|
case SPEECH_PAUSED:
|
|
|
|
_speechState = PAUSED;
|
|
|
|
break;
|
|
|
|
case SPEECH_CANCELED:
|
|
|
|
if (_speechState != PAUSED) {
|
|
|
|
_speechState = READY;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SPEECH_RESUMED:
|
|
|
|
break;
|
|
|
|
case SPEECH_BEGUN:
|
|
|
|
_speechState = SPEAKING;
|
|
|
|
break;
|
|
|
|
}
|
2019-07-11 00:14:28 +02:00
|
|
|
}
|
|
|
|
|
2019-07-18 13:34:22 +02:00
|
|
|
Common::String LinuxTextToSpeechManager::strToUtf8(Common::String str, Common::String charset) {
|
|
|
|
#if SDL_VERSION_ATLEAST(2, 0, 0)
|
|
|
|
|
|
|
|
char *conv_text = SDL_iconv_string("UTF-8", charset.c_str(), str.c_str(), str.size() + 1);
|
|
|
|
Common::String result;
|
|
|
|
if (conv_text) {
|
|
|
|
result = conv_text;
|
|
|
|
SDL_free(conv_text);
|
2019-07-22 19:35:56 +02:00
|
|
|
} else if (charset != "ASCII"){
|
|
|
|
warning("Could not convert text from %s to UTF-8, trying ASCII", charset.c_str());
|
|
|
|
return strToUtf8(str, "ASCII");
|
|
|
|
} else
|
|
|
|
warning("Could not convert text to UTF-8");
|
2019-07-18 13:34:22 +02:00
|
|
|
|
|
|
|
return result;
|
|
|
|
#else
|
|
|
|
return Common::String();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2019-07-31 15:10:12 +02:00
|
|
|
bool LinuxTextToSpeechManager::say(Common::String str, Action action, Common::String charset) {
|
2019-07-11 23:38:06 +02:00
|
|
|
if (_speechState == BROKEN)
|
|
|
|
return true;
|
2019-07-31 15:10:12 +02:00
|
|
|
|
|
|
|
if (action == DROP && isSpeaking())
|
|
|
|
return true;
|
2019-08-02 01:50:59 +02:00
|
|
|
|
|
|
|
if (action == INTERRUPT_NO_REPEAT && _lastSaid == str && isSpeaking())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (action == QUEUE_NO_REPEAT && _lastSaid == str && isSpeaking())
|
|
|
|
return true;
|
2019-07-18 13:34:22 +02:00
|
|
|
|
2019-07-18 23:43:37 +02:00
|
|
|
if (charset.empty()) {
|
2019-07-18 13:34:22 +02:00
|
|
|
#ifdef USE_TRANSLATION
|
|
|
|
charset = TransMan.getCurrentCharset();
|
|
|
|
#else
|
|
|
|
charset = "ASCII";
|
|
|
|
#endif
|
2019-07-14 21:48:26 +02:00
|
|
|
}
|
2019-07-22 19:35:56 +02:00
|
|
|
|
2019-07-18 13:34:22 +02:00
|
|
|
str = strToUtf8(str, charset);
|
|
|
|
|
2019-08-02 17:03:17 +02:00
|
|
|
if (isSpeaking() && (action == INTERRUPT || action == INTERRUPT_NO_REPEAT))
|
2019-07-11 00:14:28 +02:00
|
|
|
stop();
|
2019-08-02 17:03:17 +02:00
|
|
|
if (!str.empty()) {
|
2019-08-01 21:24:23 +02:00
|
|
|
_speechState = SPEAKING;
|
2019-08-02 17:03:17 +02:00
|
|
|
_speechQueue.push_back(str);
|
|
|
|
_lastSaid = str;
|
|
|
|
if(spd_say(_connection, SPD_MESSAGE, str.c_str()) == -1) {
|
|
|
|
//restart the connection
|
|
|
|
if (_connection != 0)
|
|
|
|
spd_close(_connection);
|
|
|
|
init();
|
|
|
|
return true;
|
|
|
|
}
|
2019-07-14 21:01:37 +02:00
|
|
|
}
|
2019-08-01 21:24:23 +02:00
|
|
|
|
2019-07-14 21:01:37 +02:00
|
|
|
return false;
|
2019-07-11 00:14:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool LinuxTextToSpeechManager::stop() {
|
2019-07-11 23:38:06 +02:00
|
|
|
if (_speechState == READY || _speechState == BROKEN)
|
|
|
|
return true;
|
2019-08-01 21:24:23 +02:00
|
|
|
_speechState = READY;
|
2019-08-02 17:03:17 +02:00
|
|
|
_speechQueue.clear();
|
2019-07-11 00:14:28 +02:00
|
|
|
return spd_cancel(_connection) == -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LinuxTextToSpeechManager::pause() {
|
2019-07-11 23:38:06 +02:00
|
|
|
if (_speechState == READY || _speechState == PAUSED || _speechState == BROKEN)
|
|
|
|
return true;
|
2019-08-02 17:03:17 +02:00
|
|
|
_speechState = PAUSED;
|
|
|
|
bool result = spd_cancel_all(_connection) == -1;
|
2019-08-01 23:34:58 +02:00
|
|
|
if (result)
|
|
|
|
return true;
|
|
|
|
if (result)
|
|
|
|
return true;
|
|
|
|
return false;
|
2019-07-11 00:14:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool LinuxTextToSpeechManager::resume() {
|
2019-07-11 23:38:06 +02:00
|
|
|
if (_speechState == READY || _speechState == SPEAKING || _speechState == BROKEN)
|
|
|
|
return true;
|
2019-08-02 17:03:17 +02:00
|
|
|
if (_speechQueue.size()) {
|
|
|
|
_speechState = SPEAKING;
|
|
|
|
for (Common::List<Common::String>::iterator i = _speechQueue.begin(); i != _speechQueue.end(); i++) {
|
2019-08-05 00:23:15 +02:00
|
|
|
if (spd_say(_connection, SPD_MESSAGE, i->c_str()) == -1) {
|
|
|
|
if (_connection != 0)
|
|
|
|
spd_close(_connection);
|
|
|
|
init();
|
2019-08-02 17:03:17 +02:00
|
|
|
return true;
|
2019-08-05 00:23:15 +02:00
|
|
|
}
|
2019-08-02 17:03:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
_speechState = READY;
|
|
|
|
return false;
|
2019-07-11 00:14:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool LinuxTextToSpeechManager::isSpeaking() {
|
2019-07-11 23:38:06 +02:00
|
|
|
return _speechState == SPEAKING;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LinuxTextToSpeechManager::isPaused() {
|
|
|
|
return _speechState == PAUSED;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LinuxTextToSpeechManager::isReady() {
|
|
|
|
return _speechState == READY;
|
2019-07-11 00:14:28 +02:00
|
|
|
}
|
|
|
|
|
2019-07-12 00:46:42 +02:00
|
|
|
void LinuxTextToSpeechManager::setVoice(unsigned index) {
|
2019-07-11 23:38:06 +02:00
|
|
|
if (_speechState == BROKEN)
|
|
|
|
return;
|
2019-07-18 23:53:52 +02:00
|
|
|
assert(index < _ttsState->_availableVoices.size());
|
|
|
|
Common::TTSVoice voice = _ttsState->_availableVoices[index];
|
2019-07-12 00:46:42 +02:00
|
|
|
spd_set_voice_type(_connection, *(SPDVoiceType *)(voice.getData()));
|
|
|
|
_ttsState->_activeVoice = index;
|
2019-07-11 00:14:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void LinuxTextToSpeechManager::setRate(int rate) {
|
2019-07-11 23:38:06 +02:00
|
|
|
if (_speechState == BROKEN)
|
|
|
|
return;
|
2019-07-11 00:14:28 +02:00
|
|
|
assert(rate >= -100 && rate <= 100);
|
|
|
|
spd_set_voice_rate(_connection, rate);
|
|
|
|
_ttsState->_rate = rate;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LinuxTextToSpeechManager::setPitch(int pitch) {
|
2019-07-11 23:38:06 +02:00
|
|
|
if (_speechState == BROKEN)
|
|
|
|
return;
|
2019-07-11 00:14:28 +02:00
|
|
|
assert(pitch >= -100 && pitch <= 100);
|
|
|
|
spd_set_voice_pitch(_connection, pitch);
|
|
|
|
_ttsState->_pitch = pitch;
|
|
|
|
}
|
|
|
|
|
2019-07-12 12:52:18 +02:00
|
|
|
void LinuxTextToSpeechManager::setVolume(unsigned volume) {
|
2019-07-11 23:38:06 +02:00
|
|
|
if (_speechState == BROKEN)
|
|
|
|
return;
|
2019-07-12 12:52:18 +02:00
|
|
|
assert(volume <= 100);
|
|
|
|
spd_set_volume(_connection, (volume - 50) * 2);
|
2019-07-11 00:14:28 +02:00
|
|
|
_ttsState->_volume = volume;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LinuxTextToSpeechManager::setLanguage(Common::String language) {
|
2019-07-11 23:38:06 +02:00
|
|
|
if (_speechState == BROKEN)
|
|
|
|
return;
|
2019-07-11 00:14:28 +02:00
|
|
|
spd_set_language(_connection, language.c_str());
|
|
|
|
_ttsState->_language = language;
|
2019-07-12 00:46:42 +02:00
|
|
|
setVoice(_ttsState->_activeVoice);
|
2019-07-11 00:14:28 +02:00
|
|
|
}
|
|
|
|
|
2019-07-17 13:33:42 +02:00
|
|
|
void LinuxTextToSpeechManager::createVoice(int typeNumber, Common::TTSVoice::Gender gender, Common::TTSVoice::Age age, char *description) {
|
2019-07-12 22:16:44 +02:00
|
|
|
SPDVoiceType *type = (SPDVoiceType *) malloc(sizeof(SPDVoiceType));
|
|
|
|
*type = static_cast<SPDVoiceType>(typeNumber);
|
2019-07-18 11:27:17 +02:00
|
|
|
Common::TTSVoice voice(gender, age, (void *) type, description);
|
2019-07-18 23:53:52 +02:00
|
|
|
_ttsState->_availableVoices.push_back(voice);
|
2019-07-12 22:16:44 +02:00
|
|
|
}
|
|
|
|
|
2019-07-11 00:14:28 +02:00
|
|
|
void LinuxTextToSpeechManager::updateVoices() {
|
2019-07-11 23:38:06 +02:00
|
|
|
if (_speechState == BROKEN)
|
|
|
|
return;
|
2019-07-11 00:14:28 +02:00
|
|
|
/* just use these voices:
|
|
|
|
SPD_MALE1, SPD_MALE2, SPD_MALE3,
|
|
|
|
SPD_FEMALE1, SPD_FEMALE2, SPD_FEMALE3,
|
|
|
|
SPD_CHILD_MALE, SPD_CHILD_FEMALE
|
|
|
|
|
|
|
|
it depends on the user to map them to the right voices in speech-dispatcher
|
|
|
|
configuration
|
|
|
|
*/
|
2019-07-23 17:54:31 +02:00
|
|
|
_ttsState->_availableVoices.clear();
|
2019-07-11 00:14:28 +02:00
|
|
|
|
2019-07-12 22:16:44 +02:00
|
|
|
char **voiceInfo = spd_list_voices(_connection);
|
2019-07-11 00:14:28 +02:00
|
|
|
|
2019-07-17 13:33:42 +02:00
|
|
|
createVoice(SPD_MALE1, Common::TTSVoice::MALE, Common::TTSVoice::ADULT, voiceInfo[0]);
|
|
|
|
createVoice(SPD_MALE2, Common::TTSVoice::MALE, Common::TTSVoice::ADULT, voiceInfo[1]);
|
|
|
|
createVoice(SPD_MALE3, Common::TTSVoice::MALE, Common::TTSVoice::ADULT, voiceInfo[2]);
|
|
|
|
createVoice(SPD_FEMALE1, Common::TTSVoice::FEMALE, Common::TTSVoice::ADULT, voiceInfo[3]);
|
|
|
|
createVoice(SPD_FEMALE2, Common::TTSVoice::FEMALE, Common::TTSVoice::ADULT, voiceInfo[4]);
|
|
|
|
createVoice(SPD_FEMALE3, Common::TTSVoice::FEMALE, Common::TTSVoice::ADULT, voiceInfo[5]);
|
|
|
|
createVoice(SPD_CHILD_MALE, Common::TTSVoice::MALE, Common::TTSVoice::CHILD, voiceInfo[6]);
|
|
|
|
createVoice(SPD_CHILD_FEMALE, Common::TTSVoice::FEMALE, Common::TTSVoice::CHILD, voiceInfo[7]);
|
2019-07-11 00:14:28 +02:00
|
|
|
|
2019-07-19 09:56:26 +02:00
|
|
|
for (int i = 0; i < 8; i++)
|
|
|
|
free(voiceInfo[i]);
|
|
|
|
|
|
|
|
free(voiceInfo);
|
2019-07-10 10:49:29 +02:00
|
|
|
}
|
|
|
|
|
2019-07-18 11:27:17 +02:00
|
|
|
void LinuxTextToSpeechManager::freeVoiceData(void *data) {
|
|
|
|
free(data);
|
|
|
|
}
|
|
|
|
|
2019-07-16 22:09:05 -07:00
|
|
|
|
2019-07-10 10:49:29 +02:00
|
|
|
#endif
|