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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 "synthdata.h" // for n_tunes, tunes
  34. #include "voice.h" // for ReadNumbers, Read8Numbers, ...
  35. static int CheckTranslator(Translator *tr, const MNEM_TAB *keyword_tab, int key);
  36. static int LookupTune(const char *name);
  37. extern const MNEM_TAB langopts_tab[];
  38. void LoadLanguageOptions(Translator *translator, int key, char *keyValue ) {
  39. if (CheckTranslator(translator, langopts_tab, key) != 0) {
  40. return;
  41. }
  42. int ix;
  43. int n;
  44. switch (key) {
  45. case V_DICTMIN: {
  46. if (sscanf(keyValue, "%d", &n) == 1)
  47. translator->dict_min_size = n;
  48. break;
  49. }
  50. case V_DICTRULES: { // conditional dictionary rules and list entries
  51. ReadNumbers(keyValue, &translator->dict_condition, 32, langopts_tab, key);
  52. break;
  53. }
  54. case V_INTONATION: {
  55. sscanf(keyValue, "%d", &option_tone_flags);
  56. if ((option_tone_flags & 0xff) != 0) {
  57. translator->langopts.intonation_group = option_tone_flags & 0xff;
  58. }
  59. break;
  60. }
  61. case V_NUMBERS: {
  62. // expect a list of numbers
  63. while (*keyValue != 0) {
  64. while (isspace(*keyValue)) keyValue++;
  65. if ((n = atoi(keyValue)) > 0) {
  66. keyValue++;
  67. if (n < 32) {
  68. translator->langopts.numbers |= (1 << n);
  69. } else {
  70. if (n < 64)
  71. translator->langopts.numbers2 |= (1 << (n-32));
  72. else
  73. fprintf(stderr, "numbers: Bad option number %d\n", n); }
  74. }
  75. while (isalnum(*keyValue)) keyValue++;
  76. }
  77. ProcessLanguageOptions(&(translator->langopts));
  78. break;
  79. }
  80. case V_LOWERCASE_SENTENCE: {
  81. translator->langopts.lowercase_sentence = true;
  82. break;
  83. }
  84. case V_STRESSADD: { // stressAdd
  85. int stress_add_set = 0;
  86. int stress_add[8];
  87. stress_add_set = Read8Numbers(keyValue, stress_add);
  88. for (ix = 0; ix < stress_add_set; ix++) {
  89. translator->stress_lengths[ix] += stress_add[ix];
  90. }
  91. break;
  92. }
  93. case V_STRESSAMP: {
  94. int stress_amps_set = 0;
  95. int stress_amps[8];
  96. stress_amps_set = Read8Numbers(keyValue, stress_amps);
  97. for (ix = 0; ix < stress_amps_set; ix++) {
  98. translator->stress_amps[ix] = stress_amps[ix];
  99. }
  100. break;
  101. }
  102. case V_STRESSLENGTH: {
  103. //printf("parsing: %s", keyValue);
  104. int stress_lengths_set = 0;
  105. int stress_lengths[8];
  106. stress_lengths_set = Read8Numbers(keyValue, stress_lengths);
  107. for (ix = 0; ix < stress_lengths_set; ix++) {
  108. translator->stress_lengths[ix] = stress_lengths[ix];
  109. }
  110. break;
  111. }
  112. case V_STRESSOPT: {
  113. ReadNumbers(keyValue, &translator->langopts.stress_flags, 32, langopts_tab, key);
  114. break;
  115. }
  116. case V_STRESSRULE: {
  117. sscanf(keyValue, "%d %d %d", &translator->langopts.stress_rule,
  118. &translator->langopts.unstressed_wd1,
  119. &translator->langopts.unstressed_wd2);
  120. break;
  121. }
  122. case V_TUNES: {
  123. char names[6][40] = { 0, 0, 0, 0, 0, 0 };
  124. n = sscanf(keyValue, "%s %s %s %s %s %s", names[0], names[1], names[2], names[3], names[4], names[5]);
  125. translator->langopts.intonation_group = 0;
  126. for (ix = 0; ix < n; ix++) {
  127. if (strcmp(names[ix], "NULL") == 0)
  128. continue;
  129. if ((n = LookupTune(names[ix])) < 0)
  130. fprintf(stderr, "Unknown tune '%s'\n", names[ix]);
  131. else
  132. translator->langopts.tunes[ix] = n;
  133. }
  134. break;
  135. }
  136. case V_WORDGAP: {
  137. sscanf(keyValue, "%d %d", &translator->langopts.word_gap, &translator->langopts.vowel_pause);
  138. break;
  139. }
  140. case V_MAINTAINER:
  141. case V_STATUS:
  142. break;
  143. default: {
  144. if ((key & 0xff00) == 0x100) {
  145. sscanf(keyValue, "%d", &translator->langopts.param[key &0xff]);
  146. }
  147. break;
  148. }
  149. }
  150. }
  151. static int LookupTune(const char *name) {
  152. int ix;
  153. for (ix = 0; ix < n_tunes; ix++) {
  154. if (strcmp(name, tunes[ix].name) == 0)
  155. return ix;
  156. }
  157. return -1;
  158. }
  159. int CheckTranslator(Translator *tr, const MNEM_TAB *keyword_tab, int key)
  160. {
  161. // Return 0 if translator is set.
  162. // Return 1 and print an error message for specified key if not
  163. // used for parsing language options
  164. if (tr)
  165. return 0;
  166. fprintf(stderr, "Cannot set %s: language not set, or is invalid.\n", LookupMnemName(keyword_tab, key));
  167. return 1;
  168. }