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:
- # Include <speex. h>
- # Include <stdio. h>
- /* The frame size is a fixed value in this routine, but it is not required */
- # Define frame_size 160
- Int main (INT argc, char ** argv)
- {
- Char * infile;
- File * fin;
- Short in [frame_size];
- Float input [frame_size];
- Char cbits [200];
- Int nbbytes;
- /* Save the encoding status */
- Void * State;
- /* Save the bytes so they can be read and written by speex */
- Speexbits bits;
- Int I, TMP;
- // Create a new encoding state in narrowband Mode
- State = speex_encoder_init (& speex_nb_mode );
- // Set the quality to 8 (15 Kbps)
- TMP = 8;
- Speex_encoder_ctl (State, speex_set_quality, & TMP );
- Infile = argv [1];
- Fin = fopen (infile, "R ");
- // Initialize the structure so that they can save the data
- Speex_bits_init (& bits );
- While (1)
- {
- // Read a 16 bits sound
- Fread (in, sizeof (short), frame_size, fin );
- If (feof (FIN ))
- Break;
- // Convert the value of 16bits to float so that the speex library can work on it
- For (I = 0; I <frame_size; I ++)
- Input [I] = in [I];
-
- // Clear all the bytes in the struct so that we can encode a new frame.
- Speex_bits_reset (& bits );
- // Encode the frame
- Speex_encode (State, input, & bits );
- // Copy bits to a char array
- Nbbytes = speex_bits_write (& bits, cbits, 200 );
- // Write the frame size first. This is a value required by the sampledec file, but it may be different in your application.
- Fwrite (& nbbytes, sizeof (INT), 1, stdout );
- // Write the compressed Array
- Fwrite (cbits, 1, nbbytes, stdout );
- }
- // Release the status of the Encoder
- Speex_encoder_destroy (State );
- // Release the bit_packing Structure
- Speex_bits_destroy (& bits );
- Fclose (FIN );
- Return 0;
- }
Source: http://blog.163.com/yuan_zhch/blog/static/193790046201172611527217/