Basic coding and decoding process for Speex

Source: Internet
Author: User

Recently studying the coding and decoding process for Speex

Before the IM used are all the voice clips, this is very simple, just need to find the Googlecode on the Gauss code, and then set up can be used.

But Googlecode to shut down, someone has imported him to GitHub, the address is here https://github.com/cczufish/OggSpeex-android

I also imported, but did not succeed, do not know what the problem, maybe the level is not enough, haha https://github.com/dongweiq/android-recorder

Now our demand is to require the speech fragment splicing synthesis, and in the UI to show the time is playing, after reading the code of Gauss found that his code is side decoding side play, according to the short[160] fragments played each

1 /*decode each segment, writing output to WAV*/2                  for(curseg = 0; curseg < segments; curseg++) {3 4                     if(thread.interrupted ()) {5 dis.close ();6 track.stop ();7 track.release ();8                         return;9                     }Ten  One                      while( This. ispaused ()) { A track.stop (); - track.release (); -                         //Thread.Sleep (+); the                     } -  -                     /*get The number of bytes in the segment*/ -Bodybytes = Header[ogg_headersize + curseg] & 0xFF; +                     if(Bodybytes = = 255) { -System.err.println ("Sorry, don ' t handle 255 sizes!"); +                         return; A                     } atdis.readfully (payload, 0, bodybytes); -Chksum = Oggcrc.checksum (chksum, payload, 0, bodybytes); -  -                     /*decode the segment*/ -                     /*if first packet, read the Speex header*/ -                     if(Packetno = = 0) { in                         if(Readspeexheader (payload, 0, Bodybytes,true)) { -packetno++; to}Else { +Packetno = 0; -                         } the}Else if(Packetno = = 1) {//Ogg Comment Packet *packetno++; $}Else {Panax Notoginseng  -                         /*get the amount of decoded data*/ the                          Short[] decoded =New  Short[160]; +                         if((Decsize = Speexdecoder.decode (payload, decoded,)) > 0) { ATrack.write (Decoded, 0, decsize); the                             floatMaxvol =Audiotrack.getmaxvolume (); +Track.setstereovolume (Maxvol, Maxvol);//set Current volume size - Track.play (); $                         } $packetno++; -                     } -}

And he has a package serial number, Pacetno 0 is the Speex file header, accounting for 80 bytes, Packetno 1 to find the package ordinal 2,2 in accordance with 160short decoding audio, and then play the decoded WAV.

The following content was later found on the Internet http://blog.163.com/yuan_zhch/blog/static/193790046201172611527217/

is equivalent to a summary of the entire Speex encoding and decoding process.

One: Coding processThe following steps are used to compress audio data using Speex's API functions:1. Define a speexbits type variable bits and a Speex encoder state variable enc_state. 2. Call Speex_bits_init (&bits) to initialize bits. 3. Call Speex_encoder_init (&speex_nb_mode) to initialize Enc_state. Where Speex_nb_mode is a variable of type Speexmode, it represents a narrowband pattern. There are also speex_wb_mode that the broadband mode, Speex_uwb_mode represents ultra-wideband mode. 4. Call the function int Speex_encoder_ ctl (void *state, int request, void *ptr) to set the parameters of the encoder, where the state of the parameter indicates the status of the encoder, and the parameter request represents the type of parameter to be defined. such as Speex_ get_ frame_size to set the frame size, speex_ set_quality represents the quantization size, which determines the quality of the encoding, the parameter PTR represents the value to set. available through Speex_encoder_ctl (Enc_state, Speex_get_frame_size, &frame_size) and Speex_encoder_ctl (Enc_state, SPEEX_SET _quality, &quality) to set the encoder parameters. 5, after the initialization is complete, the following processing for each frame sound: Call function Speex_bits_reset (&bits) again set speexbits, and then call the function Speex_encode (enc_state, Input_frame , &bits), saves the encoded data stream in the parameter bits. 6, after the end of the code, call function Speex_bits_destroy (&bits), Speex_encoder_destroy (enc_state) to Second: Decoding processSimilarly, decoding audio data that has already been encoded goes through the following steps:1. Define a speexbits type variable bits and a Speex encoded state variable enc_state. 2. Call Speex_bits_init (&bits) to initialize bits. 3. Call Speex_decoder_init (&speex_nb_mode) to initialize Enc_state. 4, call the function speex_decoder_ctl (void *state, int request, void *ptr) to set the parameters of the encoder. 5. Call the function speex_decode (void *state, speexbits *bits, float *out) to encode the audio data in the parameter bits, and save the decoded data stream in the parameter out. 6, Call function Speex_bits_destroy (&bits), speex_ decoder_ destroy (void *state) to close and destroy Speexbits and decoder. Here is an example code:
  1. #include <speex.h>
  2. #include <stdio.h>
  3. /* The size of the frame is a fixed value in this routine, but it does not have to be this way */
  4. #define FRAME_SIZE 160
  5. int main (int argc, char **argv)
  6. {
    1. Char *infile;
    2. FILE *fin;
    3. Short in[frame_size];
    4. float Input[frame_size];
    5. Char cbits[200];
    6. int nbbytes;
    7. /* Save the status of the encoding */
    8. void *state;
    9. /* Save bytes so they can be Speex regular read/write */
    10. Speexbits bits;
    11. int I, TMP;
    12. Create a new encoding state in narrow width (narrowband) mode
    13. State = Speex_encoder_init (&speex_nb_mode);
    14. Set Mass to 8 (15kbps)
    15. tmp=8;
    16. Speex_encoder_ctl (state, speex_set_quality, &tmp);
    17. InFile = argv[1];
    18. Fin = fopen (inFile, "R");
    19. Initialize the structure so that they save the data
    20. Speex_bits_init (&bits);
    21. while (1)
    22. {
        1. Read in a frame of 16bits sound
        2. Fread (in, sizeof (short), frame_size, Fin);
        3. if (feof (Fin))
          1. Break
        4. Convert the value of 16bits to float so that the Speex library can work on it
        5. for (i=0;i<frame_size;i++)
          1. Input[i]=in[i];
        6. Empty all the bytes in this struct so that we can encode a new frame
        7. Speex_bits_reset (&bits);
        8. Encode a Frame
        9. Speex_encode (state, input, &bits);
        10. Copy bits to a char array with write-out
        11. Nbbytes = Speex_bits_write (&bits, cbits, 200);
        12. First write out the size of the frame, which is a value required by the Sampledec file, but may not be the same in your application
        13. Fwrite (&nbbytes, sizeof (int), 1, stdout);
        14. Write out the compressed array
        15. Fwrite (cbits, 1, nbbytes, stdout);
    23. }
    24. Release Encoder State Amount
    25. Speex_encoder_destroy (state);
    26. Releasing the bit_packing structure
    27. Speex_bits_destroy (&bits);
    28. Fclose (Fin);
    29. return 0;
  7. }

Basic coding and decoding process for Speex

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.