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 >= 60000) {
00108 jlog("Warning: rdparam: header says it has %d frames (more than 10 minutes)\n", hd->samplenum);
00109 jlog("Warning: rdparam: it may be a little endian MFCC\n");
00110 jlog("Warning: rdparam: now try reading with endian conversion\n");
00111 swap_bytes((char *)&(hd->samplenum), sizeof(unsigned int), 1);
00112 needswap = ! needswap;
00113 }
00114
00115 myread((char *)&(hd->wshift), sizeof(unsigned int), 1, fp);
00116 myread((char *)&(hd->sampsize), sizeof(unsigned short), 1, fp);
00117 myread((char *)&(hd->samptype), sizeof(short), 1, fp);
00118 if (hd->samptype & F_COMPRESS) {
00119 pinfo->veclen = hd->sampsize / sizeof(short);
00120 } else {
00121 pinfo->veclen = hd->sampsize / sizeof(float);
00122 }
00123
00124 if (hd->samptype & F_COMPRESS) {
00125 hd->samplenum -= sizeof(float);
00126
00127 a = (float *)mymalloc(sizeof(float) * pinfo->veclen);
00128 b = (float *)mymalloc(sizeof(float) * pinfo->veclen);
00129 myread((char *)a, sizeof(float), pinfo->veclen, fp);
00130 myread((char *)b, sizeof(float), pinfo->veclen, fp);
00131 }
00132 pinfo->samplenum = hd->samplenum;
00133
00134 buf = (char *)mymalloc(hd->sampsize);
00135
00136
00137 if (param_alloc(pinfo, pinfo->samplenum, pinfo->veclen) == FALSE) {
00138 jlog("Error: rdparam: failed to allocate memory for reading MFCC\n");
00139 return FALSE;
00140 }
00141
00142
00143
00144 for (i=0;i<pinfo->samplenum;i++) {
00145 if (hd->samptype & F_COMPRESS) {
00146 myread(buf, sizeof(short), hd->sampsize / sizeof(short), fp);
00147 p = buf;
00148
00149 for (v=0;v<pinfo->veclen;v++) {
00150 d = *(short *)p;
00151 pinfo->parvec[i][v] = (d + b[v]) / a[v];
00152 p += sizeof(short);
00153 }
00154 } else {
00155 myread(buf, sizeof(float), hd->sampsize / sizeof(float), fp);
00156 p = buf;
00157 for (v=0;v<pinfo->veclen;v++) {
00158 d = *(float *)p;
00159 pinfo->parvec[i][v] = d;
00160 p += sizeof(float);
00161 }
00162 }
00163 }
00164
00165 if (hd->samptype & F_CHECKSUM) {
00166
00167
00168 myread((char *)&c, sizeof(unsigned short), 1, fp);
00169 }
00170
00171
00172
00173 free(buf);
00174 if (hd->samptype & F_COMPRESS) {
00175 free(b);
00176 free(a);
00177 }
00178
00179 return(TRUE);
00180
00181 }
00182
00191 boolean
00192 rdparam(char *filename, HTK_Param *pinfo)
00193 {
00194 FILE *fp;
00195 boolean retflag;
00196
00197 if ((fp = fopen_readfile(filename)) == NULL) return(FALSE);
00198 retflag = read_param(fp, pinfo);
00199 if (fclose_readfile(fp) < 0) return (FALSE);
00200 return (retflag);
00201 }