api.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  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 <stdlib.h>
  21. #include <string.h>
  22. #include <espeak-ng/espeak_ng.h>
  23. #include <espeak-ng/speak_lib.h>
  24. #include <espeak-ng/encoding.h>
  25. #include "speech.h"
  26. #include "phoneme.h"
  27. #include "voice.h"
  28. #include "synthesize.h"
  29. #include "translate.h"
  30. // region espeak_Initialize
  31. static void
  32. test_espeak_terminate_without_initialize()
  33. {
  34. printf("testing espeak_Terminate without espeak_Initialize\n");
  35. assert(event_list == NULL);
  36. assert(translator == NULL);
  37. assert(p_decoder == NULL);
  38. assert(espeak_Terminate() == EE_OK);
  39. assert(event_list == NULL);
  40. assert(translator == NULL);
  41. assert(p_decoder == NULL);
  42. }
  43. static void
  44. test_espeak_initialize()
  45. {
  46. printf("testing espeak_Initialize\n");
  47. assert(event_list == NULL);
  48. assert(translator == NULL);
  49. assert(p_decoder == NULL);
  50. assert(espeak_Initialize(AUDIO_OUTPUT_PLAYBACK, 0, NULL, 0) == 22050);
  51. assert(event_list != NULL);
  52. assert(translator == NULL);
  53. assert(p_decoder == NULL);
  54. assert(espeak_Terminate() == EE_OK);
  55. assert(event_list == NULL);
  56. assert(translator == NULL);
  57. assert(p_decoder == NULL);
  58. }
  59. // endregion
  60. // region espeak_Synth
  61. static void
  62. test_espeak_synth()
  63. {
  64. printf("testing espeak_Synth\n");
  65. assert(event_list == NULL);
  66. assert(translator == NULL);
  67. assert(p_decoder == NULL);
  68. assert(espeak_Initialize(AUDIO_OUTPUT_RETRIEVAL, 0, NULL, 0) == 22050);
  69. assert(event_list != NULL);
  70. assert(translator == NULL);
  71. assert(p_decoder == NULL);
  72. const char *test = "One two three.";
  73. assert(espeak_Synth(test, strlen(test)+1, 0, POS_CHARACTER, 0, espeakCHARS_AUTO, NULL, NULL) == EE_OK);
  74. assert(translator != NULL);
  75. assert(strcmp(translator->dictionary_name, "en") == 0);
  76. assert(p_decoder != NULL);
  77. assert(espeak_Synchronize() == EE_OK);
  78. assert(translator != NULL);
  79. assert(strcmp(translator->dictionary_name, "en") == 0);
  80. assert(p_decoder != NULL);
  81. assert(espeak_Terminate() == EE_OK);
  82. assert(event_list == NULL);
  83. assert(translator == NULL);
  84. assert(p_decoder == NULL);
  85. }
  86. static void
  87. test_espeak_synth_no_voices(const char *path)
  88. {
  89. printf("testing espeak_Synth in path with no voices\n");
  90. assert(event_list == NULL);
  91. assert(translator == NULL);
  92. assert(p_decoder == NULL);
  93. assert(espeak_Initialize(AUDIO_OUTPUT_RETRIEVAL, 0, path, espeakINITIALIZE_DONT_EXIT) == 22050);
  94. assert(event_list != NULL);
  95. assert(translator == NULL);
  96. assert(p_decoder == NULL);
  97. const char *test = "One two three.";
  98. assert(espeak_Synth(test, strlen(test)+1, 0, POS_CHARACTER, 0, espeakCHARS_AUTO, NULL, NULL) == EE_NOT_FOUND);
  99. assert(translator == NULL);
  100. assert(p_decoder == NULL);
  101. assert(espeak_Synchronize() == EE_OK);
  102. assert(translator == NULL);
  103. assert(p_decoder == NULL);
  104. assert(espeak_Terminate() == EE_OK);
  105. assert(event_list == NULL);
  106. assert(translator == NULL);
  107. assert(p_decoder == NULL);
  108. }
  109. // endregion
  110. // region espeak_ng_Synthesize
  111. static void
  112. test_espeak_ng_synthesize()
  113. {
  114. printf("testing espeak_ng_Synthesize\n");
  115. assert(event_list == NULL);
  116. assert(translator == NULL);
  117. assert(p_decoder == NULL);
  118. assert(espeak_Initialize(AUDIO_OUTPUT_RETRIEVAL, 0, NULL, 0) == 22050);
  119. assert(event_list != NULL);
  120. assert(translator == NULL);
  121. assert(p_decoder == NULL);
  122. const char *test = "One two three.";
  123. assert(espeak_ng_Synthesize(test, strlen(test)+1, 0, POS_CHARACTER, 0, espeakCHARS_AUTO, NULL, NULL) == ENS_OK);
  124. assert(translator != NULL);
  125. assert(strcmp(translator->dictionary_name, "en") == 0);
  126. assert(p_decoder != NULL);
  127. assert(espeak_Synchronize() == EE_OK);
  128. assert(translator != NULL);
  129. assert(strcmp(translator->dictionary_name, "en") == 0);
  130. assert(p_decoder != NULL);
  131. assert(espeak_Terminate() == EE_OK);
  132. assert(event_list == NULL);
  133. assert(translator == NULL);
  134. assert(p_decoder == NULL);
  135. }
  136. static void
  137. test_espeak_ng_synthesize_no_voices(const char *path)
  138. {
  139. printf("testing espeak_ng_Synthesize in path with no voices\n");
  140. assert(event_list == NULL);
  141. assert(translator == NULL);
  142. assert(p_decoder == NULL);
  143. assert(espeak_Initialize(AUDIO_OUTPUT_RETRIEVAL, 0, path, espeakINITIALIZE_DONT_EXIT) == 22050);
  144. assert(event_list != NULL);
  145. assert(translator == NULL);
  146. assert(p_decoder == NULL);
  147. const char *test = "One two three.";
  148. assert(espeak_ng_Synthesize(test, strlen(test)+1, 0, POS_CHARACTER, 0, espeakCHARS_AUTO, NULL, NULL) == ENS_VOICE_NOT_FOUND);
  149. assert(translator == NULL);
  150. assert(p_decoder == NULL);
  151. assert(espeak_Synchronize() == EE_OK);
  152. assert(translator == NULL);
  153. assert(p_decoder == NULL);
  154. assert(espeak_Terminate() == EE_OK);
  155. assert(event_list == NULL);
  156. assert(translator == NULL);
  157. assert(p_decoder == NULL);
  158. }
  159. // endregion
  160. // region espeak_SetVoiceByName
  161. static void
  162. test_espeak_set_voice_by_name_null_voice()
  163. {
  164. printf("testing espeak_SetVoiceByName(NULL)\n");
  165. assert(event_list == NULL);
  166. assert(translator == NULL);
  167. assert(p_decoder == NULL);
  168. assert(espeak_Initialize(AUDIO_OUTPUT_RETRIEVAL, 0, NULL, 0) == 22050);
  169. assert(event_list != NULL);
  170. assert(translator == NULL);
  171. assert(p_decoder == NULL);
  172. assert(espeak_SetVoiceByName("") == EE_NOT_FOUND);
  173. assert(translator == NULL);
  174. assert(p_decoder == NULL);
  175. const char *test = "One two three.";
  176. assert(espeak_Synth(test, strlen(test)+1, 0, POS_CHARACTER, 0, espeakCHARS_AUTO, NULL, NULL) == EE_OK);
  177. assert(translator != NULL);
  178. assert(strcmp(translator->dictionary_name, "en") == 0);
  179. assert(p_decoder != NULL);
  180. assert(espeak_Synchronize() == EE_OK);
  181. assert(translator != NULL);
  182. assert(strcmp(translator->dictionary_name, "en") == 0);
  183. assert(p_decoder != NULL);
  184. assert(espeak_Terminate() == EE_OK);
  185. assert(event_list == NULL);
  186. assert(translator == NULL);
  187. assert(p_decoder == NULL);
  188. }
  189. static void
  190. test_espeak_set_voice_by_name_blank_voice()
  191. {
  192. printf("testing espeak_SetVoiceByName(\"\")\n");
  193. assert(event_list == NULL);
  194. assert(translator == NULL);
  195. assert(p_decoder == NULL);
  196. assert(espeak_Initialize(AUDIO_OUTPUT_RETRIEVAL, 0, NULL, 0) == 22050);
  197. assert(event_list != NULL);
  198. assert(translator == NULL);
  199. assert(p_decoder == NULL);
  200. assert(espeak_SetVoiceByName("") == EE_NOT_FOUND);
  201. assert(translator == NULL);
  202. assert(p_decoder == NULL);
  203. const char *test = "One two three.";
  204. assert(espeak_Synth(test, strlen(test)+1, 0, POS_CHARACTER, 0, espeakCHARS_AUTO, NULL, NULL) == EE_OK);
  205. assert(translator != NULL);
  206. assert(strcmp(translator->dictionary_name, "en") == 0);
  207. assert(p_decoder != NULL);
  208. assert(espeak_Synchronize() == EE_OK);
  209. assert(translator != NULL);
  210. assert(strcmp(translator->dictionary_name, "en") == 0);
  211. assert(p_decoder != NULL);
  212. assert(espeak_Terminate() == EE_OK);
  213. assert(event_list == NULL);
  214. assert(translator == NULL);
  215. assert(p_decoder == NULL);
  216. }
  217. static void
  218. test_espeak_set_voice_by_name_valid_voice()
  219. {
  220. printf("testing espeak_SetVoiceByName(\"de\")\n");
  221. assert(event_list == NULL);
  222. assert(translator == NULL);
  223. assert(p_decoder == NULL);
  224. assert(espeak_Initialize(AUDIO_OUTPUT_RETRIEVAL, 0, NULL, 0) == 22050);
  225. assert(event_list != NULL);
  226. assert(translator == NULL);
  227. assert(p_decoder == NULL);
  228. assert(espeak_SetVoiceByName("de") == EE_OK);
  229. assert(translator != NULL);
  230. assert(strcmp(translator->dictionary_name, "de") == 0);
  231. assert(p_decoder == NULL);
  232. const char *test = "One two three.";
  233. assert(espeak_Synth(test, strlen(test)+1, 0, POS_CHARACTER, 0, espeakCHARS_AUTO, NULL, NULL) == EE_OK);
  234. assert(translator != NULL);
  235. assert(strcmp(translator->dictionary_name, "de") == 0);
  236. assert(p_decoder != NULL);
  237. assert(espeak_Synchronize() == EE_OK);
  238. assert(translator != NULL);
  239. assert(strcmp(translator->dictionary_name, "de") == 0);
  240. assert(p_decoder != NULL);
  241. assert(espeak_Terminate() == EE_OK);
  242. assert(event_list == NULL);
  243. assert(translator == NULL);
  244. assert(p_decoder == NULL);
  245. }
  246. static void
  247. test_espeak_set_voice_by_name_invalid_voice()
  248. {
  249. printf("testing espeak_SetVoiceByName(\"zzz\")\n");
  250. assert(event_list == NULL);
  251. assert(translator == NULL);
  252. assert(p_decoder == NULL);
  253. assert(espeak_Initialize(AUDIO_OUTPUT_RETRIEVAL, 0, NULL, 0) == 22050);
  254. assert(event_list != NULL);
  255. assert(translator == NULL);
  256. assert(p_decoder == NULL);
  257. assert(espeak_SetVoiceByName("zzz") == EE_NOT_FOUND);
  258. assert(translator == NULL);
  259. assert(p_decoder == NULL);
  260. const char *test = "One two three.";
  261. assert(espeak_Synth(test, strlen(test)+1, 0, POS_CHARACTER, 0, espeakCHARS_AUTO, NULL, NULL) == EE_OK);
  262. assert(translator != NULL);
  263. assert(strcmp(translator->dictionary_name, "en") == 0);
  264. assert(p_decoder != NULL);
  265. assert(espeak_Synchronize() == EE_OK);
  266. assert(translator != NULL);
  267. assert(strcmp(translator->dictionary_name, "en") == 0);
  268. assert(p_decoder != NULL);
  269. assert(espeak_Terminate() == EE_OK);
  270. assert(event_list == NULL);
  271. assert(translator == NULL);
  272. assert(p_decoder == NULL);
  273. }
  274. static void
  275. test_espeak_set_voice_by_name_language_variant_intonation_parameter()
  276. {
  277. printf("testing espeak_SetVoiceByName(\"!v/Annie\") (language variant; intonation)\n");
  278. assert(event_list == NULL);
  279. assert(translator == NULL);
  280. assert(p_decoder == NULL);
  281. assert(espeak_Initialize(AUDIO_OUTPUT_RETRIEVAL, 0, NULL, 0) == 22050);
  282. assert(event_list != NULL);
  283. assert(translator == NULL);
  284. assert(p_decoder == NULL);
  285. assert(espeak_SetVoiceByName("!v/Annie") == EE_OK);
  286. assert(translator != NULL);
  287. assert(strcmp(translator->dictionary_name, "en") == 0);
  288. assert(p_decoder == NULL);
  289. const char *test = "One two three.";
  290. assert(espeak_Synth(test, strlen(test)+1, 0, POS_CHARACTER, 0, espeakCHARS_AUTO, NULL, NULL) == EE_OK);
  291. assert(translator != NULL);
  292. assert(strcmp(translator->dictionary_name, "en") == 0);
  293. assert(p_decoder != NULL);
  294. assert(espeak_Synchronize() == EE_OK);
  295. assert(translator != NULL);
  296. assert(strcmp(translator->dictionary_name, "en") == 0);
  297. assert(p_decoder != NULL);
  298. assert(espeak_Terminate() == EE_OK);
  299. assert(event_list == NULL);
  300. assert(translator == NULL);
  301. assert(p_decoder == NULL);
  302. }
  303. // endregion
  304. // region espeak_SetVoiceByProperties
  305. static void
  306. test_espeak_set_voice_by_properties_empty()
  307. {
  308. printf("testing espeak_SetVoiceByProperties: (none)\n");
  309. assert(event_list == NULL);
  310. assert(translator == NULL);
  311. assert(p_decoder == NULL);
  312. assert(espeak_Initialize(AUDIO_OUTPUT_RETRIEVAL, 0, NULL, 0) == 22050);
  313. assert(event_list != NULL);
  314. assert(translator == NULL);
  315. assert(p_decoder == NULL);
  316. espeak_VOICE properties;
  317. memset(&properties, 0, sizeof(properties));
  318. assert(espeak_SetVoiceByProperties(&properties) == EE_OK);
  319. assert(translator != NULL);
  320. assert(strcmp(translator->dictionary_name, "en") == 0);
  321. assert(p_decoder == NULL);
  322. const char *test = "One two three.";
  323. assert(espeak_Synth(test, strlen(test)+1, 0, POS_CHARACTER, 0, espeakCHARS_AUTO, NULL, NULL) == EE_OK);
  324. assert(translator != NULL);
  325. assert(strcmp(translator->dictionary_name, "en") == 0);
  326. assert(p_decoder != NULL);
  327. assert(espeak_Synchronize() == EE_OK);
  328. assert(translator != NULL);
  329. assert(strcmp(translator->dictionary_name, "en") == 0);
  330. assert(p_decoder != NULL);
  331. assert(espeak_Terminate() == EE_OK);
  332. assert(event_list == NULL);
  333. assert(translator == NULL);
  334. assert(p_decoder == NULL);
  335. }
  336. static void
  337. test_espeak_set_voice_by_properties_blank_language()
  338. {
  339. printf("testing espeak_SetVoiceByProperties: languages=\"\"\n");
  340. assert(event_list == NULL);
  341. assert(translator == NULL);
  342. assert(p_decoder == NULL);
  343. assert(espeak_Initialize(AUDIO_OUTPUT_RETRIEVAL, 0, NULL, 0) == 22050);
  344. assert(event_list != NULL);
  345. assert(translator == NULL);
  346. assert(p_decoder == NULL);
  347. espeak_VOICE properties;
  348. memset(&properties, 0, sizeof(properties));
  349. properties.languages = "";
  350. assert(espeak_SetVoiceByProperties(&properties) == EE_OK);
  351. assert(translator != NULL);
  352. assert(strcmp(translator->dictionary_name, "en") == 0);
  353. assert(p_decoder == NULL);
  354. const char *test = "One two three.";
  355. assert(espeak_Synth(test, strlen(test)+1, 0, POS_CHARACTER, 0, espeakCHARS_AUTO, NULL, NULL) == EE_OK);
  356. assert(translator != NULL);
  357. assert(strcmp(translator->dictionary_name, "en") == 0);
  358. assert(p_decoder != NULL);
  359. assert(espeak_Synchronize() == EE_OK);
  360. assert(translator != NULL);
  361. assert(strcmp(translator->dictionary_name, "en") == 0);
  362. assert(p_decoder != NULL);
  363. assert(espeak_Terminate() == EE_OK);
  364. assert(event_list == NULL);
  365. assert(translator == NULL);
  366. assert(p_decoder == NULL);
  367. }
  368. static void
  369. test_espeak_set_voice_by_properties_with_valid_language()
  370. {
  371. printf("testing espeak_SetVoiceByProperties: languages=\"mk\" (valid)\n");
  372. assert(event_list == NULL);
  373. assert(translator == NULL);
  374. assert(p_decoder == NULL);
  375. assert(espeak_Initialize(AUDIO_OUTPUT_RETRIEVAL, 0, NULL, 0) == 22050);
  376. assert(event_list != NULL);
  377. assert(translator == NULL);
  378. assert(p_decoder == NULL);
  379. espeak_VOICE properties;
  380. memset(&properties, 0, sizeof(properties));
  381. properties.languages = "mk";
  382. assert(espeak_SetVoiceByProperties(&properties) == EE_OK);
  383. assert(translator != NULL);
  384. assert(strcmp(translator->dictionary_name, "mk") == 0);
  385. assert(p_decoder == NULL);
  386. const char *test = "One two three.";
  387. assert(espeak_Synth(test, strlen(test)+1, 0, POS_CHARACTER, 0, espeakCHARS_AUTO, NULL, NULL) == EE_OK);
  388. assert(translator != NULL);
  389. assert(strcmp(translator->dictionary_name, "mk") == 0);
  390. assert(p_decoder != NULL);
  391. assert(espeak_Synchronize() == EE_OK);
  392. assert(translator != NULL);
  393. assert(strcmp(translator->dictionary_name, "mk") == 0);
  394. assert(p_decoder != NULL);
  395. assert(espeak_Terminate() == EE_OK);
  396. assert(event_list == NULL);
  397. assert(translator == NULL);
  398. assert(p_decoder == NULL);
  399. }
  400. static void
  401. test_espeak_set_voice_by_properties_with_invalid_language()
  402. {
  403. printf("testing espeak_SetVoiceByProperties: languages=\"zzz\" (invalid)\n");
  404. assert(event_list == NULL);
  405. assert(translator == NULL);
  406. assert(p_decoder == NULL);
  407. assert(espeak_Initialize(AUDIO_OUTPUT_RETRIEVAL, 0, NULL, 0) == 22050);
  408. assert(event_list != NULL);
  409. assert(translator == NULL);
  410. assert(p_decoder == NULL);
  411. espeak_VOICE properties;
  412. memset(&properties, 0, sizeof(properties));
  413. properties.languages = "zzz";
  414. assert(espeak_SetVoiceByProperties(&properties) == EE_NOT_FOUND);
  415. assert(translator == NULL);
  416. assert(p_decoder == NULL);
  417. const char *test = "One two three.";
  418. assert(espeak_Synth(test, strlen(test)+1, 0, POS_CHARACTER, 0, espeakCHARS_AUTO, NULL, NULL) == EE_OK);
  419. assert(translator != NULL);
  420. assert(strcmp(translator->dictionary_name, "en") == 0);
  421. assert(p_decoder != NULL);
  422. assert(espeak_Synchronize() == EE_OK);
  423. assert(translator != NULL);
  424. assert(strcmp(translator->dictionary_name, "en") == 0);
  425. assert(p_decoder != NULL);
  426. assert(espeak_Terminate() == EE_OK);
  427. assert(event_list == NULL);
  428. assert(translator == NULL);
  429. assert(p_decoder == NULL);
  430. }
  431. // endregion
  432. int
  433. main(int argc, char **argv)
  434. {
  435. (void)argc; // unused parameter
  436. char *progdir = strdup(argv[0]);
  437. char *dir = strrchr(progdir, '/');
  438. if (dir != NULL) *dir = 0;
  439. test_espeak_terminate_without_initialize();
  440. test_espeak_initialize();
  441. test_espeak_synth();
  442. test_espeak_synth(); // Check that this does not crash when run a second time.
  443. test_espeak_synth_no_voices(progdir);
  444. test_espeak_synth();
  445. test_espeak_ng_synthesize();
  446. test_espeak_ng_synthesize(); // Check that this does not crash when run a second time.
  447. test_espeak_ng_synthesize_no_voices(progdir);
  448. test_espeak_ng_synthesize();
  449. test_espeak_set_voice_by_name_null_voice();
  450. test_espeak_set_voice_by_name_blank_voice();
  451. test_espeak_set_voice_by_name_valid_voice();
  452. test_espeak_set_voice_by_name_invalid_voice();
  453. test_espeak_set_voice_by_name_language_variant_intonation_parameter();
  454. test_espeak_set_voice_by_properties_empty();
  455. test_espeak_set_voice_by_properties_blank_language();
  456. test_espeak_set_voice_by_properties_with_valid_language();
  457. test_espeak_set_voice_by_properties_with_invalid_language();
  458. free(progdir);
  459. return EXIT_SUCCESS;
  460. }