summaryrefslogtreecommitdiffstats
path: root/rbutil/rbutilqt/base/ttssapi.cpp
diff options
context:
space:
mode:
authorDominik Wenger <domonoky@googlemail.com>2009-10-13 19:54:27 +0000
committerDominik Wenger <domonoky@googlemail.com>2009-10-13 19:54:27 +0000
commit3215c09462fe90c35dc6d9e1979e970280b052b1 (patch)
tree1fc7be334470d6d21b746a40bc7dbe440a722d35 /rbutil/rbutilqt/base/ttssapi.cpp
parent31c18116de2105deed8aa8dab84bf08a930f2112 (diff)
downloadrockbox-3215c09462fe90c35dc6d9e1979e970280b052b1.tar.gz
rockbox-3215c09462fe90c35dc6d9e1979e970280b052b1.tar.bz2
rockbox-3215c09462fe90c35dc6d9e1979e970280b052b1.zip
rbutil: split tts.cpp/h into individual files.
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@23158 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'rbutil/rbutilqt/base/ttssapi.cpp')
-rw-r--r--rbutil/rbutilqt/base/ttssapi.cpp213
1 files changed, 213 insertions, 0 deletions
diff --git a/rbutil/rbutilqt/base/ttssapi.cpp b/rbutil/rbutilqt/base/ttssapi.cpp
new file mode 100644
index 0000000000..e356d613de
--- /dev/null
+++ b/rbutil/rbutilqt/base/ttssapi.cpp
@@ -0,0 +1,213 @@
+/***************************************************************************
+* __________ __ ___.
+* Open \______ \ ____ ____ | | _\_ |__ _______ ___
+* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
+* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
+* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
+* \/ \/ \/ \/ \/
+*
+* Copyright (C) 2007 by Dominik Wenger
+* $Id$
+*
+* 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"
+
+TTSSapi::TTSSapi(QObject* parent) : TTSBase(parent)
+{
+ m_TTSTemplate = "cscript //nologo \"%exe\" /language:%lang /voice:\"%voice\" /speed:%speed \"%options\"";
+ defaultLanguage ="english";
+ m_sapi4 =false;
+}
+
+void TTSSapi::generateSettings()
+{
+ // language
+ QStringList languages = RbSettings::languages();
+ languages.sort();
+ EncTtsSetting* setting =new EncTtsSetting(this,EncTtsSetting::eSTRINGLIST,
+ tr("Language:"),RbSettings::subValue("sapi",RbSettings::TtsLanguage),languages);
+ connect(setting,SIGNAL(dataChanged()),this,SLOT(updateVoiceList()));
+ insertSetting(eLANGUAGE,setting);
+ // voice
+ setting = new EncTtsSetting(this,EncTtsSetting::eSTRINGLIST,
+ tr("Voice:"),RbSettings::subValue("sapi",RbSettings::TtsVoice),getVoiceList(RbSettings::subValue("sapi",RbSettings::TtsLanguage).toString()),EncTtsSetting::eREFRESHBTN);
+ connect(setting,SIGNAL(refresh()),this,SLOT(updateVoiceList()));
+ insertSetting(eVOICE,setting);
+ //speed
+ insertSetting(eSPEED,new EncTtsSetting(this,EncTtsSetting::eINT,
+ tr("Speed:"),RbSettings::subValue("sapi",RbSettings::TtsSpeed),-10,10));
+ // options
+ insertSetting(eOPTIONS,new EncTtsSetting(this,EncTtsSetting::eSTRING,
+ tr("Options:"),RbSettings::subValue("sapi",RbSettings::TtsOptions)));
+
+}
+
+void TTSSapi::saveSettings()
+{
+ //save settings in user config
+ RbSettings::setSubValue("sapi",RbSettings::TtsLanguage,getSetting(eLANGUAGE)->current().toString());
+ RbSettings::setSubValue("sapi",RbSettings::TtsVoice,getSetting(eVOICE)->current().toString());
+ RbSettings::setSubValue("sapi",RbSettings::TtsSpeed,getSetting(eSPEED)->current().toInt());
+ RbSettings::setSubValue("sapi",RbSettings::TtsOptions,getSetting(eOPTIONS)->current().toString());
+
+ RbSettings::sync();
+}
+
+void TTSSapi::updateVoiceList()
+{
+ qDebug() << "update 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("sapi",RbSettings::TtsOptions).toString();
+ m_TTSLanguage =RbSettings::subValue("sapi",RbSettings::TtsLanguage).toString();
+ m_TTSVoice=RbSettings::subValue("sapi",RbSettings::TtsVoice).toString();
+ m_TTSSpeed=RbSettings::subValue("sapi",RbSettings::TtsSpeed).toString();
+ m_sapi4 = RbSettings::subValue("sapi",RbSettings::TtsUseSapi4).toBool();
+
+ 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);
+
+ if(m_sapi4)
+ execstring.append(" /sapi4 ");
+
+ qDebug() << "init" << execstring;
+ voicescript = new QProcess(NULL);
+ //connect(voicescript,SIGNAL(readyReadStandardError()),this,SLOT(error()));
+
+ voicescript->start(execstring);
+ if(!voicescript->waitForStarted())
+ {
+ *errStr = tr("Could not start the Sapi-script");
+ return false;
+ }
+
+ if(!voicescript->waitForReadyRead(300))
+ {
+ *errStr = voicescript->readAllStandardError();
+ if(*errStr != "")
+ return false;
+ }
+
+ voicestream = new QTextStream(voicescript);
+ voicestream->setCodec("UTF16-LE");
+
+ return true;
+}
+
+
+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 = "cscript //nologo \"%exe\" /language:%lang /listvoices";
+ execstring.replace("%exe",m_TTSexec);
+ execstring.replace("%lang",language);
+
+ if(RbSettings::value(RbSettings::TtsUseSapi4).toBool())
+ execstring.append(" /sapi4 ");
+
+ qDebug() << "init" << execstring;
+ voicescript = new QProcess(NULL);
+ voicescript->start(execstring);
+ qDebug() << "wait for started";
+ if(!voicescript->waitForStarted())
+ return result;
+ voicescript->closeWriteChannel();
+ voicescript->waitForReadyRead();
+
+ QString dataRaw = voicescript->readAllStandardError().data();
+ 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+"\r\n";
+ qDebug() << "voicing" << query;
+ *voicestream << query;
+ *voicestream << "SYNC\tbla\r\n";
+ voicestream->flush();
+ voicescript->waitForReadyRead();
+ 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");
+ return true;
+}
+
+bool TTSSapi::configOk()
+{
+ if(RbSettings::subValue("sapi",RbSettings::TtsVoice).toString().isEmpty())
+ return false;
+ return true;
+}