TTS: Add part of linux TTS

This commit is contained in:
Jaromir Wysoglad 2019-07-11 00:14:28 +02:00 committed by Filippos Karapetis
parent f78fc85f3a
commit 39e74b027e
7 changed files with 327 additions and 5 deletions

View file

@ -26,12 +26,177 @@
#include "backends/text-to-speech/linux/linux-text-to-speech.h"
#if defined(USE_LINUX_TTS)
#include "common/translation.h"
#include <speech-dispatcher/libspeechd.h>
LinuxTextToSpeechManager::LinuxTextToSpeechManager() {
#include "common/translation.h"
#include "common/debug.h"
#include "common/system.h"
SPDConnection *_connection;
void speech_begin_callback(size_t msg_id, size_t client_id, SPDNotificationType state){
LinuxTextToSpeechManager *manager =
static_cast<LinuxTextToSpeechManager *> (g_system->getTextToSpeechManager());
manager->updateState(LinuxTextToSpeechManager::SPEAKING);
}
void speech_end_callback(size_t msg_id, size_t client_id, SPDNotificationType state){
LinuxTextToSpeechManager *manager =
static_cast<LinuxTextToSpeechManager *> (g_system->getTextToSpeechManager());
manager->updateState(LinuxTextToSpeechManager::READY);
}
void speech_cancel_callback(size_t msg_id, size_t client_id, SPDNotificationType state){
LinuxTextToSpeechManager *manager =
static_cast<LinuxTextToSpeechManager *> (g_system->getTextToSpeechManager());
manager->updateState(LinuxTextToSpeechManager::READY);
}
void speech_resume_callback(size_t msg_id, size_t client_id, SPDNotificationType state){
LinuxTextToSpeechManager *manager =
static_cast<LinuxTextToSpeechManager *> (g_system->getTextToSpeechManager());
manager->updateState(LinuxTextToSpeechManager::SPEAKING);
}
void speech_pause_callback(size_t msg_id, size_t client_id, SPDNotificationType state){
LinuxTextToSpeechManager *manager =
static_cast<LinuxTextToSpeechManager *> (g_system->getTextToSpeechManager());
manager->updateState(LinuxTextToSpeechManager::PAUSED);
}
LinuxTextToSpeechManager::LinuxTextToSpeechManager()
: _speechState(READY) {
_connection = spd_open("ScummVM", "main", NULL, SPD_MODE_THREADED);
if (_connection == 0) {
debug("couldn't open");
return;
}
_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);
setLanguage(Common::String("en"));
updateVoices();
}
LinuxTextToSpeechManager::~LinuxTextToSpeechManager() {
spd_close(_connection);
}
void LinuxTextToSpeechManager::updateState(LinuxTextToSpeechManager::SpeechState state) {
_speechState = state;
}
bool LinuxTextToSpeechManager::say(Common::String str) {
if (isSpeaking())
stop();
return spd_say(_connection, SPD_MESSAGE, str.c_str()) == -1;
}
bool LinuxTextToSpeechManager::stop() {
if (_speechState == READY)
return false;
return spd_cancel(_connection) == -1;
}
bool LinuxTextToSpeechManager::pause() {
if (_speechState == READY || _speechState == PAUSED)
return false;
return spd_pause(_connection) == -1;
}
bool LinuxTextToSpeechManager::resume() {
if (_speechState == READY || _speechState == SPEAKING)
return false;
return spd_resume(_connection) == -1;
}
bool LinuxTextToSpeechManager::isSpeaking() {
if (_speechState == SPEAKING)
return true;
return false;
}
void LinuxTextToSpeechManager::setVoice(Common::TTSVoice *voice) {
assert(voice != nullptr && voice->getData() != nullptr);
spd_set_voice_type(_connection, *(SPDVoiceType *)(voice->getData()));
_ttsState->_activeVoice = voice;
}
void LinuxTextToSpeechManager::setRate(int rate) {
assert(rate >= -100 && rate <= 100);
spd_set_voice_rate(_connection, rate);
_ttsState->_rate = rate;
}
void LinuxTextToSpeechManager::setPitch(int pitch) {
assert(pitch >= -100 && pitch <= 100);
spd_set_voice_pitch(_connection, pitch);
_ttsState->_pitch = pitch;
}
void LinuxTextToSpeechManager::setVolume(int volume) {
assert(volume >= -100 && volume <= 100);
spd_set_volume(_connection, volume);
_ttsState->_volume = volume;
}
void LinuxTextToSpeechManager::setLanguage(Common::String language) {
spd_set_language(_connection, language.c_str());
_ttsState->_language = language;
if (_ttsState->_activeVoice)
setVoice(_ttsState->_activeVoice);
}
void LinuxTextToSpeechManager::updateVoices() {
/* 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
*/
SPDVoiceType *type = (SPDVoiceType *) malloc(sizeof(SPDVoiceType));
*type = SPD_MALE1;
_ttsState->_availaibleVoices.push_back(Common::TTSVoice(Common::TTSVoice::MALE, (void *) type));
type = (SPDVoiceType *) malloc(sizeof(SPDVoiceType));
*type = SPD_MALE2;
_ttsState->_availaibleVoices.push_back(Common::TTSVoice(Common::TTSVoice::MALE, (void *) type));
type = (SPDVoiceType *) malloc(sizeof(SPDVoiceType));
*type = SPD_MALE3;
_ttsState->_availaibleVoices.push_back(Common::TTSVoice(Common::TTSVoice::MALE, (void *) type));
type = (SPDVoiceType *) malloc(sizeof(SPDVoiceType));
*type = SPD_FEMALE1;
_ttsState->_availaibleVoices.push_back(Common::TTSVoice(Common::TTSVoice::FEMALE, (void *) type));
type = (SPDVoiceType *) malloc(sizeof(SPDVoiceType));
*type = SPD_FEMALE2;
_ttsState->_availaibleVoices.push_back(Common::TTSVoice(Common::TTSVoice::FEMALE, (void *) type));
type = (SPDVoiceType *) malloc(sizeof(SPDVoiceType));
*type = SPD_FEMALE3;
_ttsState->_availaibleVoices.push_back(Common::TTSVoice(Common::TTSVoice::FEMALE, (void *) type));
type = (SPDVoiceType *) malloc(sizeof(SPDVoiceType));
*type = SPD_CHILD_MALE;
_ttsState->_availaibleVoices.push_back(Common::TTSVoice(Common::TTSVoice::MALE, (void *) type));
type = (SPDVoiceType *) malloc(sizeof(SPDVoiceType));
*type = SPD_CHILD_FEMALE;
_ttsState->_availaibleVoices.push_back(Common::TTSVoice(Common::TTSVoice::FEMALE, (void *) type));
}
#endif

View file

@ -28,11 +28,42 @@
#if defined(USE_LINUX_TTS)
#include "common/text-to-speech.h"
#include "common/str.h"
class LinuxTextToSpeechManager : public Common::TextToSpeechManager {
public:
enum SpeechState {
READY,
PAUSED,
SPEAKING
};
LinuxTextToSpeechManager();
virtual ~LinuxTextToSpeechManager();
virtual bool say(Common::String str);
virtual bool stop();
virtual bool pause();
virtual bool resume();
virtual bool isSpeaking();
virtual void setVoice(Common::TTSVoice *voice);
virtual void setRate(int rate);
virtual void setPitch(int pitch);
virtual void setVolume(int volume);
virtual void setLanguage(Common::String language);
void updateState(SpeechState state);
private:
virtual void updateVoices();
SpeechState _speechState;
};
#endif

View file

@ -99,5 +99,10 @@ MODULE_OBJS += \
lua/scummvm_file.o
endif
ifdef USE_TTS
MODULE_OBJS += \
text-to-speech.o
endif
# Include common rules
include $(srcdir)/rules.mk

47
common/text-to-speech.cpp Normal file
View file

@ -0,0 +1,47 @@
/* 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.
*
*/
#include "common/text-to-speech.h"
#if defined(USE_TTS)
namespace Common {
TextToSpeechManager::TextToSpeechManager() {
_ttsState = new TTSState;
_ttsState->_pitch = 0;
_ttsState->_volume = 0;
_ttsState->_rate = 0;
_ttsState->_activeVoice = nullptr;
_ttsState->_next = nullptr;
}
TextToSpeechManager::~TextToSpeechManager() {
TTSState *tmp = _ttsState;
while (tmp != nullptr) {
tmp = _ttsState->_next;
delete _ttsState;
_ttsState = tmp;
}
}
}
#endif

View file

@ -23,19 +23,90 @@
#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"
#include "common/debug.h"
namespace Common {
class TTSVoice {
friend class TextToSpeechManager;
public:
enum Gender {
MALE,
FEMALE,
UNKNOWN
};
public:
TTSVoice()
: _gender(UNKNOWN)
, _data(nullptr) {}
TTSVoice(Gender gender, void *data)
: _gender(gender)
, _data(data) {}
~TTSVoice() { debug("%d", * (int *)_data); if (_data != nullptr) free(_data); }
Gender getGender() { return _gender; };
void setGender(Gender gender) { _gender = gender; };
void setData(void *data) { _data = data; };
void *getData() { return _data; };
protected:
Gender _gender;
void *_data;
};
struct TTSState {
int _rate;
int _pitch;
int _volume;
String _language;
TTSVoice *_activeVoice;
Array<TTSVoice> _availaibleVoices;
TTSState *_next;
};
/**
* The TextToSpeechManager allows speech synthesis.
*
*/
class TextToSpeechManager {
public:
TextToSpeechManager() {}
virtual ~TextToSpeechManager() {}
TextToSpeechManager();
virtual ~TextToSpeechManager();
virtual bool say(String str) { return false; }
virtual bool stop() { return false; }
virtual bool pause() { return false; }
virtual bool resume() { return false; }
virtual bool isSpeaking() { return false; }
virtual void setVoice(TTSVoice *voice) {}
TTSVoice getVoice() { return *(_ttsState->_activeVoice); }
virtual void setRate(int rate) {}
int getRate() { return _ttsState->_rate; }
virtual void setPitch(int pitch) {}
int getPitch() { return _ttsState->_pitch; }
virtual void setVolume(int volume) {}
int getVolume() { return _ttsState->_volume; }
virtual void setLanguage(String language) {}
String getLanguage() { return _ttsState->_language; }
Array<TTSVoice> getVoicesArray() { return _ttsState->_availaibleVoices; }
protected:
TTSState *_ttsState;
virtual void updateVoices() {};
};
} // End of namespace Common

1
configure vendored
View file

@ -5379,6 +5379,7 @@ else
fi
define_in_config_if_yes $_tts 'USE_TTS'
define_in_config_if_yes $_linux_tts 'USE_LINUX_TTS'
append_var LIBS '-lspeechd'
#
# Check whether to build Bink video support

View file

@ -31,6 +31,8 @@
#include "graphics/surface.h"
#include "gui/object.h"
#include "gui/ThemeEngine.h"
#include "common/text-to-speech.h"
#include "common/system.h"
namespace GUI {
@ -218,7 +220,7 @@ public:
void handleMouseUp(int x, int y, int button, int clickCount);
void handleMouseDown(int x, int y, int button, int clickCount);
void handleMouseEntered(int button) { if (_duringPress) { setFlags(WIDGET_PRESSED); } else { setFlags(WIDGET_HILITED); } markAsDirty(); }
void handleMouseEntered(int button) { g_system->getTextToSpeechManager()->say(_label); if (_duringPress) { setFlags(WIDGET_PRESSED); } else { setFlags(WIDGET_HILITED); } markAsDirty(); }
void handleMouseLeft(int button) { clearFlags(WIDGET_HILITED | WIDGET_PRESSED); markAsDirty(); }
void setHighLighted(bool enable);