SphinxBase 0.6

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 
00076 #ifdef _WIN32
00077 #pragma warning (disable: 4996) 
00078 #endif
00079 
00080 #ifdef HAVE_CONFIG_H
00081 #include <config.h>
00082 #endif
00083 
00084 #include <sphinxbase/strfuncs.h>
00085 #include <sphinxbase/prim_type.h>
00086 #include <sphinxbase/cmd_ln.h>
00087 #include <sphinxbase/ckd_alloc.h>
00088 #include <sphinxbase/info.h>
00089 #include <sphinxbase/err.h>
00090 #include <sphinxbase/bio.h>
00091 #include <sphinxbase/pio.h>
00092 
00096 #define IO_ERR  (-1)
00097 #define IO_SUCCESS  (0)
00098 
00099 #define SHOW_ALL "-1"
00100 
00101 /* Default cepstral vector size */
00102 #define NUM_COEFF  "13"
00103 
00104 /* Default display size, i.e., number of coefficients displayed, less
00105  * than the vector size so we display one frame per line.
00106  */
00107 #define DISPLAY_SIZE "10"
00108 #define STR_MAX_INT "2147483647"
00109 
00110 static arg_t arg[] = {
00111 
00112     {"-logfn",
00113      ARG_STRING,
00114      NULL,
00115      "Log file (default stdout/stderr)"},
00116     {"-i",
00117      ARG_INT32,
00118      NUM_COEFF,
00119      "Number of coefficients in the feature vector."},
00120     {"-d",
00121      ARG_INT32,
00122      DISPLAY_SIZE,
00123      "Number of displayed coefficients."},
00124     {"-header",
00125      ARG_INT32,
00126      "0",
00127      "Whether header is shown."},
00128     {"-describe",
00129      ARG_INT32,
00130      "0",
00131      "Whether description will be shown."},
00132     {"-b",
00133      ARG_INT32,
00134      "0",
00135      "The beginning frame 0-based."},
00136     {"-e",
00137      ARG_INT32,
00138      "2147483647",
00139      "The ending frame."},
00140     {"-f",
00141      ARG_STRING,
00142      NULL,
00143      "Input feature file."},
00144     {NULL, ARG_INT32, NULL, NULL}
00145 };
00146 
00147 int read_cep(char const *file, float ***cep, int *nframes, int numcep);
00148 
00149 int
00150 main(int argc, char *argv[])
00151 {
00152     int i, j, offset;
00153     int32 noframe, vsize, dsize, column;
00154     int32 frm_begin, frm_end;
00155     int is_header, is_describe;
00156     float *z, **cep;
00157     char const *cepfile;
00158 
00159     print_appl_info(argv[0]);
00160     cmd_ln_appl_enter(argc, argv, "default.arg", arg);
00161 
00162     vsize = cmd_ln_int32("-i");
00163     dsize = cmd_ln_int32("-d");
00164     frm_begin = cmd_ln_int32("-b");
00165     frm_end = cmd_ln_int32("-e");
00166     is_header = cmd_ln_int32("-header");
00167     is_describe = cmd_ln_int32("-describe");
00168 
00169     if (vsize < 0)
00170         E_FATAL("-i : Input vector size should be larger than 0.\n");
00171     if (dsize < 0)
00172         E_FATAL("-d : Column size should be larger than 0\n");
00173     if (frm_begin < 0)
00174         E_FATAL("-b : Beginning frame should be larger than 0\n");
00175     /* The following condition is redundant
00176      * if (frm_end < 0) E_FATAL("-e : Ending frame should be larger than 0\n");
00177      */
00178     if (frm_begin >= frm_end)
00179         E_FATAL
00180             ("Ending frame (-e) should be larger than beginning frame (-b).\n");
00181 
00182     if ((cepfile = cmd_ln_str("-f")) == NULL) {
00183         E_FATAL("Input file was not specified with (-f)\n");
00184     }
00185     if (read_cep(cepfile, &cep, &noframe, vsize) == IO_ERR)
00186         E_FATAL("ERROR opening %s for reading\n", cepfile);
00187 
00188     z = cep[0];
00189 
00190     offset = 0;
00191     column = (vsize > dsize) ? dsize : vsize;
00192     frm_end = (frm_end > noframe) ? noframe : frm_end;
00193 
00194     E_INFO("Displaying %d out of %d columns per frame\n", column, vsize);
00195     E_INFO("Total %d frames\n\n", noframe);
00196 
00197     /* This part should be moved to a special library if this file is
00198        longer than 300 lines. */
00199 
00200     if (is_header) {
00201         if (is_describe) {
00202             printf("\n%6s", "frame#:");
00203         }
00204 
00205         for (j = 0; j < column; ++j) {
00206             printf("%3s%3d%s ", "c[", j, "]");
00207         }
00208         printf("\n");
00209     }
00210 
00211     offset += frm_begin * vsize;
00212     for (i = frm_begin; i < frm_end; ++i) {
00213         if (is_describe) {
00214             printf("%6d:", i);
00215         }
00216         for (j = 0; j < column; ++j)
00217             printf("%7.3f ", z[offset + j]);
00218         printf("\n");
00219 
00220         offset += vsize;
00221     }
00222     fflush(stdout);
00223     cmd_ln_appl_exit();
00224     ckd_free_2d(cep);
00225 
00226     return (IO_SUCCESS);
00227 
00228 }
00229 
00230 int
00231 read_cep(char const *file, float ***cep, int *numframes, int cepsize)
00232 {
00233     FILE *fp;
00234     int n_float;
00235     struct stat statbuf;
00236     int i, n, byterev, sf, ef;
00237     float32 **mfcbuf;
00238 
00239     if (stat_retry(file, &statbuf) < 0) {
00240         printf("stat(%s) failed\n", file);
00241         return IO_ERR;
00242     }
00243 
00244     if ((fp = fopen(file, "rb")) == NULL) {
00245         printf("Failed to open '%s' for reading: %s\n", file, strerror(errno));
00246         return IO_ERR;
00247     }
00248 
00249     /* Read #floats in header */
00250     if (fread(&n_float, sizeof(int), 1, fp) != 1) {
00251         fclose(fp);
00252         return IO_ERR;
00253     }
00254 
00255     /* Check if n_float matches file size */
00256     byterev = FALSE;
00257     if ((int) (n_float * sizeof(float) + 4) != statbuf.st_size) {
00258         n = n_float;
00259         SWAP_INT32(&n);
00260 
00261         if ((int) (n * sizeof(float) + 4) != statbuf.st_size) {
00262             printf
00263                 ("Header size field: %d(%08x); filesize: %d(%08x)\n",
00264                  n_float, n_float, (int) statbuf.st_size,
00265                  (int) statbuf.st_size);
00266             fclose(fp);
00267             return IO_ERR;
00268         }
00269 
00270         n_float = n;
00271         byterev = TRUE;
00272     }
00273     if (n_float <= 0) {
00274         printf("Header size field: %d\n", n_float);
00275         fclose(fp);
00276         return IO_ERR;
00277     }
00278 
00279     /* n = #frames of input */
00280     n = n_float / cepsize;
00281     if (n * cepsize != n_float) {
00282         printf("Header size field: %d; not multiple of %d\n",
00283                n_float, cepsize);
00284         fclose(fp);
00285         return IO_ERR;
00286     }
00287     sf = 0;
00288     ef = n;
00289 
00290     mfcbuf = (float **) ckd_calloc_2d(n, cepsize, sizeof(float32));
00291 
00292     /* Read mfc data and byteswap if necessary */
00293     n_float = n * cepsize;
00294     if ((int) fread(mfcbuf[0], sizeof(float), n_float, fp) != n_float) {
00295         printf("Error reading mfc data\n");
00296         fclose(fp);
00297         return IO_ERR;
00298     }
00299     if (byterev) {
00300         for (i = 0; i < n_float; i++)
00301             SWAP_FLOAT32(&(mfcbuf[0][i]));
00302     }
00303     fclose(fp);
00304 
00305     *numframes = n;
00306     *cep = mfcbuf;
00307     return IO_SUCCESS;
00308 }
00309 
00311 #if defined(_WIN32_WCE)
00312 #pragma comment(linker,"/entry:mainWCRTStartup")
00313 
00314 //Windows Mobile has the Unicode main only
00315 int wmain(int32 argc, wchar_t *wargv[]) {
00316     char** argv;
00317     size_t wlen;
00318     size_t len;
00319     int i;
00320 
00321     argv = malloc(argc*sizeof(char*));
00322     for (i=0; i<argc; i++){
00323         wlen = lstrlenW(wargv[i]);
00324         len = wcstombs(NULL, wargv[i], wlen);
00325         argv[i] = malloc(len+1);
00326         wcstombs(argv[i], wargv[i], wlen);
00327     }
00328 
00329     //assuming ASCII parameters
00330     return main(argc, argv);
00331 }
00332 #endif