Julius 4.2
|
00001 00031 /* 00032 * Copyright (c) 1991-2011 Kawahara Lab., Kyoto University 00033 * Copyright (c) 2000-2005 Shikano Lab., Nara Institute of Science and Technology 00034 * Copyright (c) 2005-2011 Julius project team, Nagoya Institute of Technology 00035 * All rights reserved 00036 */ 00037 00038 /* assume sizeof: */ 00039 /* float = 4 */ 00040 /* (unsigned) int = 4 */ 00041 /* (unsigned) short = 2 */ 00042 /* (unsigned) char = 1 */ 00043 00044 #include <sent/stddefs.h> 00045 #include <sent/htk_param.h> 00046 #include <sys/types.h> 00047 00048 static boolean needswap; 00049 00060 static boolean 00061 myread(char *buf, size_t unitbyte, int unitnum, FILE *fp) 00062 { 00063 size_t tmp; 00064 if ((tmp = myfread(buf, unitbyte, unitnum, fp)) < (size_t)unitnum) { 00065 jlog("Error: rdparam: failed to read %d bytes\n", unitbyte * unitnum); 00066 return(FALSE); 00067 } 00068 /* swap if necessary */ 00069 if (needswap) swap_bytes(buf, unitbyte, unitnum); 00070 return(TRUE); 00071 } 00072 00073 00082 static boolean 00083 read_param(FILE *fp, HTK_Param *pinfo) 00084 { 00085 unsigned int i; 00086 int v; 00087 float *a = NULL, *b = NULL; 00088 char *buf = NULL; /* for uncompressing */ 00089 char *p; 00090 float d; 00091 unsigned short c; 00092 HTK_Param_Header *hd; 00093 00094 hd = &(pinfo->header); 00095 00096 /* endian check once */ 00097 /* assume input as BIG ENDIAN */ 00098 #ifdef WORDS_BIGENDIAN 00099 needswap = FALSE; 00100 #else /* LITTLE ENDIAN */ 00101 needswap = TRUE; 00102 #endif 00103 00104 /* read in headers */ 00105 if(!myread((char *)&(hd->samplenum), sizeof(unsigned int), 1, fp)) return(FALSE); 00106 /* try to detect wav file */ 00107 if (hd->samplenum == 1380533830) { /* read string "RIFF" as an integer */ 00108 jlog("Error: rdparam: input file is WAV file, not a parameter file\n"); 00109 return FALSE; 00110 } 00111 00112 /* try to detect and read little-endian parameters from wav2mfcc... */ 00113 if (hd->samplenum >= 60000) { /* more than 10 minutes! */ 00114 jlog("Warning: rdparam: header says it has %d frames (more than 10 minutes)\n", hd->samplenum); 00115 jlog("Warning: rdparam: it may be a little endian MFCC\n"); 00116 jlog("Warning: rdparam: now try reading with endian conversion\n"); 00117 swap_bytes((char *)&(hd->samplenum), sizeof(unsigned int), 1); 00118 needswap = ! needswap; 00119 } 00120 00121 myread((char *)&(hd->wshift), sizeof(unsigned int), 1, fp); 00122 myread((char *)&(hd->sampsize), sizeof(unsigned short), 1, fp); 00123 myread((char *)&(hd->samptype), sizeof(short), 1, fp); 00124 if (hd->samptype & F_COMPRESS) { 00125 pinfo->veclen = hd->sampsize / sizeof(short); 00126 } else { 00127 pinfo->veclen = hd->sampsize / sizeof(float); 00128 } 00129 00130 if (hd->samptype & F_COMPRESS) { 00131 hd->samplenum -= sizeof(float); /* (-_-) */ 00132 /* read in compression coefficient arrays */ 00133 a = (float *)mymalloc(sizeof(float) * pinfo->veclen); 00134 b = (float *)mymalloc(sizeof(float) * pinfo->veclen); 00135 myread((char *)a, sizeof(float), pinfo->veclen, fp); 00136 myread((char *)b, sizeof(float), pinfo->veclen, fp); 00137 } 00138 pinfo->samplenum = hd->samplenum; 00139 00140 buf = (char *)mymalloc(hd->sampsize); 00141 00142 /* allocate memory for vectors */ 00143 if (param_alloc(pinfo, pinfo->samplenum, pinfo->veclen) == FALSE) { 00144 jlog("Error: rdparam: failed to allocate memory for reading MFCC\n"); 00145 return FALSE; 00146 } 00147 00148 /* read in parameter vector */ 00149 /* needs conversion of integerized */ 00150 for (i=0;i<pinfo->samplenum;i++) { 00151 if (hd->samptype & F_COMPRESS) { 00152 myread(buf, sizeof(short), hd->sampsize / sizeof(short), fp); 00153 p = buf; 00154 /* uncompress: (short(2byte) -> float(4byte)) * veclen*/ 00155 for (v=0;v<pinfo->veclen;v++) { 00156 d = *(short *)p; 00157 pinfo->parvec[i][v] = (d + b[v]) / a[v]; 00158 p += sizeof(short); 00159 } 00160 } else { 00161 myread(buf, sizeof(float), hd->sampsize / sizeof(float), fp); 00162 p = buf; 00163 for (v=0;v<pinfo->veclen;v++) { 00164 d = *(float *)p; 00165 pinfo->parvec[i][v] = d; 00166 p += sizeof(float); 00167 } 00168 } 00169 } 00170 00171 if (hd->samptype & F_CHECKSUM) { 00172 /* CRC check (2byte) */ 00173 /* skip this */ 00174 myread((char *)&c, sizeof(unsigned short), 1, fp); 00175 } 00176 00177 /*put_param(stdout, pinfo);*/ 00178 00179 free(buf); 00180 if (hd->samptype & F_COMPRESS) { 00181 free(b); 00182 free(a); 00183 } 00184 00185 return(TRUE); 00186 00187 } 00188 00197 boolean 00198 rdparam(char *filename, HTK_Param *pinfo) 00199 { 00200 FILE *fp; 00201 boolean retflag; 00202 00203 if ((fp = fopen_readfile(filename)) == NULL) return(FALSE); 00204 retflag = read_param(fp, pinfo); 00205 if (fclose_readfile(fp) < 0) return (FALSE); 00206 return (retflag); 00207 }