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

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