speex_echo.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /* Copyright (C) Jean-Marc Valin */
  2. /**
  3. @file speex_echo.h
  4. @brief Echo cancellation
  5. */
  6. /*
  7. Redistribution and use in source and binary forms, with or without
  8. modification, are permitted provided that the following conditions are
  9. met:
  10. 1. Redistributions of source code must retain the above copyright notice,
  11. this list of conditions and the following disclaimer.
  12. 2. Redistributions in binary form must reproduce the above copyright
  13. notice, this list of conditions and the following disclaimer in the
  14. documentation and/or other materials provided with the distribution.
  15. 3. The name of the author may not be used to endorse or promote products
  16. derived from this software without specific prior written permission.
  17. THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  18. IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  19. OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20. DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
  21. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  22. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  23. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  25. STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  26. ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  27. POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #ifndef SPEEX_ECHO_H
  30. #define SPEEX_ECHO_H
  31. /** @defgroup SpeexEchoState SpeexEchoState: Acoustic echo canceller
  32. * This is the acoustic echo canceller module.
  33. * @{
  34. */
  35. #include "speex/speex_types.h"
  36. #ifdef __cplusplus
  37. extern "C" {
  38. #endif
  39. /** Obtain frame size used by the AEC */
  40. #define SPEEX_ECHO_GET_FRAME_SIZE 3
  41. /** Set sampling rate */
  42. #define SPEEX_ECHO_SET_SAMPLING_RATE 24
  43. /** Get sampling rate */
  44. #define SPEEX_ECHO_GET_SAMPLING_RATE 25
  45. /* Can't set window sizes */
  46. /** Get size of impulse response (int32) */
  47. #define SPEEX_ECHO_GET_IMPULSE_RESPONSE_SIZE 27
  48. /* Can't set window content */
  49. /** Get impulse response (int32[]) */
  50. #define SPEEX_ECHO_GET_IMPULSE_RESPONSE 29
  51. /** Internal echo canceller state. Should never be accessed directly. */
  52. struct SpeexEchoState_;
  53. /** @class SpeexEchoState
  54. * This holds the state of the echo canceller. You need one per channel.
  55. */
  56. /** Internal echo canceller state. Should never be accessed directly. */
  57. typedef struct SpeexEchoState_ SpeexEchoState;
  58. /** Creates a new echo canceller state
  59. * @param frame_size Number of samples to process at one time (should correspond to 10-20 ms)
  60. * @param filter_length Number of samples of echo to cancel (should generally correspond to 100-500 ms)
  61. * @return Newly-created echo canceller state
  62. */
  63. SpeexEchoState *speex_echo_state_init(int frame_size, int filter_length);
  64. /** Creates a new multi-channel echo canceller state
  65. * @param frame_size Number of samples to process at one time (should correspond to 10-20 ms)
  66. * @param filter_length Number of samples of echo to cancel (should generally correspond to 100-500 ms)
  67. * @param nb_mic Number of microphone channels
  68. * @param nb_speakers Number of speaker channels
  69. * @return Newly-created echo canceller state
  70. */
  71. SpeexEchoState *speex_echo_state_init_mc(int frame_size, int filter_length, int nb_mic, int nb_speakers);
  72. /** Destroys an echo canceller state
  73. * @param st Echo canceller state
  74. */
  75. void speex_echo_state_destroy(SpeexEchoState *st);
  76. /** Performs echo cancellation a frame, based on the audio sent to the speaker (no delay is added
  77. * to playback in this form)
  78. *
  79. * @param st Echo canceller state
  80. * @param rec Signal from the microphone (near end + far end echo)
  81. * @param play Signal played to the speaker (received from far end)
  82. * @param out Returns near-end signal with echo removed
  83. */
  84. void speex_echo_cancellation(SpeexEchoState *st, const spx_int16_t *rec, const spx_int16_t *play, spx_int16_t *out);
  85. /** Performs echo cancellation a frame (deprecated) */
  86. void speex_echo_cancel(SpeexEchoState *st, const spx_int16_t *rec, const spx_int16_t *play, spx_int16_t *out, spx_int32_t *Yout);
  87. /** Perform echo cancellation using internal playback buffer, which is delayed by two frames
  88. * to account for the delay introduced by most soundcards (but it could be off!)
  89. * @param st Echo canceller state
  90. * @param rec Signal from the microphone (near end + far end echo)
  91. * @param out Returns near-end signal with echo removed
  92. */
  93. void speex_echo_capture(SpeexEchoState *st, const spx_int16_t *rec, spx_int16_t *out);
  94. /** Let the echo canceller know that a frame was just queued to the soundcard
  95. * @param st Echo canceller state
  96. * @param play Signal played to the speaker (received from far end)
  97. */
  98. void speex_echo_playback(SpeexEchoState *st, const spx_int16_t *play);
  99. /** Reset the echo canceller to its original state
  100. * @param st Echo canceller state
  101. */
  102. void speex_echo_state_reset(SpeexEchoState *st);
  103. /** Used like the ioctl function to control the echo canceller parameters
  104. *
  105. * @param st Echo canceller state
  106. * @param request ioctl-type request (one of the SPEEX_ECHO_* macros)
  107. * @param ptr Data exchanged to-from function
  108. * @return 0 if no error, -1 if request in unknown
  109. */
  110. int speex_echo_ctl(SpeexEchoState *st, int request, void *ptr);
  111. struct SpeexDecorrState_;
  112. typedef struct SpeexDecorrState_ SpeexDecorrState;
  113. /** Create a state for the channel decorrelation algorithm
  114. This is useful for multi-channel echo cancellation only
  115. * @param rate Sampling rate
  116. * @param channels Number of channels (it's a bit pointless if you don't have at least 2)
  117. * @param frame_size Size of the frame to process at ones (counting samples *per* channel)
  118. */
  119. SpeexDecorrState *speex_decorrelate_new(int rate, int channels, int frame_size);
  120. /** Remove correlation between the channels by modifying the phase and possibly
  121. adding noise in a way that is not (or little) perceptible.
  122. * @param st Decorrelator state
  123. * @param in Input audio in interleaved format
  124. * @param out Result of the decorrelation (out *may* alias in)
  125. * @param strength How much alteration of the audio to apply from 0 to 100.
  126. */
  127. void speex_decorrelate(SpeexDecorrState *st, const spx_int16_t *in, spx_int16_t *out, int strength);
  128. /** Destroy a Decorrelation state
  129. * @param st State to destroy
  130. */
  131. void speex_decorrelate_destroy(SpeexDecorrState *st);
  132. #ifdef __cplusplus
  133. }
  134. #endif
  135. /** @}*/
  136. #endif