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

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