fix: tts only enable current locale

too many locale tts initialization cost too much time.
This commit is contained in:
YiFang Xiao 2023-06-28 08:28:24 +08:00 committed by YiFang Xiao
parent 113d02c22b
commit 45a71d1fdf
2 changed files with 21 additions and 16 deletions

View file

@ -1,9 +1,11 @@
#include "speechclient.hh"
#include <QtCore>
#include <QLocale>
#include <QDebug>
SpeechClient::SpeechClient( Config::VoiceEngine const & e, QObject * parent ):
QObject( parent ),
internalData( std::make_unique< InternalData >( e ) )
internalData( new InternalData( e ) )
{
}
@ -14,28 +16,30 @@ SpeechClient::Engines SpeechClient::availableEngines()
const auto innerEngines = QTextToSpeech::availableEngines();
for ( const auto & engine_name : innerEngines ) {
const auto sp = new QTextToSpeech( engine_name == "default" ? nullptr : engine_name );
const auto sp = new QTextToSpeech( engine_name );
#if ( QT_VERSION >= QT_VERSION_CHECK( 6, 0, 0 ) )
#if ( QT_VERSION >= QT_VERSION_CHECK( 6, 0, 0 ) )
if ( !sp || sp->state() == QTextToSpeech::Error )
continue;
#else
if ( !sp || sp->state() == QTextToSpeech::BackendError )
continue;
#endif
#else
if ( !sp || sp->state() == QTextToSpeech::BackendError )
continue;
#endif
qDebug() << engine_name << sp->state();
const QVector< QLocale > locales = sp->availableLocales();
for ( const QLocale & locale : locales ) {
// const QVector< QLocale > locales = sp->availableLocales();
// for ( const QLocale & locale : locales )
{
QLocale locale;
//on some platforms ,change the locale will change voices too.
sp->setLocale( locale );
for ( const QVoice & voice : sp->availableVoices() ) {
const QString name( QString( "%4 - %3 %1 (%2)" )
.arg( QLocale::languageToString( locale.language() ),
( QLocale::countryToString( locale.country() ) ),
voice.name(),
engine_name ) );
.arg( QLocale::languageToString( locale.language() ),
( QLocale::countryToString( locale.country() ) ),
voice.name(),
engine_name ) );
Engine engine( Config::VoiceEngine( engine_name, name, voice.name(), QLocale( locale ), 50, 0 ) );
engines.push_back( engine );
}

View file

@ -6,6 +6,7 @@
#include <QTextToSpeech>
#include <memory>
#include <QDebug>
#include <QSharedPointer>
class SpeechClient: public QObject
{
@ -38,7 +39,7 @@ public:
struct InternalData
{
explicit InternalData( Config::VoiceEngine const & e ):
sp( std::make_unique< QTextToSpeech >( e.engine_name ) ),
sp( new QTextToSpeech( e.engine_name ) ),
engine( e )
{
qDebug() << QStringLiteral( "initialize tts" ) << e.engine_name;
@ -63,7 +64,7 @@ public:
sp->setRate( e.rate / 10.0 );
}
std::unique_ptr< QTextToSpeech > sp;
QSharedPointer< QTextToSpeech > sp;
Engine engine;
};
@ -77,7 +78,7 @@ public:
bool tell( QString const & text ) const;
private:
std::unique_ptr< InternalData > internalData;
QSharedPointer< InternalData > internalData;
};
#endif // __SPEECHCLIENT_HH_INCLUDED__