eSpeak NG is an open source speech synthesizer that supports more than hundred languages and accents.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

espeak_command.cpp 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705
  1. /***************************************************************************
  2. * Copyright (C) 2007, Gilles Casse <[email protected]> *
  3. * Copyright (C) 2013 Reece H. Dunn *
  4. * *
  5. * This program is free software; you can redistribute it and/or modify *
  6. * it under the terms of the GNU General Public License as published by *
  7. * the Free Software Foundation; either version 3 of the License, or *
  8. * (at your option) any later version. *
  9. * *
  10. * This program is distributed in the hope that it will be useful, *
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  13. * GNU General Public License for more details. *
  14. * *
  15. * You should have received a copy of the GNU General Public License *
  16. * along with this program; if not, write to the *
  17. * Free Software Foundation, Inc., *
  18. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  19. ***************************************************************************/
  20. // This source file is only used for asynchronious modes
  21. #include "speech.h"
  22. #include "espeak_command.h"
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <assert.h>
  26. #include <wchar.h>
  27. #include "debug.h"
  28. static unsigned int my_current_text_id=0;
  29. //<create_espeak_text
  30. t_espeak_command* create_espeak_text(const void *text, size_t size, unsigned int position, espeak_POSITION_TYPE position_type, unsigned int end_position, unsigned int flags, void* user_data)
  31. {
  32. ENTER("create_espeak_text");
  33. int a_error=1;
  34. void* a_text = NULL;
  35. t_espeak_text* data = NULL;
  36. t_espeak_command* a_command = (t_espeak_command*)malloc(sizeof(t_espeak_command));
  37. if (!text || !size || !a_command)
  38. {
  39. goto text_error;
  40. }
  41. a_text = malloc( size+1 );
  42. if (!a_text)
  43. {
  44. goto text_error;
  45. }
  46. memcpy(a_text, text, size);
  47. a_command->type = ET_TEXT;
  48. a_command->state = CS_UNDEFINED;
  49. data = &(a_command->u.my_text);
  50. data->unique_identifier = ++my_current_text_id;
  51. data->text = a_text;
  52. data->size = size;
  53. data->position = position;
  54. data->position_type = position_type;
  55. data->end_position = end_position;
  56. data->flags = flags;
  57. data->user_data = user_data;
  58. a_error=0;
  59. SHOW("ET_TEXT malloc text=%x, command=%x (uid=%d)\n", a_text, a_command, data->unique_identifier);
  60. text_error:
  61. if (a_error)
  62. {
  63. if (a_text)
  64. {
  65. free (a_text);
  66. }
  67. if (a_command)
  68. {
  69. free (a_command);
  70. }
  71. a_command = NULL;
  72. }
  73. SHOW("command=0x%x\n", a_command);
  74. return a_command;
  75. }
  76. //>
  77. t_espeak_command* create_espeak_terminated_msg(unsigned int unique_identifier, void* user_data)
  78. {
  79. ENTER("create_espeak_terminated_msg");
  80. int a_error=1;
  81. t_espeak_terminated_msg* data = NULL;
  82. t_espeak_command* a_command = (t_espeak_command*)malloc(sizeof(t_espeak_command));
  83. if (!a_command)
  84. {
  85. goto msg_error;
  86. }
  87. a_command->type = ET_TERMINATED_MSG;
  88. a_command->state = CS_UNDEFINED;
  89. data = &(a_command->u.my_terminated_msg);
  90. data->unique_identifier = unique_identifier;
  91. data->user_data = user_data;
  92. a_error=0;
  93. SHOW("ET_TERMINATED_MSG command=%x (uid=%d, user_data=0x%x)\n", a_command, unique_identifier, (int)user_data);
  94. msg_error:
  95. if (a_error)
  96. {
  97. if (a_command)
  98. {
  99. free (a_command);
  100. }
  101. a_command = NULL;
  102. }
  103. SHOW("command=0x%x\n", a_command);
  104. return a_command;
  105. }
  106. //<create_espeak_mark
  107. t_espeak_command* create_espeak_mark(const void *text, size_t size, const char *index_mark, unsigned int end_position, unsigned int flags, void* user_data)
  108. {
  109. ENTER("create_espeak_mark");
  110. int a_error=1;
  111. void* a_text = NULL;
  112. char *a_index_mark = NULL;
  113. t_espeak_mark* data = NULL;
  114. t_espeak_command* a_command = (t_espeak_command*)malloc(sizeof(t_espeak_command));
  115. if (!text || !size || !index_mark || !a_command)
  116. {
  117. goto mark_error;
  118. }
  119. a_text = malloc( size );
  120. if (!a_text)
  121. {
  122. goto mark_error;
  123. }
  124. memcpy(a_text, text, size);
  125. a_index_mark = strdup( index_mark);
  126. a_command->type = ET_MARK;
  127. a_command->state = CS_UNDEFINED;
  128. data = &(a_command->u.my_mark);
  129. data->unique_identifier = ++my_current_text_id;
  130. data->text = a_text;
  131. data->size = size;
  132. data->index_mark = a_index_mark;
  133. data->end_position = end_position;
  134. data->flags = flags;
  135. data->user_data = user_data;
  136. a_error=0;
  137. mark_error:
  138. if (a_error)
  139. {
  140. if (a_text)
  141. {
  142. free (a_text);
  143. }
  144. if (a_command)
  145. {
  146. free (a_command);
  147. }
  148. a_command = NULL;
  149. if (a_index_mark)
  150. {
  151. free (a_index_mark);
  152. }
  153. }
  154. SHOW("ET_MARK malloc text=%x, command=%x (uid=%d)\n", a_text, a_command, data->unique_identifier);
  155. return a_command;
  156. }
  157. //>
  158. //< create_espeak_key, create_espeak_char
  159. t_espeak_command* create_espeak_key(const char *key_name, void *user_data)
  160. {
  161. ENTER("create_espeak_key");
  162. int a_error=1;
  163. t_espeak_command* a_command = (t_espeak_command*)malloc(sizeof(t_espeak_command));
  164. if (!key_name || !a_command)
  165. {
  166. goto key_error;
  167. }
  168. a_command->type = ET_KEY;
  169. a_command->state = CS_UNDEFINED;
  170. a_command->u.my_key.user_data = user_data;
  171. a_command->u.my_key.unique_identifier = ++my_current_text_id;
  172. a_command->u.my_key.key_name = strdup( key_name);
  173. a_error=0;
  174. key_error:
  175. if (a_error)
  176. {
  177. if (a_command)
  178. {
  179. free (a_command);
  180. }
  181. a_command = NULL;
  182. }
  183. SHOW("command=0x%x\n", a_command);
  184. return a_command;
  185. }
  186. t_espeak_command* create_espeak_char(wchar_t character, void* user_data)
  187. {
  188. ENTER("create_espeak_char");
  189. int a_error=1;
  190. t_espeak_command* a_command = (t_espeak_command*)malloc(sizeof(t_espeak_command));
  191. if (!a_command)
  192. {
  193. goto char_error;
  194. }
  195. a_command->type = ET_CHAR;
  196. a_command->state = CS_UNDEFINED;
  197. a_command->u.my_char.user_data = user_data;
  198. a_command->u.my_char.unique_identifier = ++my_current_text_id;
  199. a_command->u.my_char.character = character;
  200. a_error=0;
  201. char_error:
  202. if (a_error)
  203. {
  204. if (a_command)
  205. {
  206. free (a_command);
  207. }
  208. a_command = NULL;
  209. }
  210. SHOW("command=0x%x\n", a_command);
  211. return a_command;
  212. }
  213. //>
  214. //< create_espeak_parameter
  215. t_espeak_command* create_espeak_parameter(espeak_PARAMETER parameter, int value, int relative)
  216. {
  217. ENTER("create_espeak_parameter");
  218. int a_error=1;
  219. t_espeak_parameter* data = NULL;
  220. t_espeak_command* a_command = (t_espeak_command*)malloc(sizeof(t_espeak_command));
  221. if (!a_command)
  222. {
  223. goto param_error;
  224. }
  225. a_command->type = ET_PARAMETER;
  226. a_command->state = CS_UNDEFINED;
  227. data = &(a_command->u.my_param);
  228. data->parameter = parameter;
  229. data->value = value;
  230. data->relative = relative;
  231. a_error=0;
  232. param_error:
  233. if (a_error)
  234. {
  235. if (a_command)
  236. {
  237. free (a_command);
  238. }
  239. a_command = NULL;
  240. }
  241. SHOW("command=0x%x\n", a_command);
  242. return a_command;
  243. }
  244. //>
  245. //< create_espeak_punctuation_list
  246. t_espeak_command* create_espeak_punctuation_list(const wchar_t *punctlist)
  247. {
  248. ENTER("create_espeak_punctuation_list");
  249. int a_error=1;
  250. // wchar_t *a_list = NULL;
  251. t_espeak_command* a_command = (t_espeak_command*)malloc(sizeof(t_espeak_command));
  252. if (!punctlist || !a_command)
  253. {
  254. goto list_error;
  255. }
  256. a_command->type = ET_PUNCTUATION_LIST;
  257. a_command->state = CS_UNDEFINED;
  258. {
  259. size_t len = (wcslen(punctlist) + 1)*sizeof(wchar_t);
  260. wchar_t* a_list = (wchar_t*)malloc(len);
  261. memcpy(a_list, punctlist, len);
  262. a_command->u.my_punctuation_list = a_list;
  263. }
  264. a_error=0;
  265. list_error:
  266. if (a_error)
  267. {
  268. if (a_command)
  269. {
  270. free (a_command);
  271. }
  272. a_command = NULL;
  273. }
  274. SHOW("command=0x%x\n", a_command);
  275. return a_command;
  276. }
  277. //>
  278. //< create_espeak_voice_name, create_espeak_voice_spec
  279. t_espeak_command* create_espeak_voice_name(const char *name)
  280. {
  281. ENTER("create_espeak_voice_name");
  282. int a_error=1;
  283. t_espeak_command* a_command = (t_espeak_command*)malloc(sizeof(t_espeak_command));
  284. if (!name || !a_command)
  285. {
  286. goto name_error;
  287. }
  288. a_command->type = ET_VOICE_NAME;
  289. a_command->state = CS_UNDEFINED;
  290. a_command->u.my_voice_name = strdup( name);
  291. a_error=0;
  292. name_error:
  293. if (a_error)
  294. {
  295. if (a_command)
  296. {
  297. free (a_command);
  298. }
  299. a_command = NULL;
  300. }
  301. SHOW("command=0x%x\n", a_command);
  302. return a_command;
  303. }
  304. t_espeak_command* create_espeak_voice_spec(espeak_VOICE *voice)
  305. {
  306. ENTER("create_espeak_voice_spec");
  307. int a_error=1;
  308. t_espeak_command* a_command = (t_espeak_command*)malloc(sizeof(t_espeak_command));
  309. if (!voice || !a_command)
  310. {
  311. goto spec_error;
  312. }
  313. a_command->type = ET_VOICE_SPEC;
  314. a_command->state = CS_UNDEFINED;
  315. {
  316. espeak_VOICE* data = &(a_command->u.my_voice_spec);
  317. memcpy(data, voice, sizeof(espeak_VOICE));
  318. if (voice->name)
  319. {
  320. data->name = strdup(voice->name);
  321. }
  322. if (voice->languages)
  323. {
  324. data->languages = strdup(voice->languages);
  325. }
  326. if (voice->identifier)
  327. {
  328. data->identifier = strdup(voice->identifier);
  329. }
  330. a_error=0;
  331. }
  332. spec_error:
  333. if (a_error)
  334. {
  335. if (a_command)
  336. {
  337. free (a_command);
  338. }
  339. a_command = NULL;
  340. }
  341. SHOW("command=0x%x\n", a_command);
  342. return a_command;
  343. }
  344. //>
  345. //< delete_espeak_command
  346. int delete_espeak_command( t_espeak_command* the_command)
  347. {
  348. ENTER("delete_espeak_command");
  349. int a_status = 0;
  350. if (the_command)
  351. {
  352. switch(the_command->type)
  353. {
  354. case ET_TEXT:
  355. if (the_command->u.my_text.text)
  356. {
  357. SHOW("delete_espeak_command > ET_TEXT free text=%x, command=%x, uid=%d\n", the_command->u.my_text.text, the_command, the_command->u.my_text.unique_identifier);
  358. free(the_command->u.my_text.text);
  359. }
  360. break;
  361. case ET_MARK:
  362. if (the_command->u.my_mark.text)
  363. {
  364. free(the_command->u.my_mark.text);
  365. }
  366. if (the_command->u.my_mark.index_mark)
  367. {
  368. free((void*)(the_command->u.my_mark.index_mark));
  369. }
  370. break;
  371. case ET_TERMINATED_MSG:
  372. {
  373. // if the terminated msg is pending,
  374. // it must be processed here for informing the calling program
  375. // that its message is finished.
  376. // This can be important for cleaning the related user data.
  377. t_espeak_terminated_msg* data = &(the_command->u.my_terminated_msg);
  378. if (the_command->state == CS_PENDING)
  379. {
  380. the_command->state = CS_PROCESSED;
  381. SHOW("delete_espeak_command > ET_TERMINATED_MSG callback (command=0x%x, uid=%d) \n", the_command, data->unique_identifier);
  382. sync_espeak_terminated_msg( data->unique_identifier, data->user_data);
  383. }
  384. }
  385. break;
  386. case ET_KEY:
  387. if (the_command->u.my_key.key_name)
  388. {
  389. free((void*)(the_command->u.my_key.key_name));
  390. }
  391. break;
  392. case ET_CHAR:
  393. case ET_PARAMETER:
  394. // No allocation
  395. break;
  396. case ET_PUNCTUATION_LIST:
  397. if (the_command->u.my_punctuation_list)
  398. {
  399. free((void*)(the_command->u.my_punctuation_list));
  400. }
  401. break;
  402. case ET_VOICE_NAME:
  403. if (the_command->u.my_voice_name)
  404. {
  405. free((void*)(the_command->u.my_voice_name));
  406. }
  407. break;
  408. case ET_VOICE_SPEC:
  409. {
  410. espeak_VOICE* data = &(the_command->u.my_voice_spec);
  411. if (data->name)
  412. {
  413. free((void *)data->name);
  414. }
  415. if (data->languages)
  416. {
  417. free((void *)data->languages);
  418. }
  419. if (data->identifier)
  420. {
  421. free((void *)data->identifier);
  422. }
  423. }
  424. break;
  425. default:
  426. assert(0);
  427. }
  428. SHOW("delete_espeak_command > free command=0x%x\n", the_command);
  429. free(the_command);
  430. a_status = 1;
  431. }
  432. return a_status;
  433. }
  434. //>
  435. //< process_espeak_command
  436. void process_espeak_command( t_espeak_command* the_command)
  437. {
  438. ENTER("process_espeak_command");
  439. SHOW("command=0x%x\n", the_command);
  440. if (the_command == NULL)
  441. {
  442. return;
  443. }
  444. the_command->state = CS_PROCESSED;
  445. switch(the_command->type)
  446. {
  447. case ET_TEXT:
  448. {
  449. t_espeak_text* data = &(the_command->u.my_text);
  450. sync_espeak_Synth( data->unique_identifier, data->text, data->size,
  451. data->position, data->position_type,
  452. data->end_position, data->flags, data->user_data);
  453. }
  454. break;
  455. case ET_MARK:
  456. {
  457. t_espeak_mark* data = &(the_command->u.my_mark);
  458. sync_espeak_Synth_Mark( data->unique_identifier, data->text, data->size,
  459. data->index_mark, data->end_position, data->flags,
  460. data->user_data);
  461. }
  462. break;
  463. case ET_TERMINATED_MSG:
  464. {
  465. t_espeak_terminated_msg* data = &(the_command->u.my_terminated_msg);
  466. sync_espeak_terminated_msg( data->unique_identifier, data->user_data);
  467. }
  468. break;
  469. case ET_KEY:
  470. {
  471. const char* data = the_command->u.my_key.key_name;
  472. sync_espeak_Key(data);
  473. }
  474. break;
  475. case ET_CHAR:
  476. {
  477. const wchar_t data = the_command->u.my_char.character;
  478. sync_espeak_Char( data);
  479. }
  480. break;
  481. case ET_PARAMETER:
  482. {
  483. t_espeak_parameter* data = &(the_command->u.my_param);
  484. SetParameter( data->parameter, data->value, data->relative);
  485. }
  486. break;
  487. case ET_PUNCTUATION_LIST:
  488. {
  489. const wchar_t* data = the_command->u.my_punctuation_list;
  490. sync_espeak_SetPunctuationList( data);
  491. }
  492. break;
  493. case ET_VOICE_NAME:
  494. {
  495. const char* data = the_command->u.my_voice_name;
  496. SetVoiceByName( data);
  497. }
  498. break;
  499. case ET_VOICE_SPEC:
  500. {
  501. espeak_VOICE* data = &(the_command->u.my_voice_spec);
  502. SetVoiceByProperties(data);
  503. }
  504. break;
  505. default:
  506. assert(0);
  507. break;
  508. }
  509. }
  510. //>
  511. //< process_espeak_command
  512. void display_espeak_command( t_espeak_command* the_command)
  513. {
  514. ENTER("display_espeak_command");
  515. #ifdef DEBUG_ENABLED
  516. if (the_command == NULL)
  517. {
  518. SHOW("display_espeak_command > command=%s\n","NULL");
  519. return;
  520. }
  521. SHOW("display_espeak_command > state=%d\n",the_command->state);
  522. switch(the_command->type)
  523. {
  524. case ET_TEXT:
  525. {
  526. t_espeak_text* data = &(the_command->u.my_text);
  527. SHOW("display_espeak_command > (0x%x) uid=%d, TEXT=%s, user_data=0x%x\n", the_command, data->unique_identifier, (char*)data->text, (int)(data->user_data));
  528. }
  529. break;
  530. case ET_MARK:
  531. {
  532. t_espeak_mark* data = &(the_command->u.my_mark);
  533. SHOW("display_espeak_command > (0x%x) uid=%d, MARK=%s, user_data=0x%x\n", the_command, data->unique_identifier, (char*)data->text, (int)(data->user_data));
  534. }
  535. break;
  536. case ET_KEY:
  537. {
  538. const char* data = the_command->u.my_key.key_name;
  539. SHOW("display_espeak_command > (0x%x) KEY=%c\n", the_command, data);
  540. }
  541. break;
  542. case ET_TERMINATED_MSG:
  543. {
  544. t_espeak_terminated_msg* data = &(the_command->u.my_terminated_msg);
  545. SHOW("display_espeak_command > (0x%x) TERMINATED_MSG uid=%d, user_data=0x%x, state=%d\n",
  546. the_command, data->unique_identifier, data->user_data,
  547. the_command->state);
  548. }
  549. break;
  550. case ET_CHAR:
  551. {
  552. const wchar_t data = the_command->u.my_char.character;
  553. SHOW("display_espeak_command > (0x%x) CHAR=%c\n", the_command, (char)data);
  554. }
  555. break;
  556. case ET_PARAMETER:
  557. {
  558. t_espeak_parameter* data = &(the_command->u.my_param);
  559. SHOW("display_espeak_command > (0x%x) PARAMETER=%d, value=%d, relative=%d\n",
  560. the_command, data->parameter, data->value, data->relative);
  561. }
  562. break;
  563. case ET_PUNCTUATION_LIST:
  564. {
  565. const wchar_t* data = the_command->u.my_punctuation_list;
  566. sync_espeak_SetPunctuationList( data);
  567. SHOW("display_espeak_command > (0x%x) PUNCTLIST=%s\n", the_command, (char*)data);
  568. }
  569. break;
  570. case ET_VOICE_NAME:
  571. {
  572. const char* data = the_command->u.my_voice_name;
  573. SHOW("display_espeak_command > (0x%x) VOICE_NAME=%s\n", the_command, data);
  574. }
  575. break;
  576. case ET_VOICE_SPEC:
  577. {
  578. SHOW("display_espeak_command > (0x%x) VOICE_SPEC", the_command);
  579. }
  580. break;
  581. default:
  582. assert(0);
  583. break;
  584. }
  585. #endif
  586. }
  587. //>