summaryrefslogtreecommitdiffstats
path: root/rbutil/rbutilqt/base/ttssapi.cpp
blob: d2d8cf801a95ba8af9106cfe662ce8d005b45dd7 (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
/***************************************************************************
*             __________               __   ___.
*   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
*   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
*   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
*   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
*                     \/            \/     \/    \/            \/
*
*   Copyright (C) 2007 by 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 "ttssapi.h"
#include "utils.h"
#include "rbsettings.h"
#include "systeminfo.h"
#include "Logger.h"

TTSSapi::TTSSapi(QObject* parent) : TTSBase(parent)
{
    m_TTSTemplate = "cscript //nologo \"%exe\" /language:%lang "
        "/voice:\"%voice\" /speed:%speed \"%options\"";
    m_TTSVoiceTemplate = "cscript //nologo \"%exe\" /language:%lang /listvoices";
    m_TTSType = "sapi";
    defaultLanguage = "english";
    m_started = false;
}

TTSBase::Capabilities TTSSapi::capabilities()
{
    return None;
}

void TTSSapi::generateSettings()
{
    // language
    QMap<QString, QStringList> languages = SystemInfo::languages();
    QStringList langs;
    for(int i = 0; i < languages.values().size(); ++i) {
        langs.append(languages.values().at(i).at(0));
    }
    EncTtsSetting* setting = new EncTtsSetting(this,
            EncTtsSetting::eSTRINGLIST, tr("Language:"),
            RbSettings::subValue(m_TTSType, RbSettings::TtsLanguage),
            langs);
    connect(setting,SIGNAL(dataChanged()),this,SLOT(updateVoiceList()));
    insertSetting(eLANGUAGE,setting);
    // voice
    setting = new EncTtsSetting(this,
            EncTtsSetting::eSTRINGLIST, tr("Voice:"),
            RbSettings::subValue(m_TTSType, RbSettings::TtsVoice),
            getVoiceList(RbSettings::subValue(m_TTSType,
                    RbSettings::TtsLanguage).toString()),
            EncTtsSetting::eREFRESHBTN);
    connect(setting,SIGNAL(refresh()),this,SLOT(updateVoiceList()));
    insertSetting(eVOICE,setting);
    //speed
    int speed = RbSettings::subValue(m_TTSType, RbSettings::TtsSpeed).toInt();
    if(speed > 10 || speed < -10)
        speed = 0;
    insertSetting(eSPEED, new EncTtsSetting(this,
                EncTtsSetting::eINT, tr("Speed:"), speed, -10, 10));
    // options
    insertSetting(eOPTIONS, new EncTtsSetting(this,
                EncTtsSetting::eSTRING, tr("Options:"),
                RbSettings::subValue(m_TTSType, RbSettings::TtsOptions)));

}

void TTSSapi::saveSettings()
{
    //save settings in user config
    RbSettings::setSubValue(m_TTSType, RbSettings::TtsLanguage,
            getSetting(eLANGUAGE)->current().toString());
    RbSettings::setSubValue(m_TTSType, RbSettings::TtsVoice,
            getSetting(eVOICE)->current().toString());
    RbSettings::setSubValue(m_TTSType, RbSettings::TtsSpeed,
            getSetting(eSPEED)->current().toInt());
    RbSettings::setSubValue(m_TTSType, RbSettings::TtsOptions,
            getSetting(eOPTIONS)->current().toString());

    RbSettings::sync();
}

void TTSSapi::updateVoiceList()
{
    LOG_INFO() << "updating voicelist";
    QStringList voiceList = getVoiceList(getSetting(eLANGUAGE)->current().toString());
    getSetting(eVOICE)->setList(voiceList);
    if(voiceList.size() > 0) getSetting(eVOICE)->setCurrent(voiceList.at(0));
    else getSetting(eVOICE)->setCurrent("");
}

bool TTSSapi::start(QString *errStr)
{

    m_TTSOpts = RbSettings::subValue(m_TTSType, RbSettings::TtsOptions).toString();
    m_TTSLanguage =RbSettings::subValue(m_TTSType, RbSettings::TtsLanguage).toString();
    m_TTSVoice=RbSettings::subValue(m_TTSType, RbSettings::TtsVoice).toString();
    m_TTSSpeed=RbSettings::subValue(m_TTSType, RbSettings::TtsSpeed).toString();

    QFile::remove(QDir::tempPath() +"/sapi_voice.vbs");
    QFile::copy(":/builtin/sapi_voice.vbs",QDir::tempPath() + "/sapi_voice.vbs");
    m_TTSexec = QDir::tempPath() +"/sapi_voice.vbs";

    QFileInfo tts(m_TTSexec);
    if(!tts.exists())
    {
        *errStr = tr("Could not copy the SAPI script");
        return false;
    }
    // create the voice process
    QString execstring = m_TTSTemplate;
    execstring.replace("%exe",m_TTSexec);
    execstring.replace("%options",m_TTSOpts);
    execstring.replace("%lang",m_TTSLanguage);
    execstring.replace("%voice",m_TTSVoice);
    execstring.replace("%speed",m_TTSSpeed);

    LOG_INFO() << "Start:" << execstring;
    voicescript = new QProcess(NULL);
    //connect(voicescript,SIGNAL(readyReadStandardError()),this,SLOT(error()));
    voicescript->start(execstring);
    LOG_INFO() << "wait for process";
    if(!voicescript->waitForStarted())
    {
        *errStr = tr("Could not start SAPI process");
        LOG_ERROR() << "starting process timed out!";
        return false;
    }

    if(!voicescript->waitForReadyRead(300))
    {
        *errStr = voicescript->readAllStandardError();
        if(*errStr != "")
            return false;
    }

    voicestream = new QTextStream(voicescript);
#if QT_VERSION < 0x060000
    voicestream->setCodec("UTF16-LE");
#else
    voicestream->setEncoding(QStringConverter::Utf16LE);
#endif

    m_started = true;
    return true;
}

QString TTSSapi::voiceVendor(void)
{
    bool keeprunning = m_started;
    QString vendor;
    if(!m_started) {
        QString error;
        start(&error);
    }
    *voicestream << "QUERY\tVENDOR\r\n";
    voicestream->flush();
    while((vendor = voicestream->readLine()).isEmpty())
            QCoreApplication::processEvents();

    LOG_INFO() << "TTS vendor:" << vendor;
    if(!keeprunning) {
        stop();
    }
    return vendor;
}

QStringList TTSSapi::getVoiceList(QString language)
{
    QStringList result;

    QFile::copy(":/builtin/sapi_voice.vbs",QDir::tempPath() + "/sapi_voice.vbs");
    m_TTSexec = QDir::tempPath() +"/sapi_voice.vbs";

    QFileInfo tts(m_TTSexec);
    if(!tts.exists())
        return result;

    // create the voice process
    QString execstring = m_TTSVoiceTemplate;
    execstring.replace("%exe",m_TTSexec);
    execstring.replace("%lang",language);

    LOG_INFO() << "Start:" << execstring;
    voicescript = new QProcess(NULL);
    voicescript->start(execstring);
    LOG_INFO() << "wait for process";
    if(!voicescript->waitForStarted()) {
        LOG_INFO() << "process startup timed out!";
        return result;
    }
    voicescript->closeWriteChannel();
    voicescript->waitForReadyRead();

    QString dataRaw = voicescript->readAllStandardError().data();
    if(dataRaw.startsWith("Error")) {
        LOG_INFO() << "Error:" << dataRaw;
    }
    result = dataRaw.split(";",QString::SkipEmptyParts);
    if(result.size() > 0)
    {
        result.sort();
        result.removeFirst();
        for(int i = 0; i< result.size();i++)
        {
            result[i] = result.at(i).simplified();
        }
    }

    delete voicescript;
    QFile::setPermissions(QDir::tempPath() +"/sapi_voice.vbs",
              QFile::ReadOwner | QFile::WriteOwner | QFile::ExeOwner
            | QFile::ReadUser  | QFile::WriteUser  | QFile::ExeUser
            | QFile::ReadGroup | QFile::WriteGroup | QFile::ExeGroup
            | QFile::ReadOther | QFile::WriteOther | QFile::ExeOther );
    QFile::remove(QDir::tempPath() +"/sapi_voice.vbs");
    return result;
}



TTSStatus TTSSapi::voice(QString text,QString wavfile, QString *errStr)
{
    (void) errStr;
    QString query = "SPEAK\t"+wavfile+"\t"+text;
    LOG_INFO() << "voicing" << query;
    // append newline to query. Done now to keep debug output more readable.
    query.append("\r\n");
    *voicestream << query;
    *voicestream << "SYNC\tbla\r\n";
    voicestream->flush();
    // do NOT poll the output with readLine(), this causes sync issues!
    voicescript->waitForReadyRead();

    if(!QFileInfo(wavfile).isFile()) {
        LOG_ERROR() << "output file does not exist:" << wavfile;
        return FatalError;
    }
    return NoError;
}

bool TTSSapi::stop()
{
    *voicestream << "QUIT\r\n";
    voicestream->flush();
    voicescript->waitForFinished();
    delete voicestream;
    delete voicescript;
    QFile::setPermissions(QDir::tempPath() +"/sapi_voice.vbs",
              QFile::ReadOwner | QFile::WriteOwner | QFile::ExeOwner
            | QFile::ReadUser  | QFile::WriteUser  | QFile::ExeUser
            | QFile::ReadGroup | QFile::WriteGroup | QFile::ExeGroup
            | QFile::ReadOther | QFile::WriteOther | QFile::ExeOther );
    QFile::remove(QDir::tempPath() +"/sapi_voice.vbs");
    m_started = false;
    return true;
}

bool TTSSapi::configOk()
{
    if(RbSettings::subValue(m_TTSType, RbSettings::TtsVoice).toString().isEmpty())
        return false;
    return true;
}