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 9.2KB

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