From: assaleh@eagle.rutgers.edu (Khaled Assaleh)
Newsgroups: comp.speech,comp.dsp,comp.multimedia
Subject: Re: TIMIT speech data
Date: 12 Dec 92 23:14:55 GMT
Followup-To: comp.speech
Organization: Rutgers Univ., New Brunswick, N.J.

lwang@nss10.ninds.nih.gov (Lipo Wang) writes:

>Hello,

>I have recently got a CD of the TIMIT speech database. I would 
>appreciate if you could offer advise on any of the following
>questions:

>What is the data format in the *.wav files under the header? 
>In other word, what C read function/format should I use to read out 
>the amplitude of the waveform vs. time (here is represented by 
>the integer labels), so I can display it by standard display
>programs such as Mathematica?

Here are two C codes that'll hopefully help you solve your problem.
The first program 'timit2linear.c' converts a .wav TIMIT file to
Linear 16-bit PCM. It basically swaps the two bytes of each 16-bit 
word.	      

The second program 'bin2short' converts linear 16-bit PCM to ascii
short integers. The two programs can be combined in one simple code
also.

------------------------ FIRST PROGRAM ------------------
/*
   Program Name: timit2linear.c
   Date crated : 8, July 1991
   Function: TIMIT to Linear 16-bit PCM
   Usage: <exe> TIMIT_filename output_filename
*/
#include <stdio.h>
#define TIMIT_HEADER_BYTES 1024

main(argc,argv)
int argc;
char **argv;

{
  unsigned char temp[2],
                header[TIMIT_HEADER_BYTES];
  FILE *fopen(),
       *fp_in,
       *fp_out;

if ((fp_in = fopen(argv[1],"r")) == NULL){
  printf("Can't open input file %s\n",argv[1]);
      exit(1);
    }

if ((fp_out = fopen(argv[2],"w")) == NULL){
  printf("Can't open outout file  %s\n",argv[2]);
      exit(1);
    }

    fread(header,1,TIMIT_HEADER_BYTES,fp_in);  /* READ OFF THE HEADER */

   if(argc < 3){
                printf("ERROR in using the function %s\n",argv[0]);
                printf("\t %s, <TIMIT_file> <output file>",argv[0]);
    }

     while(fread(temp,1,2,fp_in)!=NULL){
     fwrite(&temp[1],1,1,fp_out);
     fwrite(&temp[0],1,1,fp_out);
    }

  close(fp_in);
  close(fp_out);
}


--------------------- SECOND PROGRAM ----------------------
/*
   Program Name: bin2short.c
   Date created : 8, July 1991
   Function: Linear 16-bit to ascii
   Usage: <exe> <input_file> <output_file> <starting_sample> <end_sample> 
*/

#include <stdio.h>
main(argc, argv)
int argc;
char *argv[];
{

FILE *fp1,
     *fp2;
short temp;
int s,
    e,
    i;

if(argc != 5){
                printf("ERROR in using the function %s\n",argv[0]);
                printf("Usage: %s, <i_f_name> <o_f_name> 
 <e>\n",argv[0]);
                printf("i_f_name = input file name\n");
                printf("o_f_name = output file name\n");
                printf("s = starting sample number\n");
                printf("s = ending sample number\n");
    }


if((fp1 = fopen(argv[1], "r")) == NULL){
              printf("Error in opening file %s. EXITing ....\n", argv[1]);
              exit(1);
        }

if((fp2 = fopen(argv[2], "w")) == NULL){
         printf("Error in opening output file %s. EXITing ....\n", argv[2]);
              exit(1);
        }

s = atoi(argv[3]);
e = atoi(argv[4]);

for(i=0;i<s;i++)
fread(&temp,1,2,fp1);

while(i<=e && fread(&temp,1,2,fp1)!=NULL){
fprintf(fp2,"%d\n",temp);
i++;
}
close(fp1);
close(fp2);
}

If you have any problems running these programs please contact me
via email.

>Are there any programs that can output the TIMIT speech from
>the Sun SPARC speaker?

You need to convert the output of the first program to 8-bit ulaw.
If you dont have the code for that contact me and I will send you 
the needed code(s).

>Thank you very much,
>Lipo

>----------

>--
>        Lipo Wang, Ph.D.			
>	National Institutes of Health	Internet: lwang@alw.nih.gov
>	9000 Rockville Pike, Park/431   Phone:    (301) 496-8768
>	Bethesda, MD 20892

Good Luck,

Khaled Assaleh
CAIP Center,
Rutgers Univ.
Newsgroups: comp.speech