libsent/src/anlz/rdparam.c

Go to the documentation of this file.
00001 
00031 /*
00032  * Copyright (c) 1991-2007 Kawahara Lab., Kyoto University
00033  * Copyright (c) 2000-2005 Shikano Lab., Nara Institute of Science and Technology
00034  * Copyright (c) 2005-2007 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 and read little-endian parameters from wav2mfcc... */
00107   if (hd->samplenum >= 60000) { /* more than 10 minutes! */
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     /* read in compression coefficient arrays */
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   /* allocate memory for vectors */
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   /* read in parameter vector */
00143   /* needs conversion of integerized */
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       /* uncompress: (short(2byte) -> float(4byte)) * veclen*/
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     /* CRC check (2byte) */
00167     /* skip this */
00168     myread((char *)&c, sizeof(unsigned short), 1, fp);
00169   }
00170 
00171   /*put_param(stdout, pinfo);*/
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 }

Generated on Tue Dec 18 15:59:54 2007 for Julius by  doxygen 1.5.4