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.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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. memcpy(a_list, punctlist, len);
  147. a_command->u.my_punctuation_list = a_list;
  148. return a_command;
  149. }
  150. t_espeak_command *create_espeak_voice_name(const char *name)
  151. {
  152. if (!name)
  153. return NULL;
  154. t_espeak_command *a_command = (t_espeak_command *)malloc(sizeof(t_espeak_command));
  155. if (!a_command)
  156. return NULL;
  157. a_command->type = ET_VOICE_NAME;
  158. a_command->state = CS_UNDEFINED;
  159. a_command->u.my_voice_name = strdup(name);
  160. return a_command;
  161. }
  162. t_espeak_command *create_espeak_voice_spec(espeak_VOICE *voice)
  163. {
  164. if (!voice)
  165. return NULL;
  166. t_espeak_command *a_command = (t_espeak_command *)malloc(sizeof(t_espeak_command));
  167. if (!a_command)
  168. return NULL;
  169. a_command->type = ET_VOICE_SPEC;
  170. a_command->state = CS_UNDEFINED;
  171. espeak_VOICE *data = &(a_command->u.my_voice_spec);
  172. memcpy(data, voice, sizeof(espeak_VOICE));
  173. if (voice->name)
  174. data->name = strdup(voice->name);
  175. if (voice->languages)
  176. data->languages = strdup(voice->languages);
  177. if (voice->identifier)
  178. data->identifier = strdup(voice->identifier);
  179. return a_command;
  180. }
  181. int delete_espeak_command(t_espeak_command *the_command)
  182. {
  183. int a_status = 0;
  184. if (the_command) {
  185. switch (the_command->type)
  186. {
  187. case ET_TEXT:
  188. if (the_command->u.my_text.text)
  189. free(the_command->u.my_text.text);
  190. break;
  191. case ET_MARK:
  192. if (the_command->u.my_mark.text)
  193. free(the_command->u.my_mark.text);
  194. if (the_command->u.my_mark.index_mark)
  195. free((void *)(the_command->u.my_mark.index_mark));
  196. break;
  197. case ET_TERMINATED_MSG:
  198. {
  199. // if the terminated msg is pending,
  200. // it must be processed here for informing the calling program
  201. // that its message is finished.
  202. // This can be important for cleaning the related user data.
  203. t_espeak_terminated_msg *data = &(the_command->u.my_terminated_msg);
  204. if (the_command->state == CS_PENDING) {
  205. the_command->state = CS_PROCESSED;
  206. sync_espeak_terminated_msg(data->unique_identifier, data->user_data);
  207. }
  208. }
  209. break;
  210. case ET_KEY:
  211. if (the_command->u.my_key.key_name)
  212. free((void *)(the_command->u.my_key.key_name));
  213. break;
  214. case ET_CHAR:
  215. case ET_PARAMETER:
  216. // No allocation
  217. break;
  218. case ET_PUNCTUATION_LIST:
  219. if (the_command->u.my_punctuation_list)
  220. free((void *)(the_command->u.my_punctuation_list));
  221. break;
  222. case ET_VOICE_NAME:
  223. if (the_command->u.my_voice_name)
  224. free((void *)(the_command->u.my_voice_name));
  225. break;
  226. case ET_VOICE_SPEC:
  227. {
  228. espeak_VOICE *data = &(the_command->u.my_voice_spec);
  229. if (data->name)
  230. free((void *)data->name);
  231. if (data->languages)
  232. free((void *)data->languages);
  233. if (data->identifier)
  234. free((void *)data->identifier);
  235. }
  236. break;
  237. default:
  238. assert(0);
  239. }
  240. free(the_command);
  241. a_status = 1;
  242. }
  243. return a_status;
  244. }
  245. void process_espeak_command(t_espeak_command *the_command)
  246. {
  247. if (the_command == NULL)
  248. return;
  249. the_command->state = CS_PROCESSED;
  250. switch (the_command->type)
  251. {
  252. case ET_TEXT:
  253. {
  254. t_espeak_text *data = &(the_command->u.my_text);
  255. sync_espeak_Synth(data->unique_identifier, data->text,
  256. data->position, data->position_type,
  257. data->end_position, data->flags, data->user_data);
  258. }
  259. break;
  260. case ET_MARK:
  261. {
  262. t_espeak_mark *data = &(the_command->u.my_mark);
  263. sync_espeak_Synth_Mark(data->unique_identifier, data->text,
  264. data->index_mark, data->end_position, data->flags,
  265. data->user_data);
  266. }
  267. break;
  268. case ET_TERMINATED_MSG:
  269. {
  270. t_espeak_terminated_msg *data = &(the_command->u.my_terminated_msg);
  271. sync_espeak_terminated_msg(data->unique_identifier, data->user_data);
  272. }
  273. break;
  274. case ET_KEY:
  275. {
  276. const char *data = the_command->u.my_key.key_name;
  277. sync_espeak_Key(data);
  278. }
  279. break;
  280. case ET_CHAR:
  281. {
  282. const wchar_t data = the_command->u.my_char.character;
  283. sync_espeak_Char(data);
  284. }
  285. break;
  286. case ET_PARAMETER:
  287. {
  288. t_espeak_parameter *data = &(the_command->u.my_param);
  289. SetParameter(data->parameter, data->value, data->relative);
  290. }
  291. break;
  292. case ET_PUNCTUATION_LIST:
  293. {
  294. const wchar_t *data = the_command->u.my_punctuation_list;
  295. sync_espeak_SetPunctuationList(data);
  296. }
  297. break;
  298. case ET_VOICE_NAME:
  299. {
  300. const char *data = the_command->u.my_voice_name;
  301. espeak_SetVoiceByName(data);
  302. }
  303. break;
  304. case ET_VOICE_SPEC:
  305. {
  306. espeak_VOICE *data = &(the_command->u.my_voice_spec);
  307. espeak_SetVoiceByProperties(data);
  308. }
  309. break;
  310. default:
  311. assert(0);
  312. break;
  313. }
  314. }