update dict
[QFreeRecite.git] / src / gui / Speaker.cpp
blobecfacec07ce34dc1cacaf777d7470ed348e0636c
1 #include <QProcess>
2 #include <QString>
3 #include <QStringList>
4 #include <QSettings>
5 #include <QFileInfo>
6 #include <Debug.h>
7 #include <ConfigHolder.h>
8 #include "Speaker.h"
10 #ifdef WIN32
11 #include <QApplication>
12 #include "WinSpeaker.h"
13 WinSpeaker winSpeaker;
14 #endif //WIN32
16 using freeRecite::configHolder;
18 namespace Speaker {
20 QSettings settings("QFreeRecite");
22 QString getWordPath(const QString &word) {
23 QString filePath = settings.value("PronouncePath").toString()
24 + word.at(0) + "/" + word + ".wav";
26 if(QFile::exists(filePath)) {
27 return filePath;
30 filePath = settings.value("GlobalPath").toString() + "patchWRPTTS/"
31 + word.at(0) + "/" + word + ".wav";
33 if(QFile::exists(filePath)) {
34 return filePath;
37 filePath = settings.value("HomePath").toString()
38 + "patchWRPTTS/" + word + ".wav";
40 if(QFile::exists(filePath)) {
41 return filePath;
44 #ifdef UNIX
45 static QString oldWord = "";
46 if(oldWord != word) {
47 oldWord = word;
48 if(!word.contains(' ') && !word.contains('-'))
49 QProcess::startDetached(settings.value("GlobalPath").toString() + "gws",
50 QStringList(word));
52 #endif //UNIX
53 return "";
56 void speak(const QString &word) {
57 QString fileName = getWordPath(word.toLower());
58 if(fileName.isEmpty())
59 return ;
60 QFileInfo finfo(fileName);
61 #ifdef WIN32
62 winSpeaker.speak(finfo.absoluteFilePath().toStdString());
63 #else
64 QProcess::startDetached("aplay",
65 QStringList(finfo.absoluteFilePath()));
66 #endif
69 void play(const QString &sound) {
70 if(!settings.value("SoundEffectOpen").toBool())
71 return;
72 std::string fileName = configHolder.musicDir()
73 + sound.toStdString() + ".wav";
74 #ifdef WIN32
75 //If thread can't play it, then paly in a new process!
76 winSpeaker.speak(fileName);
77 #else
78 QProcess::startDetached("aplay",
79 QStringList(fileName.c_str()));
80 #endif
83 } //namespace Speaker