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.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <stdint.h>
  25. #include <espeak-ng/espeak_ng.h>
  26. #include <espeak/speak_lib.h>
  27. #include "error.h"
  28. #include "phoneme.h"
  29. #include "speech.h"
  30. #include "synthesize.h"
  31. static const char *basename(const char *filename)
  32. {
  33. const char *current = filename + strlen(filename);
  34. while (current != filename && !(*current == '/' || *current == '\\'))
  35. --current;
  36. return current == filename ? current : current + 1;
  37. }
  38. static unsigned int StringToWord(const char *string)
  39. {
  40. // Pack 4 characters into a word
  41. int ix;
  42. unsigned char c;
  43. unsigned int word;
  44. if (string == NULL)
  45. return 0;
  46. word = 0;
  47. for (ix = 0; ix < 4; ix++) {
  48. if (string[ix] == 0) break;
  49. c = string[ix];
  50. word |= (c << (ix*8));
  51. }
  52. return word;
  53. }
  54. #pragma GCC visibility push(default)
  55. espeak_ng_STATUS espeak_ng_CompileMbrolaVoice(const char *filepath, FILE *log, espeak_ng_ERROR_CONTEXT *context)
  56. {
  57. if (!log) log = stderr;
  58. char *p;
  59. FILE *f_in;
  60. FILE *f_out;
  61. int percent;
  62. int n;
  63. int *pw;
  64. int *pw_end;
  65. int count = 0;
  66. int control;
  67. char phoneme[40];
  68. char phoneme2[40];
  69. char name1[40];
  70. char name2[40];
  71. char mbrola_voice[40];
  72. char buf[sizeof(path_home)+30];
  73. int mbrola_ctrl = 20; // volume in 1/16 ths
  74. MBROLA_TAB data[N_PHONEME_TAB];
  75. strcpy(buf, filepath);
  76. if ((f_in = fopen(buf, "r")) == NULL)
  77. return create_file_error_context(context, errno, buf);
  78. while (fgets(buf, sizeof(phoneme), f_in) != NULL) {
  79. buf[sizeof(phoneme)-1] = 0;
  80. if ((p = strstr(buf, "//")) != NULL)
  81. *p = 0; // truncate line at comment
  82. if (memcmp(buf, "volume", 6) == 0) {
  83. mbrola_ctrl = atoi(&buf[6]);
  84. continue;
  85. }
  86. n = sscanf(buf, "%d %s %s %d %s %s", &control, phoneme, phoneme2, &percent, name1, name2);
  87. if (n >= 5) {
  88. data[count].name = StringToWord(phoneme);
  89. if (strcmp(phoneme2, "NULL") == 0)
  90. data[count].next_phoneme = 0;
  91. else if (strcmp(phoneme2, "VWL") == 0)
  92. data[count].next_phoneme = 2;
  93. else
  94. data[count].next_phoneme = StringToWord(phoneme2);
  95. data[count].mbr_name = 0;
  96. data[count].mbr_name2 = 0;
  97. data[count].percent = percent;
  98. data[count].control = control;
  99. if (strcmp(name1, "NULL") != 0)
  100. data[count].mbr_name = StringToWord(name1);
  101. if (n == 6)
  102. data[count].mbr_name2 = StringToWord(name2);
  103. count++;
  104. }
  105. }
  106. fclose(f_in);
  107. strcpy(mbrola_voice, basename(filepath));
  108. sprintf(buf, "%s/mbrola_ph/%s_phtrans", path_home, mbrola_voice);
  109. if ((f_out = fopen(buf, "wb")) == NULL)
  110. return create_file_error_context(context, errno, buf);
  111. data[count].name = 0; // list terminator
  112. Write4Bytes(f_out, mbrola_ctrl);
  113. pw_end = (int *)(&data[count+1]);
  114. for (pw = (int *)data; pw < pw_end; pw++)
  115. Write4Bytes(f_out, *pw);
  116. fclose(f_out);
  117. fprintf(log, "Mbrola translation file: %s -- %d phonemes\n", buf, count);
  118. return ENS_OK;
  119. }
  120. #pragma GCC visibility pop