00001
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043 #include <sent/stddefs.h>
00044 #include <sent/htk_param.h>
00045 #include <sys/types.h>
00046
00047 static boolean needswap;
00048
00059 static boolean
00060 myread(char *buf, size_t unitbyte, int unitnum, FILE *fp)
00061 {
00062 size_t tmp;
00063 if ((tmp = myfread(buf, unitbyte, unitnum, fp)) < (size_t)unitnum) {
00064 perror("Error: cannot read\n");
00065 return(FALSE);
00066 }
00067
00068 if (needswap) swap_bytes(buf, unitbyte, unitnum);
00069 return(TRUE);
00070 }
00071
00072
00081 static boolean
00082 read_param(FILE *fp, HTK_Param *pinfo)
00083 {
00084 unsigned int i;
00085 int v;
00086 float *a = NULL, *b = NULL;
00087 char *buf = NULL;
00088 char *p;
00089 float d;
00090 unsigned short c;
00091 HTK_Param_Header *hd;
00092
00093 hd = &(pinfo->header);
00094
00095
00096
00097 #ifdef WORDS_BIGENDIAN
00098 needswap = FALSE;
00099 #else
00100 needswap = TRUE;
00101 #endif
00102
00103
00104 if(!myread((char *)&(hd->samplenum), sizeof(unsigned int), 1, fp)) return(FALSE);
00105
00106 if (hd->samplenum >= 60000) {
00107 j_printerr("Warning: data corrupted?: %d frames (more than 10 minutes)\n", hd->samplenum);
00108 j_printerr("Warning: maybe MFCC made by wav2mfcc on a little-endian machine.\n");
00109 j_printerr("retrying reading with endian conversion...\n");
00110 swap_bytes((char *)&(hd->samplenum), sizeof(unsigned int), 1);
00111 needswap = ! needswap;
00112 }
00113
00114 myread((char *)&(hd->wshift), sizeof(unsigned int), 1, fp);
00115 myread((char *)&(hd->sampsize), sizeof(unsigned short), 1, fp);
00116 myread((char *)&(hd->samptype), sizeof(short), 1, fp);
00117 if (hd->samptype & F_COMPRESS) {
00118 pinfo->veclen = hd->sampsize / sizeof(short);
00119 } else {
00120 pinfo->veclen = hd->sampsize / sizeof(float);
00121 }
00122
00123 if (hd->samptype & F_COMPRESS) {
00124 hd->samplenum -= sizeof(float);
00125
00126 a = (float *)mymalloc(sizeof(float) * pinfo->veclen);
00127 b = (float *)mymalloc(sizeof(float) * pinfo->veclen);
00128 myread((char *)a, sizeof(float), pinfo->veclen, fp);
00129 myread((char *)b, sizeof(float), pinfo->veclen, fp);
00130 }
00131 pinfo->samplenum = hd->samplenum;
00132
00133 buf = (char *)mymalloc(hd->sampsize);
00134
00135
00136 pinfo->parvec = (VECT **)mymalloc(sizeof(VECT *) * hd->samplenum);
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151 for (i=0;i<hd->samplenum;i++) {
00152 pinfo->parvec[i] = (VECT *)mymalloc(sizeof(VECT) * pinfo->veclen);
00153 if (hd->samptype & F_COMPRESS) {
00154 myread(buf, sizeof(short), hd->sampsize / sizeof(short), fp);
00155 p = buf;
00156
00157 for (v=0;v<pinfo->veclen;v++) {
00158 d = *(short *)p;
00159 pinfo->parvec[i][v] = (d + b[v]) / a[v];
00160 p += sizeof(short);
00161 }
00162 } else {
00163 myread(buf, sizeof(float), hd->sampsize / sizeof(float), fp);
00164 p = buf;
00165 for (v=0;v<pinfo->veclen;v++) {
00166 d = *(float *)p;
00167 pinfo->parvec[i][v] = d;
00168 p += sizeof(float);
00169 }
00170 }
00171 }
00172
00173 if (hd->samptype & F_CHECKSUM) {
00174
00175
00176 myread((char *)&c, sizeof(unsigned short), 1, fp);
00177 }
00178
00179
00180
00181 if (hd->samptype & F_COMPRESS) {
00182 free(a);
00183 free(b);
00184 }
00185 free(buf);
00186
00187 return(TRUE);
00188
00189 }
00190
00196 HTK_Param *
00197 new_param()
00198 {
00199 HTK_Param *ret;
00200 ret = (HTK_Param *)mymalloc(sizeof(HTK_Param));
00201 ret->parvec = NULL;
00202 ret->samplenum = 0;
00203 return(ret);
00204 }
00205
00211 void
00212 free_param(HTK_Param *pinfo)
00213 {
00214 unsigned int i;
00215 if (pinfo->samplenum > 0) {
00216 for (i=0;i<pinfo->samplenum;i++) {
00217 free(pinfo->parvec[i]);
00218 }
00219 }
00220 if (pinfo->parvec != NULL) free(pinfo->parvec);
00221 free(pinfo);
00222 }
00223
00232 boolean
00233 rdparam(char *filename, HTK_Param *pinfo)
00234 {
00235 FILE *fp;
00236 boolean retflag;
00237
00238 if ((fp = fopen_readfile(filename)) == NULL) return(FALSE);
00239 retflag = read_param(fp, pinfo);
00240 if (fclose_readfile(fp) < 0) return (FALSE);
00241 return (retflag);
00242 }