153a5a1b3Sopenharmony_ci<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
253a5a1b3Sopenharmony_ci<html>
353a5a1b3Sopenharmony_ci<head>
453a5a1b3Sopenharmony_ci  <title>Speex Programming</title>
553a5a1b3Sopenharmony_ci      
653a5a1b3Sopenharmony_ci  <meta http-equiv="content-type"
753a5a1b3Sopenharmony_ci content="text/html; charset=ISO-8859-1">
853a5a1b3Sopenharmony_ci</head>
953a5a1b3Sopenharmony_ci  <body>
1053a5a1b3Sopenharmony_ci 
1153a5a1b3Sopenharmony_ci<div align="center"> 
1253a5a1b3Sopenharmony_ci<h1>Speex Programming</h1>
1353a5a1b3Sopenharmony_ci 
1453a5a1b3Sopenharmony_ci<div align="left"> 
1553a5a1b3Sopenharmony_ci<h2>Encoding</h2>
1653a5a1b3Sopenharmony_ci In order to encode speech using Speex, you first need to:<br>
1753a5a1b3Sopenharmony_ci 
1853a5a1b3Sopenharmony_ci<blockquote>   
1953a5a1b3Sopenharmony_ci  <pre><big>#include &lt;speex.h&gt;</big></pre>
2053a5a1b3Sopenharmony_ci </blockquote>
2153a5a1b3Sopenharmony_ci You then need to declare a Speex bit-packing struct<br>
2253a5a1b3Sopenharmony_ci 
2353a5a1b3Sopenharmony_ci<blockquote>   
2453a5a1b3Sopenharmony_ci  <pre><big>SpeexBits bits;</big></pre>
2553a5a1b3Sopenharmony_ci </blockquote>
2653a5a1b3Sopenharmony_ci and a Speex encoder state<br>
2753a5a1b3Sopenharmony_ci 
2853a5a1b3Sopenharmony_ci<blockquote>   
2953a5a1b3Sopenharmony_ci  <pre><big>void *enc_state;</big></pre>
3053a5a1b3Sopenharmony_ci </blockquote>
3153a5a1b3Sopenharmony_ci The two are initialized by:<br>
3253a5a1b3Sopenharmony_ci 
3353a5a1b3Sopenharmony_ci<blockquote>   
3453a5a1b3Sopenharmony_ci  <pre><big>speex_bits_init(&amp;bits);</big></pre>
3553a5a1b3Sopenharmony_ci   
3653a5a1b3Sopenharmony_ci  <pre><big>enc_state = speex_encoder_init(&amp;speex_nb_mode);</big></pre>
3753a5a1b3Sopenharmony_ci </blockquote>
3853a5a1b3Sopenharmony_ci For wideband coding, <i>speex_nb_mode</i> will be replaced by <i>speex_wb_mode</i>
3953a5a1b3Sopenharmony_ci. In most cases, you will need to know the frame size used by the mode you
4053a5a1b3Sopenharmony_ciare using. You can get that value in the <i>frame_size</i> variable with:<br>
4153a5a1b3Sopenharmony_ci<blockquote><big><tt>speex_encoder_ctl(enc_state, SPEEX_GET_FRAME_SIZE, &amp;frame_size);</tt></big><br>
4253a5a1b3Sopenharmony_ci</blockquote>
4353a5a1b3Sopenharmony_ci For every input frame:<br>
4453a5a1b3Sopenharmony_ci 
4553a5a1b3Sopenharmony_ci<blockquote>   
4653a5a1b3Sopenharmony_ci  <pre><big>speex_bits_reset(&amp;bits);</big></pre>
4753a5a1b3Sopenharmony_ci   
4853a5a1b3Sopenharmony_ci  <pre><big>speex_encode(enc_state, input_frame, &amp;bits);</big></pre>
4953a5a1b3Sopenharmony_ci   
5053a5a1b3Sopenharmony_ci  <pre><big>nbBytes = speex_bits_write(&amp;bits, byte_ptr, MAX_NB_BYTES);</big></pre>
5153a5a1b3Sopenharmony_ci </blockquote>
5253a5a1b3Sopenharmony_ci where <i>input_frame</i> is a <i>(float *)</i> pointing to the beginning 
5353a5a1b3Sopenharmony_ciof a speech frame, byte_ptr is a <i>(char *)</i> where the encoded frame will
5453a5a1b3Sopenharmony_cibe written, <i>MAX_NB_BYTES</i> is the maximum number of bytes that can be
5553a5a1b3Sopenharmony_ciwritten to <i>byte_ptr</i> without causing an overflow and <i>nbBytes</i>
5653a5a1b3Sopenharmony_ci  is the number of bytes actually written to <i>byte_ptr</i> (the encoded 
5753a5a1b3Sopenharmony_cisize in bytes).<br>
5853a5a1b3Sopenharmony_ci <br>
5953a5a1b3Sopenharmony_ci After you're done with the encoding, free all resources with:<br>
6053a5a1b3Sopenharmony_ci 
6153a5a1b3Sopenharmony_ci<blockquote>   
6253a5a1b3Sopenharmony_ci  <pre><big>speex_bits_destroy(&amp;bits);</big></pre>
6353a5a1b3Sopenharmony_ci   
6453a5a1b3Sopenharmony_ci  <pre><big>speex_encoder_destroy(&amp;enc_state);</big></pre>
6553a5a1b3Sopenharmony_ci </blockquote>
6653a5a1b3Sopenharmony_ci That's about it for the encoder.<br>
6753a5a1b3Sopenharmony_ci 
6853a5a1b3Sopenharmony_ci<h2>Decoding</h2>
6953a5a1b3Sopenharmony_ci In order to encode speech using Speex, you first need to:<br>
7053a5a1b3Sopenharmony_ci   
7153a5a1b3Sopenharmony_ci<blockquote>      
7253a5a1b3Sopenharmony_ci  <pre><big>#include &lt;speex.h&gt;</big></pre>
7353a5a1b3Sopenharmony_ci  </blockquote>
7453a5a1b3Sopenharmony_ci  You then need to declare a Speex bit-packing struct<br>
7553a5a1b3Sopenharmony_ci   
7653a5a1b3Sopenharmony_ci<blockquote>      
7753a5a1b3Sopenharmony_ci  <pre><big>SpeexBits bits;</big></pre>
7853a5a1b3Sopenharmony_ci  </blockquote>
7953a5a1b3Sopenharmony_ci  and a Speex encoder state<br>
8053a5a1b3Sopenharmony_ci   
8153a5a1b3Sopenharmony_ci<blockquote>      
8253a5a1b3Sopenharmony_ci  <pre><big>void *dec_state;</big></pre>
8353a5a1b3Sopenharmony_ci  </blockquote>
8453a5a1b3Sopenharmony_ci  The two are initialized by:<br>
8553a5a1b3Sopenharmony_ci   
8653a5a1b3Sopenharmony_ci<blockquote>      
8753a5a1b3Sopenharmony_ci  <pre><big>speex_bits_init(&amp;bits);</big></pre>
8853a5a1b3Sopenharmony_ci       
8953a5a1b3Sopenharmony_ci  <pre><big>dec_state = speex_decoder_init(&amp;speex_nb_mode);</big></pre>
9053a5a1b3Sopenharmony_ci  </blockquote>
9153a5a1b3Sopenharmony_ci  For wideband decoding, <i>speex_nb_mode</i> will be replaced by <i>speex_wb_mode</i>
9253a5a1b3Sopenharmony_ci. You can get that value in the <i>frame_size</i> variable with:<br>
9353a5a1b3Sopenharmony_ci 
9453a5a1b3Sopenharmony_ci<blockquote><big><tt>speex_decoder_ctl(dec_state, SPEEX_GET_FRAME_SIZE, &amp;frame_size);</tt></big><br>
9553a5a1b3Sopenharmony_ci</blockquote>
9653a5a1b3Sopenharmony_ci  There is also a parameter that can be set for the decoder: whether or not
9753a5a1b3Sopenharmony_cito use a perceptual post-filter. This can be set by:<br>
9853a5a1b3Sopenharmony_ci<blockquote><big><tt>speex_decoder_ctl(dec_state, SPEEX_SET_PF, &amp;pf);</tt></big><br>
9953a5a1b3Sopenharmony_ci</blockquote>
10053a5a1b3Sopenharmony_ciwhere <i>pf</i> is an <i>int</i> that with value 0 to have the post-filter
10153a5a1b3Sopenharmony_cidisabled and 1 to have it enabled.<br>
10253a5a1b3Sopenharmony_ci<br>
10353a5a1b3Sopenharmony_ciFor every input frame:<br>
10453a5a1b3Sopenharmony_ci       
10553a5a1b3Sopenharmony_ci<blockquote>   
10653a5a1b3Sopenharmony_ci  <pre><big>speex_bits_read_from(&amp;bits, input_bytes, nbBytes);</big></pre>
10753a5a1b3Sopenharmony_ci   
10853a5a1b3Sopenharmony_ci  <pre><big>speex_decode(st, &amp;bits, output_frame, 0);</big></pre>
10953a5a1b3Sopenharmony_ci </blockquote>
11053a5a1b3Sopenharmony_ci where <i>input_bytes</i> is a <i>(char *)</i> containing the bit-stream
11153a5a1b3Sopenharmony_cidata received for a frame, <i>nbBytes</i> is the size (in bytes) of that
11253a5a1b3Sopenharmony_cibit-stream, and <i>output_frame</i> is a <i>(float *)</i> and points to the
11353a5a1b3Sopenharmony_ciarea where the decoded speech frame will be written. The last argument indicates
11453a5a1b3Sopenharmony_ciwhether the frame we'd like to decode was lost. A value of 0 indicates the
11553a5a1b3Sopenharmony_cinormal case where bits points to the bit of the current frame. A value of
11653a5a1b3Sopenharmony_ci1 indicates that we don't have the bits for the current frame, in which case
11753a5a1b3Sopenharmony_cithe bits argument should be the same as the bits for the last correctly received
11853a5a1b3Sopenharmony_ciframe. When a frame is lost, the Speex decoder will do its best to "guess"
11953a5a1b3Sopenharmony_cithe sorrect signal.<br>
12053a5a1b3Sopenharmony_ci <br>
12153a5a1b3Sopenharmony_ci </div>
12253a5a1b3Sopenharmony_ci </div>
12353a5a1b3Sopenharmony_ci 
12453a5a1b3Sopenharmony_ci</body>
12553a5a1b3Sopenharmony_ci</html>
126