Speex encoding/decoding process

Source: Internet
Author: User
Description of the basic coding/decoding process of speex, with a small example.

L

I. Coding ProcessTo use the speex API function to compress audio data, perform the following steps: 1. Define a speexbits type variable bits and an speex enc_state variable. 2. Call speex_bits_init (& bits) to initialize bits. 3. Call speex_encoder_init (& speex_nb_mode) to initialize enc_state. In this example, speex_nb_mode is a variable of the speexmode type, indicating the narrowband mode. In addition, speex_wb_mode indicates the broadband mode and speex_uwb_mode indicates the ultra-broadband mode. 4. Call the int speex_encoder _ CTL (void * state, int request, void * PTR) function to set the parameters of the encoder. The state parameter indicates the state of the encoder; request indicates the parameter type to be defined. For example, speex _ GET _ frame_size indicates setting the frame size, and speex _ set_quality indicates the quantization size, which determines the encoding quality; the PTR parameter indicates the value to be set. You can use speex_encoder_ctl (enc_state, speex_get_frame_size, & frame_size) and speex_encoder_ctl (enc_state, speex_set_quality, & Quality) to set the parameters of the encoder. 5. After initialization, the sound of each frame is processed as follows: Call the speex_bits_reset (& bits) function to set speexbits again, and then call the function speex_encode (enc_state, input_frame, & bits ), the encoded data stream is saved in the bits parameter. 6. After encoding, call the speex_bits_destroy (& bits) and speex_encoder_destroy (enc_state) function. Ii. decoding processSimilarly, decoding audio data that has been encoded involves the following steps: 1. Defining a speexbits type variable bits and a speex encoding status 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 speex_decoder_ctl (void * state, int request, void * PTR) function to set the parameters of the encoder. 5. Call the speex_decode (void * state, speexbits * bits, float * Out) function to uncode the audio data in the bits parameter, and save the decoded data stream in the parameter out. 6. Call the speex_bits_destroy (& bits) function and speex _ decoder _ destroy (void * State) function to disable and destroy the speexbits and decoder. The following is an example code:
 
  1. # Include <speex. h>
  2. # Include <stdio. h>
  3. /* The frame size is a fixed value in this routine, but it is not required */
  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 encoding status */
    8. Void * State;
    9. /* Save the bytes so they can be read and written by speex */
    10. Speexbits bits;
    11. Int I, TMP;
    12. // Create a new encoding state in narrowband Mode
    13. State = speex_encoder_init (& speex_nb_mode );
    14. // Set the quality to 8 (15 Kbps)
    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 can save the data
    20. Speex_bits_init (& bits );
    21. While (1)
    22. {
      1. // Read a 16 bits 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.  
      7. // Clear all the bytes in the struct so that we can encode a new frame.
      8. Speex_bits_reset (& bits );
      9. // Encode the frame
      10. Speex_encode (State, input, & bits );
      11. // Copy bits to a char array
      12. Nbbytes = speex_bits_write (& bits, cbits, 200 );
      13. // Write the frame size first. This is a value required by the sampledec file, but it may be different in your application.
      14. Fwrite (& nbbytes, sizeof (INT), 1, stdout );
      15. // Write the compressed Array
      16. Fwrite (cbits, 1, nbbytes, stdout );
    23. }
    24. // Release the status of the Encoder
    25. Speex_encoder_destroy (State );
    26. // Release the bit_packing Structure
    27. Speex_bits_destroy (& bits );
    28. Fclose (FIN );
    29. Return 0;
  7. }
Source: http://blog.163.com/yuan_zhch/blog/static/193790046201172611527217/

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.