• Main Page
  • Related Pages
  • Data Structures
  • Files
  • File List
  • Globals

src/sphinx_cepview/main_cepview.c

Go to the documentation of this file.
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  * HISTORY
00038  * 
00039  * circa 1994   P J Moreno at Carnegie Mellon
00040  *              Created.
00041  *
00042  * For history information, please use 'cvs log'
00043  * $Log$
00044  * Revision 1.11  2006/02/24  04:06:43  arthchan2003
00045  * Merged from branch SPHINX3_5_2_RCI_IRII_BRANCH: Changed commands to macro.  Used E_INFO instead of printf in displaying no. of friends
00046  * 
00047  *
00048  * Revision 1.10  2005/08/18 21:18:09  egouvea
00049  * Added E_INFO displaying information about how many columns are being printed, and how many frames
00050  *
00051  * Revision 1.9.4.2  2005/09/07 23:51:05  arthchan2003
00052  * Fixed keyword expansion problem
00053  *
00054  * Revision 1.9.4.1  2005/07/18 23:21:23  arthchan2003
00055  * Tied command-line arguments with marcos
00056  *
00057  * Revision 1.10  2005/08/18 21:18:09  egouvea
00058  * Added E_INFO displaying information about how many columns are being printed, and how many frames
00059  *
00060  * Revision 1.9  2005/06/22 05:38:45  arthchan2003
00061  * Add
00062  *
00063  * Revision 1.2  2005/03/30 00:43:41  archan
00064  *
00065  * Add $Log$
00066  * Revision 1.11  2006/02/24  04:06:43  arthchan2003
00067  * Merged from branch SPHINX3_5_2_RCI_IRII_BRANCH: Changed commands to macro.  Used E_INFO instead of printf in displaying no. of friends
00068  * 
00069  *
00070  */
00071 
00072 #include <stdio.h>
00073 #include <stdlib.h>
00074 #include <string.h>
00075 #include <strfuncs.h>
00076 
00077 #ifdef _WIN32
00078 #pragma warning (disable: 4996) 
00079 #endif
00080 
00081 #ifdef HAVE_CONFIG_H
00082 #include <config.h>
00083 #endif
00084 
00085 #include "prim_type.h"
00086 #include "cmd_ln.h"
00087 #include "ckd_alloc.h"
00088 #include "info.h"
00089 #include "err.h"
00090 #include "bio.h"
00091 
00092 /* Silvio Moioli: switched to stat_reply that's Windows CE friendly. */
00093 #include "pio.h"
00094 
00098 #define IO_ERR  (-1)
00099 #define IO_SUCCESS  (0)
00100 
00101 #define SHOW_ALL "-1"
00102 
00103 /* Default cepstral vector size */
00104 #define NUM_COEFF  "13"
00105 
00106 /* Default display size, i.e., number of coefficients displayed, less
00107  * than the vector size so we display one frame per line.
00108  */
00109 #define DISPLAY_SIZE "10"
00110 #define STR_MAX_INT "2147483647"
00111 
00112 static arg_t arg[] = {
00113 
00114     {"-logfn",
00115      ARG_STRING,
00116      NULL,
00117      "Log file (default stdout/stderr)"},
00118     {"-i",
00119      ARG_INT32,
00120      NUM_COEFF,
00121      "Number of coefficients in the feature vector."},
00122     {"-d",
00123      ARG_INT32,
00124      DISPLAY_SIZE,
00125      "Number of displayed coefficients."},
00126     {"-header",
00127      ARG_INT32,
00128      "0",
00129      "Whether header is shown."},
00130     {"-describe",
00131      ARG_INT32,
00132      "0",
00133      "Whether description will be shown."},
00134     {"-b",
00135      ARG_INT32,
00136      "0",
00137      "The beginning frame 0-based."},
00138     {"-e",
00139      ARG_INT32,
00140      "2147483647",
00141      "The ending frame."},
00142     {"-f",
00143      ARG_STRING,
00144      NULL,
00145      "Input feature file."},
00146     {NULL, ARG_INT32, NULL, NULL}
00147 };
00148 
00149 int read_cep(char const *file, float ***cep, int *nframes, int numcep);
00150 
00151 int
00152 main(int argc, char *argv[])
00153 {
00154     int i, j, offset;
00155     int32 noframe, vsize, dsize, column;
00156     int32 frm_begin, frm_end;
00157     int is_header, is_describe;
00158     float *z, **cep;
00159     char const *cepfile;
00160 
00161     print_appl_info(argv[0]);
00162     cmd_ln_appl_enter(argc, argv, "default.arg", arg);
00163 
00164     vsize = cmd_ln_int32("-i");
00165     dsize = cmd_ln_int32("-d");
00166     frm_begin = cmd_ln_int32("-b");
00167     frm_end = cmd_ln_int32("-e");
00168     is_header = cmd_ln_int32("-header");
00169     is_describe = cmd_ln_int32("-describe");
00170 
00171     if (vsize < 0)
00172         E_FATAL("-i : Input vector size should be larger than 0.\n");
00173     if (dsize < 0)
00174         E_FATAL("-d : Column size should be larger than 0\n");
00175     if (frm_begin < 0)
00176         E_FATAL("-b : Beginning frame should be larger than 0\n");
00177     /* The following condition is redundant
00178      * if (frm_end < 0) E_FATAL("-e : Ending frame should be larger than 0\n");
00179      */
00180     if (frm_begin >= frm_end)
00181         E_FATAL
00182             ("Ending frame (-e) should be larger than beginning frame (-b).\n");
00183 
00184     if ((cepfile = cmd_ln_str("-f")) == NULL) {
00185         E_FATAL("Input file was not specified with (-f)\n");
00186     }
00187     if (read_cep(cepfile, &cep, &noframe, vsize) == IO_ERR)
00188         E_FATAL("ERROR opening %s for reading\n", cepfile);
00189 
00190     z = cep[0];
00191 
00192     offset = 0;
00193     column = (vsize > dsize) ? dsize : vsize;
00194     frm_end = (frm_end > noframe) ? noframe : frm_end;
00195 
00196     E_INFO("Displaying %d out of %d columns per frame\n", column, vsize);
00197     E_INFO("Total %d frames\n\n", noframe);
00198 
00199     /* This part should be moved to a special library if this file is
00200        longer than 300 lines. */
00201 
00202     if (is_header) {
00203         if (is_describe) {
00204             printf("\n%6s", "frame#:");
00205         }
00206 
00207         for (j = 0; j < column; ++j) {
00208             printf("%3s%3d%s ", "c[", j, "]");
00209         }
00210         printf("\n");
00211     }
00212 
00213     offset += frm_begin * vsize;
00214     for (i = frm_begin; i < frm_end; ++i) {
00215         if (is_describe) {
00216             printf("%6d:", i);
00217         }
00218         for (j = 0; j < column; ++j)
00219             printf("%7.3f ", z[offset + j]);
00220         printf("\n");
00221 
00222         offset += vsize;
00223     }
00224     fflush(stdout);
00225     cmd_ln_appl_exit();
00226     ckd_free_2d(cep);
00227 
00228     return (IO_SUCCESS);
00229 
00230 }
00231 
00232 int
00233 read_cep(char const *file, float ***cep, int *numframes, int cepsize)
00234 {
00235     FILE *fp;
00236     int n_float;
00237     struct stat statbuf;
00238     int i, n, byterev, sf, ef;
00239     float32 **mfcbuf;
00240 
00241     if (stat_retry(file, &statbuf) < 0) {
00242         printf("stat(%s) failed\n", file);
00243         return IO_ERR;
00244     }
00245 
00246     if ((fp = fopen(file, "rb")) == NULL) {
00247         printf("fopen(%s, rb) failed\n", file);
00248         return IO_ERR;
00249     }
00250 
00251     /* Read #floats in header */
00252     if (fread(&n_float, sizeof(int), 1, fp) != 1) {
00253         fclose(fp);
00254         return IO_ERR;
00255     }
00256 
00257     /* Check if n_float matches file size */
00258     byterev = FALSE;
00259     if ((int) (n_float * sizeof(float) + 4) != statbuf.st_size) {
00260         n = n_float;
00261         SWAP_INT32(&n);
00262 
00263         if ((int) (n * sizeof(float) + 4) != statbuf.st_size) {
00264             printf
00265                 ("Header size field: %d(%08x); filesize: %d(%08x)\n",
00266                  n_float, n_float, (int) statbuf.st_size,
00267                  (int) statbuf.st_size);
00268             fclose(fp);
00269             return IO_ERR;
00270         }
00271 
00272         n_float = n;
00273         byterev = TRUE;
00274     }
00275     if (n_float <= 0) {
00276         printf("Header size field: %d\n", n_float);
00277         fclose(fp);
00278         return IO_ERR;
00279     }
00280 
00281     /* n = #frames of input */
00282     n = n_float / cepsize;
00283     if (n * cepsize != n_float) {
00284         printf("Header size field: %d; not multiple of %d\n",
00285                n_float, cepsize);
00286         fclose(fp);
00287         return IO_ERR;
00288     }
00289     sf = 0;
00290     ef = n;
00291 
00292     mfcbuf = (float **) ckd_calloc_2d(n, cepsize, sizeof(float32));
00293 
00294     /* Read mfc data and byteswap if necessary */
00295     n_float = n * cepsize;
00296     if ((int) fread(mfcbuf[0], sizeof(float), n_float, fp) != n_float) {
00297         printf("Error reading mfc data\n");
00298         fclose(fp);
00299         return IO_ERR;
00300     }
00301     if (byterev) {
00302         for (i = 0; i < n_float; i++)
00303             SWAP_FLOAT32(&(mfcbuf[0][i]));
00304     }
00305     fclose(fp);
00306 
00307     *numframes = n;
00308     *cep = mfcbuf;
00309     return IO_SUCCESS;
00310 }
00311 
00313 #if defined(_WIN32_WCE)
00314 #pragma comment(linker,"/entry:mainWCRTStartup")
00315 
00316 //Windows Mobile has the Unicode main only
00317 int wmain(int32 argc, wchar_t *wargv[]) {
00318     char** argv;
00319     size_t wlen;
00320     size_t len;
00321     int i;
00322 
00323     argv = malloc(argc*sizeof(char*));
00324     for (i=0; i<argc; i++){
00325         wlen = lstrlenW(wargv[i]);
00326         len = wcstombs(NULL, wargv[i], wlen);
00327         argv[i] = malloc(len+1);
00328         wcstombs(argv[i], wargv[i], wlen);
00329     }
00330 
00331     //assuming ASCII parameters
00332     return main(argc, argv);
00333 }
00334 #endif

Generated on Thu Jan 6 2011 for SphinxBase by  doxygen 1.7.1