1*28e138c6SAndroid Build Coastguard Worker<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 2*28e138c6SAndroid Build Coastguard Worker<html> 3*28e138c6SAndroid Build Coastguard Worker<head> 4*28e138c6SAndroid Build Coastguard Worker <title>Speex Programming</title> 5*28e138c6SAndroid Build Coastguard Worker 6*28e138c6SAndroid Build Coastguard Worker <meta http-equiv="content-type" 7*28e138c6SAndroid Build Coastguard Worker content="text/html; charset=ISO-8859-1"> 8*28e138c6SAndroid Build Coastguard Worker</head> 9*28e138c6SAndroid Build Coastguard Worker <body> 10*28e138c6SAndroid Build Coastguard Worker 11*28e138c6SAndroid Build Coastguard Worker<div align="center"> 12*28e138c6SAndroid Build Coastguard Worker<h1>Speex Programming</h1> 13*28e138c6SAndroid Build Coastguard Worker 14*28e138c6SAndroid Build Coastguard Worker<div align="left"> 15*28e138c6SAndroid Build Coastguard Worker<h2>Encoding</h2> 16*28e138c6SAndroid Build Coastguard Worker In order to encode speech using Speex, you first need to:<br> 17*28e138c6SAndroid Build Coastguard Worker 18*28e138c6SAndroid Build Coastguard Worker<blockquote> 19*28e138c6SAndroid Build Coastguard Worker <pre><big>#include <speex.h></big></pre> 20*28e138c6SAndroid Build Coastguard Worker </blockquote> 21*28e138c6SAndroid Build Coastguard Worker You then need to declare a Speex bit-packing struct<br> 22*28e138c6SAndroid Build Coastguard Worker 23*28e138c6SAndroid Build Coastguard Worker<blockquote> 24*28e138c6SAndroid Build Coastguard Worker <pre><big>SpeexBits bits;</big></pre> 25*28e138c6SAndroid Build Coastguard Worker </blockquote> 26*28e138c6SAndroid Build Coastguard Worker and a Speex encoder state<br> 27*28e138c6SAndroid Build Coastguard Worker 28*28e138c6SAndroid Build Coastguard Worker<blockquote> 29*28e138c6SAndroid Build Coastguard Worker <pre><big>void *enc_state;</big></pre> 30*28e138c6SAndroid Build Coastguard Worker </blockquote> 31*28e138c6SAndroid Build Coastguard Worker The two are initialized by:<br> 32*28e138c6SAndroid Build Coastguard Worker 33*28e138c6SAndroid Build Coastguard Worker<blockquote> 34*28e138c6SAndroid Build Coastguard Worker <pre><big>speex_bits_init(&bits);</big></pre> 35*28e138c6SAndroid Build Coastguard Worker 36*28e138c6SAndroid Build Coastguard Worker <pre><big>enc_state = speex_encoder_init(&speex_nb_mode);</big></pre> 37*28e138c6SAndroid Build Coastguard Worker </blockquote> 38*28e138c6SAndroid Build Coastguard Worker For wideband coding, <i>speex_nb_mode</i> will be replaced by <i>speex_wb_mode</i> 39*28e138c6SAndroid Build Coastguard Worker. In most cases, you will need to know the frame size used by the mode you 40*28e138c6SAndroid Build Coastguard Workerare using. You can get that value in the <i>frame_size</i> variable with:<br> 41*28e138c6SAndroid Build Coastguard Worker<blockquote><big><tt>speex_encoder_ctl(enc_state, SPEEX_GET_FRAME_SIZE, &frame_size);</tt></big><br> 42*28e138c6SAndroid Build Coastguard Worker</blockquote> 43*28e138c6SAndroid Build Coastguard Worker For every input frame:<br> 44*28e138c6SAndroid Build Coastguard Worker 45*28e138c6SAndroid Build Coastguard Worker<blockquote> 46*28e138c6SAndroid Build Coastguard Worker <pre><big>speex_bits_reset(&bits);</big></pre> 47*28e138c6SAndroid Build Coastguard Worker 48*28e138c6SAndroid Build Coastguard Worker <pre><big>speex_encode(enc_state, input_frame, &bits);</big></pre> 49*28e138c6SAndroid Build Coastguard Worker 50*28e138c6SAndroid Build Coastguard Worker <pre><big>nbBytes = speex_bits_write(&bits, byte_ptr, MAX_NB_BYTES);</big></pre> 51*28e138c6SAndroid Build Coastguard Worker </blockquote> 52*28e138c6SAndroid Build Coastguard Worker where <i>input_frame</i> is a <i>(float *)</i> pointing to the beginning 53*28e138c6SAndroid Build Coastguard Workerof a speech frame, byte_ptr is a <i>(char *)</i> where the encoded frame will 54*28e138c6SAndroid Build Coastguard Workerbe written, <i>MAX_NB_BYTES</i> is the maximum number of bytes that can be 55*28e138c6SAndroid Build Coastguard Workerwritten to <i>byte_ptr</i> without causing an overflow and <i>nbBytes</i> 56*28e138c6SAndroid Build Coastguard Worker is the number of bytes actually written to <i>byte_ptr</i> (the encoded 57*28e138c6SAndroid Build Coastguard Workersize in bytes).<br> 58*28e138c6SAndroid Build Coastguard Worker <br> 59*28e138c6SAndroid Build Coastguard Worker After you're done with the encoding, free all resources with:<br> 60*28e138c6SAndroid Build Coastguard Worker 61*28e138c6SAndroid Build Coastguard Worker<blockquote> 62*28e138c6SAndroid Build Coastguard Worker <pre><big>speex_bits_destroy(&bits);</big></pre> 63*28e138c6SAndroid Build Coastguard Worker 64*28e138c6SAndroid Build Coastguard Worker <pre><big>speex_encoder_destroy(&enc_state);</big></pre> 65*28e138c6SAndroid Build Coastguard Worker </blockquote> 66*28e138c6SAndroid Build Coastguard Worker That's about it for the encoder.<br> 67*28e138c6SAndroid Build Coastguard Worker 68*28e138c6SAndroid Build Coastguard Worker<h2>Decoding</h2> 69*28e138c6SAndroid Build Coastguard Worker In order to encode speech using Speex, you first need to:<br> 70*28e138c6SAndroid Build Coastguard Worker 71*28e138c6SAndroid Build Coastguard Worker<blockquote> 72*28e138c6SAndroid Build Coastguard Worker <pre><big>#include <speex.h></big></pre> 73*28e138c6SAndroid Build Coastguard Worker </blockquote> 74*28e138c6SAndroid Build Coastguard Worker You then need to declare a Speex bit-packing struct<br> 75*28e138c6SAndroid Build Coastguard Worker 76*28e138c6SAndroid Build Coastguard Worker<blockquote> 77*28e138c6SAndroid Build Coastguard Worker <pre><big>SpeexBits bits;</big></pre> 78*28e138c6SAndroid Build Coastguard Worker </blockquote> 79*28e138c6SAndroid Build Coastguard Worker and a Speex encoder state<br> 80*28e138c6SAndroid Build Coastguard Worker 81*28e138c6SAndroid Build Coastguard Worker<blockquote> 82*28e138c6SAndroid Build Coastguard Worker <pre><big>void *dec_state;</big></pre> 83*28e138c6SAndroid Build Coastguard Worker </blockquote> 84*28e138c6SAndroid Build Coastguard Worker The two are initialized by:<br> 85*28e138c6SAndroid Build Coastguard Worker 86*28e138c6SAndroid Build Coastguard Worker<blockquote> 87*28e138c6SAndroid Build Coastguard Worker <pre><big>speex_bits_init(&bits);</big></pre> 88*28e138c6SAndroid Build Coastguard Worker 89*28e138c6SAndroid Build Coastguard Worker <pre><big>dec_state = speex_decoder_init(&speex_nb_mode);</big></pre> 90*28e138c6SAndroid Build Coastguard Worker </blockquote> 91*28e138c6SAndroid Build Coastguard Worker For wideband decoding, <i>speex_nb_mode</i> will be replaced by <i>speex_wb_mode</i> 92*28e138c6SAndroid Build Coastguard Worker. You can get that value in the <i>frame_size</i> variable with:<br> 93*28e138c6SAndroid Build Coastguard Worker 94*28e138c6SAndroid Build Coastguard Worker<blockquote><big><tt>speex_decoder_ctl(dec_state, SPEEX_GET_FRAME_SIZE, &frame_size);</tt></big><br> 95*28e138c6SAndroid Build Coastguard Worker</blockquote> 96*28e138c6SAndroid Build Coastguard Worker There is also a parameter that can be set for the decoder: whether or not 97*28e138c6SAndroid Build Coastguard Workerto use a perceptual post-filter. This can be set by:<br> 98*28e138c6SAndroid Build Coastguard Worker<blockquote><big><tt>speex_decoder_ctl(dec_state, SPEEX_SET_PF, &pf);</tt></big><br> 99*28e138c6SAndroid Build Coastguard Worker</blockquote> 100*28e138c6SAndroid Build Coastguard Workerwhere <i>pf</i> is an <i>int</i> that with value 0 to have the post-filter 101*28e138c6SAndroid Build Coastguard Workerdisabled and 1 to have it enabled.<br> 102*28e138c6SAndroid Build Coastguard Worker<br> 103*28e138c6SAndroid Build Coastguard WorkerFor every input frame:<br> 104*28e138c6SAndroid Build Coastguard Worker 105*28e138c6SAndroid Build Coastguard Worker<blockquote> 106*28e138c6SAndroid Build Coastguard Worker <pre><big>speex_bits_read_from(&bits, input_bytes, nbBytes);</big></pre> 107*28e138c6SAndroid Build Coastguard Worker 108*28e138c6SAndroid Build Coastguard Worker <pre><big>speex_decode(st, &bits, output_frame, 0);</big></pre> 109*28e138c6SAndroid Build Coastguard Worker </blockquote> 110*28e138c6SAndroid Build Coastguard Worker where <i>input_bytes</i> is a <i>(char *)</i> containing the bit-stream 111*28e138c6SAndroid Build Coastguard Workerdata received for a frame, <i>nbBytes</i> is the size (in bytes) of that 112*28e138c6SAndroid Build Coastguard Workerbit-stream, and <i>output_frame</i> is a <i>(float *)</i> and points to the 113*28e138c6SAndroid Build Coastguard Workerarea where the decoded speech frame will be written. The last argument indicates 114*28e138c6SAndroid Build Coastguard Workerwhether the frame we'd like to decode was lost. A value of 0 indicates the 115*28e138c6SAndroid Build Coastguard Workernormal case where bits points to the bit of the current frame. A value of 116*28e138c6SAndroid Build Coastguard Worker1 indicates that we don't have the bits for the current frame, in which case 117*28e138c6SAndroid Build Coastguard Workerthe bits argument should be the same as the bits for the last correctly received 118*28e138c6SAndroid Build Coastguard Workerframe. When a frame is lost, the Speex decoder will do its best to "guess" 119*28e138c6SAndroid Build Coastguard Workerthe sorrect signal.<br> 120*28e138c6SAndroid Build Coastguard Worker <br> 121*28e138c6SAndroid Build Coastguard Worker </div> 122*28e138c6SAndroid Build Coastguard Worker </div> 123*28e138c6SAndroid Build Coastguard Worker 124*28e138c6SAndroid Build Coastguard Worker</body> 125*28e138c6SAndroid Build Coastguard Worker</html> 126