00001
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
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
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;
00089 char *p;
00090 float d;
00091 unsigned short c;
00092 HTK_Param_Header *hd;
00093
00094 hd = &(pinfo->header);
00095
00096
00097
00098 #ifdef WORDS_BIGENDIAN
00099 needswap = FALSE;
00100 #else
00101 needswap = TRUE;
00102 #endif
00103
00104
00105 if(!myread((char *)&(hd->samplenum), sizeof(unsigned int), 1, fp)) return(FALSE);
00106
00107 if (hd->samplenum == 1380533830) {
00108 jlog("Error: rdparam: input file is WAV file, not a parameter file\n");
00109 return FALSE;
00110 }
00111
00112
00113 if (hd->samplenum >= 60000) {
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
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
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
00149
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
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
00173
00174 myread((char *)&c, sizeof(unsigned short), 1, fp);
00175 }
00176
00177
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 }