svn properties, late as usual
[kugel-rb.git] / rbutil / rbutilqt / base / talkgenerator.cpp
blob93a52a922e9aa7022eaafab4c3c8d60f5f14d4e7
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 "wavtrim.h"
24 TalkGenerator::TalkGenerator(QObject* parent): QObject(parent)
29 //! \brief Creates Talkfiles.
30 //!
31 TalkGenerator::Status TalkGenerator::process(QList<TalkEntry>* list,int wavtrimth)
33 m_abort = false;
34 QString errStr;
35 bool warnings = false;
37 //tts
38 emit logItem(tr("Starting TTS Engine"),LOGINFO);
39 m_tts = TTSBase::getTTS(this,RbSettings::value(RbSettings::Tts).toString());
40 if(!m_tts->start(&errStr))
42 emit logItem(errStr.trimmed(),LOGERROR);
43 emit logItem(tr("Init of TTS engine failed"),LOGERROR);
44 emit done(true);
45 return eERROR;
47 QCoreApplication::processEvents();
49 // Encoder
50 emit logItem(tr("Starting Encoder Engine"),LOGINFO);
51 m_enc = EncBase::getEncoder(this,RbSettings::value(RbSettings::CurEncoder).toString());
52 if(!m_enc->start())
54 emit logItem(tr("Init of Encoder engine failed"),LOGERROR);
55 emit done(true);
56 m_tts->stop();
57 return eERROR;
59 QCoreApplication::processEvents();
61 emit logProgress(0,0);
63 // Voice entries
64 emit logItem(tr("Voicing entries..."),LOGINFO);
65 Status voiceStatus= voiceList(list,wavtrimth);
66 if(voiceStatus == eERROR)
68 m_tts->stop();
69 m_enc->stop();
70 emit done(true);
71 return eERROR;
73 else if( voiceStatus == eWARNING)
74 warnings = true;
76 QCoreApplication::processEvents();
78 // Encoding Entries
79 emit logItem(tr("Encoding files..."),LOGINFO);
80 Status encoderStatus = encodeList(list);
81 if( encoderStatus == eERROR)
83 m_tts->stop();
84 m_enc->stop();
85 emit done(true);
86 return eERROR;
88 else if( voiceStatus == eWARNING)
89 warnings = true;
91 QCoreApplication::processEvents();
93 m_tts->stop();
94 m_enc->stop();
95 emit logProgress(1,1);
97 if(warnings)
98 return eWARNING;
99 return eOK;
102 //! \brief Voices a List of string
104 TalkGenerator::Status TalkGenerator::voiceList(QList<TalkEntry>* list,int wavtrimth)
106 int progressMax = list->size();
107 int m_progress = 0;
108 emit logProgress(m_progress,progressMax);
110 QStringList errors;
111 QStringList dublicates;
113 bool warnings = false;
114 for(int i=0; i < list->size(); i++)
116 if(m_abort)
118 emit logItem(tr("Voicing aborted"), LOGERROR);
119 return eERROR;
122 // skip dublicated wav entrys
123 if(!dublicates.contains(list->at(i).wavfilename))
124 dublicates.append(list->at(i).wavfilename);
125 else
127 qDebug() << "dublicate skipped";
128 (*list)[i].voiced = true;
129 emit logProgress(++m_progress,progressMax);
130 continue;
133 // skip already voiced entrys
134 if(list->at(i).voiced == true)
136 emit logProgress(++m_progress,progressMax);
137 continue;
139 // skip entry whith empty text
140 if(list->at(i).toSpeak == "")
142 emit logProgress(++m_progress,progressMax);
143 continue;
146 // voice entry
147 QString error;
148 qDebug() << "voicing: " << list->at(i).toSpeak << "to" << list->at(i).wavfilename;
149 TTSStatus status = m_tts->voice(list->at(i).toSpeak,list->at(i).wavfilename, &error);
150 if(status == Warning)
152 warnings = true;
153 emit logItem(tr("Voicing of %1 failed: %2").arg(list->at(i).toSpeak).arg(error),
154 LOGWARNING);
156 else if (status == FatalError)
158 emit logItem(tr("Voicing of %1 failed: %2").arg(list->at(i).toSpeak).arg(error),
159 LOGERROR);
160 return eERROR;
162 else
163 (*list)[i].voiced = true;
165 //wavetrim if needed
166 if(wavtrimth != -1)
168 char buffer[255];
169 wavtrim(list->at(i).wavfilename.toLocal8Bit().data(),wavtrimth,buffer,255);
172 emit logProgress(++m_progress,progressMax);
173 QCoreApplication::processEvents();
175 if(warnings)
176 return eWARNING;
177 else
178 return eOK;
182 //! \brief Encodes a List of strings
184 TalkGenerator::Status TalkGenerator::encodeList(QList<TalkEntry>* list)
186 QStringList dublicates;
188 int progressMax = list->size();
189 int m_progress = 0;
190 emit logProgress(m_progress,progressMax);
192 for(int i=0; i < list->size(); i++)
194 if(m_abort)
196 emit logItem(tr("Encoding aborted"), LOGERROR);
197 return eERROR;
200 //skip non-voiced entrys
201 if(list->at(i).voiced == false)
203 qDebug() << "non voiced entry" << list->at(i).toSpeak <<"detected";
204 emit logProgress(++m_progress,progressMax);
205 continue;
207 //skip dublicates
208 if(!dublicates.contains(list->at(i).talkfilename))
209 dublicates.append(list->at(i).talkfilename);
210 else
212 qDebug() << "dublicate skipped";
213 (*list)[i].encoded = true;
214 emit logProgress(++m_progress,progressMax);
215 continue;
218 //encode entry
219 qDebug() << "encoding " << list->at(i).wavfilename << "to" << list->at(i).talkfilename;
220 if(!m_enc->encode(list->at(i).wavfilename,list->at(i).talkfilename))
222 emit logItem(tr("Encoding of %1 failed").arg(list->at(i).wavfilename), LOGERROR);
223 return eERROR;
225 (*list)[i].encoded = true;
226 emit logProgress(++m_progress,progressMax);
227 QCoreApplication::processEvents();
229 return eOK;
232 //! \brief slot, which is connected to the abort of the Logger. Sets a flag, so Creating Talkfiles ends at the next possible position
234 void TalkGenerator::abort()
236 m_abort = true;