wsola.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*
  2. * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
  3. * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #ifndef __PJMEDIA_WSOLA_H__
  20. #define __PJMEDIA_WSOLA_H__
  21. /**
  22. * @file wsola.h
  23. * @brief Waveform Similarity Based Overlap-Add (WSOLA)
  24. */
  25. #include <pjmedia/types.h>
  26. /**
  27. * @defgroup PJMED_WSOLA Waveform Similarity Based Overlap-Add (WSOLA)
  28. * @ingroup PJMEDIA_FRAME_OP
  29. * @brief Time-scale modification to audio without affecting the pitch
  30. * @{
  31. *
  32. * This section describes Waveform Similarity Based Overlap-Add (WSOLA)
  33. * implementation in PJMEDIA. The WSOLA API here can be used both to
  34. * compress (speed-up) and stretch (expand, slow down) audio playback
  35. * without altering the pitch, or as a mean for performing packet loss
  36. * concealment (WSOLA).
  37. *
  38. * The WSOLA implementation is used by \ref PJMED_DELAYBUF and \ref PJMED_PLC.
  39. */
  40. PJ_BEGIN_DECL
  41. /**
  42. * Opaque declaration for WSOLA structure.
  43. */
  44. typedef struct pjmedia_wsola pjmedia_wsola;
  45. /**
  46. * WSOLA options, can be combined with bitmask operation.
  47. */
  48. enum pjmedia_wsola_option
  49. {
  50. /**
  51. * Disable Hanning window to conserve memory.
  52. */
  53. PJMEDIA_WSOLA_NO_HANNING = 1,
  54. /**
  55. * Specify that the WSOLA will not be used for PLC.
  56. */
  57. PJMEDIA_WSOLA_NO_PLC = 2,
  58. /**
  59. * Specify that the WSOLA will not be used to discard frames in
  60. * non-contiguous buffer.
  61. */
  62. PJMEDIA_WSOLA_NO_DISCARD = 4,
  63. /**
  64. * Disable fade-in and fade-out feature in the transition between
  65. * actual and synthetic frames in WSOLA. With fade feature enabled,
  66. * WSOLA will only generate a limited number of synthetic frames
  67. * (configurable with #pjmedia_wsola_set_max_expand()), fading out
  68. * the volume on every more samples it generates, and when it reaches
  69. * the limit it will only generate silence.
  70. */
  71. PJMEDIA_WSOLA_NO_FADING = 8
  72. };
  73. /**
  74. * Create and initialize WSOLA.
  75. *
  76. * @param pool Pool to allocate memory for WSOLA.
  77. * @param clock_rate Sampling rate of audio playback.
  78. * @param samples_per_frame Number of samples per frame.
  79. * @param channel_count Number of channels.
  80. * @param options Option flags, bitmask combination of
  81. * #pjmedia_wsola_option.
  82. * @param p_wsola Pointer to receive WSOLA structure.
  83. *
  84. * @return PJ_SUCCESS or the appropriate error code.
  85. */
  86. PJ_DECL(pj_status_t) pjmedia_wsola_create(pj_pool_t *pool,
  87. unsigned clock_rate,
  88. unsigned samples_per_frame,
  89. unsigned channel_count,
  90. unsigned options,
  91. pjmedia_wsola **p_wsola);
  92. /**
  93. * Specify maximum number of continuous synthetic frames that can be
  94. * generated by WSOLA, in milliseconds. This option will only take
  95. * effect if fading is not disabled via the option when the WSOLA
  96. * session was created. Default value is PJMEDIA_WSOLA_MAX_EXPAND_MSEC
  97. * (see also the documentation of PJMEDIA_WSOLA_MAX_EXPAND_MSEC for
  98. * more information).
  99. *
  100. * @param wsola The WSOLA session
  101. * @param msec The duration.
  102. *
  103. * @return PJ_SUCCESS normally.
  104. */
  105. PJ_DECL(pj_status_t) pjmedia_wsola_set_max_expand(pjmedia_wsola *wsola,
  106. unsigned msec);
  107. /**
  108. * Destroy WSOLA.
  109. *
  110. * @param wsola WSOLA session.
  111. *
  112. * @return PJ_SUCCESS normally.
  113. */
  114. PJ_DECL(pj_status_t) pjmedia_wsola_destroy(pjmedia_wsola *wsola);
  115. /**
  116. * Reset the buffer contents of WSOLA.
  117. *
  118. * @param wsola WSOLA session.
  119. * @param options Reset options, must be zero for now.
  120. *
  121. * @return PJ_SUCCESS normally.
  122. */
  123. PJ_DECL(pj_status_t) pjmedia_wsola_reset(pjmedia_wsola *wsola,
  124. unsigned options);
  125. /**
  126. * Give one good frame to WSOLA to be kept as reference. Application
  127. * must continuously give WSOLA good frames to keep its session up to
  128. * date with current playback. Depending on the WSOLA implementation,
  129. * this function may modify the content of the frame.
  130. *
  131. * @param wsola WSOLA session.
  132. * @param frm The frame, which length must match the samples per
  133. * frame setting of the WSOLA session.
  134. * @param prev_lost If application previously generated a synthetic
  135. * frame with #pjmedia_wsola_generate() before calling
  136. * this function, specify whether that was because of
  137. * packet lost. If so, set this parameter to PJ_TRUE
  138. * to make WSOLA interpolate this frame with its buffer.
  139. * Otherwise if this value is PJ_FALSE, WSOLA will
  140. * just append this frame to the end of its buffer.
  141. *
  142. * @return PJ_SUCCESS normally.
  143. */
  144. PJ_DECL(pj_status_t) pjmedia_wsola_save(pjmedia_wsola *wsola,
  145. pj_int16_t frm[],
  146. pj_bool_t prev_lost);
  147. /**
  148. * Generate one synthetic frame from WSOLA.
  149. *
  150. * @param wsola WSOLA session.
  151. * @param frm Buffer to receive the frame.
  152. *
  153. * @return PJ_SUCCESS normally.
  154. */
  155. PJ_DECL(pj_status_t) pjmedia_wsola_generate(pjmedia_wsola *wsola,
  156. pj_int16_t frm[]);
  157. /**
  158. * Compress or compact the specified buffer by removing some audio samples
  159. * from the buffer, without altering the pitch. For this function to work,
  160. * total length of the buffer must be more than twice \a erase_cnt.
  161. *
  162. * @param wsola WSOLA session.
  163. * @param buf1 Pointer to buffer.
  164. * @param buf1_cnt Number of samples in the buffer.
  165. * @param buf2 Pointer to second buffer, if the buffer is not
  166. * contiguous. Otherwise this parameter must be NULL.
  167. * @param buf2_cnt Number of samples in the second buffer, if the buffer
  168. * is not contiguous. Otherwise this parameter should be
  169. * zero.
  170. * @param erase_cnt On input, specify the number of samples to be erased.
  171. * This function may erase more or less than the requested
  172. * number, and the actual number of samples erased will be
  173. * given on this argument upon returning from the function.
  174. *
  175. * @return PJ_SUCCESS if some samples have been erased, PJ_ETOOSMALL
  176. * if buffer is too small to be reduced, PJ_EINVAL if any
  177. * of the parameters are not valid.
  178. */
  179. PJ_DECL(pj_status_t) pjmedia_wsola_discard(pjmedia_wsola *wsola,
  180. pj_int16_t buf1[],
  181. unsigned buf1_cnt,
  182. pj_int16_t buf2[],
  183. unsigned buf2_cnt,
  184. unsigned *erase_cnt);
  185. PJ_END_DECL
  186. /**
  187. * @}
  188. */
  189. #endif /* __PJMEDIA_WSOLA_H__ */