File indexing completed on 2023-05-30 10:39:36

0001 /***************************************************************************
0002  *   Copyright (C) 2002 by Gunnar Schmi Dt <kmouth@schmi-dt.de             *
0003  *             (C) 2015, 2022 by Jeremy Whiting <jpwhiting@kde.org>        *
0004  *                                                                         *
0005  *   This program is free software; you can redistribute it and/or modify  *
0006  *   it under the terms of the GNU General Public License as published by  *
0007  *   the Free Software Foundation; either version 2 of the License, or     *
0008  *   (at your option) any later version.                                   *
0009  *                                                                         *
0010  *   This program is distributed in the hope that it will be useful,       *
0011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0013  *   GNU General Public License for more details.                          *
0014  *                                                                         *
0015  *   You should have received a copy of the GNU General Public License     *
0016  *   along with this program; if not, write to the                         *
0017  *   Free Software Foundation, Inc.,                                       *
0018  *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.          *
0019  ***************************************************************************/
0020 
0021 #include "texttospeechsystem.h"
0022 
0023 #include <QTextCodec>
0024 #include <QTextToSpeech>
0025 
0026 #include <KSharedConfig>
0027 #include <KConfigGroup>
0028 
0029 #include "speech.h"
0030 
0031 TextToSpeechSystem::TextToSpeechSystem(QObject *parent)
0032     : QObject(parent),
0033       codec(Speech::Local),
0034       stdIn(true),
0035       useQtSpeech(true),
0036       ttsEngine(QLatin1String("speechd")),
0037       m_speech(new QTextToSpeech(ttsEngine))
0038 {
0039     buildCodecList();
0040 }
0041 
0042 TextToSpeechSystem::~TextToSpeechSystem()
0043 {
0044     delete codecList;
0045     delete m_speech;
0046 }
0047 
0048 void TextToSpeechSystem::speak(const QString &text, const QString &language)
0049 {
0050     if (!text.isEmpty()) {
0051         if (useQtSpeech) {
0052             m_speech->say(text);
0053             return;
0054         }
0055 
0056         if (codec < Speech::UseCodec)
0057             (new Speech())->speak(ttsCommand, stdIn, text, language, codec, nullptr);
0058         else
0059             (new Speech())->speak(ttsCommand, stdIn, text, language, Speech::UseCodec,
0060                                   codecList->at(codec - Speech::UseCodec));
0061     }
0062 }
0063 
0064 void TextToSpeechSystem::readOptions(const QString &langGroup)
0065 {
0066     KConfigGroup cg(KSharedConfig::openConfig(), langGroup);
0067     ttsCommand = cg.readPathEntry("Command", QString());
0068     stdIn = cg.readEntry("StdIn", true);
0069     useQtSpeech = cg.readEntry("useQtSpeech", true);
0070     ttsEngine = cg.readEntry("ttsEngine", "speechd");
0071     // No default, depends on current locale, etc. so just naturally
0072     // select first voice if none set by user.
0073     ttsVoice = cg.readEntry("ttsVoice", "");
0074 
0075     QString codecString = cg.readEntry("Codec", "Local");
0076     if (codecString == QLatin1String("Local"))
0077         codec = Speech::Local;
0078     else if (codecString == QLatin1String("Latin1"))
0079         codec = Speech::Latin1;
0080     else if (codecString == QLatin1String("Unicode"))
0081         codec = Speech::Unicode;
0082     else {
0083         codec = Speech::Local;
0084         for (int i = 0; i < codecList->count(); i++)
0085             if (codecString == QLatin1String(codecList->at(i)->name()))
0086                 codec = Speech::UseCodec + i;
0087     }
0088 }
0089 
0090 void TextToSpeechSystem::saveOptions(const QString &langGroup)
0091 {
0092     KConfigGroup cg(KSharedConfig::openConfig(), langGroup);
0093     cg.writePathEntry("Command", ttsCommand);
0094     cg.writeEntry("StdIn", stdIn);
0095     cg.writeEntry("useQtSpeech", useQtSpeech);
0096     cg.writeEntry("ttsEngine", ttsEngine);
0097     cg.writeEntry("ttsVoice", ttsVoice);
0098     if (codec == Speech::Local)
0099         cg.writeEntry("Codec", "Local");
0100     else if (codec == Speech::Latin1)
0101         cg.writeEntry("Codec", "Latin1");
0102     else if (codec == Speech::Unicode)
0103         cg.writeEntry("Codec", "Unicode");
0104     else {
0105         QString codeName = QLatin1String(codecList->at(codec - Speech::UseCodec)->name());
0106         cg.writeEntry("Codec", codeName);
0107     }
0108 
0109     delete m_speech;
0110     m_speech = new QTextToSpeech(ttsEngine);
0111     const QVector<QVoice> voices = m_speech->availableVoices();
0112     for (const QVoice &voice: voices) {
0113         if (voice.name() == ttsVoice) {
0114             m_speech->setVoice(voice);
0115         }
0116     }
0117 }
0118 
0119 void TextToSpeechSystem::buildCodecList()
0120 {
0121     codecList = new QList<QTextCodec*>;
0122     QList<QByteArray> availableCodecs = QTextCodec::availableCodecs();
0123     for (int i = 0; i < availableCodecs.count(); ++i) {
0124         QTextCodec *codec = QTextCodec::codecForName(availableCodecs[i]);
0125         codecList->append(codec);
0126     }
0127 }
0128