speex.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /* Copyright (C) 2002 Jean-Marc Valin
  2. File: speex.c
  3. Basic Speex functions
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions
  6. are met:
  7. - Redistributions of source code must retain the above copyright
  8. notice, this list of conditions and the following disclaimer.
  9. - Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in the
  11. documentation and/or other materials provided with the distribution.
  12. - Neither the name of the Xiph.org Foundation nor the names of its
  13. contributors may be used to endorse or promote products derived from
  14. this software without specific prior written permission.
  15. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  16. ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  17. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  18. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
  19. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  20. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  22. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  23. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  24. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #ifdef HAVE_CONFIG_H
  28. #include "config.h"
  29. #endif
  30. #include "modes.h"
  31. #include <math.h>
  32. #include "os_support.h"
  33. #ifndef NULL
  34. #define NULL 0
  35. #endif
  36. #define MAX_IN_SAMPLES 640
  37. EXPORT void *speex_encoder_init(const SpeexMode *mode)
  38. {
  39. return mode->enc_init(mode);
  40. }
  41. EXPORT void *speex_decoder_init(const SpeexMode *mode)
  42. {
  43. return mode->dec_init(mode);
  44. }
  45. EXPORT void speex_encoder_destroy(void *state)
  46. {
  47. (*((SpeexMode**)state))->enc_destroy(state);
  48. }
  49. EXPORT void speex_decoder_destroy(void *state)
  50. {
  51. (*((SpeexMode**)state))->dec_destroy(state);
  52. }
  53. int speex_encode_native(void *state, spx_word16_t *in, SpeexBits *bits)
  54. {
  55. return (*((SpeexMode**)state))->enc(state, in, bits);
  56. }
  57. int speex_decode_native(void *state, SpeexBits *bits, spx_word16_t *out)
  58. {
  59. return (*((SpeexMode**)state))->dec(state, bits, out);
  60. }
  61. #ifdef FIXED_POINT
  62. #ifndef DISABLE_FLOAT_API
  63. EXPORT int speex_encode(void *state, float *in, SpeexBits *bits)
  64. {
  65. int i;
  66. spx_int32_t N;
  67. spx_int16_t short_in[MAX_IN_SAMPLES];
  68. speex_encoder_ctl(state, SPEEX_GET_FRAME_SIZE, &N);
  69. for (i=0;i<N;i++)
  70. {
  71. if (in[i]>32767.f)
  72. short_in[i] = 32767;
  73. else if (in[i]<-32768.f)
  74. short_in[i] = -32768;
  75. else
  76. short_in[i] = (spx_int16_t)floor(.5+in[i]);
  77. }
  78. return (*((SpeexMode**)state))->enc(state, short_in, bits);
  79. }
  80. #endif /* #ifndef DISABLE_FLOAT_API */
  81. EXPORT int speex_encode_int(void *state, spx_int16_t *in, SpeexBits *bits)
  82. {
  83. SpeexMode *mode;
  84. mode = *(SpeexMode**)state;
  85. return (mode)->enc(state, in, bits);
  86. }
  87. #ifndef DISABLE_FLOAT_API
  88. EXPORT int speex_decode(void *state, SpeexBits *bits, float *out)
  89. {
  90. int i, ret;
  91. spx_int32_t N;
  92. spx_int16_t short_out[MAX_IN_SAMPLES];
  93. speex_decoder_ctl(state, SPEEX_GET_FRAME_SIZE, &N);
  94. ret = (*((SpeexMode**)state))->dec(state, bits, short_out);
  95. for (i=0;i<N;i++)
  96. out[i] = short_out[i];
  97. return ret;
  98. }
  99. #endif /* #ifndef DISABLE_FLOAT_API */
  100. EXPORT int speex_decode_int(void *state, SpeexBits *bits, spx_int16_t *out)
  101. {
  102. SpeexMode *mode = *(SpeexMode**)state;
  103. return (mode)->dec(state, bits, out);
  104. }
  105. #else
  106. EXPORT int speex_encode(void *state, float *in, SpeexBits *bits)
  107. {
  108. return (*((SpeexMode**)state))->enc(state, in, bits);
  109. }
  110. EXPORT int speex_encode_int(void *state, spx_int16_t *in, SpeexBits *bits)
  111. {
  112. int i;
  113. spx_int32_t N;
  114. float float_in[MAX_IN_SAMPLES];
  115. speex_encoder_ctl(state, SPEEX_GET_FRAME_SIZE, &N);
  116. for (i=0;i<N;i++)
  117. float_in[i] = in[i];
  118. return (*((SpeexMode**)state))->enc(state, float_in, bits);
  119. }
  120. EXPORT int speex_decode(void *state, SpeexBits *bits, float *out)
  121. {
  122. return (*((SpeexMode**)state))->dec(state, bits, out);
  123. }
  124. EXPORT int speex_decode_int(void *state, SpeexBits *bits, spx_int16_t *out)
  125. {
  126. int i;
  127. spx_int32_t N;
  128. float float_out[MAX_IN_SAMPLES];
  129. int ret;
  130. speex_decoder_ctl(state, SPEEX_GET_FRAME_SIZE, &N);
  131. ret = (*((SpeexMode**)state))->dec(state, bits, float_out);
  132. if (ret == 0)
  133. {
  134. for (i=0;i<N;i++)
  135. {
  136. if (float_out[i]>32767.f)
  137. out[i] = 32767;
  138. else if (float_out[i]<-32768.f)
  139. out[i] = -32768;
  140. else
  141. out[i] = (spx_int16_t)floor(.5+float_out[i]);
  142. }
  143. }
  144. return ret;
  145. }
  146. #endif
  147. EXPORT int speex_encoder_ctl(void *state, int request, void *ptr)
  148. {
  149. return (*((SpeexMode**)state))->enc_ctl(state, request, ptr);
  150. }
  151. EXPORT int speex_decoder_ctl(void *state, int request, void *ptr)
  152. {
  153. return (*((SpeexMode**)state))->dec_ctl(state, request, ptr);
  154. }
  155. int nb_mode_query(const void *mode, int request, void *ptr)
  156. {
  157. const SpeexNBMode *m = (const SpeexNBMode*)mode;
  158. switch (request)
  159. {
  160. case SPEEX_MODE_FRAME_SIZE:
  161. *((int*)ptr)=m->frameSize;
  162. break;
  163. case SPEEX_SUBMODE_BITS_PER_FRAME:
  164. if (*((int*)ptr)==0)
  165. *((int*)ptr) = NB_SUBMODE_BITS+1;
  166. else if (m->submodes[*((int*)ptr)]==NULL)
  167. *((int*)ptr) = -1;
  168. else
  169. *((int*)ptr) = m->submodes[*((int*)ptr)]->bits_per_frame;
  170. break;
  171. default:
  172. speex_warning_int("Unknown nb_mode_query request: ", request);
  173. return -1;
  174. }
  175. return 0;
  176. }
  177. EXPORT int speex_lib_ctl(int request, void *ptr)
  178. {
  179. switch (request)
  180. {
  181. case SPEEX_LIB_GET_MAJOR_VERSION:
  182. *((int*)ptr) = SPEEX_MAJOR_VERSION;
  183. break;
  184. case SPEEX_LIB_GET_MINOR_VERSION:
  185. *((int*)ptr) = SPEEX_MINOR_VERSION;
  186. break;
  187. case SPEEX_LIB_GET_MICRO_VERSION:
  188. *((int*)ptr) = SPEEX_MICRO_VERSION;
  189. break;
  190. case SPEEX_LIB_GET_EXTRA_VERSION:
  191. *((const char**)ptr) = SPEEX_EXTRA_VERSION;
  192. break;
  193. case SPEEX_LIB_GET_VERSION_STRING:
  194. *((const char**)ptr) = SPEEX_VERSION;
  195. break;
  196. /*case SPEEX_LIB_SET_ALLOC_FUNC:
  197. break;
  198. case SPEEX_LIB_GET_ALLOC_FUNC:
  199. break;
  200. case SPEEX_LIB_SET_FREE_FUNC:
  201. break;
  202. case SPEEX_LIB_GET_FREE_FUNC:
  203. break;*/
  204. default:
  205. speex_warning_int("Unknown wb_mode_query request: ", request);
  206. return -1;
  207. }
  208. return 0;
  209. }