plc.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Written by Steve Underwood <steveu@coppice.org>
  5. *
  6. * Copyright (C) 2004 Steve Underwood
  7. *
  8. * All rights reserved.
  9. *
  10. * See http://www.asterisk.org for more information about
  11. * the Asterisk project. Please do not directly contact
  12. * any of the maintainers of this project for assistance;
  13. * the project provides a web site, mailing lists and IRC
  14. * channels for your use.
  15. *
  16. * This program is free software, distributed under the terms of
  17. * the GNU General Public License Version 2. See the LICENSE file
  18. * at the top of the source tree.
  19. *
  20. * This version may be optionally licenced under the GNU LGPL licence.
  21. *
  22. * A license has been granted to Digium (via disclaimer) for the use of
  23. * this code.
  24. */
  25. /*! \file
  26. *
  27. * \brief SpanDSP - a series of DSP components for telephony
  28. *
  29. * \author Steve Underwood <steveu@coppice.org>
  30. */
  31. /*** MODULEINFO
  32. <support_level>core</support_level>
  33. ***/
  34. #include "asterisk.h"
  35. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  36. #include <math.h>
  37. #include "asterisk/plc.h"
  38. #if !defined(FALSE)
  39. #define FALSE 0
  40. #endif
  41. #if !defined(TRUE)
  42. #define TRUE (!FALSE)
  43. #endif
  44. #if !defined(INT16_MAX)
  45. #define INT16_MAX (32767)
  46. #define INT16_MIN (-32767-1)
  47. #endif
  48. /* We do a straight line fade to zero volume in 50ms when we are filling in for missing data. */
  49. #define ATTENUATION_INCREMENT 0.0025 /* Attenuation per sample */
  50. #define ms_to_samples(t) (((t)*DEFAULT_SAMPLE_RATE)/1000)
  51. static inline int16_t fsaturate(double damp)
  52. {
  53. if (damp > 32767.0)
  54. return INT16_MAX;
  55. if (damp < -32768.0)
  56. return INT16_MIN;
  57. return (int16_t) rint(damp);
  58. }
  59. static void save_history(plc_state_t *s, int16_t *buf, int len)
  60. {
  61. if (len >= PLC_HISTORY_LEN) {
  62. /* Just keep the last part of the new data, starting at the beginning of the buffer */
  63. memcpy(s->history, buf + len - PLC_HISTORY_LEN, sizeof(int16_t) * PLC_HISTORY_LEN);
  64. s->buf_ptr = 0;
  65. return;
  66. }
  67. if (s->buf_ptr + len > PLC_HISTORY_LEN) {
  68. /* Wraps around - must break into two sections */
  69. memcpy(s->history + s->buf_ptr, buf, sizeof(int16_t) * (PLC_HISTORY_LEN - s->buf_ptr));
  70. len -= (PLC_HISTORY_LEN - s->buf_ptr);
  71. memcpy(s->history, buf + (PLC_HISTORY_LEN - s->buf_ptr), sizeof(int16_t)*len);
  72. s->buf_ptr = len;
  73. return;
  74. }
  75. /* Can use just one section */
  76. memcpy(s->history + s->buf_ptr, buf, sizeof(int16_t)*len);
  77. s->buf_ptr += len;
  78. }
  79. /*- End of function --------------------------------------------------------*/
  80. static void normalise_history(plc_state_t *s)
  81. {
  82. int16_t tmp[PLC_HISTORY_LEN];
  83. if (s->buf_ptr == 0)
  84. return;
  85. memcpy(tmp, s->history, sizeof(int16_t)*s->buf_ptr);
  86. memmove(s->history, s->history + s->buf_ptr, sizeof(int16_t) * (PLC_HISTORY_LEN - s->buf_ptr));
  87. memcpy(s->history + PLC_HISTORY_LEN - s->buf_ptr, tmp, sizeof(int16_t) * s->buf_ptr);
  88. s->buf_ptr = 0;
  89. }
  90. /*- End of function --------------------------------------------------------*/
  91. static int __inline__ amdf_pitch(int min_pitch, int max_pitch, int16_t amp[], int len)
  92. {
  93. int i;
  94. int j;
  95. int acc;
  96. int min_acc;
  97. int pitch;
  98. pitch = min_pitch;
  99. min_acc = INT_MAX;
  100. for (i = max_pitch; i <= min_pitch; i++) {
  101. acc = 0;
  102. for (j = 0; j < len; j++)
  103. acc += abs(amp[i + j] - amp[j]);
  104. if (acc < min_acc) {
  105. min_acc = acc;
  106. pitch = i;
  107. }
  108. }
  109. return pitch;
  110. }
  111. /*- End of function --------------------------------------------------------*/
  112. int plc_rx(plc_state_t *s, int16_t amp[], int len)
  113. {
  114. int i;
  115. int pitch_overlap;
  116. float old_step;
  117. float new_step;
  118. float old_weight;
  119. float new_weight;
  120. float gain;
  121. if (s->missing_samples) {
  122. /* Although we have a real signal, we need to smooth it to fit well
  123. with the synthetic signal we used for the previous block */
  124. /* The start of the real data is overlapped with the next 1/4 cycle
  125. of the synthetic data. */
  126. pitch_overlap = s->pitch >> 2;
  127. if (pitch_overlap > len)
  128. pitch_overlap = len;
  129. gain = 1.0 - s->missing_samples*ATTENUATION_INCREMENT;
  130. if (gain < 0.0)
  131. gain = 0.0;
  132. new_step = 1.0/pitch_overlap;
  133. old_step = new_step*gain;
  134. new_weight = new_step;
  135. old_weight = (1.0 - new_step)*gain;
  136. for (i = 0; i < pitch_overlap; i++) {
  137. amp[i] = fsaturate(old_weight * s->pitchbuf[s->pitch_offset] + new_weight * amp[i]);
  138. if (++s->pitch_offset >= s->pitch)
  139. s->pitch_offset = 0;
  140. new_weight += new_step;
  141. old_weight -= old_step;
  142. if (old_weight < 0.0)
  143. old_weight = 0.0;
  144. }
  145. s->missing_samples = 0;
  146. }
  147. save_history(s, amp, len);
  148. return len;
  149. }
  150. /*- End of function --------------------------------------------------------*/
  151. int plc_fillin(plc_state_t *s, int16_t amp[], int len)
  152. {
  153. int i;
  154. int pitch_overlap;
  155. float old_step;
  156. float new_step;
  157. float old_weight;
  158. float new_weight;
  159. float gain;
  160. int orig_len;
  161. orig_len = len;
  162. if (s->missing_samples == 0) {
  163. /* As the gap in real speech starts we need to assess the last known pitch,
  164. and prepare the synthetic data we will use for fill-in */
  165. normalise_history(s);
  166. s->pitch = amdf_pitch(PLC_PITCH_MIN, PLC_PITCH_MAX, s->history + PLC_HISTORY_LEN - CORRELATION_SPAN - PLC_PITCH_MIN, CORRELATION_SPAN);
  167. /* We overlap a 1/4 wavelength */
  168. pitch_overlap = s->pitch >> 2;
  169. /* Cook up a single cycle of pitch, using a single of the real signal with 1/4
  170. cycle OLA'ed to make the ends join up nicely */
  171. /* The first 3/4 of the cycle is a simple copy */
  172. for (i = 0; i < s->pitch - pitch_overlap; i++)
  173. s->pitchbuf[i] = s->history[PLC_HISTORY_LEN - s->pitch + i];
  174. /* The last 1/4 of the cycle is overlapped with the end of the previous cycle */
  175. new_step = 1.0/pitch_overlap;
  176. new_weight = new_step;
  177. for ( ; i < s->pitch; i++) {
  178. s->pitchbuf[i] = s->history[PLC_HISTORY_LEN - s->pitch + i] * (1.0 - new_weight) + s->history[PLC_HISTORY_LEN - 2 * s->pitch + i]*new_weight;
  179. new_weight += new_step;
  180. }
  181. /* We should now be ready to fill in the gap with repeated, decaying cycles
  182. of what is in pitchbuf */
  183. /* We need to OLA the first 1/4 wavelength of the synthetic data, to smooth
  184. it into the previous real data. To avoid the need to introduce a delay
  185. in the stream, reverse the last 1/4 wavelength, and OLA with that. */
  186. gain = 1.0;
  187. new_step = 1.0 / pitch_overlap;
  188. old_step = new_step;
  189. new_weight = new_step;
  190. old_weight = 1.0 - new_step;
  191. for (i = 0; i < pitch_overlap; i++) {
  192. amp[i] = fsaturate(old_weight * s->history[PLC_HISTORY_LEN - 1 - i] + new_weight * s->pitchbuf[i]);
  193. new_weight += new_step;
  194. old_weight -= old_step;
  195. if (old_weight < 0.0)
  196. old_weight = 0.0;
  197. }
  198. s->pitch_offset = i;
  199. } else {
  200. gain = 1.0 - s->missing_samples*ATTENUATION_INCREMENT;
  201. i = 0;
  202. }
  203. for ( ; gain > 0.0 && i < len; i++) {
  204. amp[i] = s->pitchbuf[s->pitch_offset] * gain;
  205. gain -= ATTENUATION_INCREMENT;
  206. if (++s->pitch_offset >= s->pitch)
  207. s->pitch_offset = 0;
  208. }
  209. for ( ; i < len; i++)
  210. amp[i] = 0;
  211. s->missing_samples += orig_len;
  212. save_history(s, amp, len);
  213. return len;
  214. }
  215. /*- End of function --------------------------------------------------------*/
  216. plc_state_t *plc_init(plc_state_t *s)
  217. {
  218. memset(s, 0, sizeof(*s));
  219. return s;
  220. }
  221. /*- End of function --------------------------------------------------------*/
  222. /*- End of file ------------------------------------------------------------*/