BibleTime
texttospeech.cpp
Go to the documentation of this file.
1/*********
2*
3* In the name of the Father, and of the Son, and of the Holy Spirit.
4*
5* This file is part of BibleTime's source code, https://bibletime.info/
6*
7* Copyright 1999-2026 by the BibleTime developers.
8* The BibleTime source code is licensed under the GNU General Public License
9* version 2.0.
10*
11**********/
12
13#ifdef BUILD_TEXT_TO_SPEECH
14
15#include "texttospeech.h"
16
17#include "../backend/config/btconfig.h"
18
19
20namespace {
21
22std::unique_ptr<QTextToSpeech> textToSpeech;
23
24} // anonymous namespace
25
26
27namespace BtTextToSpeech {
28
29void speakText(QString const & text) {
30 if (!textToSpeech)
31 textToSpeech = newInstance();
32
33 textToSpeech->say(text);
34}
35
36std::unique_ptr<QTextToSpeech> newInstance() {
37 std::unique_ptr<QTextToSpeech> tts;
38
39 auto const configuredEngine =
40 btConfig().value<QString>(QStringLiteral("GUI/ttsEngine"));
41 if (QTextToSpeech::availableEngines().contains(configuredEngine)) {
42 tts = std::make_unique<QTextToSpeech>(configuredEngine);
43 } else {
44 tts = std::make_unique<QTextToSpeech>();
45 }
46
47 auto const configuredLocale =
48 btConfig().value<QLocale>(QStringLiteral("GUI/ttsLocale"));
49 if (tts->availableLocales().contains(configuredLocale))
50 tts->setLocale(configuredLocale);
51
52 auto const configuredVoice =
53 btConfig().value<QString>(QStringLiteral("GUI/ttsVoice"));
54 for (auto const & voice : tts->availableVoices()) {
55 if (voice.name() == configuredVoice) {
56 tts->setVoice(voice);
57 break;
58 }
59 }
60
61 return tts;
62}
63
64void reset() { textToSpeech.reset(); }
65
66} // namespace BtTextToSpeech
67
68#endif
BtConfig & btConfig()
This is a shortchand for BtConfig::getInstance().
Definition btconfig.h:305
T value(QString const &key, T const &defaultValue=T()) const
Returns the settings value for the given global key.