Debian Codegraph @

platforms/big_endian/espeak-phoneme-data.c

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