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.

espeak-phoneme-data.c 8.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <sys/types.h>
  5. #if defined(BYTE_ORDER) && BYTE_ORDER == BIG_ENDIAN
  6. #define IS_BIG_ENDIAN 1
  7. #else
  8. #define IS_BIG_ENDIAN 0
  9. #endif
  10. #if IS_BIG_ENDIAN
  11. # define SWAP_USHORT(val) ((unsigned short) ( \
  12. (unsigned short) ((unsigned short) (val) >> 8) | \
  13. (unsigned short) ((unsigned short) (val) << 8)))
  14. # define SWAP_UINT(val) ((unsigned int) ( \
  15. (((unsigned int) (val) & (unsigned int) 0x000000ffU) << 24) | \
  16. (((unsigned int) (val) & (unsigned int) 0x0000ff00U) << 8) | \
  17. (((unsigned int) (val) & (unsigned int) 0x00ff0000U) >> 8) | \
  18. (((unsigned int) (val) & (unsigned int) 0xff000000U) >> 24)))
  19. #else
  20. # define SWAP_USHORT(val) (val)
  21. # define SWAP_UINT(val) (val)
  22. #endif
  23. #define N_PHONEME_TAB_NAME 32
  24. typedef struct {
  25. unsigned int mnemonic;
  26. unsigned int phflags;
  27. unsigned short std_length;
  28. unsigned short spect;
  29. unsigned short before;
  30. unsigned short after;
  31. unsigned char code;
  32. unsigned char type;
  33. unsigned char start_type;
  34. unsigned char end_type;
  35. unsigned char length_mod;
  36. unsigned char reduce_to;
  37. unsigned char alternative_ph;
  38. unsigned char link_out;
  39. } PHONEME_TAB;
  40. typedef struct {
  41. short frflags;
  42. unsigned char length;
  43. unsigned char rms;
  44. short ffreq[9];
  45. unsigned char fheight[9];
  46. unsigned char fwidth[6];
  47. unsigned char fright[6];
  48. } frame_t;
  49. #define N_SEQ_FRAMES 25
  50. typedef struct {
  51. short length;
  52. unsigned char n_frames;
  53. unsigned char flags;
  54. frame_t frame[N_SEQ_FRAMES];
  55. } SPECT_SEQ;
  56. void swap_phondata (const char *infile, const char *outfile,
  57. const char *manifest);
  58. void swap_phonindex (const char *infile, const char *outfile);
  59. void swap_phontab (const char *infile, const char *outfile);
  60. void usage (const char *program_name);
  61. int
  62. main (int argc, char *argv[])
  63. {
  64. const char *indir = "/usr/share/espeak-data";
  65. const char *outdir = ".";
  66. const char *manifest = "phondata-manifest";
  67. char *f1, *f2;
  68. if (argc > 4)
  69. usage (argv[0]);
  70. if (argc > 1) {
  71. if (strcmp (argv[1], "-h") == 0 ||
  72. strcmp (argv[1], "--help") == 0)
  73. usage (argv[0]);
  74. indir = argv[1];
  75. }
  76. if (argc > 2)
  77. outdir = argv[2];
  78. if (argc > 3)
  79. manifest = argv[3];
  80. f1 = (char *) malloc (strlen (indir) + 20);
  81. if (f1 == NULL) {
  82. fprintf (stderr, "Unable to allocate memory\n");
  83. exit (1);
  84. }
  85. f2 = (char *) malloc (strlen (outdir) + 20);
  86. if (f2 == NULL) {
  87. fprintf (stderr, "Unable to allocate memory\n");
  88. exit (1);
  89. }
  90. #if IS_BIG_ENDIAN
  91. printf ("Host seems to be big-endian ..\n");
  92. #else
  93. printf ("Host seems to be little-endian ..\n");
  94. #endif
  95. sprintf (f1, "%s/phontab", indir);
  96. sprintf (f2, "%s/temp_1", outdir);
  97. printf ("Processing phontab ..\n");
  98. swap_phontab (f1, f2);
  99. sprintf (f1, "%s/phontab", outdir);
  100. rename (f2, f1);
  101. sprintf (f1, "%s/phonindex", indir);
  102. sprintf (f2, "%s/temp_1", outdir);
  103. printf ("Processing phonindex ..\n");
  104. swap_phonindex (f1, f2);
  105. sprintf (f1, "%s/phonindex", outdir);
  106. rename (f2, f1);
  107. sprintf (f1, "%s/phondata", indir);
  108. sprintf (f2, "%s/temp_1", outdir);
  109. printf ("Processing phondata ..\n");
  110. swap_phondata (f1, f2, manifest);
  111. sprintf (f1, "%s/phondata", outdir);
  112. rename (f2, f1);
  113. free (f1);
  114. free (f2);
  115. printf ("Done.\n");
  116. return 0;
  117. }
  118. void
  119. swap_phondata (const char *infile, const char *outfile,
  120. const char *manifest)
  121. {
  122. FILE *in, *mfest, *out;
  123. char line[1024];
  124. unsigned char buf_4[4];
  125. in = fopen (infile, "rb");
  126. if (in == NULL) {
  127. fprintf (stderr, "Unable to read from file %s\n", infile);
  128. exit (1);
  129. }
  130. mfest = fopen (manifest, "rb");
  131. if (mfest == NULL) {
  132. fprintf (stderr, "Unable to read from file %s\n", manifest);
  133. exit (1);
  134. }
  135. out = fopen (outfile, "wb");
  136. if (out == NULL) {
  137. fprintf (stderr, "Unable to open file %s for writing\n", outfile);
  138. exit (1);
  139. }
  140. fread (buf_4, 4, 1, in);
  141. fwrite (buf_4, 4, 1, out);
  142. while (fgets (line, 1024, mfest)) {
  143. if (line[0] == 'S') {
  144. SPECT_SEQ buf_spect;
  145. size_t ix;
  146. int n;
  147. fread (&buf_spect.length, 2, 1, in);
  148. fread (&buf_spect.n_frames, 1, 1, in);
  149. fseek (in, -3, SEEK_CUR);
  150. ix = (char *)(&buf_spect.frame[buf_spect.n_frames]) -
  151. (char *)(&buf_spect);
  152. ix = (ix+3) & 0xfffc;
  153. fread (&buf_spect, ix, 1, in);
  154. buf_spect.length = (short) SWAP_USHORT (buf_spect.length);
  155. for (n = 0; n < buf_spect.n_frames; n++) {
  156. int k;
  157. buf_spect.frame[n].frflags = (short)
  158. SWAP_USHORT (buf_spect.frame[n].frflags);
  159. for (k = 0; k < 9; k++) {
  160. buf_spect.frame[n].ffreq[k] = (short)
  161. SWAP_USHORT (buf_spect.frame[n].ffreq[k]);
  162. }
  163. }
  164. fwrite (&buf_spect, ix, 1, out);
  165. }
  166. else if (line[0] == 'W') {
  167. long pos;
  168. int length;
  169. char *wave_data;
  170. fread (buf_4, 4, 1, in);
  171. fwrite (buf_4, 4, 1, out);
  172. length = buf_4[1] * 256 + buf_4[0];
  173. wave_data = (char *) malloc (length);
  174. if (wave_data == NULL) {
  175. fprintf (stderr, "Memory allocation error\n");
  176. exit (1);
  177. }
  178. fread (wave_data, 1, length, in);
  179. fwrite (wave_data, 1, length, out);
  180. pos = ftell (in);
  181. while((pos & 3) != 0) {
  182. fgetc (in);
  183. pos++;
  184. }
  185. pos = ftell (out);
  186. while((pos & 3) != 0) {
  187. fputc (0, out);
  188. pos++;
  189. }
  190. free (wave_data);
  191. }
  192. else if (line[0] == 'E') {
  193. char env_buf[128];
  194. fread (env_buf, 1, 128, in);
  195. fwrite (env_buf, 1, 128, out);
  196. }
  197. }
  198. fclose (in);
  199. fclose (out);
  200. fclose (mfest);
  201. }
  202. void
  203. swap_phonindex (const char *infile, const char *outfile)
  204. {
  205. FILE *in, *out;
  206. unsigned int val;
  207. in = fopen (infile, "rb");
  208. if (in == NULL) {
  209. fprintf (stderr, "Unable to read from file %s\n", infile);
  210. exit (1);
  211. }
  212. out = fopen (outfile, "wb");
  213. if (out == NULL) {
  214. fprintf (stderr, "Unable to open file %s for writing\n", outfile);
  215. exit (1);
  216. }
  217. while (! feof (in)) {
  218. size_t n;
  219. n = fread (&val, 4, 1, in);
  220. if (n != 1)
  221. break;
  222. val = SWAP_UINT (val);
  223. fwrite (&val, 4, 1, out);
  224. }
  225. fclose (in);
  226. fclose (out);
  227. }
  228. void
  229. swap_phontab (const char *infile, const char *outfile)
  230. {
  231. FILE *in, *out;
  232. char buf_4[4];
  233. int i, n_phoneme_tables;
  234. in = fopen (infile, "rb");
  235. if (in == NULL) {
  236. fprintf (stderr, "Unable to read from file %s\n", infile);
  237. exit (1);
  238. }
  239. out = fopen (outfile, "wb");
  240. if (out == NULL) {
  241. fprintf (stderr, "Unable to open file %s for writing\n", outfile);
  242. exit (1);
  243. }
  244. fread (buf_4, 4, 1, in);
  245. fwrite (buf_4, 4, 1, out);
  246. n_phoneme_tables = buf_4[0];
  247. for (i = 0; i < n_phoneme_tables; i++) {
  248. int n_phonemes, j;
  249. char tab_name[N_PHONEME_TAB_NAME];
  250. fread (buf_4, 4, 1, in);
  251. fwrite (buf_4, 4, 1, out);
  252. n_phonemes = buf_4[0];
  253. fread (tab_name, N_PHONEME_TAB_NAME, 1, in);
  254. fwrite (tab_name, N_PHONEME_TAB_NAME, 1, out);
  255. for (j = 0; j < n_phonemes; j++) {
  256. PHONEME_TAB table;
  257. fread (&table, sizeof (PHONEME_TAB), 1, in);
  258. table.mnemonic = SWAP_UINT (table.mnemonic);
  259. table.phflags = SWAP_UINT (table.phflags);
  260. table.std_length = SWAP_USHORT (table.std_length);
  261. table.spect = SWAP_USHORT (table.spect);
  262. table.before = SWAP_USHORT (table.before);
  263. table.after = SWAP_USHORT (table.after);
  264. fwrite (&table, sizeof (PHONEME_TAB), 1, out);
  265. }
  266. }
  267. fclose (in);
  268. fclose (out);
  269. }
  270. void
  271. usage (const char *program_name)
  272. {
  273. fprintf (stderr,
  274. "This program copies the phontab, phonindex and phondata files from a given\n"
  275. "directory, swapping values to big-endian form if necessary.\n\n"
  276. "Usage:\n"
  277. " %s [INPUT_DIR] [OUTPUT_DIR] [MANIFEST_FILE]\n\n"
  278. "By default, the MANIFEST_FILE used is a file called 'phondata-manifest' in\n"
  279. "the current directory. The default INPUT_DIR is /usr/share/espeak-data and\n"
  280. "OUTPUT_DIR is the current directory.\n", program_name);
  281. exit (1);
  282. }