simplify my last commit. Thanks to obo for the hint.
[Rockbox.git] / rbutil / talkfile.cpp
blob345b3278028109e858e1044780eebb56829a2c7d
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * Module: rbutil
9 * File: tts.cpp
11 * Copyright (C) 2007 Dominik wenger
13 * All files in this archive are subject to the GNU General Public License.
14 * See the file COPYING in the source tree root for full license agreement.
16 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
17 * KIND, either express or implied.
19 ****************************************************************************/
21 #include "talkfile.h"
23 TalkFileCreator::TalkFileCreator()
25 m_supportedTTS.Add(wxT("espeak"));
26 m_supportedTTSOpts.Add(wxT(""));
28 m_supportedEnc.Add(wxT("lame"));
29 m_supportedEncOpts.Add(wxT("--vbr-new -t --nores -S"));
33 bool TalkFileCreator::initEncoder()
35 if(::wxFileExists(m_EncExec))
37 return true;
39 else
41 return false;
45 bool TalkFileCreator::initTTS()
47 if(::wxFileExists(m_TTSexec))
49 return true;
51 else
53 return false;
57 bool TalkFileCreator::createTalkFiles()
59 if(!initTTS())
61 MESG_DIALOG(wxT("Init of TTS engine failed") );
62 return false;
64 if(!initEncoder())
66 MESG_DIALOG(wxT("Init of encoder failed") );
67 return false;
70 // enumerate the dirs
71 wxDir talkdir(m_dir);
72 TalkTraverser traverser(this);
73 if(talkdir.Traverse(traverser) == (size_t)-1)
74 return false;
75 else
76 return true;
81 bool TalkFileCreator::voice(wxString text,wxString wavfile)
83 if(m_curTTS == wxT("espeak"))
85 wxArrayString out;
86 wxArrayString err;
87 wxExecute(m_TTSexec+wxT(" ")+m_TTSOpts+wxT(" -w \"")+wavfile+wxT("\" \"")+text+wxT("\""),out,err);
88 return true;
90 else
92 MESG_DIALOG(wxT("Unsupported TTS engine") );
93 return false;
97 bool TalkFileCreator::encode(wxString input,wxString output)
99 if(m_curEnc == wxT("lame"))
101 wxArrayString out;
102 wxArrayString err;
103 wxExecute(m_EncExec+wxT(" ")+m_EncOpts+wxT(" \"")+input+wxT("\" \"")+output+wxT("\""),out,err);
104 return true;
106 else
108 MESG_DIALOG(wxT("Unsupported encoder") );
109 return false;
114 wxString TalkFileCreator::getTTsOpts(wxString ttsname)
116 int index = m_supportedTTS.Index(ttsname);
118 return m_supportedTTSOpts[index];
121 wxString TalkFileCreator::getEncOpts(wxString encname)
123 int index = m_supportedEnc.Index(encname);
125 return m_supportedEncOpts[index];
128 wxDirTraverseResult TalkTraverser::OnFile(const wxString& file)
130 if(file.EndsWith(wxT(".talk")) || file.EndsWith(wxT(".talk.wav")))
132 return wxDIR_CONTINUE;
135 wxFileName fname(file);
136 wxString toSpeak;
137 if(m_talkcreator->m_stripExtensions)
139 toSpeak = fname.GetName();
141 else
143 toSpeak = fname.GetName()+fname.GetExt();
145 wxString filename = file+ wxT(".talk");
146 wxString wavname = filename + wxT(".wav");
148 if(!wxFileExists(filename) || m_talkcreator->m_overwriteTalk)
150 if(!wxFileExists(wavname) || m_talkcreator->m_overwriteWav)
152 if(!m_talkcreator->voice(toSpeak,wavname))
154 return wxDIR_STOP;
157 if(!m_talkcreator->encode(wavname,filename))
159 return wxDIR_STOP;
163 if(m_talkcreator->m_removeWav)
165 wxRemoveFile(wavname);
168 return wxDIR_CONTINUE;
171 wxDirTraverseResult TalkTraverser::OnDir(const wxString& dirname)
173 wxFileName fname(dirname,wxEmptyString);
174 wxArrayString dirs=fname.GetDirs();
175 wxString toSpeak = dirs[dirs.GetCount()-1];
177 wxString filename = dirname + wxT("" PATH_SEP "_dirname.talk");
178 wxString wavname = filename + wxT(".wav");
180 if(!wxFileExists(filename) || m_talkcreator->m_overwriteTalk)
182 if(!wxFileExists(wavname) || m_talkcreator->m_overwriteWav)
184 if(!m_talkcreator->voice(toSpeak,wavname))
186 return wxDIR_STOP;
189 if(!m_talkcreator->encode(wavname,filename))
191 return wxDIR_STOP;
195 if(m_talkcreator->m_removeWav)
197 wxRemoveFile(wavname);
200 if(!m_talkcreator->m_recursive)
201 return wxDIR_IGNORE;
202 else
203 return wxDIR_CONTINUE;