Initial import to Tizen
[profile/ivi/sphinxbase.git] / src / sphinx_cepview / main_cepview.c
1 /* -*- c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* ====================================================================
3  * Copyright (c) 1994-2001 Carnegie Mellon University.  All rights
4  * reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  *
18  * This work was supported in part by funding from the Defense Advanced
19  * Research Projects Agency and the National Science Foundation of the
20  * United States of America, and the CMU Sphinx Speech Consortium.
21  *
22  * THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND
23  * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
24  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY
26  * NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  *
34  * ====================================================================
35  *
36  * 
37  * HISTORY
38  * 
39  * circa 1994   P J Moreno at Carnegie Mellon
40  *              Created.
41  *
42  * For history information, please use 'cvs log'
43  * $Log$
44  * Revision 1.11  2006/02/24  04:06:43  arthchan2003
45  * Merged from branch SPHINX3_5_2_RCI_IRII_BRANCH: Changed commands to macro.  Used E_INFO instead of printf in displaying no. of friends
46  * 
47  *
48  * Revision 1.10  2005/08/18 21:18:09  egouvea
49  * Added E_INFO displaying information about how many columns are being printed, and how many frames
50  *
51  * Revision 1.9.4.2  2005/09/07 23:51:05  arthchan2003
52  * Fixed keyword expansion problem
53  *
54  * Revision 1.9.4.1  2005/07/18 23:21:23  arthchan2003
55  * Tied command-line arguments with marcos
56  *
57  * Revision 1.10  2005/08/18 21:18:09  egouvea
58  * Added E_INFO displaying information about how many columns are being printed, and how many frames
59  *
60  * Revision 1.9  2005/06/22 05:38:45  arthchan2003
61  * Add
62  *
63  * Revision 1.2  2005/03/30 00:43:41  archan
64  *
65  * Add $Log$
66  * Revision 1.11  2006/02/24  04:06:43  arthchan2003
67  * Merged from branch SPHINX3_5_2_RCI_IRII_BRANCH: Changed commands to macro.  Used E_INFO instead of printf in displaying no. of friends
68  * 
69  *
70  */
71
72 #include <stdio.h>
73 #include <stdlib.h>
74 #include <string.h>
75
76 #ifdef _WIN32
77 #pragma warning (disable: 4996) 
78 #endif
79
80 #ifdef HAVE_CONFIG_H
81 #include <config.h>
82 #endif
83
84 #include <sphinxbase/strfuncs.h>
85 #include <sphinxbase/prim_type.h>
86 #include <sphinxbase/cmd_ln.h>
87 #include <sphinxbase/ckd_alloc.h>
88 #include <sphinxbase/info.h>
89 #include <sphinxbase/err.h>
90 #include <sphinxbase/bio.h>
91 #include <sphinxbase/pio.h>
92
93 /** \file main_cepview.c
94     \brief Main driver of cepview
95  */
96 #define IO_ERR  (-1)
97 #define IO_SUCCESS  (0)
98
99 #define SHOW_ALL "-1"
100
101 /* Default cepstral vector size */
102 #define NUM_COEFF  "13"
103
104 /* Default display size, i.e., number of coefficients displayed, less
105  * than the vector size so we display one frame per line.
106  */
107 #define DISPLAY_SIZE "10"
108 #define STR_MAX_INT "2147483647"
109
110 static arg_t arg[] = {
111
112     {"-logfn",
113      ARG_STRING,
114      NULL,
115      "Log file (default stdout/stderr)"},
116     {"-i",
117      ARG_INT32,
118      NUM_COEFF,
119      "Number of coefficients in the feature vector."},
120     {"-d",
121      ARG_INT32,
122      DISPLAY_SIZE,
123      "Number of displayed coefficients."},
124     {"-header",
125      ARG_INT32,
126      "0",
127      "Whether header is shown."},
128     {"-describe",
129      ARG_INT32,
130      "0",
131      "Whether description will be shown."},
132     {"-b",
133      ARG_INT32,
134      "0",
135      "The beginning frame 0-based."},
136     {"-e",
137      ARG_INT32,
138      "2147483647",
139      "The ending frame."},
140     {"-f",
141      ARG_STRING,
142      NULL,
143      "Input feature file."},
144     {NULL, ARG_INT32, NULL, NULL}
145 };
146
147 int read_cep(char const *file, float ***cep, int *nframes, int numcep);
148
149 int
150 main(int argc, char *argv[])
151 {
152     int i, j, offset;
153     int32 noframe, vsize, dsize, column;
154     int32 frm_begin, frm_end;
155     int is_header, is_describe;
156     float *z, **cep;
157     char const *cepfile;
158
159     print_appl_info(argv[0]);
160     cmd_ln_appl_enter(argc, argv, "default.arg", arg);
161
162     vsize = cmd_ln_int32("-i");
163     dsize = cmd_ln_int32("-d");
164     frm_begin = cmd_ln_int32("-b");
165     frm_end = cmd_ln_int32("-e");
166     is_header = cmd_ln_int32("-header");
167     is_describe = cmd_ln_int32("-describe");
168
169     if (vsize < 0)
170         E_FATAL("-i : Input vector size should be larger than 0.\n");
171     if (dsize < 0)
172         E_FATAL("-d : Column size should be larger than 0\n");
173     if (frm_begin < 0)
174         E_FATAL("-b : Beginning frame should be larger than 0\n");
175     /* The following condition is redundant
176      * if (frm_end < 0) E_FATAL("-e : Ending frame should be larger than 0\n");
177      */
178     if (frm_begin >= frm_end)
179         E_FATAL
180             ("Ending frame (-e) should be larger than beginning frame (-b).\n");
181
182     if ((cepfile = cmd_ln_str("-f")) == NULL) {
183         E_FATAL("Input file was not specified with (-f)\n");
184     }
185     if (read_cep(cepfile, &cep, &noframe, vsize) == IO_ERR)
186         E_FATAL("ERROR opening %s for reading\n", cepfile);
187
188     z = cep[0];
189
190     offset = 0;
191     column = (vsize > dsize) ? dsize : vsize;
192     frm_end = (frm_end > noframe) ? noframe : frm_end;
193
194     E_INFO("Displaying %d out of %d columns per frame\n", column, vsize);
195     E_INFO("Total %d frames\n\n", noframe);
196
197     /* This part should be moved to a special library if this file is
198        longer than 300 lines. */
199
200     if (is_header) {
201         if (is_describe) {
202             printf("\n%6s", "frame#:");
203         }
204
205         for (j = 0; j < column; ++j) {
206             printf("%3s%3d%s ", "c[", j, "]");
207         }
208         printf("\n");
209     }
210
211     offset += frm_begin * vsize;
212     for (i = frm_begin; i < frm_end; ++i) {
213         if (is_describe) {
214             printf("%6d:", i);
215         }
216         for (j = 0; j < column; ++j)
217             printf("%7.3f ", z[offset + j]);
218         printf("\n");
219
220         offset += vsize;
221     }
222     fflush(stdout);
223     cmd_ln_appl_exit();
224     ckd_free_2d(cep);
225
226     return (IO_SUCCESS);
227
228 }
229
230 int
231 read_cep(char const *file, float ***cep, int *numframes, int cepsize)
232 {
233     FILE *fp;
234     int n_float;
235     struct stat statbuf;
236     int i, n, byterev, sf, ef;
237     float32 **mfcbuf;
238
239     if (stat_retry(file, &statbuf) < 0) {
240         printf("stat(%s) failed\n", file);
241         return IO_ERR;
242     }
243
244     if ((fp = fopen(file, "rb")) == NULL) {
245         printf("Failed to open '%s' for reading: %s\n", file, strerror(errno));
246         return IO_ERR;
247     }
248
249     /* Read #floats in header */
250     if (fread(&n_float, sizeof(int), 1, fp) != 1) {
251         fclose(fp);
252         return IO_ERR;
253     }
254
255     /* Check if n_float matches file size */
256     byterev = FALSE;
257     if ((int) (n_float * sizeof(float) + 4) != statbuf.st_size) {
258         n = n_float;
259         SWAP_INT32(&n);
260
261         if ((int) (n * sizeof(float) + 4) != statbuf.st_size) {
262             printf
263                 ("Header size field: %d(%08x); filesize: %d(%08x)\n",
264                  n_float, n_float, (int) statbuf.st_size,
265                  (int) statbuf.st_size);
266             fclose(fp);
267             return IO_ERR;
268         }
269
270         n_float = n;
271         byterev = TRUE;
272     }
273     if (n_float <= 0) {
274         printf("Header size field: %d\n", n_float);
275         fclose(fp);
276         return IO_ERR;
277     }
278
279     /* n = #frames of input */
280     n = n_float / cepsize;
281     if (n * cepsize != n_float) {
282         printf("Header size field: %d; not multiple of %d\n",
283                n_float, cepsize);
284         fclose(fp);
285         return IO_ERR;
286     }
287     sf = 0;
288     ef = n;
289
290     mfcbuf = (float **) ckd_calloc_2d(n, cepsize, sizeof(float32));
291
292     /* Read mfc data and byteswap if necessary */
293     n_float = n * cepsize;
294     if ((int) fread(mfcbuf[0], sizeof(float), n_float, fp) != n_float) {
295         printf("Error reading mfc data\n");
296         fclose(fp);
297         return IO_ERR;
298     }
299     if (byterev) {
300         for (i = 0; i < n_float; i++)
301             SWAP_FLOAT32(&(mfcbuf[0][i]));
302     }
303     fclose(fp);
304
305     *numframes = n;
306     *cep = mfcbuf;
307     return IO_SUCCESS;
308 }
309
310 /** Silvio Moioli: Windows CE/Mobile entry point added. */
311 #if defined(_WIN32_WCE)
312 #pragma comment(linker,"/entry:mainWCRTStartup")
313
314 //Windows Mobile has the Unicode main only
315 int wmain(int32 argc, wchar_t *wargv[]) {
316     char** argv;
317     size_t wlen;
318     size_t len;
319     int i;
320
321     argv = malloc(argc*sizeof(char*));
322     for (i=0; i<argc; i++){
323         wlen = lstrlenW(wargv[i]);
324         len = wcstombs(NULL, wargv[i], wlen);
325         argv[i] = malloc(len+1);
326         wcstombs(argv[i], wargv[i], wlen);
327     }
328
329     //assuming ASCII parameters
330     return main(argc, argv);
331 }
332 #endif