| 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/err.h> |
| 54 |
#include <sphinxbase/bio.h> |
| 55 |
#include <sphinxbase/pio.h> |
| 56 |
|
| 57 |
/** \file main_cepview.c |
| 58 |
\brief Main driver of cepview |
| 59 |
*/ |
| 60 |
#define IO_ERR (-1) |
| 61 |
#define IO_SUCCESS (0) |
| 62 |
|
| 63 |
#define SHOW_ALL "-1" |
| 64 |
|
| 65 |
/* Default cepstral vector size */ |
| 66 |
#define NUM_COEFF "13" |
| 67 |
|
| 68 |
/* Default display size, i.e., number of coefficients displayed, less |
| 69 |
* than the vector size so we display one frame per line. |
| 70 |
*/ |
| 71 |
#define DISPLAY_SIZE "10" |
| 72 |
#define STR_MAX_INT "2147483647" |
| 73 |
|
| 74 |
static arg_t arg[] = { |
| 75 |
{"-i", |
| 76 |
ARG_INT32, |
| 77 |
NUM_COEFF, |
| 78 |
"Number of coefficients in the feature vector."}, |
| 79 |
{"-d", |
| 80 |
ARG_INT32, |
| 81 |
DISPLAY_SIZE, |
| 82 |
"Number of displayed coefficients."}, |
| 83 |
{"-header", |
| 84 |
ARG_INT32, |
| 85 |
"0", |
| 86 |
"Whether header is shown."}, |
| 87 |
{"-describe", |
| 88 |
ARG_INT32, |
| 89 |
"0", |
| 90 |
"Whether description will be shown."}, |
| 91 |
{"-b", |
| 92 |
ARG_INT32, |
| 93 |
"0", |
| 94 |
"The beginning frame 0-based."}, |
| 95 |
{"-e", |
| 96 |
ARG_INT32, |
| 97 |
"2147483647", |
| 98 |
"The ending frame."}, |
| 99 |
{"-f", |
| 100 |
ARG_STRING, |
| 101 |
NULL, |
| 102 |
"Input feature file."}, |
| 103 |
{NULL, ARG_INT32, NULL, NULL} |
| 104 |
}; |
| 105 |
|
| 106 |
int read_cep(char const *file, float ***cep, int *nframes, int numcep); |
| 107 |
|
| 108 |
int |
| 109 |
main(int argc, char *argv[]) |
| 110 |
{ |
| 111 |
int i, j, offset; |
| 112 |
int32 noframe, vsize, dsize, column; |
| 113 |
int32 frm_begin, frm_end; |
| 114 |
int is_header, is_describe; |
| 115 |
float *z, **cep; |
| 116 |
char const *cepfile; |
| 117 |
|
| 118 |
cmd_ln_appl_enter(argc, argv, "default.arg", arg); |
| 119 |
|
| 120 |
vsize = cmd_ln_int32("-i"); |
| 121 |
dsize = cmd_ln_int32("-d"); |
| 122 |
frm_begin = cmd_ln_int32("-b"); |
| 123 |
frm_end = cmd_ln_int32("-e"); |
| 124 |
is_header = cmd_ln_int32("-header"); |
| 125 |
is_describe = cmd_ln_int32("-describe"); |
| 126 |
|
| 127 |
if (vsize < 0) |
| 128 |
E_FATAL("-i : Input vector size should be larger than 0.\n"); |
| 129 |
if (dsize < 0) |
| 130 |
E_FATAL("-d : Column size should be larger than 0\n"); |
| 131 |
if (frm_begin < 0) |
| 132 |
E_FATAL("-b : Beginning frame should be larger than 0\n"); |
| 133 |
/* The following condition is redundant |
| 134 |
* if (frm_end < 0) E_FATAL("-e : Ending frame should be larger than 0\n"); |
| 135 |
*/ |
| 136 |
if (frm_begin >= frm_end) |
| 137 |
E_FATAL |
| 138 |
("Ending frame (-e) should be larger than beginning frame (-b).\n"); |
| 139 |
|
| 140 |
if ((cepfile = cmd_ln_str("-f")) == NULL) { |
| 141 |
E_FATAL("Input file was not specified with (-f)\n"); |
| 142 |
} |
| 143 |
if (read_cep(cepfile, &cep, &noframe, vsize) == IO_ERR) |
| 144 |
E_FATAL_SYSTEM("Failed to open '%s' for reading", cepfile); |
| 145 |
|
| 146 |
z = cep[0]; |
| 147 |
|
| 148 |
offset = 0; |
| 149 |
column = (vsize > dsize) ? dsize : vsize; |
| 150 |
frm_end = (frm_end > noframe) ? noframe : frm_end; |
| 151 |
|
| 152 |
E_INFO("Displaying %d out of %d columns per frame\n", column, vsize); |
| 153 |
E_INFO("Total %d frames\n\n", noframe); |
| 154 |
|
| 155 |
/* This part should be moved to a special library if this file is |
| 156 |
longer than 300 lines. */ |
| 157 |
|
| 158 |
if (is_header) { |
| 159 |
if (is_describe) { |
| 160 |
printf("\n%6s", "frame#:"); |
| 161 |
} |
| 162 |
|
| 163 |
for (j = 0; j < column; ++j) { |
| 164 |
printf("%3s%3d%s ", "c[", j, "]"); |
| 165 |
} |
| 166 |
printf("\n"); |
| 167 |
} |
| 168 |
|
| 169 |
offset += frm_begin * vsize; |
| 170 |
for (i = frm_begin; i < frm_end; ++i) { |
| 171 |
if (is_describe) { |
| 172 |
printf("%6d:", i); |
| 173 |
} |
| 174 |
for (j = 0; j < column; ++j) |
| 175 |
printf("%7.3f ", z[offset + j]); |
| 176 |
printf("\n"); |
| 177 |
|
| 178 |
offset += vsize; |
| 179 |
} |
| 180 |
fflush(stdout); |
| 181 |
cmd_ln_appl_exit(); |
| 182 |
ckd_free_2d(cep); |
| 183 |
|
| 184 |
return (IO_SUCCESS); |
| 185 |
|
| 186 |
} |
| 187 |
|
| 188 |
int |
| 189 |
read_cep(char const *file, float ***cep, int *numframes, int cepsize) |
| 190 |
{ |
| 191 |
FILE *fp; |
| 192 |
int n_float; |
| 193 |
struct stat statbuf; |
| 194 |
int i, n, byterev; |
| 195 |
float32 **mfcbuf; |
| 196 |
|
| 197 |
if (stat_retry(file, &statbuf) < 0) { |
| 198 |
E_ERROR_SYSTEM("Failed to get file size '%s'", file); |
| 199 |
return IO_ERR; |
| 200 |
} |
| 201 |
|
| 202 |
if ((fp = fopen(file, "rb")) == NULL) { |
| 203 |
E_ERROR_SYSTEM("Failed to open '%s' for reading", file); |
| 204 |
return IO_ERR; |
| 205 |
} |
| 206 |
|
| 207 |
/* Read #floats in header */ |
| 208 |
if (fread(&n_float, sizeof(int), 1, fp) != 1) { |
| 209 |
fclose(fp); |
| 210 |
return IO_ERR; |
| 211 |
} |
| 212 |
|
| 213 |
/* Check if n_float matches file size */ |
| 214 |
byterev = FALSE; |
| 215 |
if ((int) (n_float * sizeof(float) + 4) != statbuf.st_size) { |
| 216 |
n = n_float; |
| 217 |
SWAP_INT32(&n); |
| 218 |
|
| 219 |
if ((int) (n * sizeof(float) + 4) != statbuf.st_size) { |
| 220 |
E_ERROR("Header size field: %d(%08x); filesize: %d(%08x)\n", |
| 221 |
n_float, n_float, (int) statbuf.st_size, |
| 222 |
(int) statbuf.st_size); |
| 223 |
fclose(fp); |
| 224 |
return IO_ERR; |
| 225 |
} |
| 226 |
|
| 227 |
n_float = n; |
| 228 |
byterev = TRUE; |
| 229 |
} |
| 230 |
if (n_float <= 0) { |
| 231 |
E_ERROR("Header size field: %d\n", n_float); |
| 232 |
fclose(fp); |
| 233 |
return IO_ERR; |
| 234 |
} |
| 235 |
|
| 236 |
/* n = #frames of input */ |
| 237 |
n = n_float / cepsize; |
| 238 |
if (n * cepsize != n_float) { |
| 239 |
E_ERROR("Header size field: %d; not multiple of %d\n", |
| 240 |
n_float, cepsize); |
| 241 |
fclose(fp); |
| 242 |
return IO_ERR; |
| 243 |
} |
| 244 |
|
| 245 |
mfcbuf = (float **) ckd_calloc_2d(n, cepsize, sizeof(float32)); |
| 246 |
|
| 247 |
/* Read mfc data and byteswap if necessary */ |
| 248 |
n_float = n * cepsize; |
| 249 |
if ((int) fread(mfcbuf[0], sizeof(float), n_float, fp) != n_float) { |
| 250 |
E_ERROR("Error reading mfc data from the file '%s'", file); |
| 251 |
fclose(fp); |
| 252 |
return IO_ERR; |
| 253 |
} |
| 254 |
if (byterev) { |
| 255 |
for (i = 0; i < n_float; i++) |
| 256 |
SWAP_FLOAT32(&(mfcbuf[0][i])); |
| 257 |
} |
| 258 |
fclose(fp); |
| 259 |
|
| 260 |
*numframes = n; |
| 261 |
*cep = mfcbuf; |
| 262 |
return IO_SUCCESS; |
| 263 |
} |
| 264 |
|
| 265 |
/** Windows CE/Mobile entry point added. */ |
| 266 |
#if defined(_WIN32_WCE) |
| 267 |
#pragma comment(linker,"/entry:mainWCRTStartup") |
| 268 |
|
| 269 |
/* Windows Mobile has the Unicode main only */ |
| 270 |
int wmain(int32 argc, wchar_t *wargv[]) { |
| 271 |
char** argv; |
| 272 |
size_t wlen; |
| 273 |
size_t len; |
| 274 |
int i; |
| 275 |
|
| 276 |
argv = malloc(argc*sizeof(char*)); |
| 277 |
for (i = 0; i < argc; i++){ |
| 278 |
wlen = lstrlenW(wargv[i]); |
| 279 |
len = wcstombs(NULL, wargv[i], wlen); |
| 280 |
argv[i] = malloc(len+1); |
| 281 |
wcstombs(argv[i], wargv[i], wlen); |
| 282 |
} |
| 283 |
|
| 284 |
/* assuming ASCII parameters */ |
| 285 |
return main(argc, argv); |
| 286 |
} |
| 287 |
#endif |