SphinxBase 0.6
|
00001 /* -*- c-basic-offset: 4; indent-tabs-mode: nil -*- */ 00002 /* ==================================================================== 00003 * Copyright (c) 1994-2001 Carnegie Mellon University. All rights 00004 * reserved. 00005 * 00006 * Redistribution and use in source and binary forms, with or without 00007 * modification, are permitted provided that the following conditions 00008 * are met: 00009 * 00010 * 1. Redistributions of source code must retain the above copyright 00011 * notice, this list of conditions and the following disclaimer. 00012 * 00013 * 2. Redistributions in binary form must reproduce the above copyright 00014 * notice, this list of conditions and the following disclaimer in 00015 * the documentation and/or other materials provided with the 00016 * distribution. 00017 * 00018 * This work was supported in part by funding from the Defense Advanced 00019 * Research Projects Agency and the National Science Foundation of the 00020 * United States of America, and the CMU Sphinx Speech Consortium. 00021 * 00022 * THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND 00023 * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 00024 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 00025 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY 00026 * NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00027 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 00028 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 00029 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 00030 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00031 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00032 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00033 * 00034 * ==================================================================== 00035 */ 00036 00037 #include <stdio.h> 00038 #include <stdlib.h> 00039 #include <string.h> 00040 00041 #ifdef _WIN32 00042 #pragma warning (disable: 4996) 00043 #endif 00044 00045 #ifdef HAVE_CONFIG_H 00046 #include <config.h> 00047 #endif 00048 00049 #include <sphinxbase/strfuncs.h> 00050 #include <sphinxbase/prim_type.h> 00051 #include <sphinxbase/cmd_ln.h> 00052 #include <sphinxbase/ckd_alloc.h> 00053 #include <sphinxbase/info.h> 00054 #include <sphinxbase/err.h> 00055 #include <sphinxbase/bio.h> 00056 #include <sphinxbase/pio.h> 00057 00061 #define IO_ERR (-1) 00062 #define IO_SUCCESS (0) 00063 00064 #define SHOW_ALL "-1" 00065 00066 /* Default cepstral vector size */ 00067 #define NUM_COEFF "13" 00068 00069 /* Default display size, i.e., number of coefficients displayed, less 00070 * than the vector size so we display one frame per line. 00071 */ 00072 #define DISPLAY_SIZE "10" 00073 #define STR_MAX_INT "2147483647" 00074 00075 static arg_t arg[] = { 00076 00077 {"-logfn", 00078 ARG_STRING, 00079 NULL, 00080 "Log file (default stdout/stderr)"}, 00081 {"-i", 00082 ARG_INT32, 00083 NUM_COEFF, 00084 "Number of coefficients in the feature vector."}, 00085 {"-d", 00086 ARG_INT32, 00087 DISPLAY_SIZE, 00088 "Number of displayed coefficients."}, 00089 {"-header", 00090 ARG_INT32, 00091 "0", 00092 "Whether header is shown."}, 00093 {"-describe", 00094 ARG_INT32, 00095 "0", 00096 "Whether description will be shown."}, 00097 {"-b", 00098 ARG_INT32, 00099 "0", 00100 "The beginning frame 0-based."}, 00101 {"-e", 00102 ARG_INT32, 00103 "2147483647", 00104 "The ending frame."}, 00105 {"-f", 00106 ARG_STRING, 00107 NULL, 00108 "Input feature file."}, 00109 {NULL, ARG_INT32, NULL, NULL} 00110 }; 00111 00112 int read_cep(char const *file, float ***cep, int *nframes, int numcep); 00113 00114 int 00115 main(int argc, char *argv[]) 00116 { 00117 int i, j, offset; 00118 int32 noframe, vsize, dsize, column; 00119 int32 frm_begin, frm_end; 00120 int is_header, is_describe; 00121 float *z, **cep; 00122 char const *cepfile; 00123 00124 print_appl_info(argv[0]); 00125 cmd_ln_appl_enter(argc, argv, "default.arg", arg); 00126 00127 vsize = cmd_ln_int32("-i"); 00128 dsize = cmd_ln_int32("-d"); 00129 frm_begin = cmd_ln_int32("-b"); 00130 frm_end = cmd_ln_int32("-e"); 00131 is_header = cmd_ln_int32("-header"); 00132 is_describe = cmd_ln_int32("-describe"); 00133 00134 if (vsize < 0) 00135 E_FATAL("-i : Input vector size should be larger than 0.\n"); 00136 if (dsize < 0) 00137 E_FATAL("-d : Column size should be larger than 0\n"); 00138 if (frm_begin < 0) 00139 E_FATAL("-b : Beginning frame should be larger than 0\n"); 00140 /* The following condition is redundant 00141 * if (frm_end < 0) E_FATAL("-e : Ending frame should be larger than 0\n"); 00142 */ 00143 if (frm_begin >= frm_end) 00144 E_FATAL 00145 ("Ending frame (-e) should be larger than beginning frame (-b).\n"); 00146 00147 if ((cepfile = cmd_ln_str("-f")) == NULL) { 00148 E_FATAL("Input file was not specified with (-f)\n"); 00149 } 00150 if (read_cep(cepfile, &cep, &noframe, vsize) == IO_ERR) 00151 E_FATAL_SYSTEM("Failed to open '%s' for reading", cepfile); 00152 00153 z = cep[0]; 00154 00155 offset = 0; 00156 column = (vsize > dsize) ? dsize : vsize; 00157 frm_end = (frm_end > noframe) ? noframe : frm_end; 00158 00159 E_INFO("Displaying %d out of %d columns per frame\n", column, vsize); 00160 E_INFO("Total %d frames\n\n", noframe); 00161 00162 /* This part should be moved to a special library if this file is 00163 longer than 300 lines. */ 00164 00165 if (is_header) { 00166 if (is_describe) { 00167 printf("\n%6s", "frame#:"); 00168 } 00169 00170 for (j = 0; j < column; ++j) { 00171 printf("%3s%3d%s ", "c[", j, "]"); 00172 } 00173 printf("\n"); 00174 } 00175 00176 offset += frm_begin * vsize; 00177 for (i = frm_begin; i < frm_end; ++i) { 00178 if (is_describe) { 00179 printf("%6d:", i); 00180 } 00181 for (j = 0; j < column; ++j) 00182 printf("%7.3f ", z[offset + j]); 00183 printf("\n"); 00184 00185 offset += vsize; 00186 } 00187 fflush(stdout); 00188 cmd_ln_appl_exit(); 00189 ckd_free_2d(cep); 00190 00191 return (IO_SUCCESS); 00192 00193 } 00194 00195 int 00196 read_cep(char const *file, float ***cep, int *numframes, int cepsize) 00197 { 00198 FILE *fp; 00199 int n_float; 00200 struct stat statbuf; 00201 int i, n, byterev; 00202 float32 **mfcbuf; 00203 00204 if (stat_retry(file, &statbuf) < 0) { 00205 E_ERROR_SYSTEM("Failed to get file size '%s'", file); 00206 return IO_ERR; 00207 } 00208 00209 if ((fp = fopen(file, "rb")) == NULL) { 00210 E_ERROR_SYSTEM("Failed to open '%s' for reading", file); 00211 return IO_ERR; 00212 } 00213 00214 /* Read #floats in header */ 00215 if (fread(&n_float, sizeof(int), 1, fp) != 1) { 00216 fclose(fp); 00217 return IO_ERR; 00218 } 00219 00220 /* Check if n_float matches file size */ 00221 byterev = FALSE; 00222 if ((int) (n_float * sizeof(float) + 4) != statbuf.st_size) { 00223 n = n_float; 00224 SWAP_INT32(&n); 00225 00226 if ((int) (n * sizeof(float) + 4) != statbuf.st_size) { 00227 E_ERROR("Header size field: %d(%08x); filesize: %d(%08x)\n", 00228 n_float, n_float, (int) statbuf.st_size, 00229 (int) statbuf.st_size); 00230 fclose(fp); 00231 return IO_ERR; 00232 } 00233 00234 n_float = n; 00235 byterev = TRUE; 00236 } 00237 if (n_float <= 0) { 00238 E_ERROR("Header size field: %d\n", n_float); 00239 fclose(fp); 00240 return IO_ERR; 00241 } 00242 00243 /* n = #frames of input */ 00244 n = n_float / cepsize; 00245 if (n * cepsize != n_float) { 00246 E_ERROR("Header size field: %d; not multiple of %d\n", 00247 n_float, cepsize); 00248 fclose(fp); 00249 return IO_ERR; 00250 } 00251 00252 mfcbuf = (float **) ckd_calloc_2d(n, cepsize, sizeof(float32)); 00253 00254 /* Read mfc data and byteswap if necessary */ 00255 n_float = n * cepsize; 00256 if ((int) fread(mfcbuf[0], sizeof(float), n_float, fp) != n_float) { 00257 E_ERROR("Error reading mfc data from the file '%s'", file); 00258 fclose(fp); 00259 return IO_ERR; 00260 } 00261 if (byterev) { 00262 for (i = 0; i < n_float; i++) 00263 SWAP_FLOAT32(&(mfcbuf[0][i])); 00264 } 00265 fclose(fp); 00266 00267 *numframes = n; 00268 *cep = mfcbuf; 00269 return IO_SUCCESS; 00270 } 00271 00273 #if defined(_WIN32_WCE) 00274 #pragma comment(linker,"/entry:mainWCRTStartup") 00275 00276 //Windows Mobile has the Unicode main only 00277 int wmain(int32 argc, wchar_t *wargv[]) { 00278 char** argv; 00279 size_t wlen; 00280 size_t len; 00281 int i; 00282 00283 argv = malloc(argc*sizeof(char*)); 00284 for (i=0; i<argc; i++){ 00285 wlen = lstrlenW(wargv[i]); 00286 len = wcstombs(NULL, wargv[i], wlen); 00287 argv[i] = malloc(len+1); 00288 wcstombs(argv[i], wargv[i], wlen); 00289 } 00290 00291 //assuming ASCII parameters 00292 return main(argc, argv); 00293 } 00294 #endif