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

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