Speex Audio Codec Example _ sound and video coding and decoding

Source: Internet
Author: User

Address: http://wmnmtm.blog.163.com/blog/static/38245714201110801617165/

Ubuntu under the compiler Gcc-o Speexdec Speexdec.c-lspeex-logg

[csharp]  View Plain  copy// demo.cpp : defines the entry point for  the console application.    //     #include   " StdAfx.h "     #include  <speex/speex.h>     #include  < stdio.h>      #include  <ostream>          #include  <speex/speex_preprocess.h>     #include  <speex/speex_ echo.h>      #pragma  comment (lib, "Libspeexdsp.lib")        #define  FRAME_SIZE 160     Int main (int argc, char ** argv)      {                   char *inFile;                  FILE *fin,*fout1,*fout2,*fout3;                   short in[FRAME_SIZE];          short out[frame_size];           float input[frame_ size];         float output[FRAME_SIZE];             char cbits[200];                   int nbBytes;                                      void *stateEncode;          void *stateDecode;                                                       speexbits  bitsEncode;         SpeexBits bitsDecode;             int i, tmp;                   //Create a new encoding state in narrow-width (narrowband) mode                    stateencode = speex_ Encoder_init (&speex_nb_mode);         statedecode = speex _decoder_init (&speex_nb_mode);         //Set quality to 8 (15kbps)                    tmp=0;      &NBSP;&NBSP;   speex_encoder_ctl (stateencode, speex_set_vbr, &tmp);          float q=4;         speex_encoder_ctl (Stateencode ,  speex_set_vbr_quality, &q);         speex_encoder_ctl ( STATEENCODE,&NBSP;SPEEX_SET_QUALITY,&NBSP;&AMP;TMP);                   inFile = argv[1];                   fin = fopen ("C:/DEMO.PCM",  "RB");          fout1 = fopen ("C:/demo_speex.raw",  "WB");          fout2 = fopen ("C:/DEMO1.PCM",  "WB");          fout3 = fopen ("C:/DEMO_SLIENCE.PCM",  "WB");      &NBSP;&NBSP;&NBSP;&NBSP;//initialization structure enables them to save data               speex_ Bits_init (&bitsencode);         speex_bits_init (&bitsDecode);          int ret;          int j=0;         SpeexPreprocessState * m_st;          SpeexEchoState *echo_state;           m_st=speex_preprocess_state_init (160, 8000);    //  echo_ State = speex_echo_state_init (160, 8000);           int denoise = 1;         int noisesuppress =  -25;         speex_preprocess_ctl (M_st, speex_preprocess_set _denoise, &denoise);  //Noise reduction          speex_preprocess_ctl (m_st,  speex_preprocess_set_noise_suppress, &noisesuppress);  //Set Noise db                        int agc =  1;         q=24000;          //actually default is 8000 (0,32768),here make it louder for  voice is not loudy enough by default. 8000          speex_preprocess_ctl (M_ST,&NBSP;SPEEX_PREPROCESS_SET_AGC,&NBSP;&AMP;AGC);//Gain           speex_preprocess_ctl (M_ST,&NBSP;SPEEX_PREPROCESS_SET_AGC_LEVEL,&AMP;Q);          int vad = 1;          int vadprobstart = 80;         int vadprobcontinue =  65;         speex_preprocess_ctl (m_st, speex_preprocess_set_ VAD,&NBSP;&AMP;VAD);  //Mute detection          speex_preprocess_ctl (M_st,  speex_preprocess_set_prob_start , &vadprobstart); //set probability  required for the vad to go from silence to voice           speex_preprocess_ctl (M_st, speex_preprocess_set_prob_continue,  &vadprobcontinue);  //set probability required for the vad to  stay in the voice state  (integer percent)                         while  (1)          {             memset (out,0,FRAME_ Size*sizeof (short));             //read a frame 16bits sound              j++;              int r=fread (in, sizeof (short),  frame_size, fin);                            if  (r<frame_size)                   break;                           //Converts the value of 16bits to float, So the Speex library can work on the above          spx_int16_t * ptr= (spx_int16_t * ) IN;&NBSP;&NBsp;                if  (speex_ Preprocess_run (M_ST,&NBSP;PTR)/preprocessing   turned on mute detection and noise reduction          {              printf ("speech,");             fwrite (in, sizeof (short), &NBSP;FRAME_SIZE,&NBSP;FOUT3);          }         else          {              printf ("Slience,");             fwrite (out,  sizeof (short), &NBSP;FRAME_SIZE,&NBSP;FOUT3);         }              for  (i=0;i<frame_size;i++)       &NBSP;&NBsp;                            input[i]=in[i];                           // Empty all the bytes in this structure so that we can encode a new frame                            speex_bits_reset (&bitsencode);                            //Frame Coding                            ret=speex_encode (stateencode,  Input, &bitsencode);             // Copy bits to a write-out char array              nbbytes = speex_bits_write (& bitsencode, cbits, 200);              Fwrite (Cbits, sizeof (char), &NBSP;NBBYTES,&NBSP;FOUT1);              printf ("D,", Nbbytes);                                // Empty all the bytes in this structure so that we can encode a new frame              speex_bits_ Reset (&bitsdecode);             //reads encoded data into bits              speex_bits_read_from (&bitsdecode,  cbits, nbbytes);                //to decode a frame              ret = speex_decode (stateDecode,  &bitsdecode,output);             for  (i=0; i<frame_size;i++)                   out[i]=output[i];             fwrite (out,  sizeof (short),  frame_size, fout2);         }                   //Release encoder State amount                    speex_encoder_ Destroy (Stateencode);                   //release bit_packing structure                    speex_bIts_destroy (&bitsencode);         speex_decoder_destroy (StateDecode );                  //release Bit_ Packing structure                   speex _bits_destroy (&bitsdecode);         fclose (Fin);          fclose (FOUT1);         fclose (FOUT2);          fclose (FOUT3);      return 0;              }    

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.