summaryrefslogtreecommitdiffstats
path: root/rbutil/rbutilqt/voicefile.cpp
blob: 4601bcc6985adf94f317ccf8bdbd96a2aac4ab2a (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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 *
 *   Copyright (C) 2007 by Dominik Wenger
 *   $Id: voicefile.h 15932 2007-12-15 13:13:57Z domonoky $
 *
 * 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 "voicefile.h"

#define STATE_INVALID 0
#define STATE_PHRASE 1
#define STATE_VOICE 2


VoiceFileCreator::VoiceFileCreator(QObject* parent) :QObject(parent)
{
    m_wavtrimThreshold=500;
}

void VoiceFileCreator::abort()
{
    m_abort = true;
}

bool VoiceFileCreator::createVoiceFile(ProgressloggerInterface* logger)
{
    m_abort = false;
    m_logger = logger;
    m_logger->addItem(tr("Starting Voicefile generation"),LOGINFO);
    
    // test if tempdir exists
    if(!QDir(QDir::tempPath()+"/rbvoice/").exists())
    {
        QDir(QDir::tempPath()).mkdir("rbvoice");
    }
    
    m_path = QDir::tempPath() + "/rbvoice/";   
    
    // read rockbox-info.txt
    QFile info(m_mountpoint+"/.rockbox/rockbox-info.txt");
    if(!info.open(QIODevice::ReadOnly))
    {
        m_logger->addItem(tr("failed to open rockbox-info.txt"),LOGERROR);
        return false;
    }
    
    QString target, features,version;
    while (!info.atEnd()) {
        QString line = info.readLine();
        
        if(line.contains("Target:"))
        {
            target = line.remove("Target:").trimmed();
        }
        else if(line.contains("Features:"))
        {
            features = line.remove("Features:").trimmed();
        }
        else if(line.contains("Version:"))
        {
            version = line.remove("Version:").trimmed();
            version = version.left(version.indexOf("-")).remove(0,1);
        }        
    }
    info.close();
        
    //prepare download url
    QUrl genlangUrl = settings->genlangUrl() +"?lang=" +m_lang+"&t="+target+"&rev="+version+"&f="+features;
    
    qDebug() << "downloading " << genlangUrl;
    
    //download the correct genlang output   
    QTemporaryFile *downloadFile = new QTemporaryFile(this);
    downloadFile->open();
    filename = downloadFile->fileName();
    downloadFile->close();
    // get the real file.
    getter = new HttpGet(this);
    getter->setProxy(m_proxy);
    getter->setFile(downloadFile);
    getter->getFile(genlangUrl);

    connect(getter, SIGNAL(done(bool)), this, SLOT(downloadDone(bool)));
    connect(getter, SIGNAL(downloadDone(int, bool)), this, SLOT(downloadRequestFinished(int, bool)));
    connect(getter, SIGNAL(dataReadProgress(int, int)), this, SLOT(updateDataReadProgress(int, int)));
    connect(m_logger, SIGNAL(aborted()), getter, SLOT(abort()));
    return true;
 }


void VoiceFileCreator::downloadRequestFinished(int id, bool error)
{
    qDebug() << "Install::downloadRequestFinished" << id << error;
    qDebug() << "error:" << getter->errorString();

    downloadDone(error);
}


void VoiceFileCreator::downloadDone(bool error)
{
    qDebug() << "Voice creator::downloadDone, error:" << error;

    // update progress bar
    int max = m_logger->getProgressMax();
    if(max == 0) {
        max = 100;
        m_logger->setProgressMax(max);
    }
    m_logger->setProgressValue(max);
    if(getter->httpResponse() != 200 && !getter->isCached()) {
        m_logger->addItem(tr("Download error: received HTTP error %1.").arg(getter->httpResponse()),LOGERROR);
        m_logger->abort();
        return;
    }
    if(getter->isCached()) m_logger->addItem(tr("Cached file used."), LOGINFO);
    if(error) {
        m_logger->addItem(tr("Download error: %1").arg(getter->errorString()),LOGERROR);
        m_logger->abort();
        return;
    }
    else m_logger->addItem(tr("Download finished."),LOGOK);
    QApplication::processEvents();
     
     
    m_logger->setProgressMax(0); 
    //open downloaded file
    QFile genlang(filename);
    if(!genlang.open(QIODevice::ReadOnly))
    {
        m_logger->addItem(tr("failed to open downloaded file"),LOGERROR);
        m_logger->abort();
        return;
    }    

    //tts
    m_tts = getTTS(settings->curTTS());  
    m_tts->setCfg(settings);
    
    QString errStr;
    if(!m_tts->start(&errStr))
    {
        m_logger->addItem(errStr,LOGERROR);
        m_logger->addItem(tr("Init of TTS engine failed"),LOGERROR);
        m_logger->abort();
        return;
    }

    // Encoder
    m_enc = getEncoder(settings->curEncoder());  
    m_enc->setCfg(settings);
  
    if(!m_enc->start())
    {
        m_logger->addItem(tr("Init of Encoder engine failed"),LOGERROR);
        m_tts->stop();
        m_logger->abort();
        return;
    }

    QApplication::processEvents();
    connect(m_logger,SIGNAL(aborted()),this,SLOT(abort()));
   
    //read in downloaded file
    QList<QPair<QString,QString> > voicepairs;    
    QTextStream in(&genlang);
    in.setCodec("UTF-8");
    QString id, voice;
    bool idfound = false;
    bool voicefound=false;
    while (!in.atEnd()) 
    {
        QString line = in.readLine();
        if(line.contains("id:"))  //ID found
        {
            id = line.remove("id:").remove('"').trimmed();
            idfound = true;
        }
        else if(line.contains("voice:"))  // voice found
        {
            voice = line.remove("voice:").remove('"').trimmed();
            voicefound=true;
        }
        
        if(idfound && voicefound)
        {
            voicepairs.append(QPair<QString,QString>(id,voice));
            idfound=false;
            voicefound=false;
        }        
    }
    genlang.close();
    
    // check for empty list
    if(voicepairs.size() == 0)
    {
        m_logger->addItem(tr("The downloaded file was empty!"),LOGERROR);    
        m_logger->abort();
        m_tts->stop();
        return;
    }
    
    m_logger->setProgressMax(voicepairs.size());
    m_logger->setProgressValue(0);
  
    // create voice clips
    QStringList mp3files;
    for(int i=0; i< voicepairs.size(); i++)
    {  
        if(m_abort)
        {
            m_logger->addItem("aborted.",LOGERROR);    
            m_logger->abort();
            m_tts->stop();
            return;
        }   
        
        m_logger->setProgressValue(i);
        
        QString wavname = m_path + "/" + voicepairs.at(i).first + ".wav";
        QString toSpeak = voicepairs.at(i).second;
        QString encodedname = m_path + "/" + voicepairs.at(i).first +".mp3";
    
        // todo PAUSE
        if(voicepairs.at(i).first == "VOICE_PAUSE")
        {
            QFile::copy(":/builtin/builtin/VOICE_PAUSE.wav",m_path + "/VOICE_PAUSE.wav");
     
        }
        else
        {    
            if(toSpeak == "") continue;
           
            m_logger->addItem(tr("creating ")+toSpeak,LOGINFO);    
            QApplication::processEvents();       
            m_tts->voice(toSpeak,wavname); // generate wav
        }
        
        // todo strip
        char buffer[255];
        
        wavtrim((char*)qPrintable(wavname),m_wavtrimThreshold,buffer,255);
        
        // encode wav 
        m_enc->encode(wavname,encodedname);
        // remove the wav file 
        QFile::remove(wavname);
        // remember the mp3 file for later removing
        mp3files << encodedname;            
    }
    
    
    //make voicefile    
    FILE* ids2 = fopen(filename.toUtf8(), "r");
    if (ids2 == NULL)
    {
        m_logger->addItem(tr("Error opening downloaded file"),LOGERROR);    
        m_logger->abort();
        return;
    }

    FILE* output = fopen(QString(m_mountpoint + "/.rockbox/langs/" + m_lang + ".voice").toUtf8(), "wb");
    if (output == NULL)
    {
        m_logger->addItem(tr("Error opening output file"),LOGERROR);    
        return;
    }
    
    voicefont(ids2,m_targetid,(char*)(const char*)m_path.toUtf8(), output);
    
    //remove .mp3 files
    for(int i=0;i< mp3files.size(); i++)
    {
        QFile::remove(mp3files.at(i));
    }        
    
    m_logger->setProgressMax(100);
    m_logger->setProgressValue(100);
    m_logger->addItem(tr("successfully created."),LOGOK);    
    m_logger->abort();    
}

void VoiceFileCreator::updateDataReadProgress(int read, int total)
{
    m_logger->setProgressMax(total);
    m_logger->setProgressValue(read);
    //qDebug() << "progress:" << read << "/" << total;

}