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.

compilembrola.c 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * Copyright (C) 2005 to 2014 by Jonathan Duddington
  3. * email: [email protected]
  4. * Copyright (C) 2013-2016 Reece H. Dunn
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, see: <http://www.gnu.org/licenses/>.
  18. */
  19. #include "config.h"
  20. #include <errno.h>
  21. #include <stdint.h>
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <espeak-ng/espeak_ng.h>
  26. #include <espeak-ng/speak_lib.h>
  27. #include "mbrola.h"
  28. #include "error.h" // for create_file_error_context
  29. #include "mbrola.h" // for MBROLA_TAB
  30. #include "phoneme.h" // for N_PHONEME_TAB
  31. #include "speech.h" // for path_home
  32. #include "synthesize.h" // for Write4Bytes
  33. static const char *basename(const char *filename)
  34. {
  35. const char *current = filename + strlen(filename);
  36. while (current != filename && !(*current == '/' || *current == '\\'))
  37. --current;
  38. return current == filename ? current : current + 1;
  39. }
  40. static unsigned int StringToWord(const char *string)
  41. {
  42. // Pack 4 characters into a word
  43. int ix;
  44. unsigned char c;
  45. unsigned int word;
  46. if (string == NULL)
  47. return 0;
  48. word = 0;
  49. for (ix = 0; ix < 4; ix++) {
  50. if (string[ix] == 0) break;
  51. c = string[ix];
  52. word |= (c << (ix*8));
  53. }
  54. return word;
  55. }
  56. #pragma GCC visibility push(default)
  57. espeak_ng_STATUS espeak_ng_CompileMbrolaVoice(const char *filepath, FILE *log, espeak_ng_ERROR_CONTEXT *context)
  58. {
  59. if (!log) log = stderr;
  60. char *p;
  61. FILE *f_in;
  62. FILE *f_out;
  63. int percent;
  64. int n;
  65. int *pw;
  66. int *pw_end;
  67. int count = 0;
  68. int control;
  69. char phoneme[40];
  70. char phoneme2[40];
  71. char name1[40];
  72. char name2[40];
  73. char mbrola_voice[40];
  74. char buf[sizeof(path_home)+30];
  75. int mbrola_ctrl = 20; // volume in 1/16 ths
  76. MBROLA_TAB data[N_PHONEME_TAB];
  77. if ((f_in = fopen(filepath, "r")) == NULL)
  78. return create_file_error_context(context, errno, filepath);
  79. while (fgets(buf, sizeof(phoneme), f_in) != NULL) {
  80. buf[sizeof(phoneme)-1] = 0;
  81. if ((p = strstr(buf, "//")) != NULL)
  82. *p = 0; // truncate line at comment
  83. if (strncmp(buf, "volume", 6) == 0) {
  84. mbrola_ctrl = atoi(&buf[6]);
  85. continue;
  86. }
  87. n = sscanf(buf, "%d %s %s %d %s %s", &control, phoneme, phoneme2, &percent, name1, name2);
  88. if (n >= 5) {
  89. data[count].name = StringToWord(phoneme);
  90. if (strcmp(phoneme2, "NULL") == 0)
  91. data[count].next_phoneme = 0;
  92. else if (strcmp(phoneme2, "VWL") == 0)
  93. data[count].next_phoneme = 2;
  94. else
  95. data[count].next_phoneme = StringToWord(phoneme2);
  96. data[count].mbr_name = 0;
  97. data[count].mbr_name2 = 0;
  98. data[count].percent = percent;
  99. data[count].control = control;
  100. if (strcmp(name1, "NULL") != 0)
  101. data[count].mbr_name = StringToWord(name1);
  102. if (n == 6)
  103. data[count].mbr_name2 = StringToWord(name2);
  104. count++;
  105. }
  106. }
  107. fclose(f_in);
  108. strcpy(mbrola_voice, basename(filepath));
  109. sprintf(buf, "%s/mbrola_ph/%s_phtrans", path_home, mbrola_voice);
  110. if ((f_out = fopen(buf, "wb")) == NULL)
  111. return create_file_error_context(context, errno, buf);
  112. memset(&data[count], 0, sizeof(data[count]));
  113. data[count].name = 0; // list terminator
  114. Write4Bytes(f_out, mbrola_ctrl);
  115. pw_end = (int *)(&data[count+1]);
  116. for (pw = (int *)data; pw < pw_end; pw++)
  117. Write4Bytes(f_out, *pw);
  118. fclose(f_out);
  119. fprintf(log, "Mbrola translation file: %s -- %d phonemes\n", buf, count);
  120. return ENS_OK;
  121. }
  122. #pragma GCC visibility pop