direcs  2012-09-30
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
speakThread.cpp
Go to the documentation of this file.
1 /*************************************************************************
2  * Copyright (C) Markus Knapp *
3  * www.direcs.de *
4  * *
5  * This file is part of direcs. *
6  * *
7  * direcs is free software: you can redistribute it and/or modify it *
8  * under the terms of the GNU General Public License as published *
9  * by the Free Software Foundation, version 3 of the License. *
10  * *
11  * direcs is distributed in the hope that it will be useful, *
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14  * GNU General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU General Public License *
17  * along with direcs. If not, see <http://www.gnu.org/licenses/>. *
18  * *
19  *************************************************************************/
20 
21 #include "speakThread.h"
22 
23 
25 {
26  stopped = false;
27  saySomething = false;
28  mPhase = 0;
29 
30 #ifdef Q_OS_LINUX // supported only under linux (no MAC OS, Windoze at the moment)
31  // Synchronous playback
32  // 0 ms length sound buffer for SynthCallBack
33  // default location for espeak-data directory
34  // dont allow espeakEVENT_PHONEME events
36  // set speech rate down to 150 words per minute
37  setRate(150);
38 #endif
39 }
40 
41 
43 {
44 #ifdef Q_OS_LINUX // supported only under linux (no MAC OS, Windoze at the moment)
46 #endif
47  saySomething = false;
48  stopped = false;
49 }
50 
51 
53 {
54 #ifdef Q_OS_LINUX // currently supported only under linux (no MAC OS at the moment)
55  // check if already speaking
56  if (espeak_IsPlaying() == 1)
57  {
58  // shut up
59  espeak_Cancel();
60  }
61 #endif
62 
63  stopped = true;
64 }
65 
66 
68 {
69  //
70  // start "threading"...
71  //
72  while (!stopped)
73  {
74  // let the thread sleep some time
75  // for having more time for the other threads
76  msleep(THREADSLEEPTIME);
77 
78 #ifdef Q_OS_LINUX // supported only under linux (no MAC OS, Windoze at the moment)
79  if (saySomething == true)
80  {
81  saySomething = false;
82 
83  // remove HTML tags from string (needed for reading messages from the GUI log)
85 
86  // speak!
87  espeak_Synth( textToSpeak.toAscii(), textToSpeak.length()+1, 0, POS_CHARACTER, 0, espeakCHARS_AUTO, NULL, NULL );
89 
90  // let other Slots know that we completed the sentence.
91  emit speechCompleted(mPhase);
92  }
93 #endif
94 
95  }
96  stopped = false;
97 }
98 
99 
100 void SpeakThread::speak(QString text, int phase)
101 {
102 #ifdef Q_OS_LINUX // supported only under linux (no MAC OS, Windoze at the moment)
103  // store the text in the class member
104  textToSpeak = text;
105 
106  // store the phase locally
107  mPhase = phase;
108 
109  // enbale the run method to speak :-)
110  saySomething = true;
111 
112  // check if already speaking
113  if (espeak_IsPlaying() == 1)
114  {
115  // shut up
116  espeak_Cancel();
117  }
118 #else
119  Q_UNUSED(text);
120  Q_UNUSED(phase);
121 #endif
122 }
123 
124 
125 void SpeakThread::setLanguage(QString language)
126 {
127 #ifdef Q_OS_LINUX // joystick support only under linux (no MAC OS, Windoze at the moment)
128  espeak_SetVoiceByName(language.toAscii());
129 #else
130  Q_UNUSED(language);
131 #endif
132 }
133 
134 
135 void SpeakThread::setRate(int value)
136 {
137 #ifdef Q_OS_LINUX // joystick support only under linux (no MAC OS, Windoze at the moment)
138  espeak_SetParameter(espeakRATE, value, 0);
139 #else
140  Q_UNUSED(value);
141 #endif
142 }
143 
144 
145 void SpeakThread::setVoice(unsigned char gender,unsigned char age)
146 {
147 #ifdef Q_OS_LINUX // supported only under linux (no MAC OS, Windoze at the moment)
148  espeak_VOICE *voice_spec=espeak_GetCurrentVoice();
149  voice_spec->gender=gender;
150  voice_spec->age = age;
151  espeak_SetVoiceByProperties(voice_spec);
152 #else
153  Q_UNUSED(gender);
154  Q_UNUSED(age);
155 #endif
156 }
157 
158 
159 QString SpeakThread::removeHTML(QString string)
160 {
161  int start= -1;
162 
163 
164  do
165  {
166  // search for the first HTML "<"
167  start = string.indexOf("<");
168 
169  if (start != 1)
170  {
171  string.remove(start, string.indexOf(">")+1 - start);
172  }
173  } while (string.contains(">"));
174  // to the last HTML ">" found
175 
176  return string;
177 }