Main Page | Modules | Data Structures | Directories | File List | Data Fields | Globals | Related Pages

rdparam.c

Go to the documentation of this file.
00001 
00030 /*
00031  * Copyright (c) 1991-2006 Kawahara Lab., Kyoto University
00032  * Copyright (c) 2000-2005 Shikano Lab., Nara Institute of Science and Technology
00033  * Copyright (c) 2005-2006 Julius project team, Nagoya Institute of Technology, Nagoya Institute of Technology
00034  * All rights reserved
00035  */
00036 
00037 /* assume sizeof: */
00038 /*            float = 4 */
00039 /* (unsigned)   int = 4 */
00040 /* (unsigned) short = 2 */
00041 /* (unsigned)  char = 1 */
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   /* swap if necessary */
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; /* for uncompressing */
00088   char *p;
00089   float d;
00090   unsigned short c;
00091   HTK_Param_Header *hd;
00092 
00093   hd = &(pinfo->header);
00094 
00095   /* endian check once */
00096   /* assume input as BIG ENDIAN */
00097 #ifdef WORDS_BIGENDIAN
00098   needswap = FALSE;
00099 #else  /* LITTLE ENDIAN */
00100   needswap = TRUE;
00101 #endif
00102   
00103   /* read in headers */
00104   if(!myread((char *)&(hd->samplenum), sizeof(unsigned int), 1, fp)) return(FALSE);
00105   /* try to detect and read little-endian parameters from wav2mfcc... */
00106   if (hd->samplenum >= 60000) { /* more than 10 minutes! */
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     /* read in compression coefficient arrays */
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   /* read in parameter vector */
00136   pinfo->parvec = (VECT **)mymalloc(sizeof(VECT *) * hd->samplenum);
00137 /* 
00138  *   {
00139  *     int size;
00140  *     float *ftmp;
00141  *     size = hd->samplenum * pinfo->veclen;
00142  *     if (size != 0) {
00143  *       ftmp = (float *)mymalloc(sizeof(float) * size);
00144  *       for(i=0;i<hd->samplenum;i++) {
00145  *         pinfo->parvec[i] = &(ftmp[i * pinfo->veclen]);
00146  *       }
00147  *     }
00148  *   }
00149  */
00150   /* needs conversion of integerized */
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       /* uncompress: (short(2byte) -> float(4byte)) * veclen*/
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     /* CRC check (2byte) */
00175     /* skip this */
00176     myread((char *)&c, sizeof(unsigned short), 1, fp);
00177   }
00178 
00179   /*put_param(pinfo);*/
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 }

Generated on Tue Mar 28 16:17:43 2006 for Julius by  doxygen 1.4.2