PageRenderTime 75ms CodeModel.GetById 18ms app.highlight 51ms RepoModel.GetById 1ms app.codeStats 0ms

/native/external/espeak/platforms/big_endian/espeak-phoneme-data.c

http://eyes-free.googlecode.com/
C | 368 lines | 282 code | 86 blank | 0 comment | 47 complexity | 5633c2782d2ac007997d64ae194d6fde MD5 | raw file
  1#include <stdio.h>
  2#include <stdlib.h>
  3#include <string.h>
  4#include <sys/types.h>
  5
  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
 12
 13#if IS_BIG_ENDIAN
 14
 15#  define SWAP_USHORT(val)    ((unsigned short) ( \
 16     (unsigned short) ((unsigned short) (val) >> 8) |  \
 17     (unsigned short) ((unsigned short) (val) << 8)))
 18
 19#  define SWAP_UINT(val)    ((unsigned int) ( \
 20     (((unsigned int) (val) & (unsigned int) 0x000000ffU) << 24) | \
 21     (((unsigned int) (val) & (unsigned int) 0x0000ff00U) <<  8) | \
 22     (((unsigned int) (val) & (unsigned int) 0x00ff0000U) >>  8) | \
 23     (((unsigned int) (val) & (unsigned int) 0xff000000U) >> 24)))
 24
 25#else
 26
 27#  define SWAP_USHORT(val) (val)
 28#  define SWAP_UINT(val) (val)
 29
 30#endif
 31
 32
 33
 34#define N_PHONEME_TAB_NAME  32
 35
 36typedef struct {
 37    unsigned int mnemonic;
 38    unsigned int phflags;
 39
 40    unsigned short std_length;
 41    unsigned short  spect;
 42    unsigned short  before;
 43    unsigned short  after;
 44
 45    unsigned char  code;
 46    unsigned char  type;
 47    unsigned char  start_type;
 48    unsigned char  end_type;
 49
 50    unsigned char  length_mod;
 51    unsigned char  reduce_to;
 52    unsigned char  alternative_ph;
 53    unsigned char  link_out;
 54} PHONEME_TAB;
 55
 56
 57typedef struct {
 58    short frflags;
 59    unsigned char length;
 60    unsigned char rms;
 61    short ffreq[9];
 62    unsigned char fheight[9];
 63    unsigned char fwidth[6];
 64    unsigned char fright[6];
 65} frame_t;
 66
 67#define N_SEQ_FRAMES   25
 68
 69typedef struct {
 70   short length;
 71   unsigned char  n_frames;
 72   unsigned char  flags;
 73   frame_t  frame[N_SEQ_FRAMES];
 74} SPECT_SEQ;
 75
 76
 77void swap_phondata  (const char *infile, const char *outfile,
 78                     const char *manifest);
 79void swap_phonindex (const char *infile, const char *outfile);
 80void swap_phontab   (const char *infile, const char *outfile);
 81
 82void usage (const char *program_name);
 83
 84
 85
 86int
 87main (int argc, char *argv[])
 88{
 89    const char *indir = "/usr/share/espeak-data";
 90    const char *outdir = ".";
 91    const char *manifest = "phondata-manifest";
 92    char *f1, *f2;
 93
 94
 95    if (argc > 4)
 96        usage (argv[0]);
 97
 98    if (argc > 1) {
 99        if (strcmp (argv[1], "-h") == 0 ||
100            strcmp (argv[1], "--help") == 0)
101            usage (argv[0]);
102
103        indir = argv[1];
104    }
105
106    if (argc > 2)
107        outdir = argv[2];
108
109    if (argc > 3)
110        manifest = argv[3];
111
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
123#if IS_BIG_ENDIAN
124    printf ("Host seems to be big-endian ..\n");
125#else
126    printf ("Host seems to be little-endian ..\n");
127#endif
128
129    sprintf (f1, "%s/phontab", indir);
130    sprintf (f2, "%s/temp_1", outdir);
131
132    printf ("Processing phontab ..\n");
133    swap_phontab (f1, f2);
134    sprintf (f1, "%s/phontab", outdir);
135    rename (f2, f1);
136
137    sprintf (f1, "%s/phonindex", indir);
138    sprintf (f2, "%s/temp_1", outdir);
139
140    printf ("Processing phonindex ..\n");
141    swap_phonindex (f1, f2);
142    sprintf (f1, "%s/phonindex", outdir);
143    rename (f2, f1);
144
145    sprintf (f1, "%s/phondata", indir);
146    sprintf (f2, "%s/temp_1", outdir);
147
148    printf ("Processing phondata ..\n");
149    swap_phondata (f1, f2, manifest);
150    sprintf (f1, "%s/phondata", outdir);
151    rename (f2, f1);
152
153    free (f1);
154    free (f2);
155
156    printf ("Done.\n");
157
158    return 0;
159}
160
161void
162swap_phondata  (const char *infile, const char *outfile,
163                const char *manifest)
164{
165    FILE *in, *mfest, *out;
166    char line[1024];
167    unsigned char buf_4[4];
168
169    in = fopen (infile, "rb");
170    if (in == NULL) {
171        fprintf (stderr, "Unable to read from file %s\n", infile);
172        exit (1);
173    }
174
175    mfest = fopen (manifest, "rb");
176    if (mfest == NULL) {
177        fprintf (stderr, "Unable to read from file %s\n", manifest);
178        exit (1);
179    }
180
181    out = fopen (outfile, "wb");
182    if (out == NULL) {
183        fprintf (stderr, "Unable to open file %s for writing\n", outfile);
184        exit (1);
185    }
186
187    fread (buf_4, 4, 1, in);
188    fwrite (buf_4, 4, 1, out);
189
190    while (fgets (line, 1024, mfest)) {
191        if (line[0] == 'S') {
192            SPECT_SEQ buf_spect;
193            size_t ix;
194            int n;
195
196            fread (&buf_spect.length, 2, 1, in);
197            fread (&buf_spect.n_frames, 1, 1, in);
198            fseek (in, -3, SEEK_CUR);
199
200            ix = (char *)(&buf_spect.frame[buf_spect.n_frames]) -
201                (char *)(&buf_spect);
202            ix = (ix+3) & 0xfffc;
203
204            fread (&buf_spect, ix, 1, in);
205
206            buf_spect.length = (short) SWAP_USHORT (buf_spect.length);
207            for (n = 0; n < buf_spect.n_frames; n++) {
208                int k;
209
210                buf_spect.frame[n].frflags = (short)
211                    SWAP_USHORT (buf_spect.frame[n].frflags);
212
213                for (k = 0; k < 9; k++) {
214                    buf_spect.frame[n].ffreq[k] = (short)
215                        SWAP_USHORT (buf_spect.frame[n].ffreq[k]);
216                }
217            }
218
219            fwrite (&buf_spect, ix, 1, out);
220        }
221        else if (line[0] == 'W') {
222            long pos;
223            int length;
224            char *wave_data;
225
226            fread (buf_4, 4, 1, in);
227            fwrite (buf_4, 4, 1, out);
228
229            length = buf_4[1] * 256 + buf_4[0];
230
231            wave_data = (char *) malloc (length);
232            if (wave_data == NULL) {
233                fprintf (stderr, "Memory allocation error\n");
234                exit (1);
235            }
236
237            fread (wave_data, 1, length, in);
238            fwrite (wave_data, 1, length, out);
239
240            pos = ftell (in);
241            while((pos & 3) != 0) {
242                fgetc (in);
243                pos++;
244            }
245
246            pos = ftell (out);
247            while((pos & 3) != 0) {
248                fputc (0, out);
249                pos++;
250            }
251
252            free (wave_data);
253        }
254        else if (line[0] == 'E') {
255            char env_buf[128];
256
257            fread (env_buf, 1, 128, in);
258            fwrite (env_buf, 1, 128, out);
259        }
260    }
261
262    fclose (in);
263    fclose (out);
264    fclose (mfest);
265}
266
267void
268swap_phonindex (const char *infile, const char *outfile)
269{
270    FILE *in, *out;
271    unsigned int val;
272
273    in = fopen (infile, "rb");
274    if (in == NULL) {
275        fprintf (stderr, "Unable to read from file %s\n", infile);
276        exit (1);
277    }
278
279    out = fopen (outfile, "wb");
280    if (out == NULL) {
281        fprintf (stderr, "Unable to open file %s for writing\n", outfile);
282        exit (1);
283    }
284
285    while (! feof (in)) {
286        size_t n;
287
288        n = fread (&val, 4, 1, in);
289        if (n != 1)
290            break;
291
292        val = SWAP_UINT (val);
293        fwrite (&val, 4, 1, out);
294    }
295
296    fclose (in);
297    fclose (out);
298}
299
300void
301swap_phontab (const char *infile, const char *outfile)
302{
303    FILE *in, *out;
304    char buf_4[4];
305    int i, n_phoneme_tables;
306
307    in = fopen (infile, "rb");
308    if (in == NULL) {
309        fprintf (stderr, "Unable to read from file %s\n", infile);
310        exit (1);
311    }
312
313    out = fopen (outfile, "wb");
314    if (out == NULL) {
315        fprintf (stderr, "Unable to open file %s for writing\n", outfile);
316        exit (1);
317    }
318
319    fread (buf_4, 4, 1, in);
320    fwrite (buf_4, 4, 1, out);
321    n_phoneme_tables = buf_4[0];
322
323    for (i = 0; i < n_phoneme_tables; i++) {
324        int n_phonemes, j;
325        char tab_name[N_PHONEME_TAB_NAME];
326
327        fread (buf_4, 4, 1, in);
328        fwrite (buf_4, 4, 1, out);
329
330        n_phonemes = buf_4[0];
331
332        fread (tab_name, N_PHONEME_TAB_NAME, 1, in);
333        fwrite (tab_name, N_PHONEME_TAB_NAME, 1, out);
334
335        for (j = 0; j < n_phonemes; j++) {
336            PHONEME_TAB table;
337
338            fread (&table, sizeof (PHONEME_TAB), 1, in);
339
340            table.mnemonic = SWAP_UINT (table.mnemonic);
341            table.phflags = SWAP_UINT (table.phflags);
342
343            table.std_length = SWAP_USHORT (table.std_length);
344            table.spect = SWAP_USHORT (table.spect);
345            table.before = SWAP_USHORT (table.before);
346            table.after = SWAP_USHORT (table.after);
347
348            fwrite (&table, sizeof (PHONEME_TAB), 1, out);
349        }
350    }
351
352    fclose (in);
353    fclose (out);
354}
355
356void
357usage (const char *program_name)
358{
359    fprintf (stderr,
360"This program copies the phontab, phonindex and phondata files from a given\n"
361"directory, swapping values to big-endian form if necessary.\n\n"
362"Usage:\n"
363"  %s [INPUT_DIR] [OUTPUT_DIR] [MANIFEST_FILE]\n\n"
364"By default, the MANIFEST_FILE used is a file called 'phondata-manifest' in\n"
365"the current directory. The default INPUT_DIR is /usr/share/espeak-data and\n"
366"OUTPUT_DIR is the current directory.\n", program_name);
367    exit (1);
368}