The
libspeex library contains all the functions for encoding and decoding
speech with the Speex codec. When linking on a UNIX system, one must add
-lspeex -lm to the compiler command line. One important
thing to know is that libspeex calls are reentrant, but not
thread-safe. That means that it is fine to use calls from many threads,
but calls using the same state from multiple threads must be protected
by mutexes. Examples of code can also be found in Appendix
A and the complete API documentation is included in the Documentation
section of the Speex website (http://www.speex.org/).
Speex编解码器的libspeex包囊括了所有的语音编码和解码函数。在Linux系统中连接时,必须在编译器命令行中加入-lspeex
–lm。需要知道的是,虽然libspeex的函数调用是可重入的,但不是线程安全的,所以在多线程调用时,如果使用共享资源需要进行互斥保护。附录A中有代码实例,在Speex站点(http://www.speex.org/
)的文档部分能下到完整的API文档。
5.1编码
In order to encode speech using Speex, one first needs to:
#include <speex/speex.h>
Then in the code, a Speex bit-packing struct must be declared, along with a Speex encoder state:
SpeexBits bits;
void *enc_state;
The two are initialized by:
speex_bits_init(&bits);
enc_state = speex_encoder_init(&speex_nb_mode);
For wideband coding, speex_nb_mode will be replaced by speex_wb_mode. In
most cases, you will need to know the frame size used at the sampling
rate you are using. You can get that value in the frame_size variable
(expressed in samples, not
bytes) with:
speex_encoder_ctl(enc_state,SPEEX_GET_FRAME_SIZE,&frame_size);
In practice, frame_size will correspond to 20 ms when using 8, 16, or 32
kHz sampling rate. There are many parameters that can be set for the
Speex encoder, but the most useful one is the quality parameter that
controls the quality vs bit-rate tradeoff.
This is set by:
speex_encoder_ctl(enc_state,SPEEX_SET_QUALITY,&quality);
where quality is an integer value ranging from 0 to 10 (inclusively).
The mapping between quality and bit-rate is described in Fig. 9.2 for
narrowband.
Once the initialization is done, for every input frame:
speex_bits_reset(&bits);
speex_encode_int(enc_state, input_frame, &bits);
nbBytes = speex_bits_write(&bits, byte_ptr, MAX_NB_BYTES);
where input_frame is a (short *) pointing to the beginning of a speech
frame, byte_ptr is a (char *) where the encoded frame will be
written,MAX_NB_BYTES is the maximumnumber of bytes that can be written
to byte_ptr without causing an overflow and nbBytes is
the number of bytes actually written to byte_ptr (