KTextAddons

speechtotextmanager.cpp
1/*
2 SPDX-FileCopyrightText: 2023-2024 Laurent Montel <montel.org>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6#include "speechtotextmanager.h"
7#include "speechtotextclient.h"
8#include "speechtotextengineloader.h"
9#include "speechtotextplugin.h"
10#include "textspeechtotext_debug.h"
11#include <QAudioSource>
12
13using namespace TextSpeechToText;
14
15class SpeechToTextManager::SpeechToTextPluginPrivate
16{
17public:
18 QString mEngineName;
19 TextSpeechToText::SpeechToTextPlugin *mSpeechToTextPlugin = nullptr;
20 TextSpeechToText::SpeechToTextClient *mSpeechToTextClient = nullptr;
21 QAudioSource *mAudioSource = nullptr;
22};
23
24SpeechToTextManager::SpeechToTextManager(QObject *parent)
25 : QObject{parent}
26 , d(new SpeechToTextManager::SpeechToTextPluginPrivate)
27{
28}
29
30SpeechToTextManager::~SpeechToTextManager() = default;
31
32void SpeechToTextManager::deletePlugin()
33{
34 if (d->mSpeechToTextPlugin) {
35 disconnect(d->mSpeechToTextPlugin);
36 delete d->mSpeechToTextPlugin;
37 d->mSpeechToTextPlugin = nullptr;
38 }
39 // TODO delete mSpeechToTextClient ?
40}
41
42void SpeechToTextManager::switchEngine(const QString &engineName)
43{
44 d->mEngineName = engineName;
45 deletePlugin();
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);
51 } else {
52 qCWarning(TEXTSPEECHTOTEXT_LOG) << "Fallback engine not found.";
53 }
54 }
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);
59 } else {
60 qCWarning(TEXTSPEECHTOTEXT_LOG) << "Impossible to initialize text to speech plugin";
61 deletePlugin();
62 }
63 }
64}
65
66SpeechToTextManager *SpeechToTextManager::self()
67{
68 static SpeechToTextManager s_self;
69 return &s_self;
70}
71
72bool SpeechToTextManager::hasEngine() const
73{
74 return TextSpeechToText::SpeechToTextEngineLoader::self()->hasEngine();
75}
76
77void SpeechToTextManager::speechToText()
78{
79 d->mSpeechToTextPlugin->speechToText();
80}
81
82void SpeechToTextManager::initializeInput()
83{
84 if (!d->mSpeechToTextPlugin) {
85 return;
86 }
87
88 delete d->mAudioSource;
89
90 QAudioFormat format;
91 format.setSampleRate(d->mSpeechToTextPlugin->sampleRate());
92 format.setChannelCount(1);
93 format.setSampleFormat(QAudioFormat::Int16);
94
95 d->mAudioSource = new QAudioSource(format, this);
96 d->mAudioSource->setBufferSize(8000);
97 d->mAudioSource->start(d->mSpeechToTextPlugin->audioDevice());
98 // TODO verify mic status!
99}
100
101#include "moc_speechtotextmanager.cpp"
The SpeechToTextClient class.
The SpeechToTextManager class.
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
bool disconnect(const QMetaObject::Connection &connection)
bool isEmpty() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 4 2024 16:29:59 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.