This repository has been archived by the owner on Mar 22, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
tts.c
576 lines (468 loc) · 15.5 KB
/
tts.c
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
/*
+----------------------------------------------------------------------+
| PHP Version 7 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2017 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: renshan <[email protected]> |
+----------------------------------------------------------------------+
*/
/* $Id$ */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "php_tts.h"
/* If you declare any globals in php_tts.h uncomment this:
ZEND_DECLARE_MODULE_GLOBALS(tts)
*/
/* True global resources - no need for thread safety here */
static int le_tts;
zend_class_entry *tts_ce;
/* {{{ PHP_INI
*/
/* Remove comments and fill if you need to have entries in php.ini
PHP_INI_BEGIN()
STD_PHP_INI_ENTRY("tts.global_value", "42", PHP_INI_ALL, OnUpdateLong, global_value, zend_tts_globals, tts_globals)
STD_PHP_INI_ENTRY("tts.global_string", "foobar", PHP_INI_ALL, OnUpdateString, global_string, zend_tts_globals, tts_globals)
PHP_INI_END()
*/
/* }}} */
/* Remove the following function when you have successfully modified config.m4
so that your module can be compiled into PHP, it exists only for testing
purposes. */
/* Every user-visible function in PHP should document itself in the source */
/* {{{ proto string confirm_tts_compiled(string arg)
Return a string to confirm that the module is compiled in */
ZEND_METHOD(tts, __construct)
{
char *appid;
size_t a_len;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &appid, &a_len) == FAILURE) {
return;
}
if (strlen(appid) > 0) {
zend_update_property_string(tts_ce, getThis(), "appid", 5, appid TSRMLS_CC);
}
}
/* }}} */
/* {{{ proto setTxt(string arg)
设置需转换的内容 */
ZEND_METHOD(tts, setTxt)
{
char *text;
size_t len;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &text, &len) == FAILURE) {
return;
}
zend_update_property_string(tts_ce, getThis(), "text", 4, text TSRMLS_CC);
}
/* }}} */
/* {{{ protp string getTxt
获取转换的文字 */
ZEND_METHOD(tts, getTxt)
{
zval *rv;
zval *text = zend_read_property(tts_ce, getThis(), "text", 4, 1, rv);
if (Z_TYPE_P(text) == IS_NULL) {
RETURN_NULL();
}
RETURN_STR(strpprintf(0, "%s", text->value.str->val));
}
/* }}} */
/* {{{ proto setDest(arg)
设置保存位置 */
ZEND_METHOD(tts, setDest)
{
char *dest;
size_t len;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &dest, &len) == FAILURE) {
return;
}
zend_update_property_string(tts_ce, getThis(), "dest", 4, dest TSRMLS_CC);
}
/* }}} */
/* {{{ protp string getDest
获取保存位置*/
ZEND_METHOD(tts, getDest)
{
zval *rv;
zval *dest = zend_read_property(tts_ce, getThis(), "dest", 4, 1, rv);
if (Z_TYPE_P(dest) == IS_NULL) {
RETURN_NULL();
}
RETURN_STR(strpprintf(0, "%s", dest->value.str->val));
}
/* }}} */
/* {{{ proto setVoice(arg)
设置语音类型*/
ZEND_METHOD(tts, setVoice)
{
char *voice;
size_t len;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &voice, &len) == FAILURE) {
return;
}
zend_update_property_string(tts_ce, getThis(), "voice", 5, voice TSRMLS_CC);
}
/* }}} */
/* {{{ proto string getVoice
获取语音类型*/
ZEND_METHOD(tts, getVoice)
{
zval *rv;
zval *voice = zend_read_property(tts_ce, getThis(), "voice", 5, 1, rv);
if (Z_TYPE_P(voice) == IS_NULL) {
RETURN_NULL();
}
RETURN_STR(strpprintf(0, "%s", voice->value.str->val));
}
/* }}} */
/* {{{ protp setSPeed(arg)
设置语速 */
ZEND_METHOD(tts, setSpeed)
{
size_t speed;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &speed) == FAILURE) {
return;
}
zend_update_property_long(tts_ce, getThis(), "speed", 5, speed);
}
/* }}} */
/* {{{ proto long getSpeed
获取语速 */
ZEND_METHOD(tts, getSpeed)
{
zval *rv;
zval *speed = zend_read_property(tts_ce, getThis(), "speed", 5, 1, rv);
if (Z_TYPE_P(speed) == IS_NULL) {
RETURN_NULL();
}
RETURN_LONG(Z_LVAL_P(speed));
}
/* }}} */
/* {{{ proto setVolume(arg)
设置音量 */
ZEND_METHOD(tts, setVolume)
{
size_t volume;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &volume) == FAILURE) {
return;
}
zend_update_property_long(tts_ce, getThis(), "volume", 7, volume);
}
/* }}} */
/* {{{ proto long getVolume
获取音量 */
ZEND_METHOD(tts, getVolume)
{
zval *rv;
zval *volume = zend_read_property(tts_ce, getThis(), "volume", 7, 1, rv);
if (Z_TYPE_P(volume) == IS_NULL) {
RETURN_NULL();
}
RETURN_LONG(Z_LVAL_P(volume));
}
/* }}} */
/* {{{ proto setPitch(arg)
设置音高 */
ZEND_METHOD(tts, setPitch)
{
size_t pitch;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &pitch) == FAILURE) {
return;
}
zend_update_property_long(tts_ce, getThis(), "pitch", 5, pitch);
}
/* }}} */
/* {{{ proto long getPitch
获取音高 */
ZEND_METHOD(tts, getPitch)
{
zval *rv;
zval *pitch = zend_read_property(tts_ce, getThis(), "pitch", 5, 1, rv);
if (Z_TYPE_P(pitch) == IS_NULL) {
RETURN_NULL();
}
RETURN_LONG(Z_LVAL_P(pitch));
}
/* }}} */
/* {{{ proto setRdn(arg)
设置数字语音合成方式 */
ZEND_METHOD(tts, setRdn)
{
size_t rdn;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &rdn) == FAILURE) {
return;
}
zend_update_property_long(tts_ce, getThis(), "rdn", 3, rdn);
}
/* }}} */
/* {{{ proto long getRdn
获取语音合成方式*/
ZEND_METHOD(tts, getRdn)
{
zval *rv;
zval *rdn = zend_read_property(tts_ce, getThis(), "rdn", 3, 1, rv);
if (Z_TYPE_P(rdn) == IS_NULL) {
RETURN_NULL();
}
RETURN_LONG(Z_LVAL_P(rdn));
}
/* }}} */
ZEND_METHOD(tts, run)
{
zval *rev;
zval *appid;
zval *text;
zval *dest;
zval *voice;
zval *volume;
zval *pitch;
zval *speed;
zval *rdn;
appid = zend_read_property(tts_ce, getThis(), "appid", 5, 1, rev);
text = zend_read_property(tts_ce, getThis(), "text", 4, 1, rev);
dest = zend_read_property(tts_ce, getThis(), "dest", 4, 1, rev);
voice = zend_read_property(tts_ce, getThis(), "voice", 5, 1, rev);
volume = zend_read_property(tts_ce, getThis(), "volume", 7, 1, rev);
pitch = zend_read_property(tts_ce, getThis(), "pitch", 5, 1, rev);
speed = zend_read_property(tts_ce, getThis(), "speed", 5, 1, rev);
rdn = zend_read_property(tts_ce, getThis(), "rdn", 3, 1, rev);
convert_to_string(volume);
convert_to_string(pitch);
convert_to_string(speed);
convert_to_string(rdn);
if (Z_TYPE_P(appid) == IS_NULL ||
Z_TYPE_P(text) == IS_NULL ||
Z_TYPE_P(dest) == IS_NULL ||
Z_TYPE_P(volume) == IS_NULL ||
Z_TYPE_P(pitch) == IS_NULL ||
Z_TYPE_P(speed) == IS_NULL ||
Z_TYPE_P(rdn) == IS_NULL ||
Z_TYPE_P(voice) == IS_NULL) {
RETURN_LONG(-1);
}
int ret = MSP_SUCCESS;
char login_params[Z_STRLEN_P(appid) + 22];
// "appid = $appid, work_dir = .";//登录参数,appid与msc库绑定,请勿随意改动 */
sprintf(login_params, "appid = %s, work_dir = .", Z_STRVAL_P(appid));
/*
* rdn: 合成音频数字发音方式
* volume: 合成音频的音量
* pitch: 合成音频的音调
* speed: 合成音频对应的语速
* voice_name: 合成发音人
* sample_rate: 合成音频采样率
* text_encoding: 合成文本编码格式
*
* 详细参数说明请参阅《讯飞语音云MSC--API文档》
*/
char *session_begin_params = safe_emalloc(1, Z_STRLEN_P(voice) + Z_STRLEN_P(speed) + Z_STRLEN_P(volume) + Z_STRLEN_P(pitch) + Z_STRLEN_P(rdn) + 102, 1);
/* const char* session_begin_params = "voice_name = xiaoyan, text_encoding = utf8, sample_rate = 16000, speed = 50, volume = 50, pitch = 50, rdn = 2"; */
sprintf(session_begin_params,
"voice_name = %s, text_encoding = utf8, sample_rate = 16000, speed = %s, volume = %s, pitch = %s, rdn = %s",
voice->value.str->val,
Z_STRVAL_P(speed),
Z_STRVAL_P(volume),
Z_STRVAL_P(pitch),
Z_STRVAL_P(rdn));
char *_text = safe_emalloc(1, Z_STRLEN_P(text), 1);
sprintf(_text, "%s", Z_STRVAL_P(text));
char *filename = safe_emalloc(1, Z_STRLEN_P(dest), 1);
sprintf(filename, "%s", Z_STRVAL_P(dest));
/* 用户登录 */
ret = MSPLogin(NULL, NULL, login_params); //第一个参数是用户名,第二个参数是密码,第三个参数是登录参数,用户名和密码可在http://www.xfyun.cn注册获取
if (MSP_SUCCESS != ret)
{
MSPLogout(); //退出登录
RETURN_LONG(ret);
}
ret = text_to_speech(_text, filename, session_begin_params);
if (MSP_SUCCESS != ret)
{
MSPLogout(); //退出登录
RETURN_LONG(ret);
}
MSPLogout(); //退出登录
efree(session_begin_params);
efree(_text);
efree(filename);
RETURN_LONG(ret);
}
/* }}} */
/* The previous line is meant for vim and emacs, so it can correctly fold and
unfold functions in source code. See the corresponding marks just before
function definition, where the functions purpose is also documented. Please
follow this convention for the convenience of others editing your code.
*/
/* {{{ php_tts_init_globals
*/
/* Uncomment this function if you have INI entries
static void php_tts_init_globals(zend_tts_globals *tts_globals)
{
tts_globals->global_value = 0;
tts_globals->global_string = NULL;
}
*/
/* }}} */
/* {{{ tts_functions[]
*
* Every user visible function must have an entry in tts_functions[].
*/
const zend_function_entry tts_functions[] = {
//PHP_FE(tts, NULL) /* For testing, remove later. */
//PHP_FE_END /* Must be the last line in tts_functions[] */
/* 注册类方法 */
ZEND_ME(tts, __construct, NULL, ZEND_ACC_PUBLIC)
ZEND_ME(tts, setTxt, NULL, ZEND_ACC_PUBLIC)
ZEND_ME(tts, getTxt, NULL, ZEND_ACC_PUBLIC)
ZEND_ME(tts, setDest, NULL, ZEND_ACC_PUBLIC)
ZEND_ME(tts, getDest, NULL, ZEND_ACC_PUBLIC)
ZEND_ME(tts, setVoice, NULL, ZEND_ACC_PUBLIC)
ZEND_ME(tts, getVoice, NULL, ZEND_ACC_PUBLIC)
ZEND_ME(tts, setSpeed, NULL, ZEND_ACC_PUBLIC)
ZEND_ME(tts, getSpeed, NULL, ZEND_ACC_PUBLIC)
ZEND_ME(tts, setVolume, NULL, ZEND_ACC_PUBLIC)
ZEND_ME(tts, getVolume, NULL, ZEND_ACC_PUBLIC)
ZEND_ME(tts, setPitch, NULL, ZEND_ACC_PUBLIC)
ZEND_ME(tts, getPitch, NULL, ZEND_ACC_PUBLIC)
ZEND_ME(tts, setRdn, NULL, ZEND_ACC_PUBLIC)
ZEND_ME(tts, getRdn, NULL, ZEND_ACC_PUBLIC)
ZEND_ME(tts, run, NULL, ZEND_ACC_PUBLIC)
ZEND_FE_END
};
/* }}} */
/* {{{ PHP_MINIT_FUNCTION
*/
PHP_MINIT_FUNCTION(tts)
{
/* If you have INI entries, uncomment these lines
REGISTER_INI_ENTRIES();
*/
/* nginx 或 apache 启动时PHP会初始化这些 */
zend_class_entry ce;
INIT_CLASS_ENTRY(ce, "TTS", tts_functions);
tts_ce = zend_register_internal_class(&ce TSRMLS_CC);
/* 初始化一些值 */
zend_declare_property_null(tts_ce, "appid", 5, ZEND_ACC_PRIVATE TSRMLS_DC);
zend_declare_property_null(tts_ce, "text", 4, ZEND_ACC_PRIVATE TSRMLS_DC);
zend_declare_property_null(tts_ce, "dest", 4, ZEND_ACC_PRIVATE TSRMLS_DC);
/* 默认声音是xiaoyan */
zend_declare_property_string(tts_ce, "voice", 5, "xiaoyan", ZEND_ACC_PRIVATE TSRMLS_DC);
/* 默认语速是50 */
zend_declare_property_long(tts_ce, "speed", 5, 50, ZEND_ACC_PRIVATE TSRMLS_DC);
/* 默认音量是50 */
zend_declare_property_long(tts_ce, "volume", 7, 50, ZEND_ACC_PRIVATE TSRMLS_DC);
/* 默认音高是50 */
zend_declare_property_long(tts_ce, "pitch", 5, 50, ZEND_ACC_PRIVATE TSRMLS_DC);
/* 默认语音合成方式 */
zend_declare_property_long(tts_ce, "rdn", 3, 2, ZEND_ACC_PRIVATE TSRMLS_DC);
///
/// 语音合成设置的常量
/// 用法:
/// $tts->setPitch(TTS::TTS_RN_AUTO_VALUE); // $tts->setPitch(0);
/// $tts->setPitch(TTS::TTS_RN_RN_VALUE); // $tts->setPitch(1);
/// $tts->setPitch(TTS::TTS_RN_RN_DIGIT); // $tts->setPitch(2);
/// $tts->setPitch(TTS::TTS_RN_AUTO_DIGIT); // $tts->setPitch(3);
///
/* 自动,不确定时按照值发音*/
zend_declare_class_constant_long(tts_ce, "TTS_RN_AUTO_VALUE ", 17, 0 TSRMLS_DC);
/* 按照值发音 */
zend_declare_class_constant_long(tts_ce, "TTS_RN_VALUE ", 12, 1 TSRMLS_DC);
/* 按照 串发音*/
zend_declare_class_constant_long(tts_ce, "TTS_RN_DIGIT ", 12, 2 TSRMLS_DC);
/*自动,不确定时按照串发音 */
zend_declare_class_constant_long(tts_ce, "TTS_RN_AUTO_DIGIT ", 17, 3 TSRMLS_DC);
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MSHUTDOWN_FUNCTION
*/
PHP_MSHUTDOWN_FUNCTION(tts)
{
/* uncomment this line if you have INI entries
UNREGISTER_INI_ENTRIES();
*/
return SUCCESS;
}
/* }}} */
/* Remove if there's nothing to do at request start */
/* {{{ PHP_RINIT_FUNCTION
*/
PHP_RINIT_FUNCTION(tts)
{
#if defined(COMPILE_DL_TTS) && defined(ZTS)
ZEND_TSRMLS_CACHE_UPDATE();
#endif
return SUCCESS;
}
/* }}} */
/* Remove if there's nothing to do at request end */
/* {{{ PHP_RSHUTDOWN_FUNCTION
*/
PHP_RSHUTDOWN_FUNCTION(tts)
{
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MINFO_FUNCTION
*/
PHP_MINFO_FUNCTION(tts)
{
php_info_print_table_start();
php_info_print_table_header(2, "TTS support", "enabled");
php_info_print_table_row(2, "Default Voice name", "xiaoyan");
php_info_print_table_row(2, "Text encoding", "utf8");
php_info_print_table_row(2, "Sample rate", "16000");
php_info_print_table_row(2, "Default Speed", "50");
php_info_print_table_row(2, "Default Volume", "50");
php_info_print_table_row(2, "Default Pitch", "50");
php_info_print_table_row(2, "Default Rdn", "2");
php_info_print_table_header(2, "Version", "1.1.0");
php_info_print_table_header(2, "Author", "renshan <[email protected]>");
php_info_print_table_end();
/* Remove comments if you have entries in php.ini
DISPLAY_INI_ENTRIES();
*/
}
/* }}} */
/* {{{ tts_module_entry
*/
zend_module_entry tts_module_entry = {
STANDARD_MODULE_HEADER,
"tts",
tts_functions,
PHP_MINIT(tts),
PHP_MSHUTDOWN(tts),
PHP_RINIT(tts), /* Replace with NULL if there's nothing to do at request start */
PHP_RSHUTDOWN(tts), /* Replace with NULL if there's nothing to do at request end */
PHP_MINFO(tts),
PHP_TTS_VERSION,
STANDARD_MODULE_PROPERTIES
};
/* }}} */
#ifdef COMPILE_DL_TTS
#ifdef ZTS
ZEND_TSRMLS_CACHE_DEFINE()
#endif
ZEND_GET_MODULE(tts)
#endif
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: noet sw=4 ts=4 fdm=marker
* vim<600: noet sw=4 ts=4
*/