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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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. sprintf (f1, "%s/phondata", indir);
  127. sprintf (f2, "%s/temp_1", outdir);
  128. printf ("Processing phondata ..\n");
  129. swap_phondata (f1, f2, manifest);
  130. if(GetFileLength(f1) != GetFileLength(f2))
  131. {
  132. fprintf(stderr, "Error: phondata length is different from the original\n");
  133. exit(1);
  134. }
  135. sprintf (f1, "%s/phondata", outdir);
  136. rename (f2, f1);
  137. sprintf (f1, "%s/phontab", indir);
  138. sprintf (f2, "%s/temp_1", outdir);
  139. printf ("Processing phontab ..\n");
  140. swap_phontab (f1, f2);
  141. sprintf (f1, "%s/phontab", outdir);
  142. rename (f2, f1);
  143. sprintf (f1, "%s/phonindex", indir);
  144. sprintf (f2, "%s/temp_1", outdir);
  145. printf ("Processing phonindex ..\n");
  146. swap_phonindex (f1, f2);
  147. sprintf (f1, "%s/phonindex", outdir);
  148. rename (f2, f1);
  149. free (f1);
  150. free (f2);
  151. printf ("Done.\n");
  152. return 0;
  153. } // end of main
  154. void swap_phondata (const char *infile, const char *outfile,
  155. const char *manifest)
  156. {//==========================================================
  157. FILE *in, *mfest, *out;
  158. int displ;
  159. char line[1024];
  160. unsigned char buf_4[4];
  161. in = fopen (infile, "rb");
  162. if (in == NULL) {
  163. fprintf (stderr, "Unable to read from file %s\n", infile);
  164. exit (1);
  165. }
  166. mfest = fopen (manifest, "rb");
  167. if (mfest == NULL) {
  168. fprintf (stderr, "Unable to read from file %s\n", manifest);
  169. exit (1);
  170. }
  171. out = fopen (outfile, "wb");
  172. if (out == NULL) {
  173. fprintf (stderr, "Unable to open file %s for writing\n", outfile);
  174. exit (1);
  175. }
  176. xread = fread(buf_4, 4, 1, in);
  177. fwrite(buf_4, 4, 1, out);
  178. while (fgets (line, sizeof(line), mfest))
  179. {
  180. if(!isupper(line[0])) continue;
  181. sscanf(&line[2],"%x",&displ);
  182. fseek(in, displ, SEEK_SET);
  183. if (line[0] == 'S') {
  184. SPECT_SEQ buf_spect;
  185. size_t frame_start;
  186. int n;
  187. xread = fread(&buf_spect, 4, 1, in);
  188. buf_spect.length = (short) SWAP_USHORT (buf_spect.length);
  189. fwrite(&buf_spect, 4, 1, out);
  190. for (n = 0; n < buf_spect.n_frames; n++) {
  191. int k;
  192. frame_start = ftell(in);
  193. xread = fread(&buf_spect.frame[0], sizeof(frame_t), 1, in);
  194. buf_spect.frame[0].frflags = (short)
  195. SWAP_USHORT (buf_spect.frame[0].frflags);
  196. // Changed for eSpeak 1.41
  197. for (k = 0; k < 7; k++) {
  198. buf_spect.frame[0].ffreq[k] = (short)
  199. SWAP_USHORT (buf_spect.frame[0].ffreq[k]);
  200. }
  201. // is this a long or a short frame?
  202. if(buf_spect.frame[0].frflags & FRFLAG_KLATT)
  203. {
  204. fwrite(&buf_spect.frame[0], sizeof(frame_t), 1, out);
  205. fseek(in, frame_start + sizeof(frame_t), SEEK_SET);
  206. }
  207. else
  208. {
  209. fwrite(&buf_spect.frame[0], sizeof(frame_t2), 1, out);
  210. fseek(in, frame_start + sizeof(frame_t2), SEEK_SET);
  211. }
  212. }
  213. }
  214. else if (line[0] == 'W') {
  215. long pos;
  216. int length;
  217. char *wave_data;
  218. xread = fread (buf_4, 4, 1, in);
  219. fwrite (buf_4, 4, 1, out);
  220. length = buf_4[1] * 256 + buf_4[0];
  221. wave_data = (char *) malloc (length);
  222. if (wave_data == NULL) {
  223. fprintf (stderr, "Memory allocation error\n");
  224. exit (1);
  225. }
  226. xread = fread (wave_data, 1, length, in);
  227. fwrite (wave_data, 1, length, out);
  228. pos = ftell (in);
  229. while((pos & 3) != 0) {
  230. fgetc (in);
  231. pos++;
  232. }
  233. pos = ftell (out);
  234. while((pos & 3) != 0) {
  235. fputc (0, out);
  236. pos++;
  237. }
  238. free (wave_data);
  239. }
  240. else if (line[0] == 'E') {
  241. char env_buf[128];
  242. xread = fread (env_buf, 1, 128, in);
  243. fwrite (env_buf, 1, 128, out);
  244. }
  245. else if (line[0] == 'Q') {
  246. unsigned char pb[4];
  247. unsigned length;
  248. char *buf;
  249. xread = fread (pb, 1, 4, in);
  250. fwrite (pb, 1, 4, out);
  251. length = (pb[2] << 8) + pb[3]; // size in words
  252. length *= 4;
  253. buf = (char *) malloc (length);
  254. xread = fread (buf, length, 1, in);
  255. fwrite (buf, length, 1, out);
  256. free (buf);
  257. }
  258. }
  259. fclose (in);
  260. fclose (out);
  261. fclose (mfest);
  262. } // end of swap_phondata
  263. void swap_phonindex (const char *infile, const char *outfile)
  264. {//==========================================================
  265. FILE *in, *out;
  266. char buf_4[4];
  267. unsigned short val;
  268. in = fopen (infile, "rb");
  269. if (in == NULL) {
  270. fprintf (stderr, "Unable to read from file %s\n", infile);
  271. exit (1);
  272. }
  273. out = fopen (outfile, "wb");
  274. if (out == NULL) {
  275. fprintf (stderr, "Unable to open file %s for writing\n", outfile);
  276. exit (1);
  277. }
  278. xread = fread (buf_4, 4, 1, in); // skip first 4 bytes
  279. fwrite(buf_4, 4, 1, out);
  280. while (! feof (in)) {
  281. size_t n;
  282. n = fread (&val, 2, 1, in);
  283. if (n != 1)
  284. break;
  285. val = SWAP_USHORT (val);
  286. fwrite (&val, 2, 1, out);
  287. }
  288. fclose (in);
  289. fclose (out);
  290. } // end of swap_phonindex
  291. void swap_phontab (const char *infile, const char *outfile)
  292. {//========================================================
  293. FILE *in, *out;
  294. char buf_8[8];
  295. int i, n_phoneme_tables;
  296. in = fopen (infile, "rb");
  297. if (in == NULL) {
  298. fprintf (stderr, "Unable to read from file %s\n", infile);
  299. exit (1);
  300. }
  301. out = fopen (outfile, "wb");
  302. if (out == NULL) {
  303. fprintf (stderr, "Unable to open file %s for writing\n", outfile);
  304. exit (1);
  305. }
  306. xread = fread (buf_8, 4, 1, in);
  307. fwrite (buf_8, 4, 1, out);
  308. n_phoneme_tables = buf_8[0];
  309. for (i = 0; i < n_phoneme_tables; i++) {
  310. int n_phonemes, j;
  311. char tab_name[N_PHONEME_TAB_NAME];
  312. xread = fread (buf_8, 8, 1, in);
  313. fwrite (buf_8, 8, 1, out);
  314. n_phonemes = buf_8[0];
  315. xread = fread (tab_name, N_PHONEME_TAB_NAME, 1, in);
  316. fwrite (tab_name, N_PHONEME_TAB_NAME, 1, out);
  317. for (j = 0; j < n_phonemes; j++) {
  318. PHONEME_TAB table;
  319. xread = fread (&table, sizeof (PHONEME_TAB), 1, in);
  320. table.mnemonic = SWAP_UINT (table.mnemonic);
  321. table.phflags = SWAP_UINT (table.phflags);
  322. table.program = SWAP_USHORT (table.program);
  323. fwrite (&table, sizeof (PHONEME_TAB), 1, out);
  324. }
  325. }
  326. fclose (in);
  327. fclose (out);
  328. } // end of swap_phontab
  329. void
  330. usage (const char *program_name)
  331. {
  332. fprintf (stderr,
  333. "This program copies the phontab, phonindex and phondata files from a given\n"
  334. "directory, swapping values to big-endian form if necessary.\n\n"
  335. "Usage:\n"
  336. " %s [INPUT_DIR] [OUTPUT_DIR] [MANIFEST_FILE]\n\n"
  337. "By default, the MANIFEST_FILE used is a file called 'phondata-manifest' in\n"
  338. "the current directory. The default INPUT_DIR is /usr/share/espeak-data and\n"
  339. "OUTPUT_DIR is the current directory.\n", program_name);
  340. exit (1);
  341. }