From b3d9d798168ae98e0d2337b45e48c2a89930848a Mon Sep 17 00:00:00 2001
From: xiaoyifang <105986+xiaoyifang@users.noreply.github.com>
Date: Tue, 18 Jul 2023 07:57:02 +0800
Subject: [PATCH] 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>
---
src/article_maker.cc | 4 ++++
src/audiolink.cc | 1 +
src/pronounceengine.cc | 30 ++++++++++++++++++++++++++----
src/pronounceengine.hh | 8 +++++---
4 files changed, 36 insertions(+), 7 deletions(-)
diff --git a/src/article_maker.cc b/src/article_maker.cc
index 4d1eb497..ee60b9a1 100644
--- a/src/article_maker.cc
+++ b/src/article_maker.cc
@@ -638,6 +638,10 @@ void ArticleRequest::bodyFinished()
activeDicts[ activeDicts.size() - bodyRequests.size() ];
string dictId = activeDict->getId();
+
+ //signal finished dictionray for pronounciation
+ GlobalBroadcaster::instance()->pronounce_engine.finishDictionary( dictId );
+
dictIds << QString::fromStdString(dictId);
string head;
diff --git a/src/audiolink.cc b/src/audiolink.cc
index 0d1b0339..cdbcc83f 100644
--- a/src/audiolink.cc
+++ b/src/audiolink.cc
@@ -10,6 +10,7 @@ std::string addAudioLink( std::string const & url,
if ( url.empty() || url.length() < 2 )
return {};
GlobalBroadcaster::instance()->pronounce_engine.sendAudio(
+ dictionaryId,
QString::fromStdString( url.substr( 1, url.length() - 2 ) ) );
return std::string( "" );
diff --git a/src/pronounceengine.cc b/src/pronounceengine.cc
index e8a2c241..789a584a 100644
--- a/src/pronounceengine.cc
+++ b/src/pronounceengine.cc
@@ -1,4 +1,5 @@
#include "pronounceengine.hh"
+#include
PronounceEngine::PronounceEngine( QObject * parent ):
QObject{ parent }
@@ -10,13 +11,34 @@ void PronounceEngine::reset()
{
QMutexLocker _( &mutex );
state = PronounceState::AVAILABLE;
+
+ dictAudioMap.clear();
}
-void PronounceEngine::sendAudio( QString audioLink )
+
+void PronounceEngine::sendAudio( std::string dictId, QString audioLink )
{
- QMutexLocker _( &mutex );
if ( state == PronounceState::OCCUPIED )
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() );
+ }
}
diff --git a/src/pronounceengine.hh b/src/pronounceengine.hh
index 0db5b699..4a807469 100644
--- a/src/pronounceengine.hh
+++ b/src/pronounceengine.hh
@@ -2,7 +2,7 @@
#define PRONOUNCEENGINE_HH
#include
-#include
+#include
#include
@@ -17,11 +17,13 @@ class PronounceEngine: public QObject
PronounceState state = PronounceState::AVAILABLE;
QMutex mutex;
+ QMap< std::string, QList< QString > > dictAudioMap;
+
public:
explicit PronounceEngine( QObject * parent = nullptr );
void reset();
- void sendAudio( QString audioLink );
-
+ void sendAudio( std::string dictId, QString audioLink );
+ void finishDictionary( std::string dictId );
signals:
void emitAudio( QString audioLink );
};