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.

readclause.c 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /*
  2. * Copyright (C) 2017 Reece H. Dunn
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write see:
  16. * <http://www.gnu.org/licenses/>.
  17. */
  18. #include "config.h"
  19. #include <assert.h>
  20. #include <errno.h>
  21. #include <stdint.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <stdio.h>
  25. #include <sys/stat.h>
  26. #include <espeak-ng/espeak_ng.h>
  27. #include <espeak-ng/encoding.h>
  28. #include "speech.h"
  29. #include "phoneme.h"
  30. #include "synthesize.h"
  31. #include "translate.h"
  32. // Arguments to ReadClause. Declared here to avoid duplicating them across the
  33. // different test functions.
  34. static char source[N_TR_SOURCE+40]; // extra space for embedded command & voice change info at end
  35. static short charix[N_TR_SOURCE+4];
  36. static int charix_top = 0;
  37. static int tone2;
  38. static char voice_change_name[40];
  39. static int terminator;
  40. static espeak_ng_STATUS
  41. set_text(const char *text, const char *voicename)
  42. {
  43. espeak_ng_STATUS status = espeak_ng_SetVoiceByName(voicename);
  44. if (status != ENS_OK)
  45. return status;
  46. if (p_decoder == NULL)
  47. p_decoder = create_text_decoder();
  48. count_characters = 0;
  49. return text_decoder_decode_string(p_decoder, text, -1, ESPEAKNG_ENCODING_UTF_8);
  50. }
  51. void
  52. test_latin()
  53. {
  54. printf("testing Latin (Latn)\n");
  55. assert(clause_type_from_codepoint('?') == CLAUSE_QUESTION);
  56. assert(clause_type_from_codepoint('!') == CLAUSE_EXCLAMATION);
  57. assert(clause_type_from_codepoint(',') == CLAUSE_COMMA);
  58. assert(clause_type_from_codepoint(':') == CLAUSE_COLON);
  59. assert(clause_type_from_codepoint(';') == CLAUSE_SEMICOLON);
  60. assert(clause_type_from_codepoint(0x00A1) == (CLAUSE_SEMICOLON | CLAUSE_OPTIONAL_SPACE_AFTER));
  61. assert(clause_type_from_codepoint(0x00Bf) == (CLAUSE_SEMICOLON | CLAUSE_OPTIONAL_SPACE_AFTER));
  62. assert(clause_type_from_codepoint(0x2013) == CLAUSE_SEMICOLON);
  63. assert(clause_type_from_codepoint(0x2014) == CLAUSE_SEMICOLON);
  64. assert(clause_type_from_codepoint(0x2026) == (CLAUSE_SEMICOLON | CLAUSE_SPEAK_PUNCTUATION_NAME | CLAUSE_OPTIONAL_SPACE_AFTER));
  65. }
  66. void
  67. test_latin_sentence()
  68. {
  69. printf("testing Latin (Latn) ... sentence\n");
  70. assert(clause_type_from_codepoint('a') == CLAUSE_NONE);
  71. assert(clause_type_from_codepoint('.') == CLAUSE_PERIOD);
  72. short retix[] = {
  73. 0, 2, 3, 4, 5, 6, // Jane
  74. 0, 8, 9, 10, 11, 12, 13, 14, 15, // finished
  75. 0, 17, 18, // #1
  76. 0, 20, 21, // in
  77. 0, 23, 24, 25, // the
  78. 0, 27, 28, 29, 30 }; // race
  79. assert(set_text("Janet finished #1 in the race.", "en") == ENS_OK);
  80. charix_top = 0;
  81. assert(ReadClause(translator, source, charix, &charix_top, N_TR_SOURCE, &tone2, voice_change_name) == (CLAUSE_PERIOD | CLAUSE_DOT_AFTER_LAST_WORD));
  82. assert(!strcmp(source, "Janet finished #1 in the race "));
  83. assert(charix_top == (sizeof(retix)/sizeof(retix[0])) - 1);
  84. assert(!memcmp(charix, retix, sizeof(retix)));
  85. assert(tone2 == 0);
  86. assert(voice_change_name[0] == 0);
  87. charix_top = 0;
  88. assert(ReadClause(translator, source, charix, &charix_top, N_TR_SOURCE, &tone2, voice_change_name) == CLAUSE_EOF);
  89. assert(!strcmp(source, " "));
  90. assert(charix_top == 0);
  91. }
  92. void
  93. test_greek()
  94. {
  95. printf("testing Greek (Grek)\n");
  96. assert(clause_type_from_codepoint(0x037E) == CLAUSE_QUESTION);
  97. assert(clause_type_from_codepoint(0x0387) == CLAUSE_SEMICOLON);
  98. }
  99. void
  100. test_armenian()
  101. {
  102. printf("testing Armenian (Armn)\n");
  103. assert(clause_type_from_codepoint(0x055B) == (CLAUSE_EXCLAMATION | CLAUSE_PUNCTUATION_IN_WORD));
  104. assert(clause_type_from_codepoint(0x055C) == (CLAUSE_EXCLAMATION | CLAUSE_PUNCTUATION_IN_WORD));
  105. assert(clause_type_from_codepoint(0x055D) == CLAUSE_COMMA);
  106. assert(clause_type_from_codepoint(0x055E) == (CLAUSE_QUESTION | CLAUSE_PUNCTUATION_IN_WORD));
  107. assert(clause_type_from_codepoint(0x0589) == (CLAUSE_PERIOD | CLAUSE_OPTIONAL_SPACE_AFTER));
  108. }
  109. void
  110. test_arabic()
  111. {
  112. printf("testing Arabic (Arab)\n");
  113. assert(clause_type_from_codepoint(0x060C) == CLAUSE_COMMA);
  114. assert(clause_type_from_codepoint(0x061B) == CLAUSE_SEMICOLON);
  115. assert(clause_type_from_codepoint(0x061F) == CLAUSE_QUESTION);
  116. assert(clause_type_from_codepoint(0x06D4) == CLAUSE_PERIOD);
  117. }
  118. void
  119. test_devanagari()
  120. {
  121. printf("testing Devanagari (Deva)\n");
  122. assert(clause_type_from_codepoint(0x0964) == (CLAUSE_PERIOD | CLAUSE_OPTIONAL_SPACE_AFTER));
  123. }
  124. void
  125. test_tibetan()
  126. {
  127. printf("testing Tibetan (Tibt)\n");
  128. assert(clause_type_from_codepoint(0x0F0D) == (CLAUSE_PERIOD | CLAUSE_OPTIONAL_SPACE_AFTER));
  129. assert(clause_type_from_codepoint(0x0F0E) == CLAUSE_PARAGRAPH);
  130. }
  131. void
  132. test_sinhala()
  133. {
  134. printf("testing Sinhala (Sinh)\n");
  135. assert(clause_type_from_codepoint(0x0DF4) == (CLAUSE_PERIOD | CLAUSE_OPTIONAL_SPACE_AFTER));
  136. }
  137. void
  138. test_georgian()
  139. {
  140. printf("testing Georgian (Geor)\n");
  141. assert(clause_type_from_codepoint(0x10FB) == CLAUSE_PARAGRAPH);
  142. }
  143. void
  144. test_ethiopic()
  145. {
  146. printf("testing Ethiopic (Ethi)\n");
  147. assert(clause_type_from_codepoint(0x1362) == CLAUSE_PERIOD);
  148. assert(clause_type_from_codepoint(0x1363) == CLAUSE_COMMA);
  149. assert(clause_type_from_codepoint(0x1364) == CLAUSE_SEMICOLON);
  150. assert(clause_type_from_codepoint(0x1365) == CLAUSE_COLON);
  151. assert(clause_type_from_codepoint(0x1366) == CLAUSE_COLON);
  152. assert(clause_type_from_codepoint(0x1367) == CLAUSE_QUESTION);
  153. assert(clause_type_from_codepoint(0x1368) == CLAUSE_PARAGRAPH);
  154. }
  155. void
  156. test_ideographic()
  157. {
  158. printf("testing Ideographic (Hani)\n");
  159. assert(clause_type_from_codepoint(0x3001) == (CLAUSE_COMMA | CLAUSE_OPTIONAL_SPACE_AFTER));
  160. assert(clause_type_from_codepoint(0x3002) == (CLAUSE_PERIOD | CLAUSE_OPTIONAL_SPACE_AFTER));
  161. }
  162. void
  163. test_fullwidth()
  164. {
  165. printf("testing Full Width\n");
  166. assert(clause_type_from_codepoint(0xFF01) == (CLAUSE_EXCLAMATION | CLAUSE_OPTIONAL_SPACE_AFTER));
  167. assert(clause_type_from_codepoint(0xFF0C) == (CLAUSE_COMMA | CLAUSE_OPTIONAL_SPACE_AFTER));
  168. assert(clause_type_from_codepoint(0xFF0E) == (CLAUSE_PERIOD | CLAUSE_OPTIONAL_SPACE_AFTER));
  169. assert(clause_type_from_codepoint(0xFF1A) == (CLAUSE_COLON | CLAUSE_OPTIONAL_SPACE_AFTER));
  170. assert(clause_type_from_codepoint(0xFF1B) == (CLAUSE_SEMICOLON | CLAUSE_OPTIONAL_SPACE_AFTER));
  171. assert(clause_type_from_codepoint(0xFF1F) == (CLAUSE_QUESTION | CLAUSE_OPTIONAL_SPACE_AFTER));
  172. }
  173. void
  174. test_uts51_emoji_character()
  175. {
  176. printf("testing Emoji ... UTS-51 ED-3. emoji character\n");
  177. short retix[] = {
  178. 0, -1, -1,
  179. 2, -1, -1,
  180. 3, -1, -1,
  181. 4, -1, -1, -1,
  182. 5, -1, -1, -1,
  183. 6 };
  184. assert(set_text(
  185. "\xE2\x86\x94" // [2194] left right arrow
  186. "\xE2\x86\x95" // [2195] up down arrow
  187. "\xE2\x9B\x94" // [26D5] no entry
  188. "\xF0\x9F\x90\x8B" // [1F40B] whale
  189. "\xF0\x9F\x90\xAC", // [1F42C] dolphin
  190. "en") == ENS_OK);
  191. charix_top = 0;
  192. assert(ReadClause(translator, source, charix, &charix_top, N_TR_SOURCE, &tone2, voice_change_name) == CLAUSE_EOF);
  193. assert(!strcmp(source,
  194. "\xE2\x86\x94" // [2194] left right arrow
  195. "\xE2\x86\x95" // [2195] up down arrow
  196. "\xE2\x9B\x94" // [26D5] no entry
  197. "\xF0\x9F\x90\x8B" // [1F40B] whale
  198. "\xF0\x9F\x90\xAC" // [1F42C] dolphin
  199. " "));
  200. assert(charix_top == (sizeof(retix)/sizeof(retix[0])) - 1);
  201. assert(!memcmp(charix, retix, sizeof(retix)));
  202. assert(tone2 == 0);
  203. assert(voice_change_name[0] == 0);
  204. }
  205. void
  206. test_uts51_text_presentation_sequence()
  207. {
  208. printf("testing Emoji ... UTS-51 ED-8a. text presentation sequence\n");
  209. short retix[] = {
  210. 0, 2, -1, -1,
  211. 3, 4, -1, -1,
  212. 5, -1, -1, 6, -1, -1,
  213. 7, -1, -1, -1, 8, -1, -1,
  214. 9 };
  215. assert(set_text(
  216. "#\xEF\xB8\x8E" // [0023 FE0E] number sign (text style)
  217. "4\xEF\xB8\x8E" // [0034 FE0E] digit four (text style)
  218. "\xE2\x80\xBC\xEF\xB8\x8E" // [203C FE0E] double exclamation mark (text style)
  219. "\xF0\x9F\x97\x92\xEF\xB8\x8E", // [1F5D2 FE0E] spiral note pad (text style)
  220. "en") == ENS_OK);
  221. charix_top = 0;
  222. assert(ReadClause(translator, source, charix, &charix_top, N_TR_SOURCE, &tone2, voice_change_name) == CLAUSE_EOF);
  223. assert(!strcmp(source,
  224. "#\xEF\xB8\x8E" // [0023 FE0E] number sign (text style)
  225. "4\xEF\xB8\x8E" // [0034 FE0E] digit four (text style)
  226. "\xE2\x80\xBC\xEF\xB8\x8E" // [203C FE0E] double exclamation mark (text style)
  227. "\xF0\x9F\x97\x92\xEF\xB8\x8E" // [1F5D2 FE0E] spiral note pad (text style)
  228. " "));
  229. assert(charix_top == (sizeof(retix)/sizeof(retix[0])) - 1);
  230. assert(!memcmp(charix, retix, sizeof(retix)));
  231. assert(tone2 == 0);
  232. assert(voice_change_name[0] == 0);
  233. }
  234. void
  235. test_uts51_emoji_presentation_sequence()
  236. {
  237. printf("testing Emoji ... UTS-51 ED-9a. emoji presentation sequence\n");
  238. short retix[] = {
  239. 0, 2, -1, -1,
  240. 3, 4, -1, -1,
  241. 5, -1, -1, 6, -1, -1,
  242. 7, -1, -1, -1, 8, -1, -1,
  243. 9 };
  244. assert(set_text(
  245. "#\xEF\xB8\x8F" // [0023 FE0F] number sign (emoji style)
  246. "4\xEF\xB8\x8F" // [0034 FE0F] digit four (emoji style)
  247. "\xE2\x80\xBC\xEF\xB8\x8F" // [203C FE0F] double exclamation mark (emoji style)
  248. "\xF0\x9F\x97\x92\xEF\xB8\x8F", // [1F5D2 FE0F] spiral note pad (emoji style)
  249. "en") == ENS_OK);
  250. charix_top = 0;
  251. assert(ReadClause(translator, source, charix, &charix_top, N_TR_SOURCE, &tone2, voice_change_name) == CLAUSE_EOF);
  252. assert(!strcmp(source,
  253. "#\xEF\xB8\x8F" // [0023 FE0F] number sign (emoji style)
  254. "4\xEF\xB8\x8F" // [0034 FE0F] digit four (emoji style)
  255. "\xE2\x80\xBC\xEF\xB8\x8F" // [203C FE0F] double exclamation mark (emoji style)
  256. "\xF0\x9F\x97\x92\xEF\xB8\x8F" // [1F5D2 FE0F] spiral note pad (emoji style)
  257. " "));
  258. assert(charix_top == (sizeof(retix)/sizeof(retix[0])) - 1);
  259. assert(!memcmp(charix, retix, sizeof(retix)));
  260. assert(tone2 == 0);
  261. assert(voice_change_name[0] == 0);
  262. }
  263. int
  264. main(int argc, char **argv)
  265. {
  266. assert(espeak_Initialize(AUDIO_OUTPUT_SYNCHRONOUS, 0, NULL, espeakINITIALIZE_DONT_EXIT) == 22050);
  267. test_latin();
  268. test_latin_sentence();
  269. test_greek();
  270. test_armenian();
  271. test_arabic();
  272. test_devanagari();
  273. test_tibetan();
  274. test_sinhala();
  275. test_georgian();
  276. test_ethiopic();
  277. test_ideographic();
  278. test_fullwidth();
  279. test_uts51_emoji_character();
  280. test_uts51_text_presentation_sequence();
  281. test_uts51_emoji_presentation_sequence();
  282. assert(espeak_Terminate() == EE_OK);
  283. return EXIT_SUCCESS;
  284. }
  285. // References:
  286. // [UTS-51] Unicode Emoji (http://www.unicode.org/reports/tr51/tr51-12.html) 5.0-12. 2017-05-18