www.pudn.com > g729.rar > coder.c


/* 
   ITU-T G.729A Speech Coder    ANSI-C Source Code 
   Version 1.1    Last modified: September 1996 
 
   Copyright (c) 1996, 
   AT&T, France Telecom, NTT, Universite de Sherbrooke 
   All rights reserved. 
*/ 
 
/*-------------------------------------------------------------------* 
 * Main program of the ITU-T G.729A  8 kbit/s encoder.               * 
 *                                                                   * 
 *    Usage : coder speech_file  bitstream_file                      * 
 *-------------------------------------------------------------------*/ 
 
#include  
#include  
//Add by WL for testing time pass 
#include  
#include  
#include  
#include  
#include  
#include  
#include  
#include "typedef.h" 
#include "basic_op.h" 
#include "ld8a.h" 
typedef  struct 
{	char    	RiffID [4] ; 
	u_long    	RiffSize ; 
	char    	WaveID [4] ; 
	char    	FmtID  [4] ; 
	u_long    	FmtSize ; 
	u_short   	wFormatTag ; 
	u_short   	nChannels ; 
	u_long		nSamplesPerSec ; 
	u_long		nAvgBytesPerSec ; 
	u_short		nBlockAlign ; 
	u_short		wBitsPerSample ; 
	char		DataID [4] ; 
	u_long		nDataBytes ; 
} WAVE_HEADER ; 
 
int main(int argc, char *argv[] ) 
{ 
  FILE *f_speech;               /* File of speech data                   */ 
  FILE *f_serial;               /* File of serial bits for transmission  */ 
//  int audiodev; 
  struct timeval tvstart,tvstop; 
 
  extern Word16 *new_speech;     /* Pointer to new speech data            */ 
  WAVE_HEADER mywaveheader; 
   
  Word16 prm[PRM_SIZE];          /* Analysis parameters.                  */ 
  Word16 serial[SERIAL_SIZE];    /* Output bitstream buffer               */ 
  // added_by_fengwei 
  Word16 serialmat[5]; 
  // ended_by_fengwei 
  volatile unsigned int frame;                  /* frame counter */ 
//  char  myframe[4]; 
//  volatile int myarg;	 
 
  printf("\n"); 
  printf("***********    ITU G.729A 8 KBIT/S SPEECH CODER    ***********\n"); 
  printf("\n"); 
  printf("------------------- Fixed point C simulation -----------------\n"); 
  printf("\n"); 
  printf("-------------------       Version 1.1        -----------------\n"); 
  printf("\n"); 
 
 
/*--------------------------------------------------------------------------* 
 * Open speech file and result file (output serial bit stream)              * 
 *--------------------------------------------------------------------------*/ 
 
  if ( argc != 3 ) 
    { 
       printf("Usage : coder speech_file  bitstream_file\n"); 
       printf("\n"); 
       printf("Format for speech_file:\n"); 
       printf("  Speech is read from a binary file of 16 bits PCM data.\n"); 
       printf("\n"); 
       printf("Format for bitstream_file:\n"); 
       printf("  One (2-byte) synchronization word \n"); 
       printf("  One (2-byte) size word,\n"); 
       printf("  80 words (2-byte) containing 80 bits.\n"); 
       printf("\n"); 
       exit(1); 
    } 
 
 
  if ( (f_speech = fopen(argv[1], "rb")) == NULL) { 
     printf("%s - Error opening file  %s !!\n", argv[0], argv[1]); 
     exit(0); 
  } 
  printf(" Input speech file    :  %s\n", argv[1]); 
 
 
  if ( (f_serial = fopen(argv[2], "wb")) == NULL) { 
     printf("%s - Error opening file  %s !!\n", argv[0], argv[2]); 
     exit(0); 
  } 
  printf(" Output bitstream file:  %s\n", argv[2]); 
 
/* 
  if((audiodev=open("/dev/dsp",O_RDWR,0x666))<0) 
  { 
	  printf("Error: Fail to open /dev/dsp!\n"); 
	  exit(0); 
  } 
  printf("Open /dev/dsp has successfully finished!\n"); 
   
  myarg=0; 
  if((ioctl(audiodev,SNDCTL_DSP_STEREO,&myarg)) < 0) 
  { 
	  printf("Error, Fail to set the STEREO parameter!\n"); 
	  exit(0); 
  } 
  printf(" Set the STEREO parameter to Mono Channel!\n"); 
   
  myarg=16; 
  if((ioctl(audiodev,SNDCTL_DSP_SAMPLESIZE,&myarg))<0) 
  { 
	  printf("Error, Fail to set the samplesize parameters!\n"); 
	  exit(0); 
  } 
  printf(" Set the samplesize to 16bits!\n"); 
	   
  myarg=8000; 
  if((ioctl(audiodev,SNDCTL_DSP_SPEED,&myarg))<0) 
  { 
	  printf("Error, Fail to set the sampling rate parameters!\n"); 
	  exit(0); 
  } 
  printf(" Set the sampling rate to 8000!\n"); 
 
  myarg=80; 
  if((ioctl(audiodev,SNDCTL_DSP_SETFRAGMENT,&myarg))<0) 
  { 
	  printf("Error, Fail to set the sampling rate parameters!\n"); 
	  exit(0); 
  } 
  printf(" Set the fragment size to 80!\n"); 
*/   
  
   
 
		   
/*--------------------------------------------------------------------------* 
 * Initialization of the coder.                                             * 
 *--------------------------------------------------------------------------*/ 
 
  Init_Pre_Process(); 
  Init_Coder_ld8a(); 
  Set_zero(prm, PRM_SIZE); 
 
  /* Loop for each "L_FRAME" speech data. */ 
 
  frame =0; 
  printf("Number of WaveHeader is %d\n!",sizeof(mywaveheader)); 
  fread(&mywaveheader,sizeof(mywaveheader),1,f_speech); 
 
  gettimeofday(&tvstart,NULL); 
 
//    while( read(audiodev,new_speech,(sizeof(Word16)* L_FRAME)) == (L_FRAME*sizeof(Word16))) 
  while(fread(new_speech,sizeof(Word16),L_FRAME,f_speech)==L_FRAME)    
//    while(frame<1000) 
    { 
 
    printf("Frame =%d\n", frame++); 
//    read(audiodev,new_speech,(sizeof(Word16)*L_FRAME));	 
    Pre_Process(new_speech, L_FRAME); 
 
    Coder_ld8a(prm); 
 
    prm2bits_ld8k( prm, serial); 
    	 
//    fwrite(serial, sizeof(Word16), SERIAL_SIZE, f_serial); 
    bitstore(serial,serialmat); 
    fwrite(serialmat,sizeof(Word16),5,f_serial);     
//    frame ++; 
    } 
 
 
    fwrite(&mywaveheader,sizeof(mywaveheader),1,f_serial); 
     
    //close(audiodev); 
     
    fclose(f_speech); 
    fclose(f_serial); 
    gettimeofday(&tvstop,NULL); 
    printf("Now the time used is %ld\n",(tvstop.tv_usec/1000+tvstop.tv_sec*1000-tvstart.tv_usec/1000-tvstart.tv_sec*1000)); 
  return (0); 
   
}