summaryrefslogtreecommitdiffstats
path: root/rbutil/talkfile.cpp
blob: 345b3278028109e858e1044780eebb56829a2c7d (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
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * Module: rbutil
 * File: tts.cpp
 *
 * Copyright (C) 2007 Dominik wenger
 *
 * All files in this archive are subject to the GNU General Public License.
 * See the file COPYING in the source tree root for full license agreement.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/

#include "talkfile.h"

TalkFileCreator::TalkFileCreator()
{
    m_supportedTTS.Add(wxT("espeak"));
    m_supportedTTSOpts.Add(wxT(""));

    m_supportedEnc.Add(wxT("lame"));
    m_supportedEncOpts.Add(wxT("--vbr-new -t --nores -S"));

}

bool TalkFileCreator::initEncoder()
{
    if(::wxFileExists(m_EncExec))
    {
        return true;
    }
    else
    {
        return false;
    }
}

bool TalkFileCreator::initTTS()
{
    if(::wxFileExists(m_TTSexec))
    {
        return true;
    }
    else
    {
        return false;
    }
}

bool TalkFileCreator::createTalkFiles()
{
    if(!initTTS())
    {
        MESG_DIALOG(wxT("Init of TTS engine failed") );
        return false;
    }
    if(!initEncoder())
    {
        MESG_DIALOG(wxT("Init of encoder failed") );
        return false;
    }

    // enumerate the dirs
    wxDir talkdir(m_dir);
    TalkTraverser traverser(this);
    if(talkdir.Traverse(traverser) == (size_t)-1)
        return false;
    else
        return true;


}

bool TalkFileCreator::voice(wxString text,wxString wavfile)
{
    if(m_curTTS == wxT("espeak"))
    {
        wxArrayString out;
        wxArrayString err;
        wxExecute(m_TTSexec+wxT(" ")+m_TTSOpts+wxT(" -w \"")+wavfile+wxT("\" \"")+text+wxT("\""),out,err);
        return true;
    }
    else
    {
        MESG_DIALOG(wxT("Unsupported TTS engine") );
        return false;
    }
}

bool TalkFileCreator::encode(wxString input,wxString output)
{
    if(m_curEnc == wxT("lame"))
    {
        wxArrayString out;
        wxArrayString err;
        wxExecute(m_EncExec+wxT(" ")+m_EncOpts+wxT(" \"")+input+wxT("\" \"")+output+wxT("\""),out,err);
        return true;
    }
    else
    {
        MESG_DIALOG(wxT("Unsupported encoder") );
        return false;
    }

}

wxString TalkFileCreator::getTTsOpts(wxString ttsname)
{
    int index = m_supportedTTS.Index(ttsname);

    return m_supportedTTSOpts[index];
}

wxString TalkFileCreator::getEncOpts(wxString encname)
{
    int index = m_supportedEnc.Index(encname);

    return m_supportedEncOpts[index];
}

wxDirTraverseResult TalkTraverser::OnFile(const wxString& file)
{
    if(file.EndsWith(wxT(".talk")) || file.EndsWith(wxT(".talk.wav")))
    {
        return wxDIR_CONTINUE;
    }

    wxFileName fname(file);
    wxString toSpeak;
    if(m_talkcreator->m_stripExtensions)
    {
        toSpeak = fname.GetName();
    }
    else
    {
        toSpeak = fname.GetName()+fname.GetExt();
    }
    wxString filename = file+ wxT(".talk");
    wxString wavname = filename + wxT(".wav");

    if(!wxFileExists(filename) || m_talkcreator->m_overwriteTalk)
    {
        if(!wxFileExists(wavname) || m_talkcreator->m_overwriteWav)
        {
            if(!m_talkcreator->voice(toSpeak,wavname))
            {
                return wxDIR_STOP;
            }
        }
        if(!m_talkcreator->encode(wavname,filename))
        {
            return wxDIR_STOP;
        }
    }

    if(m_talkcreator->m_removeWav)
    {
        wxRemoveFile(wavname);
    }

    return wxDIR_CONTINUE;
}

wxDirTraverseResult TalkTraverser::OnDir(const wxString& dirname)
{
    wxFileName fname(dirname,wxEmptyString);
    wxArrayString dirs=fname.GetDirs();
    wxString toSpeak = dirs[dirs.GetCount()-1];

    wxString filename = dirname + wxT("" PATH_SEP "_dirname.talk");
    wxString wavname = filename + wxT(".wav");

    if(!wxFileExists(filename) || m_talkcreator->m_overwriteTalk)
    {
        if(!wxFileExists(wavname) || m_talkcreator->m_overwriteWav)
        {
            if(!m_talkcreator->voice(toSpeak,wavname))
            {
                return wxDIR_STOP;
            }
        }
        if(!m_talkcreator->encode(wavname,filename))
        {
            return wxDIR_STOP;
        }
    }

    if(m_talkcreator->m_removeWav)
    {
        wxRemoveFile(wavname);
    }

    if(!m_talkcreator->m_recursive)
        return wxDIR_IGNORE;
    else
        return wxDIR_CONTINUE;
}