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