SphinxBase  0.6
main_cepview.c
Go to the documentation of this file.
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 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 
41 #ifdef _WIN32
42 #pragma warning (disable: 4996)
43 #endif
44 
45 #ifdef HAVE_CONFIG_H
46 #include <config.h>
47 #endif
48 
49 #include <sphinxbase/strfuncs.h>
50 #include <sphinxbase/prim_type.h>
51 #include <sphinxbase/cmd_ln.h>
52 #include <sphinxbase/ckd_alloc.h>
53 #include <sphinxbase/info.h>
54 #include <sphinxbase/err.h>
55 #include <sphinxbase/bio.h>
56 #include <sphinxbase/pio.h>
57 
61 #define IO_ERR (-1)
62 #define IO_SUCCESS (0)
63 
64 #define SHOW_ALL "-1"
65 
66 /* Default cepstral vector size */
67 #define NUM_COEFF "13"
68 
69 /* Default display size, i.e., number of coefficients displayed, less
70  * than the vector size so we display one frame per line.
71  */
72 #define DISPLAY_SIZE "10"
73 #define STR_MAX_INT "2147483647"
74 
75 static arg_t arg[] = {
76 
77  {"-logfn",
78  ARG_STRING,
79  NULL,
80  "Log file (default stdout/stderr)"},
81  {"-i",
82  ARG_INT32,
83  NUM_COEFF,
84  "Number of coefficients in the feature vector."},
85  {"-d",
86  ARG_INT32,
87  DISPLAY_SIZE,
88  "Number of displayed coefficients."},
89  {"-header",
90  ARG_INT32,
91  "0",
92  "Whether header is shown."},
93  {"-describe",
94  ARG_INT32,
95  "0",
96  "Whether description will be shown."},
97  {"-b",
98  ARG_INT32,
99  "0",
100  "The beginning frame 0-based."},
101  {"-e",
102  ARG_INT32,
103  "2147483647",
104  "The ending frame."},
105  {"-f",
106  ARG_STRING,
107  NULL,
108  "Input feature file."},
109  {NULL, ARG_INT32, NULL, NULL}
110 };
111 
112 int read_cep(char const *file, float ***cep, int *nframes, int numcep);
113 
114 int
115 main(int argc, char *argv[])
116 {
117  int i, j, offset;
118  int32 noframe, vsize, dsize, column;
119  int32 frm_begin, frm_end;
120  int is_header, is_describe;
121  float *z, **cep;
122  char const *cepfile;
123 
124  print_appl_info(argv[0]);
125  cmd_ln_appl_enter(argc, argv, "default.arg", arg);
126 
127  vsize = cmd_ln_int32("-i");
128  dsize = cmd_ln_int32("-d");
129  frm_begin = cmd_ln_int32("-b");
130  frm_end = cmd_ln_int32("-e");
131  is_header = cmd_ln_int32("-header");
132  is_describe = cmd_ln_int32("-describe");
133 
134  if (vsize < 0)
135  E_FATAL("-i : Input vector size should be larger than 0.\n");
136  if (dsize < 0)
137  E_FATAL("-d : Column size should be larger than 0\n");
138  if (frm_begin < 0)
139  E_FATAL("-b : Beginning frame should be larger than 0\n");
140  /* The following condition is redundant
141  * if (frm_end < 0) E_FATAL("-e : Ending frame should be larger than 0\n");
142  */
143  if (frm_begin >= frm_end)
144  E_FATAL
145  ("Ending frame (-e) should be larger than beginning frame (-b).\n");
146 
147  if ((cepfile = cmd_ln_str("-f")) == NULL) {
148  E_FATAL("Input file was not specified with (-f)\n");
149  }
150  if (read_cep(cepfile, &cep, &noframe, vsize) == IO_ERR)
151  E_FATAL_SYSTEM("Failed to open '%s' for reading", cepfile);
152 
153  z = cep[0];
154 
155  offset = 0;
156  column = (vsize > dsize) ? dsize : vsize;
157  frm_end = (frm_end > noframe) ? noframe : frm_end;
158 
159  E_INFO("Displaying %d out of %d columns per frame\n", column, vsize);
160  E_INFO("Total %d frames\n\n", noframe);
161 
162  /* This part should be moved to a special library if this file is
163  longer than 300 lines. */
164 
165  if (is_header) {
166  if (is_describe) {
167  printf("\n%6s", "frame#:");
168  }
169 
170  for (j = 0; j < column; ++j) {
171  printf("%3s%3d%s ", "c[", j, "]");
172  }
173  printf("\n");
174  }
175 
176  offset += frm_begin * vsize;
177  for (i = frm_begin; i < frm_end; ++i) {
178  if (is_describe) {
179  printf("%6d:", i);
180  }
181  for (j = 0; j < column; ++j)
182  printf("%7.3f ", z[offset + j]);
183  printf("\n");
184 
185  offset += vsize;
186  }
187  fflush(stdout);
189  ckd_free_2d(cep);
190 
191  return (IO_SUCCESS);
192 
193 }
194 
195 int
196 read_cep(char const *file, float ***cep, int *numframes, int cepsize)
197 {
198  FILE *fp;
199  int n_float;
200  struct stat statbuf;
201  int i, n, byterev;
202  float32 **mfcbuf;
203 
204  if (stat_retry(file, &statbuf) < 0) {
205  E_ERROR_SYSTEM("Failed to get file size '%s'", file);
206  return IO_ERR;
207  }
208 
209  if ((fp = fopen(file, "rb")) == NULL) {
210  E_ERROR_SYSTEM("Failed to open '%s' for reading", file);
211  return IO_ERR;
212  }
213 
214  /* Read #floats in header */
215  if (fread(&n_float, sizeof(int), 1, fp) != 1) {
216  fclose(fp);
217  return IO_ERR;
218  }
219 
220  /* Check if n_float matches file size */
221  byterev = FALSE;
222  if ((int) (n_float * sizeof(float) + 4) != statbuf.st_size) {
223  n = n_float;
224  SWAP_INT32(&n);
225 
226  if ((int) (n * sizeof(float) + 4) != statbuf.st_size) {
227  E_ERROR("Header size field: %d(%08x); filesize: %d(%08x)\n",
228  n_float, n_float, (int) statbuf.st_size,
229  (int) statbuf.st_size);
230  fclose(fp);
231  return IO_ERR;
232  }
233 
234  n_float = n;
235  byterev = TRUE;
236  }
237  if (n_float <= 0) {
238  E_ERROR("Header size field: %d\n", n_float);
239  fclose(fp);
240  return IO_ERR;
241  }
242 
243  /* n = #frames of input */
244  n = n_float / cepsize;
245  if (n * cepsize != n_float) {
246  E_ERROR("Header size field: %d; not multiple of %d\n",
247  n_float, cepsize);
248  fclose(fp);
249  return IO_ERR;
250  }
251 
252  mfcbuf = (float **) ckd_calloc_2d(n, cepsize, sizeof(float32));
253 
254  /* Read mfc data and byteswap if necessary */
255  n_float = n * cepsize;
256  if ((int) fread(mfcbuf[0], sizeof(float), n_float, fp) != n_float) {
257  E_ERROR("Error reading mfc data from the file '%s'", file);
258  fclose(fp);
259  return IO_ERR;
260  }
261  if (byterev) {
262  for (i = 0; i < n_float; i++)
263  SWAP_FLOAT32(&(mfcbuf[0][i]));
264  }
265  fclose(fp);
266 
267  *numframes = n;
268  *cep = mfcbuf;
269  return IO_SUCCESS;
270 }
271 
273 #if defined(_WIN32_WCE)
274 #pragma comment(linker,"/entry:mainWCRTStartup")
275 
276 //Windows Mobile has the Unicode main only
277 int wmain(int32 argc, wchar_t *wargv[]) {
278  char** argv;
279  size_t wlen;
280  size_t len;
281  int i;
282 
283  argv = malloc(argc*sizeof(char*));
284  for (i=0; i<argc; i++){
285  wlen = lstrlenW(wargv[i]);
286  len = wcstombs(NULL, wargv[i], wlen);
287  argv[i] = malloc(len+1);
288  wcstombs(argv[i], wargv[i], wlen);
289  }
290 
291  //assuming ASCII parameters
292  return main(argc, argv);
293 }
294 #endif
Command-line and other configurationparsing and handling.
Miscellaneous useful string functions.
#define ckd_calloc_2d(d1, d2, sz)
Macro for ckd_calloc_2d
Definition: ckd_alloc.h:270
#define ARG_INT32
Definition: cmd_ln.h:144
#define E_INFO
Print logging information to standard error stream.
Definition: err.h:147
Sphinx's memory allocation/deallocation routines.
SPHINXBASE_EXPORT int32 stat_retry(const char *file, struct stat *statbuf)
There is no bitstream decoder, because a stream abstraction is too slow.
Definition: pio.c:480
Cross platform binary IO to process files in sphinx3 format.
#define ARG_STRING
String argument (optional).
Definition: cmd_ln.h:114
Basic type definitions used in Sphinx.
#define E_FATAL_SYSTEM
Print error text; Call perror(""); exit(errno);.
Definition: err.h:132
SPHINXBASE_EXPORT void cmd_ln_appl_exit(void)
Finalization routine corresponding to cmd_ln_appl_enter().
Definition: cmd_ln.c:544
Implementation of logging routines.
Argument definition structure.
SPHINXBASE_EXPORT void print_appl_info(char *appl_name)
This function prints the hostname, the directory, compile time and date .
Definition: info.c:55
Print hostname, directory name, compile time and date.
#define E_FATAL
Exit with non-zero status after error message.
Definition: err.h:127
#define E_ERROR
Print error message to standard error stream.
Definition: err.h:169
SPHINXBASE_EXPORT void ckd_free_2d(void *ptr)
Free a 2-D array (ptr) previously allocated by ckd_calloc_2d.
Definition: ckd_alloc.c:252
#define E_ERROR_SYSTEM
Print error text; Call perror("");.
Definition: err.h:142
#define cmd_ln_str(name)
Retrieve a string from the global command line.
Definition: cmd_ln.h:489
SPHINXBASE_EXPORT void cmd_ln_appl_enter(int argc, char *argv[], char const *default_argfn, const arg_t *defn)
Old application initialization routine for Sphinx3 code.
Definition: cmd_ln.c:494
file IO related operations.
#define cmd_ln_int32(name)
Retrieve a 32-bit integer from the global command line.
Definition: cmd_ln.h:505