summaryrefslogtreecommitdiffstats
path: root/kttsd/plugins/freetts/freettsproc.cpp
blob: e78a75ce8ad255666f641e199910dead7d864f01 (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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/****************************************************************************
	Main speaking functions for the FreeTTS 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.				 *
 *																					 *
 ***************************************************************************/

#include <tqstring.h>
#include <tqstringlist.h>
#include <tqfileinfo.h>

#include <kdebug.h>
#include <kconfig.h>
#include <kstandarddirs.h>
#include <kprocess.h>

#include "freettsproc.h" 

/** Constructor */
FreeTTSProc::FreeTTSProc( TQObject* parent, const char* name, const TQStringList& /*args*/) : 
	PlugInProc( parent, name ) {
	kdDebug() << "Running: FreeTTSProc::FreeTTSProc" << endl;
	m_state = psIdle;
	m_waitingStop = false;
	m_freettsProc = 0;
}

/** Desctructor */
FreeTTSProc::~FreeTTSProc() {
	kdDebug() << "Running: FreeTTSProc::~FreeTTSProc" << endl;
	if (m_freettsProc) {
		stopText();
		delete m_freettsProc;
	}
}

/** Initializate the speech */
bool FreeTTSProc::init(TDEConfig *config, const TQString &configGroup) {
	kdDebug() << "Running: FreeTTSProc::init()" << endl;
	kdDebug() << "Initializing plug in: FreeTTS" << endl;
	config->setGroup(configGroup);
	m_freettsJarPath = config->readEntry("FreeTTSJarPath", "freetts.jar");
	kdDebug() << "FreeTTSProc::init: path to freetts.jar: " << m_freettsJarPath << endl;
	return true;
}

/** 
 * Say a text.  Synthesize and audibilize it.
 * @param text                    The text to be spoken.
 *
 * If the plugin supports asynchronous operation, it should return immediately.
 */
void FreeTTSProc::sayText(const TQString &text) {
	synth(text, TQString(), m_freettsJarPath);
}

/**
 * Synthesize text into an audio file, but do not send to the audio device.
 * @param text                    The text to be synthesized.
 * @param suggestedFilename       Full pathname of file to create.  The plugin
 *                                may ignore this parameter and choose its own
 *                                filename.  KTTSD will query the generated
 *                                filename using getFilename().
 *
 * If the plugin supports asynchronous operation, it should return immediately.
 */
void FreeTTSProc::synthText(const TQString& text, const TQString& suggestedFilename) {
	kdDebug() << "Running: FreeTTSProc::synthText" << endl;
	synth(text, suggestedFilename, m_freettsJarPath);
}

// A little helper function because KDE 3.2 kdDebug() does not support TQValueList<TQCString>.
TQStringList argsToTQStringList(const TQValueList<TQCString> list)
{
    TQStringList newList;
    TQValueList<TQCString>::ConstIterator it = list.begin();
    for ( ; it != list.end(); ++it ) newList.append(*it);
    return newList;
}

void FreeTTSProc::synth(
	const TQString &text,
	const TQString &synthFilename,
	const TQString& freettsJarPath) {
    
	kdDebug() << "Running: FreeTTSProc::synth" << endl;

	if (m_freettsProc) {
		if (m_freettsProc->isRunning()) m_freettsProc->kill();
		delete m_freettsProc;
		m_freettsProc = 0;
        
	}
	
	m_freettsProc = new TDEProcess;
	connect(m_freettsProc, TQT_SIGNAL(processExited(TDEProcess*)),
		   this, TQT_SLOT(slotProcessExited(TDEProcess*)));
	connect(m_freettsProc, TQT_SIGNAL(receivedStdout(TDEProcess*, char*, int)),
		   this, TQT_SLOT(slotReceivedStdout(TDEProcess*, char*, int)));
	connect(m_freettsProc, TQT_SIGNAL(receivedStderr(TDEProcess*, char*, int)),
		   this, TQT_SLOT(slotReceivedStderr(TDEProcess*, char*, int)));
	connect(m_freettsProc, TQT_SIGNAL(wroteStdin(TDEProcess*)),
		   this, TQT_SLOT(slotWroteStdin(TDEProcess* )));
	if (synthFilename.isNull())
		m_state = psSaying;
	else
		m_state = psSynthing;

	
	TQString saidText = text;
	saidText += "\n";
	
	/// As freetts.jar doesn't seem to like being called from an absolute path, 
	/// we need to strip off the path to freetts.jar and pass it to 
	/// TDEProcess::setWorkingDirectory()
	/// We could just strip off 11 characters from the end of the path to freetts.jar, but thats
	/// not exactly very portable...
	TQString filename = TQFileInfo(freettsJarPath).baseName().append(TQString(".").append(TQFileInfo(freettsJarPath).extension()));
	TQString freettsJarDir = freettsJarPath.left((freettsJarPath.length() - filename.length()) - 1);

	m_freettsProc->setWorkingDirectory(freettsJarDir);
	kdDebug() << "FreeTTSProc::synthText: moved to directory '" << freettsJarDir << "'" << endl;
	kdDebug() << "FreeTTSProc::synthText: Running file: '" << filename << "'" << endl;
	*m_freettsProc << "java" << "-jar" << filename;

	/// Dump audio into synthFilename

	if (!synthFilename.isNull()) *m_freettsProc << "-dumpAudio" << synthFilename;
	
	m_synthFilename = synthFilename;
	
	kdDebug() << "FreeTTSProc::synth: Synthing text: '" << saidText << "' using FreeTTS plug in" << endl;
	if (!m_freettsProc->start(TDEProcess::NotifyOnExit, TDEProcess::All)) {
		kdDebug() << "FreeTTSProc::synth: Error starting FreeTTS process.  Is freetts.jar in the PATH?" << endl;
		m_state = psIdle;
		kdDebug() << "TDEProcess args: " << argsToTQStringList(m_freettsProc->args()) << endl;
		return;
	}
	kdDebug()<< "FreeTTSProc:synth: FreeTTS initialized" << endl;
	m_freettsProc->writeStdin(saidText.latin1(), saidText.length());
}

/**
 * Returning the filename of the synth'd text
 * @returns                    The filename of the last synth'd text
 */
TQString FreeTTSProc::getFilename() {
	kdDebug() << "FreeTTSProc::getFilename: returning " << m_synthFilename << endl;
	return m_synthFilename;
}

/**
 * Stop current operation (saying or synthesizing text).
 * Important: This function may be called from a thread different from the
 * one that called sayText or synthText.
 * If the plugin cannot stop an in-progress @ref sayText or
 * @ref synthText operation, it must not block waiting for it to complete.
 * Instead, return immediately.
 *
 * If a plugin returns before the operation has actually been stopped,
 * the plugin must emit the @ref stopped signal when the operation has
 * actually stopped.
 *
 * The plugin should change to the psIdle state after stopping the
 * operation.
 */
void FreeTTSProc::stopText() {
	kdDebug() << "FreeTTSProc::stopText:: Running" << endl;
	if (m_freettsProc) {
		if (m_freettsProc->isRunning()) {
			kdDebug() << "FreeTTSProc::stopText: killing FreeTTS." << endl;
			m_waitingStop = true;
			m_freettsProc->kill();
		} 
		else m_state = psIdle;
	}
	else m_state = psIdle;
	kdDebug() << "FreeTTSProc::stopText: FreeTTS stopped." << endl;
}

void FreeTTSProc::slotProcessExited(TDEProcess*) {
	kdDebug() << "FreeTTSProc:slotProcessExited: FreeTTS process has exited." << endl;
	pluginState prevState = m_state;
	if (m_waitingStop) {
		m_waitingStop = false;
		m_state = psIdle;
		emit stopped();
	} 
	else {
		m_state = psFinished;
		if (prevState == psSaying)
			emit sayFinished();
		else if (prevState == psSynthing)
			emit synthFinished();
	}
}

void FreeTTSProc::slotReceivedStdout(TDEProcess*, char* buffer, int buflen) {
	TQString buf = TQString::fromLatin1(buffer, buflen);
	kdDebug() << "FreeTTSProc::slotReceivedStdout: Received output from FreeTTS: " << buf << endl;
}

void FreeTTSProc::slotReceivedStderr(TDEProcess*, char* buffer, int buflen) {
	TQString buf = TQString::fromLatin1(buffer, buflen);
	kdDebug() << "FreeTTSProc::slotReceivedStderr: Received error from FreeTTS: " << buf << endl;
}

void FreeTTSProc::slotWroteStdin(TDEProcess*) {
	kdDebug() << "FreeTTSProc::slotWroteStdin: closing Stdin" << endl;
	m_freettsProc->closeStdin();
}

/**
 * Return the current state of the plugin.
 * This function only makes sense in asynchronous mode.
 * @return                        The pluginState of the plugin.
 *
 * @see pluginState
 */
pluginState FreeTTSProc::getState() { 
	return m_state;
}

/**
 * Acknowledges a finished state and resets the plugin state to psIdle.
 *
 * If the plugin is not in state psFinished, nothing happens.
 * The plugin may use this call to do any post-processing cleanup,
 * for example, blanking the stored filename (but do not delete the file).
 * Calling program should call getFilename prior to ackFinished.
 */
void FreeTTSProc::ackFinished() {
	if (m_state == psFinished) {
		m_state = psIdle;
		m_synthFilename = TQString();
	}
}

/**
 * Returns True if the plugin supports asynchronous processing,
 * i.e., returns immediately from sayText or synthText.
 * @return                        True if this plugin supports asynchronous processing.
 *
 * If the plugin returns True, it must also implement @ref getState .
 * It must also emit @ref sayFinished or @ref synthFinished signals when
 * saying or synthesis is completed.
 */
bool FreeTTSProc::supportsAsync() { 
// 	return true; 
	return true;
}

/**
 * Returns True if the plugIn supports synthText method,
 * i.e., is able to synthesize text to a sound file without
 * audibilizing the text.
 * @return                        True if this plugin supports synthText method.
 */
bool FreeTTSProc::supportsSynth() { 
// 	return true; 
	return true;
}


#include "freettsproc.moc"