Fix wpsbuild to properly generate "-" for theme related settings (to get the default...
[kugel-rb.git] / rbutil / rbutilqt / base / talkgenerator.cpp
blob5c0f8e985bf0f4ad5a40ad475310b09ea7aa704d
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
9 * Copyright (C) 2007 by Dominik Wenger
10 * $Id$
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
20 #include "talkgenerator.h"
21 #include "rbsettings.h"
22 #include "systeminfo.h"
23 #include "wavtrim.h"
25 TalkGenerator::TalkGenerator(QObject* parent): QObject(parent)
30 //! \brief Creates Talkfiles.
31 //!
32 TalkGenerator::Status TalkGenerator::process(QList<TalkEntry>* list,int wavtrimth)
34 m_abort = false;
35 QString errStr;
36 bool warnings = false;
38 //tts
39 emit logItem(tr("Starting TTS Engine"),LOGINFO);
40 m_tts = TTSBase::getTTS(this,RbSettings::value(RbSettings::Tts).toString());
41 if(!m_tts->start(&errStr))
43 emit logItem(errStr.trimmed(),LOGERROR);
44 emit logItem(tr("Init of TTS engine failed"),LOGERROR);
45 emit done(true);
46 return eERROR;
48 QCoreApplication::processEvents();
50 // Encoder
51 emit logItem(tr("Starting Encoder Engine"),LOGINFO);
52 m_enc = EncBase::getEncoder(this,SystemInfo::value(SystemInfo::CurEncoder).toString());
53 if(!m_enc->start())
55 emit logItem(tr("Init of Encoder engine failed"),LOGERROR);
56 emit done(true);
57 m_tts->stop();
58 return eERROR;
60 QCoreApplication::processEvents();
62 emit logProgress(0,0);
64 // Voice entries
65 emit logItem(tr("Voicing entries..."),LOGINFO);
66 Status voiceStatus= voiceList(list,wavtrimth);
67 if(voiceStatus == eERROR)
69 m_tts->stop();
70 m_enc->stop();
71 emit done(true);
72 return eERROR;
74 else if( voiceStatus == eWARNING)
75 warnings = true;
77 QCoreApplication::processEvents();
79 // Encoding Entries
80 emit logItem(tr("Encoding files..."),LOGINFO);
81 Status encoderStatus = encodeList(list);
82 if( encoderStatus == eERROR)
84 m_tts->stop();
85 m_enc->stop();
86 emit done(true);
87 return eERROR;
89 else if( voiceStatus == eWARNING)
90 warnings = true;
92 QCoreApplication::processEvents();
94 m_tts->stop();
95 m_enc->stop();
96 emit logProgress(1,1);
98 if(warnings)
99 return eWARNING;
100 return eOK;
103 //! \brief Voices a List of string
105 TalkGenerator::Status TalkGenerator::voiceList(QList<TalkEntry>* list,int wavtrimth)
107 int progressMax = list->size();
108 int m_progress = 0;
109 emit logProgress(m_progress,progressMax);
111 QStringList errors;
112 QStringList dublicates;
114 bool warnings = false;
115 for(int i=0; i < list->size(); i++)
117 if(m_abort)
119 emit logItem(tr("Voicing aborted"), LOGERROR);
120 return eERROR;
123 // skip dublicated wav entrys
124 if(!dublicates.contains(list->at(i).wavfilename))
125 dublicates.append(list->at(i).wavfilename);
126 else
128 qDebug() << "dublicate skipped";
129 (*list)[i].voiced = true;
130 emit logProgress(++m_progress,progressMax);
131 continue;
134 // skip already voiced entrys
135 if(list->at(i).voiced == true)
137 emit logProgress(++m_progress,progressMax);
138 continue;
140 // skip entry whith empty text
141 if(list->at(i).toSpeak == "")
143 emit logProgress(++m_progress,progressMax);
144 continue;
147 // voice entry
148 QString error;
149 qDebug() << "voicing: " << list->at(i).toSpeak << "to" << list->at(i).wavfilename;
150 TTSStatus status = m_tts->voice(list->at(i).toSpeak,list->at(i).wavfilename, &error);
151 if(status == Warning)
153 warnings = true;
154 emit logItem(tr("Voicing of %1 failed: %2").arg(list->at(i).toSpeak).arg(error),
155 LOGWARNING);
157 else if (status == FatalError)
159 emit logItem(tr("Voicing of %1 failed: %2").arg(list->at(i).toSpeak).arg(error),
160 LOGERROR);
161 return eERROR;
163 else
164 (*list)[i].voiced = true;
166 //wavetrim if needed
167 if(wavtrimth != -1)
169 char buffer[255];
170 wavtrim(list->at(i).wavfilename.toLocal8Bit().data(),wavtrimth,buffer,255);
173 emit logProgress(++m_progress,progressMax);
174 QCoreApplication::processEvents();
176 if(warnings)
177 return eWARNING;
178 else
179 return eOK;
183 //! \brief Encodes a List of strings
185 TalkGenerator::Status TalkGenerator::encodeList(QList<TalkEntry>* list)
187 QStringList dublicates;
189 int progressMax = list->size();
190 int m_progress = 0;
191 emit logProgress(m_progress,progressMax);
193 for(int i=0; i < list->size(); i++)
195 if(m_abort)
197 emit logItem(tr("Encoding aborted"), LOGERROR);
198 return eERROR;
201 //skip non-voiced entrys
202 if(list->at(i).voiced == false)
204 qDebug() << "non voiced entry" << list->at(i).toSpeak <<"detected";
205 emit logProgress(++m_progress,progressMax);
206 continue;
208 //skip dublicates
209 if(!dublicates.contains(list->at(i).talkfilename))
210 dublicates.append(list->at(i).talkfilename);
211 else
213 qDebug() << "dublicate skipped";
214 (*list)[i].encoded = true;
215 emit logProgress(++m_progress,progressMax);
216 continue;
219 //encode entry
220 qDebug() << "encoding " << list->at(i).wavfilename << "to" << list->at(i).talkfilename;
221 if(!m_enc->encode(list->at(i).wavfilename,list->at(i).talkfilename))
223 emit logItem(tr("Encoding of %1 failed").arg(list->at(i).wavfilename), LOGERROR);
224 return eERROR;
226 (*list)[i].encoded = true;
227 emit logProgress(++m_progress,progressMax);
228 QCoreApplication::processEvents();
230 return eOK;
233 //! \brief slot, which is connected to the abort of the Logger. Sets a flag, so Creating Talkfiles ends at the next possible position
235 void TalkGenerator::abort()
237 m_abort = true;