fix: pronounce the first dictionary audio if availabe (#981)

* fix: pronounce the first dictionary audio if availabe

fix #978

* [autofix.ci] apply automated fixes

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
This commit is contained in:
xiaoyifang 2023-07-18 07:57:02 +08:00 committed by GitHub
parent 5f2a1981c0
commit b3d9d79816
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 36 additions and 7 deletions

View file

@ -638,6 +638,10 @@ void ArticleRequest::bodyFinished()
activeDicts[ activeDicts.size() - bodyRequests.size() ]; activeDicts[ activeDicts.size() - bodyRequests.size() ];
string dictId = activeDict->getId(); string dictId = activeDict->getId();
//signal finished dictionray for pronounciation
GlobalBroadcaster::instance()->pronounce_engine.finishDictionary( dictId );
dictIds << QString::fromStdString(dictId); dictIds << QString::fromStdString(dictId);
string head; string head;

View file

@ -10,6 +10,7 @@ std::string addAudioLink( std::string const & url,
if ( url.empty() || url.length() < 2 ) if ( url.empty() || url.length() < 2 )
return {}; return {};
GlobalBroadcaster::instance()->pronounce_engine.sendAudio( GlobalBroadcaster::instance()->pronounce_engine.sendAudio(
dictionaryId,
QString::fromStdString( url.substr( 1, url.length() - 2 ) ) ); QString::fromStdString( url.substr( 1, url.length() - 2 ) ) );
return std::string( "<script type=\"text/javascript\">" + makeAudioLinkScript( url, dictionaryId ) + "</script>" ); return std::string( "<script type=\"text/javascript\">" + makeAudioLinkScript( url, dictionaryId ) + "</script>" );

View file

@ -1,4 +1,5 @@
#include "pronounceengine.hh" #include "pronounceengine.hh"
#include <QMutexLocker>
PronounceEngine::PronounceEngine( QObject * parent ): PronounceEngine::PronounceEngine( QObject * parent ):
QObject{ parent } QObject{ parent }
@ -10,13 +11,34 @@ void PronounceEngine::reset()
{ {
QMutexLocker _( &mutex ); QMutexLocker _( &mutex );
state = PronounceState::AVAILABLE; state = PronounceState::AVAILABLE;
dictAudioMap.clear();
} }
void PronounceEngine::sendAudio( QString audioLink )
void PronounceEngine::sendAudio( std::string dictId, QString audioLink )
{ {
QMutexLocker _( &mutex );
if ( state == PronounceState::OCCUPIED ) if ( state == PronounceState::OCCUPIED )
return; return;
state = PronounceState::OCCUPIED;
emit emitAudio( audioLink ); QMutexLocker _( &mutex );
dictAudioMap.operator[]( dictId ).push_back( audioLink );
}
void PronounceEngine::finishDictionary( std::string dictId )
{
if ( state == PronounceState::OCCUPIED )
return;
if ( dictAudioMap.contains( dictId ) ) {
{
//limit the mutex scope.
QMutexLocker _( &mutex );
if ( state == PronounceState::OCCUPIED )
return;
state = PronounceState::OCCUPIED;
}
emit emitAudio( dictAudioMap[ dictId ].first() );
}
} }

View file

@ -2,7 +2,7 @@
#define PRONOUNCEENGINE_HH #define PRONOUNCEENGINE_HH
#include <QObject> #include <QObject>
#include <QMutexLocker> #include <QMap>
#include <QMutex> #include <QMutex>
@ -17,11 +17,13 @@ class PronounceEngine: public QObject
PronounceState state = PronounceState::AVAILABLE; PronounceState state = PronounceState::AVAILABLE;
QMutex mutex; QMutex mutex;
QMap< std::string, QList< QString > > dictAudioMap;
public: public:
explicit PronounceEngine( QObject * parent = nullptr ); explicit PronounceEngine( QObject * parent = nullptr );
void reset(); void reset();
void sendAudio( QString audioLink ); void sendAudio( std::string dictId, QString audioLink );
void finishDictionary( std::string dictId );
signals: signals:
void emitAudio( QString audioLink ); void emitAudio( QString audioLink );
}; };