6#include "speechtotextmanager.h"
7#include "speechtotextclient.h"
8#include "speechtotextengineloader.h"
9#include "speechtotextplugin.h"
10#include "textspeechtotext_debug.h"
11#include <QAudioSource>
13using namespace TextSpeechToText;
15class SpeechToTextManager::SpeechToTextPluginPrivate
19 TextSpeechToText::SpeechToTextPlugin *mSpeechToTextPlugin =
nullptr;
20 TextSpeechToText::SpeechToTextClient *mSpeechToTextClient =
nullptr;
21 QAudioSource *mAudioSource =
nullptr;
24SpeechToTextManager::SpeechToTextManager(
QObject *parent)
30SpeechToTextManager::~SpeechToTextManager() =
default;
32void SpeechToTextManager::deletePlugin()
34 if (d->mSpeechToTextPlugin) {
36 delete d->mSpeechToTextPlugin;
37 d->mSpeechToTextPlugin =
nullptr;
42void SpeechToTextManager::switchEngine(
const QString &engineName)
44 d->mEngineName = engineName;
46 d->mSpeechToTextClient = TextSpeechToText::SpeechToTextEngineLoader::self()->createSpeechToTextClient(d->mEngineName);
47 if (!d->mSpeechToTextClient) {
48 const QString fallBackEngineName = TextSpeechToText::SpeechToTextEngineLoader::self()->fallbackFirstEngine();
49 if (!fallBackEngineName.
isEmpty()) {
50 d->mSpeechToTextClient = TextSpeechToText::SpeechToTextEngineLoader::self()->createSpeechToTextClient(fallBackEngineName);
52 qCWarning(TEXTSPEECHTOTEXT_LOG) <<
"Fallback engine not found.";
55 if (d->mSpeechToTextClient) {
56 d->mSpeechToTextPlugin = d->mSpeechToTextClient->createTextToSpeech();
57 if (d->mSpeechToTextPlugin->loadSettings()) {
58 connect(d->mSpeechToTextPlugin, &TextSpeechToText::SpeechToTextPlugin::speechToTextDone,
this, &SpeechToTextManager::textToSpeechDone);
60 qCWarning(TEXTSPEECHTOTEXT_LOG) <<
"Impossible to initialize text to speech plugin";
68 static SpeechToTextManager s_self;
72bool SpeechToTextManager::hasEngine()
const
74 return TextSpeechToText::SpeechToTextEngineLoader::self()->hasEngine();
77void SpeechToTextManager::speechToText()
79 d->mSpeechToTextPlugin->speechToText();
82void SpeechToTextManager::initializeInput()
84 if (!d->mSpeechToTextPlugin) {
88 delete d->mAudioSource;
91 format.setSampleRate(d->mSpeechToTextPlugin->sampleRate());
92 format.setChannelCount(1);
93 format.setSampleFormat(QAudioFormat::Int16);
95 d->mAudioSource =
new QAudioSource(format,
this);
96 d->mAudioSource->setBufferSize(8000);
97 d->mAudioSource->start(d->mSpeechToTextPlugin->audioDevice());
101#include "moc_speechtotextmanager.cpp"
The SpeechToTextManager class.
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
bool disconnect(const QMetaObject::Connection &connection)
bool isEmpty() const const