2024-03-20 03:08:17 +00:00
|
|
|
#pragma once
|
|
|
|
#ifndef NO_TTS_SUPPORT
|
2013-04-24 14:52:04 +00:00
|
|
|
|
2024-03-20 03:08:17 +00:00
|
|
|
#include <QObject>
|
|
|
|
#include "config.hh"
|
|
|
|
#include <QTextToSpeech>
|
|
|
|
#include <memory>
|
|
|
|
#include <QDebug>
|
|
|
|
#include <QSharedPointer>
|
2013-04-24 14:52:04 +00:00
|
|
|
|
|
|
|
class SpeechClient: public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
struct Engine
|
|
|
|
{
|
2022-07-31 01:19:50 +00:00
|
|
|
//engine name
|
|
|
|
QString engine_name;
|
2013-04-24 14:52:04 +00:00
|
|
|
QString name;
|
2022-07-31 01:19:50 +00:00
|
|
|
//voice name
|
|
|
|
QString voice_name;
|
|
|
|
QString locale;
|
|
|
|
// Volume vary from 0~1 and rate vary from -1 to 1
|
2013-04-26 13:41:39 +00:00
|
|
|
int volume;
|
|
|
|
int rate;
|
2022-07-31 01:19:50 +00:00
|
|
|
explicit Engine( Config::VoiceEngine const & e ):
|
|
|
|
engine_name( e.engine_name ),
|
|
|
|
name( e.name ),
|
|
|
|
voice_name( e.voice_name ),
|
|
|
|
locale( e.locale.name() ),
|
|
|
|
volume( e.volume ),
|
|
|
|
rate( e.rate )
|
|
|
|
{
|
|
|
|
}
|
2013-04-24 14:52:04 +00:00
|
|
|
};
|
|
|
|
|
2022-07-31 01:19:50 +00:00
|
|
|
struct InternalData
|
|
|
|
{
|
|
|
|
explicit InternalData( Config::VoiceEngine const & e ):
|
2023-06-28 00:28:24 +00:00
|
|
|
sp( new QTextToSpeech( e.engine_name ) ),
|
2022-07-31 01:19:50 +00:00
|
|
|
engine( e )
|
|
|
|
{
|
2023-06-22 08:30:41 +00:00
|
|
|
qDebug() << QStringLiteral( "initialize tts" ) << e.engine_name;
|
2023-06-23 01:26:06 +00:00
|
|
|
#if ( QT_VERSION >= QT_VERSION_CHECK( 6, 0, 0 ) )
|
|
|
|
if ( !sp || sp->state() == QTextToSpeech::Error )
|
|
|
|
return;
|
|
|
|
#else
|
|
|
|
if ( !sp || sp->state() == QTextToSpeech::BackendError )
|
|
|
|
return;
|
|
|
|
#endif
|
2022-07-31 01:19:50 +00:00
|
|
|
sp->setLocale( e.locale );
|
|
|
|
auto voices = sp->availableVoices();
|
2023-06-23 01:26:06 +00:00
|
|
|
for ( const auto & voice : voices ) {
|
|
|
|
if ( voice.name() == e.voice_name ) {
|
2022-07-31 01:19:50 +00:00
|
|
|
sp->setVoice( voice );
|
2013-04-24 14:52:04 +00:00
|
|
|
|
2022-07-31 01:19:50 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2013-04-24 14:52:04 +00:00
|
|
|
|
2022-07-31 01:19:50 +00:00
|
|
|
sp->setVolume( e.volume / 100.0 );
|
|
|
|
sp->setRate( e.rate / 10.0 );
|
|
|
|
}
|
2013-04-24 14:52:04 +00:00
|
|
|
|
2023-06-28 00:28:24 +00:00
|
|
|
QSharedPointer< QTextToSpeech > sp;
|
2022-07-31 01:19:50 +00:00
|
|
|
Engine engine;
|
|
|
|
};
|
2013-04-26 13:41:39 +00:00
|
|
|
|
2022-07-31 01:19:50 +00:00
|
|
|
using Engines = QList< Engine >;
|
2013-04-24 14:52:04 +00:00
|
|
|
|
2022-07-31 01:19:50 +00:00
|
|
|
explicit SpeechClient( Config::VoiceEngine const & e, QObject * parent = nullptr );
|
2013-04-24 14:52:04 +00:00
|
|
|
|
2022-07-31 01:19:50 +00:00
|
|
|
static Engines availableEngines();
|
|
|
|
|
2023-05-02 00:24:51 +00:00
|
|
|
bool tell( QString const & text, int volume, int rate ) const;
|
|
|
|
bool tell( QString const & text ) const;
|
2013-04-24 14:52:04 +00:00
|
|
|
|
2022-07-31 01:19:50 +00:00
|
|
|
private:
|
2023-06-28 00:28:24 +00:00
|
|
|
QSharedPointer< InternalData > internalData;
|
2013-04-24 14:52:04 +00:00
|
|
|
};
|
|
|
|
|
2024-03-20 03:08:17 +00:00
|
|
|
#endif
|