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

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