summaryrefslogtreecommitdiffstats
path: root/kttsd/plugins/freetts/freettsconf.cpp
blob: 05c1fabd0b33d09318d85363b1fe96f01180ff2a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/****************************************************************************
	Configuration widget and functions for FreeTTS (interactive) plug in
	-------------------
	Copyright : (C) 2004 Paul Giannaros
	-------------------
	Original author: Paul Giannaros <ceruleanblaze@gmail.com>
	Current Maintainer: Paul Giannaros <ceruleanblaze@gmail.com>
 ******************************************************************************/

/***************************************************************************
 *																					*
 *	 This program is free software; you can redistribute it and/or modify	*
 *	 it under the terms of the GNU General Public License as published by	*
 *	 the Free Software Foundation; version 2 of the License.				 *
 *																					 *
 ***************************************************************************/

// Qt includes. 
#include <tqlayout.h>
#include <tqlabel.h>
#include <tqstring.h>
#include <tqstringlist.h>
#include <tqfile.h>
#include <tqapplication.h>

// KDE includes.
#include <kdialog.h>
#include <ktempfile.h>
#include <kstandarddirs.h>
#include <kmessagebox.h>
#include <klocale.h>
#include <kprogress.h>

// KTTS includes.
#include <pluginconf.h>
#include <testplayer.h>

// FreeTTS includes.
#include "freettsconf.h"
#include "freettsconfigwidget.h"

/** Constructor */
FreeTTSConf::FreeTTSConf( TQWidget* parent, const char* name, const TQStringList&/*args*/) : 
	PlugInConf( parent, name ) {
	
	// kdDebug() << "FreeTTSConf::FreeTTSConf: Running" << endl;
	m_freettsProc = 0;
        m_progressDlg = 0;
	
	TQVBoxLayout *layout = new TQVBoxLayout(this, KDialog::marginHint(),
								KDialog::spacingHint(), "FreeTTSConfigWidgetLayout");
	layout->setAlignment (Qt::AlignTop);
	m_widget = new FreeTTSConfWidget(this, "FreeTTSConfigWidget");
	layout->addWidget(m_widget);
		
	defaults();
	
	connect(m_widget->freettsPath, TQT_SIGNAL(textChanged(const TQString&)),
		this, TQT_SLOT(configChanged()));
	connect(m_widget->freettsTest, TQT_SIGNAL(clicked()), this, TQT_SLOT(slotFreeTTSTest_clicked()));
}

/** Destructor */
FreeTTSConf::~FreeTTSConf() {
	// kdDebug() << "Running: FreeTTSConf::~FreeTTSConf()" << endl;
	if (!m_waveFile.isNull()) TQFile::remove(m_waveFile);
	delete m_freettsProc;
        delete m_progressDlg;
}

void FreeTTSConf::load(KConfig *config, const TQString &configGroup) {
	// kdDebug() << "FreeTTSConf::load: Running" << endl;

	config->setGroup(configGroup);
        TQString freeTTSJar = config->readEntry("FreeTTSJarPath", TQString::null);
        if (freeTTSJar.isEmpty())
        {
            config->setGroup("FreeTTS");
            freeTTSJar = config->readEntry("FreeTTSJarPath", TQString::null);
        }
	if (freeTTSJar.isEmpty())
	    freeTTSJar = getLocation("freetts.jar");
        m_widget->freettsPath->setURL(freeTTSJar);
	/// If freettsPath is still empty, then we couldn't find the file in the path.
}

void FreeTTSConf::save(KConfig *config, const TQString &configGroup){
	// kdDebug() << "FreeTTSConf::save: Running" << endl;

    config->setGroup("FreeTTS");
    config->writeEntry("FreeTTSJarPath",
        realFilePath(m_widget->freettsPath->url()));

    config->setGroup(configGroup);
    if(m_widget->freettsPath->url().isEmpty())
        KMessageBox::sorry(0, i18n("Unable to locate freetts.jar in your path.\nPlease specify the path to freetts.jar in the Properties tab before using KDE Text-to-Speech"), i18n("KDE Text-to-Speech"));
    config->writeEntry("FreeTTSJarPath",
        realFilePath(m_widget->freettsPath->url()));
}

void FreeTTSConf::defaults(){
	// kdDebug() << "Running: FreeTTSConf::defaults()" << endl;
	m_widget->freettsPath->setURL("");
}

void FreeTTSConf::setDesiredLanguage(const TQString &lang)
{
    m_languageCode = lang;
}

TQString FreeTTSConf::getTalkerCode()
{
    TQString freeTTSJar = realFilePath(m_widget->freettsPath->url());
    if (!freeTTSJar.isEmpty())
    {
        if (!getLocation(freeTTSJar).isEmpty())
        {
            return TQString(
                    "<voice lang=\"%1\" name=\"%2\" gender=\"%3\" />"
                    "<prosody volume=\"%4\" rate=\"%5\" />"
                    "<kttsd synthesizer=\"%6\" />")
                    .arg(m_languageCode)
                    .arg("fixed")
                    .arg("neutral")
                    .arg("medium")
                    .arg("medium")
                    .arg("FreeTTS");
        }
    }
    return TQString::null;
}

// TQString FreeTTSConf::getLocation(const TQString &name) {
// 	/// Iterate over the path and see if 'name' exists in it. Return the
// 	/// full path to it if it does. Else return an empty TQString.
// 	kdDebug() << "FreeTTSConf::getLocation: Searching for " << name << " in the path... " << endl;
// 	kdDebug() << m_path << endl;
// 	for(TQStringList::iterator it = m_path.begin(); it != m_path.end(); ++it) {
// 		TQString fullName = *it;
// 		fullName += "/";
// 		fullName += name;
// 		/// The user either has the directory of the file in the path...
// 		if(TQFile::exists(fullName)) {
// 			return fullName;
// 			kdDebug() << fullName << endl;
// 		}
// 		/// ....Or the file itself
// 		else if(TQFileInfo(*it).baseName().append(TQString(".").append(TQFileInfo(*it).extension())) == name) {
// 			return fullName;
// 			kdDebug() << fullName << endl;
// 		}
// 	}
// 	return "";
// }


void FreeTTSConf::slotFreeTTSTest_clicked()
{
	// kdDebug() << "FreeTTSConf::slotFreeTTSTest_clicked(): Running" << endl;
        // If currently synthesizing, stop it.
	if (m_freettsProc)
		m_freettsProc->stopText();
	else
        {
		m_freettsProc = new FreeTTSProc();
                connect (m_freettsProc, TQT_SIGNAL(stopped()), this, TQT_SLOT(slotSynthStopped()));
        }
        // Create a temp file name for the wave file.
	KTempFile tempFile (locateLocal("tmp", "freettsplugin-"), ".wav");
	TQString tmpWaveFile = tempFile.file()->name();
	tempFile.close();

    // Get test message in the language of the voice.
    TQString testMsg = testMessage(m_languageCode);

        // Tell user to wait.
        m_progressDlg = new KProgressDialog(m_widget, "kttsmgr_freetts_testdlg",
            i18n("Testing"),
            i18n("Testing."),
            true);
        m_progressDlg->progressBar()->hide();
        m_progressDlg->setAllowCancel(true);

	// I think FreeTTS only officialy supports English, but if anyone knows of someone
	// whos built up a different language lexicon and has it working with FreeTTS gimme an email at ceruleanblaze@gmail.com
        connect (m_freettsProc, TQT_SIGNAL(synthFinished()), this, TQT_SLOT(slotSynthFinished()));
        m_freettsProc->synth(
            testMsg,
            tmpWaveFile,
            realFilePath(m_widget->freettsPath->url()));

        // Display progress dialog modally.  Processing continues when plugin signals synthFinished,
        // or if user clicks Cancel button.
        m_progressDlg->exec();
        disconnect (m_freettsProc, TQT_SIGNAL(synthFinished()), this, TQT_SLOT(slotSynthFinished()));
        if (m_progressDlg->wasCancelled()) m_freettsProc->stopText();
        delete m_progressDlg;
        m_progressDlg = 0;
}

void FreeTTSConf::slotSynthFinished()
{
    // If user canceled, progress dialog is gone, so exit.
    if (!m_progressDlg)
    {
        m_freettsProc->ackFinished();
        return;
    }
    // Hide the Cancel button so user can't cancel in the middle of playback.
    m_progressDlg->showCancelButton(false);
    // Get new wavefile name.
    m_waveFile = m_freettsProc->getFilename();
    // Tell synth we're done.
    m_freettsProc->ackFinished();
    // Play the wave file (possibly adjusting its Speed).
    // Player object deletes the wave file when done.
    if (m_player) m_player->play(m_waveFile);
    TQFile::remove(m_waveFile);
    m_waveFile = TQString::null;
    if (m_progressDlg) m_progressDlg->close();
}

void FreeTTSConf::slotSynthStopped()
{
    // Clean up after canceling test.
    TQString filename = m_freettsProc->getFilename();
    if (!filename.isNull()) TQFile::remove(filename);
}

#include "freettsconf.moc"