speex.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. for (i=0;i<N;i++)
  133. {
  134. if (float_out[i]>32767.f)
  135. out[i] = 32767;
  136. else if (float_out[i]<-32768.f)
  137. out[i] = -32768;
  138. else
  139. out[i] = (spx_int16_t)floor(.5+float_out[i]);
  140. }
  141. return ret;
  142. }
  143. #endif
  144. EXPORT int speex_encoder_ctl(void *state, int request, void *ptr)
  145. {
  146. return (*((SpeexMode**)state))->enc_ctl(state, request, ptr);
  147. }
  148. EXPORT int speex_decoder_ctl(void *state, int request, void *ptr)
  149. {
  150. return (*((SpeexMode**)state))->dec_ctl(state, request, ptr);
  151. }
  152. int nb_mode_query(const void *mode, int request, void *ptr)
  153. {
  154. const SpeexNBMode *m = (const SpeexNBMode*)mode;
  155. switch (request)
  156. {
  157. case SPEEX_MODE_FRAME_SIZE:
  158. *((int*)ptr)=m->frameSize;
  159. break;
  160. case SPEEX_SUBMODE_BITS_PER_FRAME:
  161. if (*((int*)ptr)==0)
  162. *((int*)ptr) = NB_SUBMODE_BITS+1;
  163. else if (m->submodes[*((int*)ptr)]==NULL)
  164. *((int*)ptr) = -1;
  165. else
  166. *((int*)ptr) = m->submodes[*((int*)ptr)]->bits_per_frame;
  167. break;
  168. default:
  169. speex_warning_int("Unknown nb_mode_query request: ", request);
  170. return -1;
  171. }
  172. return 0;
  173. }
  174. EXPORT int speex_lib_ctl(int request, void *ptr)
  175. {
  176. switch (request)
  177. {
  178. case SPEEX_LIB_GET_MAJOR_VERSION:
  179. *((int*)ptr) = SPEEX_MAJOR_VERSION;
  180. break;
  181. case SPEEX_LIB_GET_MINOR_VERSION:
  182. *((int*)ptr) = SPEEX_MINOR_VERSION;
  183. break;
  184. case SPEEX_LIB_GET_MICRO_VERSION:
  185. *((int*)ptr) = SPEEX_MICRO_VERSION;
  186. break;
  187. case SPEEX_LIB_GET_EXTRA_VERSION:
  188. *((const char**)ptr) = SPEEX_EXTRA_VERSION;
  189. break;
  190. case SPEEX_LIB_GET_VERSION_STRING:
  191. *((const char**)ptr) = SPEEX_VERSION;
  192. break;
  193. /*case SPEEX_LIB_SET_ALLOC_FUNC:
  194. break;
  195. case SPEEX_LIB_GET_ALLOC_FUNC:
  196. break;
  197. case SPEEX_LIB_SET_FREE_FUNC:
  198. break;
  199. case SPEEX_LIB_GET_FREE_FUNC:
  200. break;*/
  201. default:
  202. speex_warning_int("Unknown wb_mode_query request: ", request);
  203. return -1;
  204. }
  205. return 0;
  206. }