-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSpeakReal.cpp
176 lines (158 loc) · 4.64 KB
/
SpeakReal.cpp
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
#include "SpeakReal.h"
#include "pathmanage.h"
#include "myglobal.h"
#include "QTimer"
SpeakReal::SpeakReal(QObject *parent, const QString &bookname) :
QObject(parent)
{
/////////////////////////////////////////////////////////////////
//phonon
mediaObject = new Phonon::MediaObject(this);
audioOutput = new Phonon::AudioOutput(Phonon::MusicCategory, this);
Phonon::createPath(mediaObject, audioOutput);
connect(mediaObject,
SIGNAL(stateChanged(Phonon::State,Phonon::State)),
this,
SLOT(stateChanged(Phonon::State, Phonon::State)));
////////////////////////////////////////////////////////////////
//
currUser = Global::Conf->getCurrentUser();
qDebug() << QString("真人语音所需的当前用户是: %1").arg(currUser);
bookName = bookname;
qDebug() << QString("真人语音所需的当前词库是: %1").arg(bookName);
wordText = "";
soundPath = "";
soundTime = 0;
}
SpeakReal::~SpeakReal()
{
mediaObject->deleteLater();
mediaObject = NULL;
audioOutput->deleteLater();
audioOutput = NULL;
}
void SpeakReal::stateChanged(Phonon::State newState, Phonon::State oldstate)
{
Q_UNUSED(oldstate);
curState = newState;
if(curState == Phonon::PausedState)
{
qDebug() << "状态改变为PausedState,开始启动发音函数realSpeak()";
realSpeak();
}
else if(curState == Phonon::StoppedState)
{
qDebug() << "状态改变为StoppedState,发送结束信号";
emit soundEnded();
}
else if(curState == Phonon::ErrorState)
{
qDebug() << "状态改变为ErrorState,发送发音错误信号";
emit speakError();
}
}
void SpeakReal::realSpeak()
{
qDebug() << QString("realSpeak()函数启动,初始化发音位子和发音源……");
mediaObject->setCurrentSource(Phonon::MediaSource(soundPath));
realSpeakRestart();
time++;
if(time == 1)
{
realSpeakPlay();
}
else if(time > 1 && time <= soundTime)
{
qDebug() << QString("realSpeak()函数判断发音次数是否达到……");
qDebug() << QString("播放次数%1未达到%2,继续播放").arg(time).arg(soundTime);
qDebug() << QString("启动发音函数realSpeakPlay()……");
// QTimer::singleShot(Interval, this, SLOT(realSpeakPlay()));
realSpeakPlay();
}
else
{
qDebug() << QString("播放次数达到,停止播放");
realSpeakStop();
}
}
void SpeakReal::setSoundTime(const int &soundtime)
{
soundTime = soundtime;
}
void SpeakReal::setInterval(const int &interval)
{
Interval = interval;
}
void SpeakReal::realPersonSpeak(const QString &text)
{
qDebug() << QString("启动发音函数真人发音引擎……");
wordText = "";
if(mediaObject->currentTime() == mediaObject->totalTime())
{
mediaObject->seek(0);
}
///////////////////////////////////////////////////////
//
wordText = text;
if(wordText.contains(QRegExp("[\?\\*\\:<>\\|]")))
{
wordText.remove(QRegExp("[\?\\*\\:<>\\|]"));
}
soundPath = PathManage::makePathStr(QString("/userdata/%1/RealSpeak/%2/%3.mp3")
.arg(currUser)
.arg(bookName)
.arg(wordText));
qDebug() << QString("真人语音的音频文件为: %1").arg(soundPath);
time = 0;
qDebug() << "寻找真人发音文件……";
QFile mp3File(soundPath);
if(mp3File.exists())
{
qDebug() << "真人语音在对应词库音频库中存在,将进行播放";
realSpeak();
}
else
{
qDebug() << "真人语音在对应词库音频库中不存在,切换寻找路径为根目录音频库";
soundPath = PathManage::makePathStr(QString("/Sound/%1.mp3")
.arg(wordText));
qDebug() << QString("真人语音的音频文件为: %1").arg(soundPath);
time = 0;
QFile mp3FileSound(soundPath);
if(mp3FileSound.exists())
{
qDebug() << "真人语音根目录音频库中存在,将启动realSpeak()进行播放";
realSpeak();
}
else
{
qDebug() << "真人语音不存在,发送信号请求转换发音方式";
wordText = "";
soundPath = "";
soundTime = 0;
time = 0;
emit UncontionsFile();
}
}
}
void SpeakReal::realSpeakRestart()
{
if(mediaObject->currentTime() == mediaObject->totalTime())
{
mediaObject->seek(0);
}
}
void SpeakReal::realSpeakPlay()
{
qDebug() << "realSpeakPlay():真人语音存在,开始播放真人语音";
mediaObject->play();
}
void SpeakReal::realSpeakStop()
{
qDebug() << "停止真人语音播放";
mediaObject->stop();
}
Phonon::MediaObject *SpeakReal::getMediaObject()
{
return mediaObject;
}