programming.html 4.3 KB

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