more krazy fixes (include own header first, and missing e-mail addresses
[kdeaccessibility.git] / kttsd / plugins / flite / fliteconf.cpp
blobefb9f0469e7a91bae4ea7dbf8e305ddff3b56505
1 /***************************************************** vim:set ts=4 sw=4 sts=4:
2 Configuration widget and functions for Festival (Interactive) plug in
3 -------------------
4 Copyright:
5 (C) 2004 by Gary Cramblitt <garycramblitt@comcast.net>
6 -------------------
7 Original author: Gary Cramblitt <garycramblitt@comcast.net>
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 ******************************************************************************/
24 // Flite Plugin includes.
25 #include "fliteconf.h"
26 #include "fliteconf.moc"
27 #include "fliteproc.h"
29 // Qt includes.
30 #include <QtCore/QFile>
31 #include <QtGui/QApplication>
32 #include <QtGui/QProgressBar>
34 // KDE includes.
35 #include <klocale.h>
36 #include <kdialog.h>
37 #include <ktemporaryfile.h>
38 #include <kstandarddirs.h>
39 #include <kprogressdialog.h>
41 // KTTS includes.
42 #include <testplayer.h>
44 /** Constructor */
45 FliteConf::FliteConf( QWidget* parent, const QStringList& /*args*/) :
46 PlugInConf(parent, "fliteconf")
48 // kDebug() << "FliteConf::FliteConf: Running";
49 m_fliteProc = 0;
50 m_progressDlg = 0;
52 setupUi(this);
54 defaults();
56 connect(flitePath, SIGNAL(textChanged(const QString&)),
57 this, SLOT(configChanged()));
58 connect(fliteTest, SIGNAL(clicked()), this, SLOT(slotFliteTest_clicked()));
61 /** Destructor */
62 FliteConf::~FliteConf(){
63 // kDebug() << "Running: FliteConf::~FliteConf()";
64 if (!m_waveFile.isNull()) QFile::remove(m_waveFile);
65 delete m_fliteProc;
66 delete m_progressDlg;
69 void FliteConf::load(KConfig *c, const QString &configGroup){
70 // kDebug() << "FliteConf::load: Loading configuration for language " << langGroup << " with plug in " << "Festival Lite (flite)";
72 KConfigGroup config(c, configGroup);
73 QString fliteExe = config.readEntry("FliteExePath", QString());
74 if (fliteExe.isEmpty())
76 KConfigGroup fliteConfig(c, "Flite");
77 fliteExe = fliteConfig.readEntry("FliteExePath", "flite");
79 flitePath->setUrl(KUrl::fromPath(fliteExe));
82 void FliteConf::save(KConfig *c, const QString &configGroup){
83 // kDebug() << "FliteConf::save: Saving configuration for language " << langGroup << " with plug in " << "Festival Lite (flite)";
85 KConfigGroup fliteConfig(c, "Flite");
86 fliteConfig.writeEntry("FliteExePath",
87 realFilePath(flitePath->url().path()));
88 KConfigGroup config(c, configGroup);
89 config.writeEntry("FliteExePath",
90 realFilePath(flitePath->url().path()));
93 void FliteConf::defaults(){
94 // kDebug() << "FliteConf::defaults: Running";
95 flitePath->setUrl(KUrl::fromPath("flite"));
98 void FliteConf::setDesiredLanguage(const QString &lang)
100 m_languageCode = lang;
103 QString FliteConf::getTalkerCode()
105 QString fliteExe = realFilePath(flitePath->url().path());
106 if (!fliteExe.isEmpty())
108 if (!getLocation(fliteExe).isEmpty())
110 return QString(
111 "<voice lang=\"%1\" name=\"%2\" gender=\"%3\" />"
112 "<prosody volume=\"%4\" rate=\"%5\" />"
113 "<kttsd synthesizer=\"%6\" />")
114 .arg(m_languageCode)
115 .arg("fixed")
116 .arg("neutral")
117 .arg("medium")
118 .arg("medium")
119 .arg("Festival Lite (flite)");
122 return QString();
125 void FliteConf::slotFliteTest_clicked()
127 // kDebug() << "FliteConf::slotFliteTest_clicked(): Running";
128 // If currently synthesizing, stop it.
129 if (m_fliteProc)
130 m_fliteProc->stopText();
131 else
133 m_fliteProc = new FliteProc();
134 connect (m_fliteProc, SIGNAL(stopped()), this, SLOT(slotSynthStopped()));
136 // Create a temp file name for the wave file.
137 KTemporaryFile tempFile;
138 tempFile.setPrefix("fliteplugin-");
139 tempFile.setSuffix(".wav");
140 tempFile.setAutoRemove(false);
141 tempFile.open();
142 QString tmpWaveFile = tempFile.fileName();
144 // Get test message in the language of the voice.
145 QString testMsg = testMessage(m_languageCode);
147 // Tell user to wait.
148 m_progressDlg = new KProgressDialog(this,
149 i18n("Testing"),
150 i18n("Testing."));
152 m_progressDlg->setModal(true);
153 m_progressDlg->progressBar()->hide();
154 m_progressDlg->setAllowCancel(true);
156 // Play an English test. Flite only supports English.
157 connect (m_fliteProc, SIGNAL(synthFinished()), this, SLOT(slotSynthFinished()));
158 m_fliteProc->synth(
159 testMsg,
160 tmpWaveFile,
161 realFilePath(flitePath->url().path()));
163 // Display progress dialog modally. Processing continues when plugin signals synthFinished,
164 // or if user clicks Cancel button.
165 m_progressDlg->exec();
166 disconnect (m_fliteProc, SIGNAL(synthFinished()), this, SLOT(slotSynthFinished()));
167 if (m_progressDlg->wasCancelled()) m_fliteProc->stopText();
168 delete m_progressDlg;
169 m_progressDlg = 0;
172 void FliteConf::slotSynthFinished()
174 // If user canceled, progress dialog is gone, so exit.
175 if (!m_progressDlg)
177 m_fliteProc->ackFinished();
178 return;
180 // Hide the Cancel button so user can't cancel in the middle of playback.
181 m_progressDlg->showCancelButton(false);
182 // Get new wavefile name.
183 m_waveFile = m_fliteProc->getFilename();
184 // Tell synth we're done.
185 m_fliteProc->ackFinished();
186 // Play the wave file (possibly adjusting its Speed).
187 // Player object deletes the wave file when done.
188 if (m_player) m_player->play(m_waveFile);
189 QFile::remove(m_waveFile);
190 m_waveFile.clear();
191 if (m_progressDlg) m_progressDlg->close();
194 void FliteConf::slotSynthStopped()
196 // Clean up after canceling test.
197 QString filename = m_fliteProc->getFilename();
198 if (!filename.isNull()) QFile::remove(filename);