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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. 0 };
  80. assert(set_text("Janet finished #1 in the race.", "en") == ENS_OK);
  81. charix_top = 0;
  82. assert(ReadClause(translator, source, charix, &charix_top, N_TR_SOURCE, &tone2, voice_change_name) == (CLAUSE_PERIOD | CLAUSE_DOT_AFTER_LAST_WORD));
  83. assert(!strcmp(source, "Janet finished #1 in the race "));
  84. assert(charix_top == (sizeof(retix)/sizeof(retix[0])) - 2);
  85. assert(!memcmp(charix, retix, sizeof(retix)));
  86. assert(tone2 == 0);
  87. assert(voice_change_name[0] == 0);
  88. charix_top = 0;
  89. assert(ReadClause(translator, source, charix, &charix_top, N_TR_SOURCE, &tone2, voice_change_name) == CLAUSE_EOF);
  90. assert(!strcmp(source, " "));
  91. assert(charix_top == 0);
  92. }
  93. void
  94. test_greek()
  95. {
  96. printf("testing Greek (Grek)\n");
  97. assert(clause_type_from_codepoint(0x037E) == CLAUSE_QUESTION);
  98. assert(clause_type_from_codepoint(0x0387) == CLAUSE_SEMICOLON);
  99. }
  100. void
  101. test_armenian()
  102. {
  103. printf("testing Armenian (Armn)\n");
  104. assert(clause_type_from_codepoint(0x055B) == (CLAUSE_EXCLAMATION | CLAUSE_PUNCTUATION_IN_WORD));
  105. assert(clause_type_from_codepoint(0x055C) == (CLAUSE_EXCLAMATION | CLAUSE_PUNCTUATION_IN_WORD));
  106. assert(clause_type_from_codepoint(0x055D) == CLAUSE_COMMA);
  107. assert(clause_type_from_codepoint(0x055E) == (CLAUSE_QUESTION | CLAUSE_PUNCTUATION_IN_WORD));
  108. assert(clause_type_from_codepoint(0x0589) == (CLAUSE_PERIOD | CLAUSE_OPTIONAL_SPACE_AFTER));
  109. }
  110. void
  111. test_arabic()
  112. {
  113. printf("testing Arabic (Arab)\n");
  114. assert(clause_type_from_codepoint(0x060C) == CLAUSE_COMMA);
  115. assert(clause_type_from_codepoint(0x061B) == CLAUSE_SEMICOLON);
  116. assert(clause_type_from_codepoint(0x061F) == CLAUSE_QUESTION);
  117. assert(clause_type_from_codepoint(0x06D4) == CLAUSE_PERIOD);
  118. }
  119. void
  120. test_devanagari()
  121. {
  122. printf("testing Devanagari (Deva)\n");
  123. assert(clause_type_from_codepoint(0x0964) == (CLAUSE_PERIOD | CLAUSE_OPTIONAL_SPACE_AFTER));
  124. }
  125. void
  126. test_tibetan()
  127. {
  128. printf("testing Tibetan (Tibt)\n");
  129. assert(clause_type_from_codepoint(0x0F0D) == (CLAUSE_PERIOD | CLAUSE_OPTIONAL_SPACE_AFTER));
  130. assert(clause_type_from_codepoint(0x0F0E) == CLAUSE_PARAGRAPH);
  131. }
  132. void
  133. test_sinhala()
  134. {
  135. printf("testing Sinhala (Sinh)\n");
  136. assert(clause_type_from_codepoint(0x0DF4) == (CLAUSE_PERIOD | CLAUSE_OPTIONAL_SPACE_AFTER));
  137. }
  138. void
  139. test_georgian()
  140. {
  141. printf("testing Georgian (Geor)\n");
  142. assert(clause_type_from_codepoint(0x10FB) == CLAUSE_PARAGRAPH);
  143. }
  144. void
  145. test_ethiopic()
  146. {
  147. printf("testing Ethiopic (Ethi)\n");
  148. assert(clause_type_from_codepoint(0x1362) == CLAUSE_PERIOD);
  149. assert(clause_type_from_codepoint(0x1363) == CLAUSE_COMMA);
  150. assert(clause_type_from_codepoint(0x1364) == CLAUSE_SEMICOLON);
  151. assert(clause_type_from_codepoint(0x1365) == CLAUSE_COLON);
  152. assert(clause_type_from_codepoint(0x1366) == CLAUSE_COLON);
  153. assert(clause_type_from_codepoint(0x1367) == CLAUSE_QUESTION);
  154. assert(clause_type_from_codepoint(0x1368) == CLAUSE_PARAGRAPH);
  155. }
  156. void
  157. test_ideographic()
  158. {
  159. printf("testing Ideographic (Hani)\n");
  160. assert(clause_type_from_codepoint(0x3001) == (CLAUSE_COMMA | CLAUSE_OPTIONAL_SPACE_AFTER));
  161. assert(clause_type_from_codepoint(0x3002) == (CLAUSE_PERIOD | CLAUSE_OPTIONAL_SPACE_AFTER));
  162. }
  163. void
  164. test_fullwidth()
  165. {
  166. printf("testing Full Width\n");
  167. assert(clause_type_from_codepoint(0xFF01) == (CLAUSE_EXCLAMATION | CLAUSE_OPTIONAL_SPACE_AFTER));
  168. assert(clause_type_from_codepoint(0xFF0C) == (CLAUSE_COMMA | CLAUSE_OPTIONAL_SPACE_AFTER));
  169. assert(clause_type_from_codepoint(0xFF0E) == (CLAUSE_PERIOD | CLAUSE_OPTIONAL_SPACE_AFTER));
  170. assert(clause_type_from_codepoint(0xFF1A) == (CLAUSE_COLON | CLAUSE_OPTIONAL_SPACE_AFTER));
  171. assert(clause_type_from_codepoint(0xFF1B) == (CLAUSE_SEMICOLON | CLAUSE_OPTIONAL_SPACE_AFTER));
  172. assert(clause_type_from_codepoint(0xFF1F) == (CLAUSE_QUESTION | CLAUSE_OPTIONAL_SPACE_AFTER));
  173. }
  174. void
  175. test_uts51_emoji_character()
  176. {
  177. printf("testing Emoji ... UTS-51 ED-3. emoji character\n");
  178. short retix[] = {
  179. 0, -1, -1,
  180. 2, -1, -1,
  181. 3, -1, -1,
  182. 4, -1, -1, -1,
  183. 5, -1, -1, -1,
  184. 6,
  185. 0 };
  186. assert(set_text(
  187. "\xE2\x86\x94" // [2194] left right arrow
  188. "\xE2\x86\x95" // [2195] up down arrow
  189. "\xE2\x9B\x94" // [26D5] no entry
  190. "\xF0\x9F\x90\x8B" // [1F40B] whale
  191. "\xF0\x9F\x90\xAC", // [1F42C] dolphin
  192. "en") == ENS_OK);
  193. charix_top = 0;
  194. assert(ReadClause(translator, source, charix, &charix_top, N_TR_SOURCE, &tone2, voice_change_name) == CLAUSE_EOF);
  195. assert(!strcmp(source,
  196. "\xE2\x86\x94" // [2194] left right arrow
  197. "\xE2\x86\x95" // [2195] up down arrow
  198. "\xE2\x9B\x94" // [26D5] no entry
  199. "\xF0\x9F\x90\x8B" // [1F40B] whale
  200. "\xF0\x9F\x90\xAC" // [1F42C] dolphin
  201. " "));
  202. assert(charix_top == (sizeof(retix)/sizeof(retix[0])) - 2);
  203. assert(!memcmp(charix, retix, sizeof(retix)));
  204. assert(tone2 == 0);
  205. assert(voice_change_name[0] == 0);
  206. }
  207. int
  208. main(int argc, char **argv)
  209. {
  210. assert(espeak_Initialize(AUDIO_OUTPUT_SYNCHRONOUS, 0, NULL, espeakINITIALIZE_DONT_EXIT) == 22050);
  211. test_latin();
  212. test_latin_sentence();
  213. test_greek();
  214. test_armenian();
  215. test_arabic();
  216. test_devanagari();
  217. test_tibetan();
  218. test_sinhala();
  219. test_georgian();
  220. test_ethiopic();
  221. test_ideographic();
  222. test_fullwidth();
  223. test_uts51_emoji_character();
  224. assert(espeak_Terminate() == EE_OK);
  225. return EXIT_SUCCESS;
  226. }
  227. // References:
  228. // [UTS-51] Unicode Emoji (http://www.unicode.org/reports/tr51/tr51-12.html) 5.0-12. 2017-05-18