plc.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. #include "asterisk.h"
  32. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  33. #include <math.h>
  34. #include "asterisk/plc.h"
  35. #if !defined(FALSE)
  36. #define FALSE 0
  37. #endif
  38. #if !defined(TRUE)
  39. #define TRUE (!FALSE)
  40. #endif
  41. #if !defined(INT16_MAX)
  42. #define INT16_MAX (32767)
  43. #define INT16_MIN (-32767-1)
  44. #endif
  45. /* We do a straight line fade to zero volume in 50ms when we are filling in for missing data. */
  46. #define ATTENUATION_INCREMENT 0.0025 /* Attenuation per sample */
  47. #define ms_to_samples(t) (((t)*DEFAULT_SAMPLE_RATE)/1000)
  48. static inline int16_t fsaturate(double damp)
  49. {
  50. if (damp > 32767.0)
  51. return INT16_MAX;
  52. if (damp < -32768.0)
  53. return INT16_MIN;
  54. return (int16_t) rint(damp);
  55. }
  56. static void save_history(plc_state_t *s, int16_t *buf, int len)
  57. {
  58. if (len >= PLC_HISTORY_LEN) {
  59. /* Just keep the last part of the new data, starting at the beginning of the buffer */
  60. memcpy(s->history, buf + len - PLC_HISTORY_LEN, sizeof(int16_t) * PLC_HISTORY_LEN);
  61. s->buf_ptr = 0;
  62. return;
  63. }
  64. if (s->buf_ptr + len > PLC_HISTORY_LEN) {
  65. /* Wraps around - must break into two sections */
  66. memcpy(s->history + s->buf_ptr, buf, sizeof(int16_t) * (PLC_HISTORY_LEN - s->buf_ptr));
  67. len -= (PLC_HISTORY_LEN - s->buf_ptr);
  68. memcpy(s->history, buf + (PLC_HISTORY_LEN - s->buf_ptr), sizeof(int16_t)*len);
  69. s->buf_ptr = len;
  70. return;
  71. }
  72. /* Can use just one section */
  73. memcpy(s->history + s->buf_ptr, buf, sizeof(int16_t)*len);
  74. s->buf_ptr += len;
  75. }
  76. /*- End of function --------------------------------------------------------*/
  77. static void normalise_history(plc_state_t *s)
  78. {
  79. int16_t tmp[PLC_HISTORY_LEN];
  80. if (s->buf_ptr == 0)
  81. return;
  82. memcpy(tmp, s->history, sizeof(int16_t)*s->buf_ptr);
  83. memcpy(s->history, s->history + s->buf_ptr, sizeof(int16_t) * (PLC_HISTORY_LEN - s->buf_ptr));
  84. memcpy(s->history + PLC_HISTORY_LEN - s->buf_ptr, tmp, sizeof(int16_t) * s->buf_ptr);
  85. s->buf_ptr = 0;
  86. }
  87. /*- End of function --------------------------------------------------------*/
  88. static int __inline__ amdf_pitch(int min_pitch, int max_pitch, int16_t amp[], int len)
  89. {
  90. int i;
  91. int j;
  92. int acc;
  93. int min_acc;
  94. int pitch;
  95. pitch = min_pitch;
  96. min_acc = INT_MAX;
  97. for (i = max_pitch; i <= min_pitch; i++) {
  98. acc = 0;
  99. for (j = 0; j < len; j++)
  100. acc += abs(amp[i + j] - amp[j]);
  101. if (acc < min_acc) {
  102. min_acc = acc;
  103. pitch = i;
  104. }
  105. }
  106. return pitch;
  107. }
  108. /*- End of function --------------------------------------------------------*/
  109. int plc_rx(plc_state_t *s, int16_t amp[], int len)
  110. {
  111. int i;
  112. int pitch_overlap;
  113. float old_step;
  114. float new_step;
  115. float old_weight;
  116. float new_weight;
  117. float gain;
  118. if (s->missing_samples) {
  119. /* Although we have a real signal, we need to smooth it to fit well
  120. with the synthetic signal we used for the previous block */
  121. /* The start of the real data is overlapped with the next 1/4 cycle
  122. of the synthetic data. */
  123. pitch_overlap = s->pitch >> 2;
  124. if (pitch_overlap > len)
  125. pitch_overlap = len;
  126. gain = 1.0 - s->missing_samples*ATTENUATION_INCREMENT;
  127. if (gain < 0.0)
  128. gain = 0.0;
  129. new_step = 1.0/pitch_overlap;
  130. old_step = new_step*gain;
  131. new_weight = new_step;
  132. old_weight = (1.0 - new_step)*gain;
  133. for (i = 0; i < pitch_overlap; i++) {
  134. amp[i] = fsaturate(old_weight * s->pitchbuf[s->pitch_offset] + new_weight * amp[i]);
  135. if (++s->pitch_offset >= s->pitch)
  136. s->pitch_offset = 0;
  137. new_weight += new_step;
  138. old_weight -= old_step;
  139. if (old_weight < 0.0)
  140. old_weight = 0.0;
  141. }
  142. s->missing_samples = 0;
  143. }
  144. save_history(s, amp, len);
  145. return len;
  146. }
  147. /*- End of function --------------------------------------------------------*/
  148. int plc_fillin(plc_state_t *s, int16_t amp[], int len)
  149. {
  150. int i;
  151. int pitch_overlap;
  152. float old_step;
  153. float new_step;
  154. float old_weight;
  155. float new_weight;
  156. float gain;
  157. int16_t *orig_amp;
  158. int orig_len;
  159. orig_amp = amp;
  160. orig_len = len;
  161. if (s->missing_samples == 0) {
  162. /* As the gap in real speech starts we need to assess the last known pitch,
  163. and prepare the synthetic data we will use for fill-in */
  164. normalise_history(s);
  165. s->pitch = amdf_pitch(PLC_PITCH_MIN, PLC_PITCH_MAX, s->history + PLC_HISTORY_LEN - CORRELATION_SPAN - PLC_PITCH_MIN, CORRELATION_SPAN);
  166. /* We overlap a 1/4 wavelength */
  167. pitch_overlap = s->pitch >> 2;
  168. /* Cook up a single cycle of pitch, using a single of the real signal with 1/4
  169. cycle OLA'ed to make the ends join up nicely */
  170. /* The first 3/4 of the cycle is a simple copy */
  171. for (i = 0; i < s->pitch - pitch_overlap; i++)
  172. s->pitchbuf[i] = s->history[PLC_HISTORY_LEN - s->pitch + i];
  173. /* The last 1/4 of the cycle is overlapped with the end of the previous cycle */
  174. new_step = 1.0/pitch_overlap;
  175. new_weight = new_step;
  176. for ( ; i < s->pitch; i++) {
  177. 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;
  178. new_weight += new_step;
  179. }
  180. /* We should now be ready to fill in the gap with repeated, decaying cycles
  181. of what is in pitchbuf */
  182. /* We need to OLA the first 1/4 wavelength of the synthetic data, to smooth
  183. it into the previous real data. To avoid the need to introduce a delay
  184. in the stream, reverse the last 1/4 wavelength, and OLA with that. */
  185. gain = 1.0;
  186. new_step = 1.0 / pitch_overlap;
  187. old_step = new_step;
  188. new_weight = new_step;
  189. old_weight = 1.0 - new_step;
  190. for (i = 0; i < pitch_overlap; i++) {
  191. amp[i] = fsaturate(old_weight * s->history[PLC_HISTORY_LEN - 1 - i] + new_weight * s->pitchbuf[i]);
  192. new_weight += new_step;
  193. old_weight -= old_step;
  194. if (old_weight < 0.0)
  195. old_weight = 0.0;
  196. }
  197. s->pitch_offset = i;
  198. } else {
  199. gain = 1.0 - s->missing_samples*ATTENUATION_INCREMENT;
  200. i = 0;
  201. }
  202. for ( ; gain > 0.0 && i < len; i++) {
  203. amp[i] = s->pitchbuf[s->pitch_offset] * gain;
  204. gain -= ATTENUATION_INCREMENT;
  205. if (++s->pitch_offset >= s->pitch)
  206. s->pitch_offset = 0;
  207. }
  208. for ( ; i < len; i++)
  209. amp[i] = 0;
  210. s->missing_samples += orig_len;
  211. save_history(s, amp, len);
  212. return len;
  213. }
  214. /*- End of function --------------------------------------------------------*/
  215. plc_state_t *plc_init(plc_state_t *s)
  216. {
  217. memset(s, 0, sizeof(*s));
  218. return s;
  219. }
  220. /*- End of function --------------------------------------------------------*/
  221. /*- End of file ------------------------------------------------------------*/