summaryrefslogtreecommitdiffstats
path: root/rbutil/rbutilqt/base/ttsexes.cpp
blob: 446725968f390a7319e8366439b18174fa103f6d (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
/***************************************************************************
*             __________               __   ___.
*   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 <QtCore>
#include "ttsexes.h"
#include "utils.h"
#include "rbsettings.h"
#include "Logger.h"

TTSExes::TTSExes(QObject* parent) : TTSBase(parent)
{
    /* default to espeak */
    m_name = "espeak";
    m_capabilities = TTSBase::CanSpeak;
    m_TTSTemplate = "\"%exe\" %options -w \"%wavfile\" -- \"%text\"";
    m_TTSSpeakTemplate = "\"%exe\" %options -- \"%text\"";
}


TTSBase::Capabilities TTSExes::capabilities()
{
    return m_capabilities;
}

void TTSExes::generateSettings()
{
    loadSettings();
    insertSetting(eEXEPATH, new EncTtsSetting(this, EncTtsSetting::eSTRING,
        tr("Path to TTS engine:"), m_TTSexec, EncTtsSetting::eBROWSEBTN));
    insertSetting(eOPTIONS, new EncTtsSetting(this, EncTtsSetting::eSTRING,
        tr("TTS engine options:"), m_TTSOpts));
}

void TTSExes::saveSettings()
{
    RbSettings::setSubValue(m_name, RbSettings::TtsPath,
            getSetting(eEXEPATH)->current().toString());
    RbSettings::setSubValue(m_name, RbSettings::TtsOptions,
            getSetting(eOPTIONS)->current().toString());
    RbSettings::sync();
}


void TTSExes::loadSettings(void)
{
    m_TTSexec = RbSettings::subValue(m_name, RbSettings::TtsPath).toString();
    if(m_TTSexec.isEmpty()) m_TTSexec = Utils::findExecutable(m_name);
    m_TTSOpts = RbSettings::subValue(m_name, RbSettings::TtsOptions).toString();
}


bool TTSExes::start(QString *errStr)
{
    loadSettings();

    QFileInfo tts(m_TTSexec);
    if(tts.exists())
    {
        return true;
    }
    else
    {
        *errStr = tr("TTS executable not found");
        return false;
    }
}

TTSStatus TTSExes::voice(QString text, QString wavfile, QString *errStr)
{
    (void) errStr;
    QString execstring;
    if(wavfile.isEmpty() && m_capabilities & TTSBase::CanSpeak) {
        if(m_TTSSpeakTemplate.isEmpty()) {
            LOG_ERROR() << "internal error: TTS announces CanSpeak "
                           "but template empty!";
            return FatalError;
        }
        execstring = m_TTSSpeakTemplate;
    }
    else if(wavfile.isEmpty()) {
        LOG_ERROR() << "no output file passed to voice() "
                       "but TTS can't speak directly.";
        return FatalError;
    }
    else {
        execstring = m_TTSTemplate;
    }

    execstring.replace("%exe",m_TTSexec);
    execstring.replace("%options",m_TTSOpts);
    execstring.replace("%wavfile",wavfile);
    execstring.replace("%text",text);

    QProcess::execute(execstring);

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

}

bool TTSExes::configOk()
{
    loadSettings();
    if (QFileInfo::exists(m_TTSexec))
        return true;
    else
        return false;
}