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.c 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. /*
  2. * Copyright (C) 2007, Gilles Casse <[email protected]>
  3. * Copyright (C) 2013-2015 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, see: <http://www.gnu.org/licenses/>.
  17. */
  18. #include "config.h"
  19. #include "espeak_ng.h"
  20. #include "speech.h"
  21. #include "espeak_command.h"
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <assert.h>
  25. #include <wchar.h>
  26. static unsigned int my_current_text_id = 0;
  27. 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)
  28. {
  29. int a_error = 1;
  30. void *a_text = NULL;
  31. t_espeak_text *data = NULL;
  32. t_espeak_command *a_command = (t_espeak_command *)malloc(sizeof(t_espeak_command));
  33. if (!text || !size || !a_command)
  34. goto text_error;
  35. a_text = malloc(size+1);
  36. if (!a_text)
  37. goto text_error;
  38. memcpy(a_text, text, size);
  39. a_command->type = ET_TEXT;
  40. a_command->state = CS_UNDEFINED;
  41. data = &(a_command->u.my_text);
  42. data->unique_identifier = ++my_current_text_id;
  43. data->text = a_text;
  44. data->position = position;
  45. data->position_type = position_type;
  46. data->end_position = end_position;
  47. data->flags = flags;
  48. data->user_data = user_data;
  49. a_error = 0;
  50. text_error:
  51. if (a_error) {
  52. if (a_text)
  53. free(a_text);
  54. if (a_command)
  55. free(a_command);
  56. a_command = NULL;
  57. }
  58. return a_command;
  59. }
  60. t_espeak_command *create_espeak_terminated_msg(unsigned int unique_identifier, void *user_data)
  61. {
  62. int a_error = 1;
  63. t_espeak_terminated_msg *data = NULL;
  64. t_espeak_command *a_command = (t_espeak_command *)malloc(sizeof(t_espeak_command));
  65. if (!a_command)
  66. goto msg_error;
  67. a_command->type = ET_TERMINATED_MSG;
  68. a_command->state = CS_UNDEFINED;
  69. data = &(a_command->u.my_terminated_msg);
  70. data->unique_identifier = unique_identifier;
  71. data->user_data = user_data;
  72. a_error = 0;
  73. msg_error:
  74. if (a_error) {
  75. if (a_command)
  76. free(a_command);
  77. a_command = NULL;
  78. }
  79. return a_command;
  80. }
  81. 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)
  82. {
  83. int a_error = 1;
  84. void *a_text = NULL;
  85. char *a_index_mark = NULL;
  86. t_espeak_mark *data = NULL;
  87. t_espeak_command *a_command = (t_espeak_command *)malloc(sizeof(t_espeak_command));
  88. if (!text || !size || !index_mark || !a_command)
  89. goto mark_error;
  90. a_text = malloc(size);
  91. if (!a_text)
  92. goto mark_error;
  93. memcpy(a_text, text, size);
  94. a_index_mark = strdup(index_mark);
  95. a_command->type = ET_MARK;
  96. a_command->state = CS_UNDEFINED;
  97. data = &(a_command->u.my_mark);
  98. data->unique_identifier = ++my_current_text_id;
  99. data->text = a_text;
  100. data->index_mark = a_index_mark;
  101. data->end_position = end_position;
  102. data->flags = flags;
  103. data->user_data = user_data;
  104. a_error = 0;
  105. mark_error:
  106. if (a_error) {
  107. if (a_text)
  108. free(a_text);
  109. if (a_command)
  110. free(a_command);
  111. a_command = NULL;
  112. if (a_index_mark)
  113. free(a_index_mark);
  114. }
  115. return a_command;
  116. }
  117. t_espeak_command *create_espeak_key(const char *key_name, void *user_data)
  118. {
  119. int a_error = 1;
  120. t_espeak_command *a_command = (t_espeak_command *)malloc(sizeof(t_espeak_command));
  121. if (!key_name || !a_command)
  122. goto key_error;
  123. a_command->type = ET_KEY;
  124. a_command->state = CS_UNDEFINED;
  125. a_command->u.my_key.user_data = user_data;
  126. a_command->u.my_key.unique_identifier = ++my_current_text_id;
  127. a_command->u.my_key.key_name = strdup(key_name);
  128. a_error = 0;
  129. key_error:
  130. if (a_error) {
  131. if (a_command)
  132. free(a_command);
  133. a_command = NULL;
  134. }
  135. return a_command;
  136. }
  137. t_espeak_command *create_espeak_char(wchar_t character, void *user_data)
  138. {
  139. int a_error = 1;
  140. t_espeak_command *a_command = (t_espeak_command *)malloc(sizeof(t_espeak_command));
  141. if (!a_command)
  142. goto char_error;
  143. a_command->type = ET_CHAR;
  144. a_command->state = CS_UNDEFINED;
  145. a_command->u.my_char.user_data = user_data;
  146. a_command->u.my_char.unique_identifier = ++my_current_text_id;
  147. a_command->u.my_char.character = character;
  148. a_error = 0;
  149. char_error:
  150. if (a_error) {
  151. if (a_command)
  152. free(a_command);
  153. a_command = NULL;
  154. }
  155. return a_command;
  156. }
  157. t_espeak_command *create_espeak_parameter(espeak_PARAMETER parameter, int value, int relative)
  158. {
  159. int a_error = 1;
  160. t_espeak_parameter *data = NULL;
  161. t_espeak_command *a_command = (t_espeak_command *)malloc(sizeof(t_espeak_command));
  162. if (!a_command)
  163. goto param_error;
  164. a_command->type = ET_PARAMETER;
  165. a_command->state = CS_UNDEFINED;
  166. data = &(a_command->u.my_param);
  167. data->parameter = parameter;
  168. data->value = value;
  169. data->relative = relative;
  170. a_error = 0;
  171. param_error:
  172. if (a_error) {
  173. if (a_command)
  174. free(a_command);
  175. a_command = NULL;
  176. }
  177. return a_command;
  178. }
  179. t_espeak_command *create_espeak_punctuation_list(const wchar_t *punctlist)
  180. {
  181. int a_error = 1;
  182. t_espeak_command *a_command = (t_espeak_command *)malloc(sizeof(t_espeak_command));
  183. if (!punctlist || !a_command)
  184. goto list_error;
  185. a_command->type = ET_PUNCTUATION_LIST;
  186. a_command->state = CS_UNDEFINED;
  187. size_t len = (wcslen(punctlist) + 1)*sizeof(wchar_t);
  188. wchar_t *a_list = (wchar_t *)malloc(len);
  189. memcpy(a_list, punctlist, len);
  190. a_command->u.my_punctuation_list = a_list;
  191. a_error = 0;
  192. list_error:
  193. if (a_error) {
  194. if (a_command)
  195. free(a_command);
  196. a_command = NULL;
  197. }
  198. return a_command;
  199. }
  200. t_espeak_command *create_espeak_voice_name(const char *name)
  201. {
  202. int a_error = 1;
  203. t_espeak_command *a_command = (t_espeak_command *)malloc(sizeof(t_espeak_command));
  204. if (!name || !a_command)
  205. goto name_error;
  206. a_command->type = ET_VOICE_NAME;
  207. a_command->state = CS_UNDEFINED;
  208. a_command->u.my_voice_name = strdup(name);
  209. a_error = 0;
  210. name_error:
  211. if (a_error) {
  212. if (a_command)
  213. free(a_command);
  214. a_command = NULL;
  215. }
  216. return a_command;
  217. }
  218. t_espeak_command *create_espeak_voice_spec(espeak_VOICE *voice)
  219. {
  220. int a_error = 1;
  221. t_espeak_command *a_command = (t_espeak_command *)malloc(sizeof(t_espeak_command));
  222. if (!voice || !a_command)
  223. goto spec_error;
  224. a_command->type = ET_VOICE_SPEC;
  225. a_command->state = CS_UNDEFINED;
  226. espeak_VOICE *data = &(a_command->u.my_voice_spec);
  227. memcpy(data, voice, sizeof(espeak_VOICE));
  228. if (voice->name)
  229. data->name = strdup(voice->name);
  230. if (voice->languages)
  231. data->languages = strdup(voice->languages);
  232. if (voice->identifier)
  233. data->identifier = strdup(voice->identifier);
  234. a_error = 0;
  235. spec_error:
  236. if (a_error) {
  237. if (a_command)
  238. free(a_command);
  239. a_command = NULL;
  240. }
  241. return a_command;
  242. }
  243. int delete_espeak_command(t_espeak_command *the_command)
  244. {
  245. int a_status = 0;
  246. if (the_command) {
  247. switch (the_command->type)
  248. {
  249. case ET_TEXT:
  250. if (the_command->u.my_text.text)
  251. free(the_command->u.my_text.text);
  252. break;
  253. case ET_MARK:
  254. if (the_command->u.my_mark.text)
  255. free(the_command->u.my_mark.text);
  256. if (the_command->u.my_mark.index_mark)
  257. free((void *)(the_command->u.my_mark.index_mark));
  258. break;
  259. case ET_TERMINATED_MSG:
  260. {
  261. // if the terminated msg is pending,
  262. // it must be processed here for informing the calling program
  263. // that its message is finished.
  264. // This can be important for cleaning the related user data.
  265. t_espeak_terminated_msg *data = &(the_command->u.my_terminated_msg);
  266. if (the_command->state == CS_PENDING) {
  267. the_command->state = CS_PROCESSED;
  268. sync_espeak_terminated_msg(data->unique_identifier, data->user_data);
  269. }
  270. }
  271. break;
  272. case ET_KEY:
  273. if (the_command->u.my_key.key_name)
  274. free((void *)(the_command->u.my_key.key_name));
  275. break;
  276. case ET_CHAR:
  277. case ET_PARAMETER:
  278. // No allocation
  279. break;
  280. case ET_PUNCTUATION_LIST:
  281. if (the_command->u.my_punctuation_list)
  282. free((void *)(the_command->u.my_punctuation_list));
  283. break;
  284. case ET_VOICE_NAME:
  285. if (the_command->u.my_voice_name)
  286. free((void *)(the_command->u.my_voice_name));
  287. break;
  288. case ET_VOICE_SPEC:
  289. {
  290. espeak_VOICE *data = &(the_command->u.my_voice_spec);
  291. if (data->name)
  292. free((void *)data->name);
  293. if (data->languages)
  294. free((void *)data->languages);
  295. if (data->identifier)
  296. free((void *)data->identifier);
  297. }
  298. break;
  299. default:
  300. assert(0);
  301. }
  302. free(the_command);
  303. a_status = 1;
  304. }
  305. return a_status;
  306. }
  307. void process_espeak_command(t_espeak_command *the_command)
  308. {
  309. if (the_command == NULL)
  310. return;
  311. the_command->state = CS_PROCESSED;
  312. switch (the_command->type)
  313. {
  314. case ET_TEXT:
  315. {
  316. t_espeak_text *data = &(the_command->u.my_text);
  317. sync_espeak_Synth(data->unique_identifier, data->text,
  318. data->position, data->position_type,
  319. data->end_position, data->flags, data->user_data);
  320. }
  321. break;
  322. case ET_MARK:
  323. {
  324. t_espeak_mark *data = &(the_command->u.my_mark);
  325. sync_espeak_Synth_Mark(data->unique_identifier, data->text,
  326. data->index_mark, data->end_position, data->flags,
  327. data->user_data);
  328. }
  329. break;
  330. case ET_TERMINATED_MSG:
  331. {
  332. t_espeak_terminated_msg *data = &(the_command->u.my_terminated_msg);
  333. sync_espeak_terminated_msg(data->unique_identifier, data->user_data);
  334. }
  335. break;
  336. case ET_KEY:
  337. {
  338. const char *data = the_command->u.my_key.key_name;
  339. sync_espeak_Key(data);
  340. }
  341. break;
  342. case ET_CHAR:
  343. {
  344. const wchar_t data = the_command->u.my_char.character;
  345. sync_espeak_Char(data);
  346. }
  347. break;
  348. case ET_PARAMETER:
  349. {
  350. t_espeak_parameter *data = &(the_command->u.my_param);
  351. SetParameter(data->parameter, data->value, data->relative);
  352. }
  353. break;
  354. case ET_PUNCTUATION_LIST:
  355. {
  356. const wchar_t *data = the_command->u.my_punctuation_list;
  357. sync_espeak_SetPunctuationList(data);
  358. }
  359. break;
  360. case ET_VOICE_NAME:
  361. {
  362. const char *data = the_command->u.my_voice_name;
  363. espeak_SetVoiceByName(data);
  364. }
  365. break;
  366. case ET_VOICE_SPEC:
  367. {
  368. espeak_VOICE *data = &(the_command->u.my_voice_spec);
  369. espeak_SetVoiceByProperties(data);
  370. }
  371. break;
  372. default:
  373. assert(0);
  374. break;
  375. }
  376. }