libsent/src/adin/adin_file.c

Go to the documentation of this file.
00001 
00062 /*
00063  * Copyright (c) 1991-2007 Kawahara Lab., Kyoto University
00064  * Copyright (c) 2000-2005 Shikano Lab., Nara Institute of Science and Technology
00065  * Copyright (c) 2005-2007 Julius project team, Nagoya Institute of Technology
00066  * All rights reserved
00067  */
00068 
00069 
00070 #include <sent/stddefs.h>
00071 #include <sent/speech.h>
00072 #include <sent/adin.h>
00073 
00074 static FILE *gfp;               
00075 static boolean wav_p;           
00076 static int maxlen;              
00077 static int nowlen;              
00078 
00083 static SP16 pre_data[2];
00084 static boolean has_pre;         
00085 
00086 static unsigned int sfreq;      
00087 
00088 static char speechfilename[MAXPATHLEN]; 
00089 
00090 
00091 /* read .wav data with endian conversion */
00092 /* (all .wav datas are in little endian) */
00093 
00105 static boolean
00106 myread(void *buf, size_t unitbyte, int unitnum, FILE *fp)
00107 {
00108   int tmp;
00109   if ((tmp = myfread(buf, unitbyte, unitnum, fp)) < unitnum) {
00110     return(FALSE);
00111   }
00112 #ifdef WORDS_BIGENDIAN
00113   swap_bytes(buf, unitbyte, unitnum);
00114 #endif
00115   return(TRUE);
00116 }
00118 #define MYREAD(A,B,C,D)  if (!myread(A, B, C, D)) {jlog("Error: adin_file: file is corrupted\n"); return -1;}
00119 
00133 static boolean
00134 setup_wav(FILE *fp)
00135 {
00136   char dummy[9];
00137   unsigned int i, len;
00138   unsigned short s;
00139 
00140   /* 4 byte: byte num of rest ( = filesize - 8) */
00141   /* --- just skip them */
00142   MYREAD(dummy, 1, 4, fp);
00143   /* first part: WAVE format specifications */
00144   /* 4 byte: "WAVE" */
00145   MYREAD(dummy, 1, 4, fp);
00146   if (dummy[0] != 'W' ||
00147       dummy[1] != 'A' ||
00148       dummy[2] != 'V' ||
00149       dummy[3] != 'E') {
00150     jlog("Error: adin_file: WAVE header not found, file corrupted?\n");
00151     fclose_readfile(fp); return FALSE;
00152   }
00153   /* format chunk: "fmt " */
00154   MYREAD(dummy, 1, 4, fp);
00155   if (dummy[0] != 'f' ||
00156       dummy[1] != 'm' ||
00157       dummy[2] != 't' ||
00158       dummy[3] != ' ') {
00159     jlog("Error: adin_file: fmt chunk not found, file corrupted?\n");
00160     fclose_readfile(fp); return FALSE;
00161   }
00162   /* 4byte: byte size of this part */
00163   MYREAD(&len, 4, 1, fp);
00164 
00165   /* 2byte: data format */
00166   MYREAD(&s, 2, 1, fp);
00167   if (s != 1) {
00168     jlog("Error: adin_file: data format != PCM (id=%d)\n", s);
00169     fclose_readfile(fp); return FALSE;
00170   }
00171   /* 2byte: channel num */
00172   MYREAD(&s, 2, 1, fp);
00173   if (s >= 2) {
00174     jlog("Error: adin_file: channel num != 1 (%d)\n", s);
00175     fclose_readfile(fp); return FALSE;
00176   }
00177   /* 4byte: sampling rate */
00178   MYREAD(&i, 4, 1, fp);
00179   if (i != sfreq) {
00180     jlog("Error: adin_file: sampling rate != %d (%d)\n", sfreq, i);
00181     fclose_readfile(fp); return FALSE;
00182   }
00183   /* 4byte: bytes per second */
00184   MYREAD(&i, 4, 1, fp);
00185   if (i != sfreq * sizeof(SP16)) {
00186     jlog("Error: adin_file: bytes per second != %d (%d)\n", sfreq * sizeof(SP16), i);
00187     fclose_readfile(fp); return FALSE;
00188   }
00189   /* 2bytes: bytes per frame ( = (bytes per sample) x channel ) */
00190   MYREAD(&s, 2, 1, fp);
00191   if (s != 2) {
00192     jlog("Error: adin_file: (bytes per sample) x channel != 2 (%d)\n", s);
00193     fclose_readfile(fp); return FALSE;
00194   }
00195   /* 2bytes: bits per sample */
00196   MYREAD(&s, 2, 1, fp);
00197   if (s != 16) {
00198     jlog("Error: adin_file: bits per sample != 16 (%d)\n", s);
00199     fclose_readfile(fp); return FALSE;
00200   }
00201   /* skip rest */
00202   if (len > 16) {
00203     len -= 16;
00204     while (len > 0) {
00205       if (len > 8) {
00206         MYREAD(dummy, 1, 8, fp);
00207         len -= 8;
00208       } else {
00209         MYREAD(dummy, 1, len, fp);
00210         len = 0;
00211       }
00212     }
00213   }
00214   /* end of fmt part */
00215 
00216   /* seek for 'data' part */
00217   while (myread(dummy, 1, 4, fp)) {
00218     MYREAD(&len, 4, 1, fp);
00219     if (dummy[0] == 'd' &&
00220         dummy[1] == 'a' &&
00221         dummy[2] == 't' &&
00222         dummy[3] == 'a') {
00223       break;
00224     }
00225     for (i=0;i<len;i++) myread(dummy, 1, 1, fp);
00226   }
00227   /* ready to read in "data" part --- this is speech data */
00228   maxlen = len / sizeof(SP16);
00229   nowlen = 0;
00230   return TRUE;
00231 }
00232 
00242 static boolean
00243 adin_file_open(char *filename)  /* NULL for standard input */
00244 {
00245   FILE *fp;
00246   char dummy[4];
00247 
00248   if (filename != NULL) {
00249     if ((fp = fopen_readfile(filename)) == NULL) {
00250       jlog("Error: adin_file: failed to open %s\n",filename);
00251       return(FALSE);
00252     }
00253   } else {
00254     fp = stdin;
00255 #if defined(_WIN32) && !defined(__CYGWIN32__) && !defined(__MINGW32__)
00256     if (_setmode( _fileno( stdin ), _O_BINARY ) == -1) {
00257       jlog("Error: adin_file: _setmode() failed\n");
00258     }
00259 #endif
00260   }
00261     
00262   /* check first 4 byte to detect Microsoft WAVE format */
00263   if (myfread(dummy, 1, 4, fp) < 4) {
00264     jlog("Error: adin_file: size less than 4 bytes?\n",filename);
00265     fclose_readfile(fp);
00266     return(FALSE);
00267   }
00268   if (dummy[0] == 'R' &&
00269       dummy[1] == 'I' &&
00270       dummy[2] == 'F' &&
00271       dummy[3] == 'F') {
00272     /* it's a WAVE file */
00273     wav_p = TRUE;
00274     has_pre = FALSE;
00275     if (setup_wav(fp) == FALSE) {
00276       jlog("Error: adin_file: failed to read %s as a wav file\n",filename);
00277       fclose_readfile(fp);
00278       return(FALSE);
00279     }
00280   } else {
00281     /* read as raw format file */
00282     wav_p = FALSE;
00283     memcpy(pre_data, dummy, 4);    /* already read (4/sizeof(SP)) samples */
00284     has_pre = TRUE;
00285   }
00286 
00287   gfp = fp;
00288 
00289   return(TRUE);
00290 }
00291 
00297 static boolean
00298 adin_file_close()
00299 {
00300   FILE *fp;
00301 
00302   fp = gfp;
00303   if (fclose_readfile(fp) != 0) {
00304     jlog("Error: adin_file: failed to close file\n");
00305     return FALSE;
00306   }
00307  return TRUE; 
00308 }
00309 
00310 static boolean from_file;       
00311 static FILE *fp_list;           
00312 
00321 boolean
00322 adin_file_standby(int freq, void *arg)
00323 {
00324   char *fname = arg;
00325   if (fname != NULL) {
00326     /* read input filename from file */
00327     if ((fp_list = fopen(fname, "r")) == NULL) {
00328       jlog("Error: adin_file: failed to open %s\n", fname);
00329       return(FALSE);
00330     }
00331     from_file = TRUE;
00332   } else {
00333     /* read filename from stdin */
00334     from_file = FALSE;
00335   }
00336   /* store sampling frequency */
00337   sfreq = freq;
00338   
00339   return(TRUE);
00340 }
00341 
00351 boolean
00352 adin_file_begin()
00353 {
00354   boolean readp;
00355 
00356   /* ready to read next input */
00357   readp = FALSE;
00358   while(readp == FALSE) {
00359     if (from_file) {
00360       /* read file name from listfile */
00361       do {
00362         if (getl_fp(speechfilename, MAXPATHLEN, fp_list) == NULL) { /* end of input */
00363           fclose(fp_list);
00364           return(FALSE); /* end of input */
00365         }
00366       } while (speechfilename[0] == '#'); /* skip comment */
00367     } else {
00368       /* read file name from stdin */
00369       if (get_line_from_stdin(speechfilename, MAXPATHLEN, "enter filename->") == NULL) {
00370         return (FALSE); /* end of input */
00371       }
00372     }
00373     /* open input file */
00374     if (adin_file_open(speechfilename) == FALSE) {
00375       jlog("Error: adin_file: failed to read speech data: \"%s\"\n", speechfilename);
00376     } else {
00377       jlog("Stat: adin_file: input speechfile: %s\n", speechfilename);
00378       readp = TRUE;
00379     }
00380   }
00381   return TRUE;
00382 }
00383 
00392 int
00393 adin_file_read(SP16 *buf, int sampnum)
00394 {
00395   FILE *fp;
00396   int cnt;
00397 
00398   fp = gfp;
00399   
00400   if (wav_p) {
00401     cnt = myfread(buf, sizeof(SP16), sampnum, fp);
00402     if (nowlen + cnt > maxlen) {
00403       cnt = maxlen - nowlen;
00404     }
00405     nowlen += cnt;
00406   } else {
00407     if (has_pre) {
00408       buf[0] = pre_data[0]; buf[1] = pre_data[1];
00409       has_pre = FALSE;
00410       cnt = myfread(&(buf[2]), sizeof(SP16), sampnum - 2, fp);
00411       if (cnt > 0) cnt += 2;
00412     } else {
00413       cnt = myfread(buf, sizeof(SP16), sampnum, fp);
00414     }
00415   }
00416   if (cnt == 0) {               /* error or EOF */
00417     if (myfeof(fp) == 1) {              /* EOF */
00418       return -1;
00419     }
00420     jlog("Error: adin_file: an error occured file reading file\n");
00421     adin_file_close();
00422     return -2;          /* error */
00423   }
00424   /* all .wav data are in little endian */
00425   /* assume .raw data are in big endian */
00426 #ifdef WORDS_BIGENDIAN
00427   if (wav_p) swap_sample_bytes(buf, cnt);
00428 #else
00429   if (!wav_p) swap_sample_bytes(buf, cnt);
00430 #endif
00431   return cnt;
00432 }
00433 
00439 boolean
00440 adin_file_end()
00441 {
00442   /* nothing needed */
00443   adin_file_close();
00444   return TRUE;
00445 }
00446 
00455 boolean
00456 adin_stdin_standby(int freq, void *arg)
00457 {
00458   /* store sampling frequency */
00459   sfreq = freq;
00460   return(TRUE);
00461 }
00462 
00468 boolean
00469 adin_stdin_begin()
00470 {
00471   if (feof(stdin)) {            /* already reached the end of input stream */
00472     return FALSE;               /* terminate search here */
00473   } else {
00474     /* open input stream */
00475     if (adin_file_open(NULL) == FALSE) {
00476       jlog("Error: adin_stdin: failed to read speech data from stdin\n");
00477       return FALSE;
00478     }
00479     jlog("Stat: adin_stding: reading wavedata from stdin...\n");
00480   }
00481   return TRUE;
00482 }
00483 
00492 int
00493 adin_stdin_read(SP16 *buf, int sampnum)
00494 {
00495   int cnt;
00496 
00497   if (wav_p) {
00498     cnt = myfread(buf, sizeof(SP16), sampnum, stdin);
00499   } else {
00500     if (has_pre) {
00501       buf[0] = pre_data[0]; buf[1] = pre_data[1];
00502       has_pre = FALSE;
00503       cnt = fread(&(buf[2]), sizeof(SP16), sampnum - 2, stdin);
00504       if (cnt > 0) cnt += 2;
00505     } else {
00506       cnt = fread(buf, sizeof(SP16), sampnum, stdin);
00507     }
00508   }
00509   if (cnt == 0) {     
00510     if (ferror(stdin)) {                /* error */
00511       jlog("Error: adin_file: an error occured file reading file\n");
00512       return -2;                /* error */
00513     }
00514     return -1;                  /* EOF */
00515   }
00516   /* all .wav data are in little endian */
00517   /* assume .raw data are in big endian */
00518 #ifdef WORDS_BIGENDIAN
00519   if (wav_p) swap_sample_bytes(buf, cnt);
00520 #else
00521   if (!wav_p) swap_sample_bytes(buf, cnt);
00522 #endif
00523   return cnt;
00524 }
00525 
00533 char *
00534 adin_file_get_current_filename()
00535 {
00536   return(speechfilename);
00537 }

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