libsent/src/adin/adin_sndfile.c

Go to the documentation of this file.
00001 
00067 /*
00068  * Copyright (c) 1991-2007 Kawahara Lab., Kyoto University
00069  * Copyright (c) 2000-2005 Shikano Lab., Nara Institute of Science and Technology
00070  * Copyright (c) 2005-2007 Julius project team, Nagoya Institute of Technology
00071  * All rights reserved
00072  */
00073 
00074 #include <sent/stddefs.h>
00075 #include <sent/speech.h>
00076 #include <sent/adin.h>
00077 
00078 #ifdef HAVE_LIBSNDFILE
00079 
00080 /* sound header */
00081 #include <sndfile.h>
00082 
00083 static int sfreq;               
00084 static SF_INFO sinfo;           
00085 static SNDFILE *sp;             
00086 static boolean from_file;       
00087 static FILE *fp_list;           
00088 static char speechfilename[MAXPATHLEN]; 
00089 
00091 static boolean
00092 check_format(SF_INFO *s)
00093 {
00094   if ((s->format & SF_FORMAT_TYPEMASK) != SF_FORMAT_RAW) {
00095     if (s->samplerate != sfreq) {
00096       jlog("Error: adin_sndfile: sample rate != %d, it's %d Hz data\n", sfreq, s->samplerate);
00097       return FALSE;
00098     }
00099   }
00100   if (s->channels != 1) {
00101     jlog("Error: adin_sndfile: channel num != 1, it has %d channels\n", s->channels);
00102     return FALSE;
00103   }
00104 #ifdef HAVE_LIBSNDFILE_VER1
00105   if ((s->format & SF_FORMAT_SUBMASK) != SF_FORMAT_PCM_16) {
00106     jlog("Error: adin_sndfile: not 16-bit data\n");
00107     return FALSE;
00108   }
00109 #else
00110   if (s->pcmbitwidth != 16) {
00111     jlog("Error: adin_sndfile: not 16-bit data, it's %d bit\n", s->pcmbitwidth);
00112     return FALSE;
00113   }
00114 #endif
00115   return TRUE;
00116 }
00117 
00119 static void
00120 print_format(SF_INFO *s)
00121 {
00122   switch(s->format & SF_FORMAT_TYPEMASK) {
00123   case SF_FORMAT_WAV:    jlog("Stat: adin_sndfile: input format = Microsoft WAV\n"); break;
00124   case SF_FORMAT_AIFF:   jlog("Stat: adin_sndfile: input format = Apple/SGI AIFF\n"); break;
00125   case SF_FORMAT_AU:     jlog("Stat: adin_sndfile: input format = Sun/NeXT AU\n"); break;
00126 #ifndef HAVE_LIBSNDFILE_VER1
00127   case SF_FORMAT_AULE:   jlog("Stat: adin_sndfile: input format = DEC AU\n"); break;
00128 #endif
00129   case SF_FORMAT_RAW:    jlog("Stat: adin_sndfile: input format = RAW\n"); break;
00130   case SF_FORMAT_PAF:    jlog("Stat: adin_sndfile: input format = Ensoniq PARIS\n"); break;
00131   case SF_FORMAT_SVX:    jlog("Stat: adin_sndfile: input format = Amiga IFF / SVX8 / SV16\n"); break;
00132   case SF_FORMAT_NIST:   jlog("Stat: adin_sndfile: input format = Sphere NIST\n"); break;
00133 #ifdef HAVE_LIBSNDFILE_VER1
00134   case SF_FORMAT_VOC:    jlog("Stat: adin_sndfile: input format = VOC file\n"); break;
00135   case SF_FORMAT_IRCAM:  jlog("Stat: adin_sndfile: input format = Berkeley/IRCAM/CARL\n"); break;
00136   case SF_FORMAT_W64:    jlog("Stat: adin_sndfile: input format = Sonic Foundry's 64bit RIFF/WAV\n"); break;
00137   case SF_FORMAT_MAT4:   jlog("Stat: adin_sndfile: input format = Matlab (tm) V4.2 / GNU Octave 2.0\n"); break;
00138   case SF_FORMAT_MAT5:   jlog("Stat: adin_sndfile: input format = Matlab (tm) V5.0 / GNU Octave 2.1\n"); break;
00139 #endif
00140   default: jlog("Stat: adin_sndfile: input format = UNKNOWN TYPE\n"); break;
00141   }
00142   switch(s->format & SF_FORMAT_SUBMASK) {
00143 #ifdef HAVE_LIBSNDFILE_VER1
00144   case SF_FORMAT_PCM_U8:    jlog("Stat: adin_sndfile: input type = Unsigned 8 bit PCM\n"); break;
00145   case SF_FORMAT_PCM_S8:    jlog("Stat: adin_sndfile: input type = Signed 8 bit PCM\n"); break;
00146   case SF_FORMAT_PCM_16:    jlog("Stat: adin_sndfile: input type = Signed 16 bit PCM\n"); break;
00147   case SF_FORMAT_PCM_24:    jlog("Stat: adin_sndfile: input type = Signed 24 bit PCM\n"); break;
00148   case SF_FORMAT_PCM_32:    jlog("Stat: adin_sndfile: input type = Signed 32 bit PCM\n"); break;
00149   case SF_FORMAT_FLOAT:     jlog("Stat: adin_sndfile: input type = 32bit float\n"); break;
00150   case SF_FORMAT_DOUBLE:    jlog("Stat: adin_sndfile: input type = 64bit float\n"); break;
00151   case SF_FORMAT_ULAW:      jlog("Stat: adin_sndfile: input type = U-Law\n"); break;
00152   case SF_FORMAT_ALAW:      jlog("Stat: adin_sndfile: input type = A-Law\n"); break;
00153   case SF_FORMAT_IMA_ADPCM: jlog("Stat: adin_sndfile: input type = IMA ADPCM\n"); break;
00154   case SF_FORMAT_MS_ADPCM:  jlog("Stat: adin_sndfile: input type = Microsoft ADPCM\n"); break;
00155   case SF_FORMAT_GSM610:    jlog("Stat: adin_sndfile: input type = GSM 6.10, \n"); break;
00156   case SF_FORMAT_G721_32:   jlog("Stat: adin_sndfile: input type = 32kbs G721 ADPCM\n"); break;
00157   case SF_FORMAT_G723_24:   jlog("Stat: adin_sndfile: input type = 24kbs G723 ADPCM\n"); break;
00158   case SF_FORMAT_G723_40:   jlog("Stat: adin_sndfile: input type = 40kbs G723 ADPCM\n"); break;
00159 #else
00160   case SF_FORMAT_PCM:       jlog("Stat: adin_sndfile: input type = PCM\n"); break;
00161   case SF_FORMAT_FLOAT:     jlog("Stat: adin_sndfile: input type = floats\n"); break;
00162   case SF_FORMAT_ULAW:      jlog("Stat: adin_sndfile: input type = U-Law\n"); break;
00163   case SF_FORMAT_ALAW:      jlog("Stat: adin_sndfile: input type = A-Law\n"); break;
00164   case SF_FORMAT_IMA_ADPCM: jlog("Stat: adin_sndfile: input type = IMA ADPCM\n"); break;
00165   case SF_FORMAT_MS_ADPCM:  jlog("Stat: adin_sndfile: input type = Microsoft ADPCM\n"); break;
00166   case SF_FORMAT_PCM_BE:    jlog("Stat: adin_sndfile: input type = Big endian PCM\n"); break;
00167   case SF_FORMAT_PCM_LE:    jlog("Stat: adin_sndfile: input type = Little endian PCM\n"); break;
00168   case SF_FORMAT_PCM_S8:    jlog("Stat: adin_sndfile: input type = Signed 8 bit PCM\n"); break;
00169   case SF_FORMAT_PCM_U8:    jlog("Stat: adin_sndfile: input type = Unsigned 8 bit PCM\n"); break;
00170   case SF_FORMAT_SVX_FIB:   jlog("Stat: adin_sndfile: input type = SVX Fibonacci Delta\n"); break;
00171   case SF_FORMAT_SVX_EXP:   jlog("Stat: adin_sndfile: input type = SVX Exponential Delta\n"); break;
00172   case SF_FORMAT_GSM610:    jlog("Stat: adin_sndfile: input type = GSM 6.10, \n"); break;
00173   case SF_FORMAT_G721_32:   jlog("Stat: adin_sndfile: input type = 32kbs G721 ADPCM\n"); break;
00174   case SF_FORMAT_G723_24:   jlog("Stat: adin_sndfile: input type = 24kbs G723 ADPCM\n"); break;
00175 #endif
00176   default: jlog("Stat: adin_sndfile: input type = UNKNOWN SUBTYPE\n"); break;
00177   }
00178 
00179 #ifdef HAVE_LIBSNDFILE_VER1
00180   switch(s->format & SF_FORMAT_ENDMASK) {
00181   case SF_ENDIAN_FILE:      jlog("Stat: adin_sndfile: endian = file native endian\n"); break;
00182   case SF_ENDIAN_LITTLE:    jlog("Stat: adin_sndfile: endian = forced little endian\n"); break;
00183   case SF_ENDIAN_BIG:       jlog("Stat: adin_sndfile: endian = forced big endian\n"); break;
00184   case SF_ENDIAN_CPU:       jlog("Stat: adin_sndfile: endian = forced CPU native endian\n"); break;
00185   }
00186   jlog("Stat: adin_sndfile: %d Hz, %d channels\n", s->samplerate, s->channels);
00187 #else
00188   jlog("Stat: adin_sndfile: %d bit, %d Hz, %d channels\n", s->pcmbitwidth, s->samplerate, s->channels);
00189 #endif
00190 }
00191 
00201 boolean
00202 adin_sndfile_standby(int freq, void *arg)
00203 {
00204   char *fname = arg;
00205   if (fname != NULL) {
00206     /* read input filename from file */
00207     if ((fp_list = fopen(fname, "r")) == NULL) {
00208       jlog("Error: adin_sndfile: failed to open %s\n", fname);
00209       return(FALSE);
00210     }
00211     from_file = TRUE;
00212   } else {
00213     /* read filename from stdin */
00214     from_file = FALSE;
00215   }
00216   /* store sampling frequency */
00217   sfreq = freq;
00218   
00219   return(TRUE);
00220 }
00221 
00231 boolean
00232 adin_sndfile_begin()
00233 {
00234   boolean readp;
00235 
00236   /* ready to read next input */
00237   readp = FALSE;
00238   while(readp == FALSE) {
00239     if (from_file) {
00240       /* read file name from listfile */
00241       do {
00242         if (getl_fp(speechfilename, MAXPATHLEN, fp_list) == NULL) { /* end of input */
00243           fclose(fp_list);
00244           return(FALSE); /* end of input */
00245         }
00246       } while (speechfilename[0] == '#'); /* skip comment */
00247     } else {
00248       /* read file name from stdin */
00249       if (get_line_from_stdin(speechfilename, MAXPATHLEN, "enter filename->") == NULL) {
00250         return (FALSE); /* end of input */
00251       }
00252     }
00253     /* open input file */
00254 #ifndef HAVE_LIBSNDFILE_VER1
00255     sinfo.samplerate = sfreq;
00256     sinfo.pcmbitwidth = 16;
00257     sinfo.channels = 1;
00258 #endif
00259     sinfo.format = 0x0;
00260     if ((sp = 
00261 #ifdef HAVE_LIBSNDFILE_VER1
00262          sf_open(speechfilename, SFM_READ, &sinfo)
00263 #else
00264          sf_open_read(speechfilename, &sinfo)
00265 #endif
00266          ) == NULL) {
00267       /* retry assuming raw format */
00268       sinfo.samplerate = sfreq;
00269       sinfo.channels = 1;
00270 #ifdef HAVE_LIBSNDFILE_VER1
00271       sinfo.format = SF_FORMAT_RAW | SF_FORMAT_PCM_16 | SF_ENDIAN_BIG;
00272 #else
00273       sinfo.pcmbitwidth = 16;
00274       sinfo.format = SF_FORMAT_RAW | SF_FORMAT_PCM_BE;
00275 #endif
00276       if ((sp =
00277 #ifdef HAVE_LIBSNDFILE_VER1
00278            sf_open(speechfilename, SFM_READ, &sinfo)
00279 #else
00280            sf_open_read(speechfilename, &sinfo)
00281 #endif
00282            ) == NULL) {
00283         sf_perror(sp);
00284         jlog("Error: adin_sndfile: failed to open speech data: \"%s\"\n",speechfilename);
00285       }
00286     }
00287     if (sp != NULL) {           /* open success */
00288       if (! check_format(&sinfo)) {
00289         jlog("Error: adin_sndfile: invalid format: \"%s\"\n",speechfilename);
00290         print_format(&sinfo);
00291       } else {
00292         jlog("Stat: adin_sndfile: input speechfile: %s\n",speechfilename);
00293         print_format(&sinfo);
00294         readp = TRUE;
00295       }
00296     }
00297   }
00298   return TRUE;
00299 }
00300 
00309 int
00310 adin_sndfile_read(SP16 *buf, int sampnum)
00311 {
00312   int cnt;
00313 
00314   cnt = sf_read_short(sp, buf, sampnum);
00315   if (cnt == 0) {               /* EOF */
00316     return -1;
00317   } else if (cnt < 0) {         /* error */
00318     sf_perror(sp);
00319     sf_close(sp);
00320     return -2;          /* error */
00321   }
00322   return cnt;
00323 }
00324 
00330 boolean
00331 adin_sndfile_end()
00332 {
00333   /* close files */
00334   if (sf_close(sp) != 0) {
00335     sf_perror(sp);
00336     jlog("Error: adin_sndfile: failed to close\n");
00337     return FALSE;
00338   }
00339   return TRUE;
00340 }
00341 
00349 char *
00350 adin_sndfile_get_current_filename()
00351 {
00352   return(speechfilename);
00353 }
00354 
00355 #endif /* ~HAVE_LIBSNDFILE */

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