This repository was archived by the owner on Jul 3, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRapidTextToSpeech.cpp
More file actions
277 lines (211 loc) · 6.52 KB
/
Copy pathRapidTextToSpeech.cpp
File metadata and controls
277 lines (211 loc) · 6.52 KB
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
274
275
276
277
/*
RapidTextToSpeech.cpp
Copyright (C) 2015 Shun ITO <movingentity@gmail.com>
This file is part of RapidTextToSpeech.
RapidTextToSpeech is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
RapidTextToSpeech is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with RapidTextToSpeech. If not, see <http://www.gnu.org/licenses/>.
*/
#include "RapidTextToSpeech.h"
#include <cstdio>
#include <iostream>
#include <string>
#include <time.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/stat.h>
using namespace std;
pthread_mutex_t RapidTextToSpeech::mtx = PTHREAD_MUTEX_INITIALIZER;
RapidTextToSpeech::RapidTextToSpeech(string dicDir, string voiceDir)
{
m_pCurrentTTS = new TextToSpeech(sampling_rate, fperiod, alpha, stage,
beta, audio_buff_size, uv_threshold,
gv_weight_mgc, gv_weight_lf0, gv_weight_lpf,
use_log_gain, voiceDir, dicDir);
m_pReserveTTS = new TextToSpeech(sampling_rate, fperiod, alpha, stage,
beta, audio_buff_size, uv_threshold,
gv_weight_mgc, gv_weight_lf0, gv_weight_lpf,
use_log_gain, voiceDir, dicDir);
}
RapidTextToSpeech::~RapidTextToSpeech()
{
delete m_pCurrentTTS;
delete m_pReserveTTS;
}
//- Operation -------------------------------------------------------------------------//
int RapidTextToSpeech::PauseSpeech()
{
return m_pCurrentTTS->PauseSpeech();
}
int RapidTextToSpeech::ResumeSpeech()
{
return m_pCurrentTTS->ResumeSpeech();
}
int RapidTextToSpeech::Speech(const string& text)
{
int r;
// 処理が終わるまで連続で次の呼び出しの処理を行わないようにする。
r = pthread_mutex_lock(&mtx);
if (r !=0) {
cerr << "ERROR: RapidTextToSpeech::Speech() pthread_mutex_lock" << endl;
return -1;
}
// 現在再生中のスピーチを停止
r = m_pCurrentTTS->StopSpeech();
if (r !=0) {
cerr << "ERROR: RapidTextToSpeech::Speech() StopSpeech()" << endl;
return -1;
}
// 2番手のTextToSpeechを使って読み上げ開始
r = m_pReserveTTS->Speech(text);
if (r !=0) {
cerr << "ERROR: RapidTextToSpeech::Speech() Speech()" << endl;
return -1;
}
// 新しく読み上げを開始したTextoToSpeechを、現在のTextToSpeechに設定。
// 読み上げが終わったTextToSpeechは、2番手に設定。次の読み上げを待つ。
TextToSpeech *tmp = m_pCurrentTTS;
m_pCurrentTTS = m_pReserveTTS;
m_pReserveTTS = tmp;
// アンロック
r = pthread_mutex_unlock(&mtx);
if (r !=0) {
cout << "ERROR: RapidTextToSpeech::Speech() pthread_mutex_unlock" << endl;
m_pCurrentTTS->StopSpeech();
return -1;
}
return 0;
}
int RapidTextToSpeech::StopSpeech()
{
return m_pCurrentTTS->StopSpeech();
}
//- CallBack --------------------------------------------------------------------------//
void RapidTextToSpeech::SetSpeechEndCallBack(void (*pFunc)(void*), void *pUserData)
{
m_pCurrentTTS->SetSpeechEndCallBack(pFunc, pUserData);
m_pReserveTTS->SetSpeechEndCallBack(pFunc, pUserData);
}
void RapidTextToSpeech::SetSpeechPauseCallBack(void (*pFunc)(void*), void *pUserData)
{
m_pCurrentTTS->SetSpeechPauseCallBack(pFunc, pUserData);
m_pReserveTTS->SetSpeechPauseCallBack(pFunc, pUserData);
}
void RapidTextToSpeech::SetSpeechResumeCallBack(void (*pFunc)(void*), void *pUserData)
{
m_pCurrentTTS->SetSpeechResumeCallBack(pFunc, pUserData);
m_pReserveTTS->SetSpeechResumeCallBack(pFunc, pUserData);
}
void RapidTextToSpeech::SetSpeechStartCallBack(void (*pFunc)(void*), void *pUserData)
{
m_pCurrentTTS->SetSpeechStartCallBack(pFunc, pUserData);
m_pReserveTTS->SetSpeechStartCallBack(pFunc, pUserData);
}
void RapidTextToSpeech::SpeechEndCallBack(void *pUserData)
{
// beep x2
cout << "\007" << "\007" << flush;
}
void RapidTextToSpeech::SpeechPauseCallBack(void *pUserData)
{
// beep
cout << "\007" << flush;
}
void RapidTextToSpeech::SpeechResumeCallBack(void *pUserData)
{
// beep
cout << "\007" << flush;
}
void RapidTextToSpeech::SpeechStartCallBack(void *pUserData)
{
// beep
cout << "\007" << flush;
}
int main(int argc, char *argv[])
{
int opt;
char *pStr;
string dicDir;
string voiceDir;
// 引数解析
while ((opt = getopt(argc, argv, "d:v:")) != -1) {
switch (opt) {
case 'd':
{
// 辞書データ
pStr = optarg;
if (pStr != NULL) {
struct stat sb;
if (stat(pStr, &sb) == -1) {
cout << "Not found such a directory. (" << pStr << ")" << endl;
exit(-1);
}
dicDir = pStr;
} else {
cout << "Unknow option argument" << endl;
exit(-1);
}
break;
}
case 'v':
{
// ボイスデータ
pStr = optarg;
if (pStr != NULL) {
struct stat sb;
if (stat(pStr, &sb) == -1) {
cout << "Not found such a directory. (" << pStr << ")" << endl;
exit(-1);
}
voiceDir = pStr;
} else {
cout << "Unknow option argument" << endl;
exit(-1);
}
break;
}
case ':': ; // 引数が足りない
case '?': ; // 不明なオプション
default: exit(-1);
}
}
if (dicDir.empty()) {
cout << "エラー 辞書データのあるディレクトリが見つかりません。-d オプションで指定してください。" << endl;
exit(-1);
}
if (voiceDir.empty()) {
cout << "エラー ボイスデータのあるディレクトリが見つかりません。-v オプションで指定してください。" << endl;
exit(-1);
}
RapidTextToSpeech *pRtts = new RapidTextToSpeech(dicDir, voiceDir);
pRtts->SetSpeechEndCallBack(pRtts->SpeechEndCallBack, NULL);
pRtts->SetSpeechPauseCallBack(pRtts->SpeechPauseCallBack, NULL);
pRtts->SetSpeechResumeCallBack(pRtts->SpeechResumeCallBack, NULL);
pRtts->SetSpeechStartCallBack(pRtts->SpeechStartCallBack, NULL);
string in = "";
while (1) {
cout << ">" << flush;
cin >> in;
if (in == "exit") {
break;
} else if (in == "pause") {
pRtts->PauseSpeech();
} else if (in == "resume") {
pRtts->ResumeSpeech();
} else if (in == "stop") {
pRtts->StopSpeech();
} else {
int r = pRtts->Speech(in);
if (r != 0 ) { cerr << "ERROR. Speech()" << endl; break; }
}
}
delete pRtts;
return 0;
}