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.

langopts.c 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. * Copyright (C) 2005 to 2015 by Jonathan Duddington
  3. * email: [email protected]
  4. * Copyright (C) 2015-2017 Reece H. Dunn
  5. * Copyright (C) 2022 Juho Hiltunen
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, see: <http://www.gnu.org/licenses/>.
  19. */
  20. #include "config.h"
  21. #include <ctype.h>
  22. #include <errno.h>
  23. #include <stdint.h>
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <espeak-ng/espeak_ng.h>
  28. #include <espeak-ng/speak_lib.h>
  29. #include <espeak-ng/encoding.h>
  30. #include "langopts.h"
  31. #include "mnemonics.h" // for MNEM_TAB
  32. #include "translate.h" // for Translator
  33. #include "soundicon.h" // for soundicon_tab, n_soundicon_tab
  34. #include "speech.h" // for path_home, PATHSEP
  35. #include "synthdata.h" // for n_tunes, tunes
  36. #include "voice.h" // for ReadNumbers, Read8Numbers, ...
  37. static int CheckTranslator(Translator *tr, const MNEM_TAB *keyword_tab, int key);
  38. static int LookupTune(const char *name);
  39. extern const MNEM_TAB langopts_tab[];
  40. void LoadLanguageOptions(Translator *translator, int key, char *keyValue ) {
  41. if (CheckTranslator(translator, langopts_tab, key) != 0) {
  42. return;
  43. }
  44. int ix;
  45. int n;
  46. switch (key) {
  47. case V_DICTMIN: {
  48. if (sscanf(keyValue, "%d", &n) == 1)
  49. translator->dict_min_size = n;
  50. break;
  51. }
  52. case V_DICTRULES: { // conditional dictionary rules and list entries
  53. ReadNumbers(keyValue, &translator->dict_condition, 32, langopts_tab, key);
  54. break;
  55. }
  56. case V_INTONATION: {
  57. sscanf(keyValue, "%d", &option_tone_flags);
  58. if ((option_tone_flags & 0xff) != 0) {
  59. translator->langopts.intonation_group = option_tone_flags & 0xff;
  60. }
  61. break;
  62. }
  63. case V_NUMBERS: {
  64. // expect a list of numbers
  65. while (*keyValue != 0) {
  66. while (isspace(*keyValue)) keyValue++;
  67. if ((n = atoi(keyValue)) > 0) {
  68. keyValue++;
  69. if (n < 32) {
  70. translator->langopts.numbers |= (1 << n);
  71. } else {
  72. if (n < 64)
  73. translator->langopts.numbers2 |= (1 << (n-32));
  74. else
  75. fprintf(stderr, "numbers: Bad option number %d\n", n); }
  76. }
  77. while (isalnum(*keyValue)) keyValue++;
  78. }
  79. ProcessLanguageOptions(&(translator->langopts));
  80. break;
  81. }
  82. case V_LOWERCASE_SENTENCE: {
  83. translator->langopts.lowercase_sentence = true;
  84. break;
  85. }
  86. case V_SPELLINGSTRESS: {
  87. translator->langopts.spelling_stress = true;
  88. break;
  89. }
  90. case V_STRESSADD: { // stressAdd
  91. int stress_add_set = 0;
  92. int stress_add[8];
  93. stress_add_set = Read8Numbers(keyValue, stress_add);
  94. for (ix = 0; ix < stress_add_set; ix++) {
  95. translator->stress_lengths[ix] += stress_add[ix];
  96. }
  97. break;
  98. }
  99. case V_STRESSAMP: {
  100. int stress_amps_set = 0;
  101. int stress_amps[8];
  102. stress_amps_set = Read8Numbers(keyValue, stress_amps);
  103. for (ix = 0; ix < stress_amps_set; ix++) {
  104. translator->stress_amps[ix] = stress_amps[ix];
  105. }
  106. break;
  107. }
  108. case V_STRESSLENGTH: {
  109. //printf("parsing: %s", keyValue);
  110. int stress_lengths_set = 0;
  111. int stress_lengths[8];
  112. stress_lengths_set = Read8Numbers(keyValue, stress_lengths);
  113. for (ix = 0; ix < stress_lengths_set; ix++) {
  114. translator->stress_lengths[ix] = stress_lengths[ix];
  115. }
  116. break;
  117. }
  118. case V_STRESSOPT: {
  119. ReadNumbers(keyValue, &translator->langopts.stress_flags, 32, langopts_tab, key);
  120. break;
  121. }
  122. case V_STRESSRULE: {
  123. sscanf(keyValue, "%d %d %d", &translator->langopts.stress_rule,
  124. &translator->langopts.unstressed_wd1,
  125. &translator->langopts.unstressed_wd2);
  126. break;
  127. }
  128. case V_TUNES: {
  129. char names[6][40] = { 0, 0, 0, 0, 0, 0 };
  130. n = sscanf(keyValue, "%s %s %s %s %s %s", names[0], names[1], names[2], names[3], names[4], names[5]);
  131. translator->langopts.intonation_group = 0;
  132. int value;
  133. for (ix = 0; ix < n; ix++) {
  134. if (strcmp(names[ix], "NULL") == 0)
  135. continue;
  136. if ((value = LookupTune(names[ix])) < 0)
  137. fprintf(stderr, "Unknown tune '%s'\n", names[ix]);
  138. else
  139. translator->langopts.tunes[ix] = value;
  140. }
  141. break;
  142. }
  143. case V_WORDGAP: {
  144. sscanf(keyValue, "%d %d", &translator->langopts.word_gap, &translator->langopts.vowel_pause);
  145. break;
  146. }
  147. default: {
  148. if ((key & 0xff00) == 0x100) {
  149. sscanf(keyValue, "%d", &translator->langopts.param[key &0xff]);
  150. }
  151. break;
  152. }
  153. }
  154. }
  155. void LoadConfig(void) {
  156. // Load configuration file, if one exists
  157. char buf[sizeof(path_home)+10];
  158. FILE *f;
  159. int ix;
  160. char c1;
  161. char string[200];
  162. sprintf(buf, "%s%c%s", path_home, PATHSEP, "config");
  163. if ((f = fopen(buf, "r")) == NULL)
  164. return;
  165. while (fgets(buf, sizeof(buf), f) != NULL) {
  166. if (buf[0] == '/') continue;
  167. if (memcmp(buf, "tone", 4) == 0)
  168. ReadTonePoints(&buf[5], tone_points);
  169. else if (memcmp(buf, "soundicon", 9) == 0) {
  170. ix = sscanf(&buf[10], "_%c %s", &c1, string);
  171. if (ix == 2) {
  172. // add sound file information to soundicon array
  173. // the file will be loaded to memory by LoadSoundFile2()
  174. soundicon_tab[n_soundicon_tab].name = c1;
  175. soundicon_tab[n_soundicon_tab].filename = strdup(string);
  176. soundicon_tab[n_soundicon_tab++].length = 0;
  177. }
  178. }
  179. }
  180. fclose(f);
  181. }
  182. static int LookupTune(const char *name) {
  183. int ix;
  184. for (ix = 0; ix < n_tunes; ix++) {
  185. if (strcmp(name, tunes[ix].name) == 0)
  186. return ix;
  187. }
  188. return -1;
  189. }
  190. int CheckTranslator(Translator *tr, const MNEM_TAB *keyword_tab, int key)
  191. {
  192. // Return 0 if translator is set.
  193. // Return 1 and print an error message for specified key if not
  194. // used for parsing language options
  195. if (tr)
  196. return 0;
  197. fprintf(stderr, "Cannot set %s: language not set, or is invalid.\n", LookupMnemName(keyword_tab, key));
  198. return 1;
  199. }