[speex-dev] About sample code

gayla M9115023 at mail.ntust.edu.tw
Sat Aug 16 06:15:49 PDT 2003



I write a sample code like speex provide , as follows:
SpeeXCodec::SpeeXCodec()
{
 rate = 8000;
 speex_bits_init(&enbits);
 speex_bits_init(&debits);
 enc_state = speex_encoder_init(&speex_nb_mode);
 dec_state = speex_decoder_init(&speex_nb_mode);

 // set option for encoder
 speex_encoder_ctl(enc_state, SPEEX_GET_FRAME_SIZE, &frame_size);
 speex_encoder_ctl(enc_state, SPEEX_SET_COMPLEXITY, &complexity);
    speex_encoder_ctl(enc_state, SPEEX_SET_SAMPLING_RATE, &rate);
 int tmp=8;
 speex_encoder_ctl(enc_state,SPEEX_SET_QUALITY,&tmp);

    // set option for decoder
 speex_decoder_ctl(dec_state, SPEEX_GET_FRAME_SIZE, &frame_size);
    speex_decoder_ctl(dec_state, SPEEX_SET_SAMPLING_RATE, &rate);
 int tmp2=1;
 speex_decoder_ctl(dec_state,SPEEX_SET_ENH,&tmp2);
}

SpeeXCodec::~SpeeXCodec()
{
 // Free encoder resource
 speex_bits_destroy(&enbits);
 speex_encoder_destroy(enc_state);
 // Free decoder resource
 speex_bits_destroy(&debits);
 speex_decoder_destroy(dec_state);
}

int
SpeeXCodec::Encode(void *input, int inputSizeBytes, void *output, int
*outputSizeBytes)
{
    float *dataIn = (float *)input;
 char *dataOut = (char *)output;
    int nbBytes;
 if(dataIn==NULL)  return 1;        //error occur
 else{

        /***********encode frame ***************
  input : dataIn ; output:enbits
  ***************************************/
  speex_encode(enc_state,dataIn,&enbits);

  /*************************************************************
  Copy the bits to an array(cbits) of char that can be written
  200 is max len to tell the func. it can use how large space
  input : enbits ; output : dataOut
        **************************************************************/
  nbBytes=speex_bits_write(&enbits,dataOut,200);

        speex_bits_reset(&enbits);
 }
 *outputSizeBytes = nbBytes;

 return 0;
}

int
SpeeXCodec::Decode(void *input, int inputSizeBytes, void *output, int
*outputSizeBytes)
{
 char *dataIn = (char *)input;
// float spxout[FRAME_SIZE];          //hold data for speex output used in
func. decode
// short *dataOut = (short *)output;
 float* spxout = (float *)output;
 if(dataIn==NULL)  return 1;        //error occur
 else{

     /***************************************
  Copy the data into the bit-stream struct
  input : dataIn ; output : debits
  inputSizeBytes = 38
  ***************************************/
  inputSizeBytes = 38;
  speex_bits_read_from(&debits,dataIn,inputSizeBytes);

  /********************************
          Decode frame
     input : debits ; output : spxout
  ********************************/
  speex_decode(dec_state,&debits,spxout);

  /******************************************
  Copy data from float(speex) to short(PCM)
  ******************************************/
//  for(int i=0;i<FRAME_SIZE;i++)
//   dataOut[i]=spxout[i];

//  speex_bits_reset(&debits);
 }

    *outputSizeBytes = FRAME_SIZE;

 return 0;
}

Encode seems to work fine , but the decoder have many problem like follows:
SpeeXCodec speexcodec;
 FILE* fin=fopen("test.spx","r");

 if(!fin) {cerr<<"Can't open file"<<endl; exit(1);}
 FILE* fout=fopen("test2.pcm","wb");

 char buffer[200];
 int read=0;
 read=fread(buffer,sizeof(char),38,fin);

<p> float output[FRAME_SIZE];
    int output_size;

    short de_output[FRAME_SIZE];

 int count=0;

    int i;

 while(!feof(fin))
 {
  count++;
  speexcodec.Decode(buffer,read,output,&output_size);
  for(i=0;i<FRAME_SIZE;i++)
   de_output[i]=output[i];

  write_file(fout,de_output,output_size);

  read=fread(buffer,sizeof(char),38,fin);
 }

 cout<<count;

 fclose(fin);
 fclose(fout);
}

I've tried to  debug it long time , can any one can tell me where the
problem.............Thanks a lot

<p>--- >8 ----
List archives:  http://www.xiph.org/archives/
Ogg project homepage: http://www.xiph.org/ogg/
To unsubscribe from this list, send a message to 'speex-dev-request at xiph.org'
containing only the word 'unsubscribe' in the body.  No subject is needed.
Unsubscribe messages sent to the list will be ignored/filtered.



More information about the Speex-dev mailing list