|
26 | 26 | #include <sys/time.h>
|
27 | 27 | #include <sys/types.h>
|
28 | 28 | #include <unistd.h>
|
| 29 | + |
29 | 30 | #include <cstdlib>
|
30 | 31 | #include <ctime>
|
31 | 32 | #include <fstream>
|
@@ -236,6 +237,22 @@ std::string timestamp_str() {
|
236 | 237 | return tmp;
|
237 | 238 | }
|
238 | 239 |
|
| 240 | +bool isNotEmptyAndNotSpace(const char* str) { |
| 241 | + if (str == NULL) { |
| 242 | + return false; |
| 243 | + } |
| 244 | + size_t length = strlen(str); |
| 245 | + if (length == 0) { |
| 246 | + return false; |
| 247 | + } |
| 248 | + for (size_t i = 0; i < length; ++i) { |
| 249 | + if (!std::isspace(static_cast<unsigned char>(str[i]))) { |
| 250 | + return true; |
| 251 | + } |
| 252 | + } |
| 253 | + return false; |
| 254 | +} |
| 255 | + |
239 | 256 | static void vectorSetParams(unsigned long pid, bool add,
|
240 | 257 | struct ParamStatistics params) {
|
241 | 258 | pthread_mutex_lock(¶ms_mtx);
|
@@ -1095,6 +1112,8 @@ void* pthreadFunc(void* arg) {
|
1095 | 1112 | // start()调用超时,cancel()取消当次请求。
|
1096 | 1113 | request->cancel();
|
1097 | 1114 | run_cancel++;
|
| 1115 | + AlibabaNls::NlsClient::getInstance() |
| 1116 | + ->releaseFlowingSynthesizerRequest(request); |
1098 | 1117 | break;
|
1099 | 1118 | }
|
1100 | 1119 | pthread_mutex_unlock(&(cbParam.mtxWord));
|
@@ -1134,30 +1153,33 @@ void* pthreadFunc(void* arg) {
|
1134 | 1153 | std::cout << "total text: " << text_str << std::endl;
|
1135 | 1154 | for (std::vector<std::string>::const_iterator it = sentences.begin();
|
1136 | 1155 | it != sentences.end(); ++it) {
|
1137 |
| - std::cout << "sendText: " << *it << std::endl; |
1138 |
| - ret = request->sendText(it->c_str()); |
1139 |
| - if (ret < 0) { |
1140 |
| - break; |
1141 |
| - } |
1142 |
| - if (sendFlushFlag) { |
1143 |
| - if (enableLongSilence) { |
1144 |
| - request->sendFlush("{\"enable_long_silence\":true}"); |
1145 |
| - } else { |
1146 |
| - request->sendFlush(); |
| 1156 | + if (isNotEmptyAndNotSpace(it->c_str())) { |
| 1157 | + std::cout << "sendText: " << *it << std::endl; |
| 1158 | + ret = request->sendText(it->c_str()); |
| 1159 | + if (ret < 0) { |
| 1160 | + break; |
| 1161 | + } |
| 1162 | + if (sendFlushFlag) { |
| 1163 | + if (enableLongSilence) { |
| 1164 | + request->sendFlush("{\"enable_long_silence\":true}"); |
| 1165 | + } else { |
| 1166 | + request->sendFlush(); |
| 1167 | + } |
1147 | 1168 | }
|
| 1169 | + usleep(500 * 1000); |
1148 | 1170 | }
|
1149 |
| - usleep(500 * 1000); |
1150 | 1171 |
|
1151 | 1172 | if (!global_run) break;
|
1152 | 1173 | } // for
|
1153 | 1174 | if (ret < 0) {
|
1154 |
| - std::cout << "sendText failed. pid:" << pthread_self() << std::endl; |
| 1175 | + std::cout << "sendText failed. pid:" << pthread_self() |
| 1176 | + << ". ret: " << ret << std::endl; |
1155 | 1177 | const char* request_info = request->dumpAllInfo();
|
1156 | 1178 | if (request_info) {
|
1157 | 1179 | std::cout << " all info: " << request_info << std::endl;
|
1158 | 1180 | }
|
1159 | 1181 | AlibabaNls::NlsClient::getInstance()->releaseFlowingSynthesizerRequest(
|
1160 |
| - request); // start()失败,释放request对象 |
| 1182 | + request); |
1161 | 1183 | break;
|
1162 | 1184 | }
|
1163 | 1185 | }
|
@@ -1200,8 +1222,10 @@ void* pthreadFunc(void* arg) {
|
1200 | 1222 | pthread_mutex_lock(&(cbParam.mtxWord));
|
1201 | 1223 | if (ETIMEDOUT == pthread_cond_timedwait(&(cbParam.cvWord),
|
1202 | 1224 | &(cbParam.mtxWord), &outtime)) {
|
1203 |
| - std::cout << "stop timeout" << std::endl; |
| 1225 | + std::cout << "stop timeout, release this request" << std::endl; |
1204 | 1226 | pthread_mutex_unlock(&(cbParam.mtxWord));
|
| 1227 | + AlibabaNls::NlsClient::getInstance() |
| 1228 | + ->releaseFlowingSynthesizerRequest(request); |
1205 | 1229 | break;
|
1206 | 1230 | }
|
1207 | 1231 | pthread_mutex_unlock(&(cbParam.mtxWord));
|
@@ -1283,7 +1307,7 @@ void* pthreadLongConnectionFunc(void* arg) {
|
1283 | 1307 | * 1. 创建流式文本语音合成FlowingSynthesizerRequest对象.
|
1284 | 1308 | *
|
1285 | 1309 | * 流式文本语音合成文档详见:
|
1286 |
| - * https://help.aliyun.com/zh/isi/developer-reference/streaming-text-to-speech-synthesis/?spm=a2c4g.11186623.0.0.638b1f016dQylG |
| 1310 | + * https://help.aliyun.com/zh/isi/developer-reference/streaming-text-to-speech-synthesis |
1287 | 1311 | */
|
1288 | 1312 | AlibabaNls::FlowingSynthesizerRequest* request =
|
1289 | 1313 | AlibabaNls::NlsClient::getInstance()->createFlowingSynthesizerRequest(
|
@@ -1475,19 +1499,21 @@ void* pthreadLongConnectionFunc(void* arg) {
|
1475 | 1499 | std::cout << "total text: " << text_str << std::endl;
|
1476 | 1500 | for (std::vector<std::string>::const_iterator it = sentences.begin();
|
1477 | 1501 | it != sentences.end(); ++it) {
|
1478 |
| - std::cout << "sendText: " << *it << std::endl; |
1479 |
| - ret = request->sendText(it->c_str()); |
1480 |
| - if (ret < 0) { |
1481 |
| - break; |
1482 |
| - } |
1483 |
| - if (sendFlushFlag) { |
1484 |
| - if (enableLongSilence) { |
1485 |
| - request->sendFlush("{\"enable_long_silence\":true}"); |
1486 |
| - } else { |
1487 |
| - request->sendFlush(); |
| 1502 | + if (isNotEmptyAndNotSpace(it->c_str())) { |
| 1503 | + std::cout << "sendText: " << *it << std::endl; |
| 1504 | + ret = request->sendText(it->c_str()); |
| 1505 | + if (ret < 0) { |
| 1506 | + break; |
| 1507 | + } |
| 1508 | + if (sendFlushFlag) { |
| 1509 | + if (enableLongSilence) { |
| 1510 | + request->sendFlush("{\"enable_long_silence\":true}"); |
| 1511 | + } else { |
| 1512 | + request->sendFlush(); |
| 1513 | + } |
1488 | 1514 | }
|
| 1515 | + usleep(500 * 1000); |
1489 | 1516 | }
|
1490 |
| - usleep(500 * 1000); |
1491 | 1517 |
|
1492 | 1518 | if (!global_run) break;
|
1493 | 1519 | } // for
|
|
0 commit comments