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

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