Debian Codegraph @

3rdparty/iaxclient/lib/spandsp/plc.c

raw symbols SCIP
1 /*
2 * SpanDSP - a series of DSP components for telephony
3 *
4 * plc.c
5 *
6 * Written by Steve Underwood <[email protected]>
7 *
8 * Copyright (C) 2004 Steve Underwood
9 *
10 * All rights reserved.
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 *
26 * This version may be optionally licenced under the GNU LGPL licence.
27 * This version is disclaimed to DIGIUM for inclusion in the Asterisk project.
28 */
29
30 /*! \file */
31 #ifdef HAVE_CONIG_H
32 #include "config.h"
33 #endif
34
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <math.h>
39 #include <limits.h>
40
41 #include "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 /* msvc doesn't know rint() */
56 #if defined(WIN32) && defined(_MSC_VER)
57 #define rint(x) floor((x) + 0.5)
58 #undef inline
59 #define inline __inline
60 #ifndef int16_t
61 typedef short int16_t;
62 #endif
63 #endif
64
65 /* We do a straight line fade to zero volume in 50ms when we are filling in for missing data. */
66 #define ATTENUATION_INCREMENT 0.0025 /* Attenuation per sample */
67
68 #define ms_to_samples(t) (((t)*SAMPLE_RATE)/1000)
69
70 static inline int16_t fsaturate(double damp)
71 {
72 if (damp > 32767.0)
73 return INT16_MAX;
74 if (damp < -32768.0)
75 return INT16_MIN;
76 return (int16_t) rint(damp);
77 }
78
79 static void save_history(plc_state_t *s, int16_t *buf, int len)
80 {
81 if (len >= PLC_HISTORY_LEN)
82 {
83 /* Just keep the last part of the new data, starting at the beginning of the buffer */
84 memcpy(s->history, buf + len - PLC_HISTORY_LEN, sizeof(int16_t)*PLC_HISTORY_LEN);
85 s->buf_ptr = 0;
86 return;
87 }
88 if (s->buf_ptr + len > PLC_HISTORY_LEN)
89 {
90 /* Wraps around - must break into two sections */
91 memcpy(s->history + s->buf_ptr, buf, sizeof(int16_t)*(PLC_HISTORY_LEN - s->buf_ptr));
92 len -= (PLC_HISTORY_LEN - s->buf_ptr);
93 memcpy(s->history, buf + (PLC_HISTORY_LEN - s->buf_ptr), sizeof(int16_t)*len);
94 s->buf_ptr = len;
95 return;
96 }
97 /* Can use just one section */
98 memcpy(s->history + s->buf_ptr, buf, sizeof(int16_t)*len);
99 s->buf_ptr += len;
100 }
101 /*- End of function --------------------------------------------------------*/
102
103 static void normalise_history(plc_state_t *s)
104 {
105 int16_t tmp[PLC_HISTORY_LEN];
106
107 if (s->buf_ptr == 0)
108 return;
109 memcpy(tmp, s->history, sizeof(int16_t)*s->buf_ptr);
110
111 // FlightGear fix: use memove, becuase ASan reports overlaps here
112 memmove(s->history, s->history + s->buf_ptr, sizeof(int16_t)*(PLC_HISTORY_LEN - s->buf_ptr));
113 memcpy(s->history + PLC_HISTORY_LEN - s->buf_ptr, tmp, sizeof(int16_t)*s->buf_ptr);
114 s->buf_ptr = 0;
115 }
116 /*- End of function --------------------------------------------------------*/
117
118 static int inline amdf_pitch(int min_pitch, int max_pitch, int16_t amp[], int len)
119 {
120 int i;
121 int j;
122 int acc;
123 int min_acc;
124 int pitch;
125
126 pitch = min_pitch;
127 min_acc = INT_MAX;
128 for (i = max_pitch; i <= min_pitch; i++)
129 {
130 acc = 0;
131 for (j = 0; j < len; j++)
132 acc += abs(amp[i + j] - amp[j]);
133 if (acc < min_acc)
134 {
135 min_acc = acc;
136 pitch = i;
137 }
138 }
139 return pitch;
140 }
141 /*- End of function --------------------------------------------------------*/
142
143 int plc_rx(plc_state_t *s, int16_t amp[], int len)
144 {
145 int i;
146 int pitch_overlap;
147 float old_step;
148 float new_step;
149 float old_weight;
150 float new_weight;
151 float gain;
152
153 if (s->missing_samples)
154 {
155 /* Although we have a real signal, we need to smooth it to fit well
156 with the synthetic signal we used for the previous block */
157
158 /* The start of the real data is overlapped with the next 1/4 cycle
159 of the synthetic data. */
160 pitch_overlap = s->pitch >> 2;
161 if (pitch_overlap > len)
162 pitch_overlap = len;
163 gain = 1.0 - s->missing_samples*ATTENUATION_INCREMENT;
164 if (gain < 0.0)
165 gain = 0.0;
166 new_step = 1.0/pitch_overlap;
167 old_step = new_step*gain;
168 new_weight = new_step;
169 old_weight = (1.0 - new_step)*gain;
170 for (i = 0; i < pitch_overlap; i++)
171 {
172 amp[i] = fsaturate(old_weight*s->pitchbuf[s->pitch_offset] + new_weight*amp[i]);
173 if (++s->pitch_offset >= s->pitch)
174 s->pitch_offset = 0;
175 new_weight += new_step;
176 old_weight -= old_step;
177 if (old_weight < 0.0)
178 old_weight = 0.0;
179 }
180 s->missing_samples = 0;
181 }
182 save_history(s, amp, len);
183 return len;
184 }
185 /*- End of function --------------------------------------------------------*/
186
187 int plc_fillin(plc_state_t *s, int16_t amp[], int len)
188 {
189 int i;
190 int pitch_overlap;
191 float old_step;
192 float new_step;
193 float old_weight;
194 float new_weight;
195 float gain;
196 //int16_t *orig_amp;
197 int orig_len;
198
199 //orig_amp = amp;
200 orig_len = len;
201 if (s->missing_samples == 0)
202 {
203 /* As the gap in real speech starts we need to assess the last known pitch,
204 and prepare the synthetic data we will use for fill-in */
205 normalise_history(s);
206 s->pitch = amdf_pitch(PLC_PITCH_MIN, PLC_PITCH_MAX, s->history + PLC_HISTORY_LEN - CORRELATION_SPAN - PLC_PITCH_MIN, CORRELATION_SPAN);
207 /* We overlap a 1/4 wavelength */
208 pitch_overlap = s->pitch >> 2;
209 /* Cook up a single cycle of pitch, using a single of the real signal with 1/4
210 cycle OLA'ed to make the ends join up nicely */
211 /* The first 3/4 of the cycle is a simple copy */
212 for (i = 0; i < s->pitch - pitch_overlap; i++)
213 s->pitchbuf[i] = s->history[PLC_HISTORY_LEN - s->pitch + i];
214 /* The last 1/4 of the cycle is overlapped with the end of the previous cycle */
215 new_step = 1.0/pitch_overlap;
216 new_weight = new_step;
217 for ( ; i < s->pitch; i++)
218 {
219 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;
220 new_weight += new_step;
221 }
222 /* We should now be ready to fill in the gap with repeated, decaying cycles
223 of what is in pitchbuf */
224
225 /* We need to OLA the first 1/4 wavelength of the synthetic data, to smooth
226 it into the previous real data. To avoid the need to introduce a delay
227 in the stream, reverse the last 1/4 wavelength, and OLA with that. */
228 gain = 1.0;
229 new_step = 1.0/pitch_overlap;
230 old_step = new_step;
231 new_weight = new_step;
232 old_weight = 1.0 - new_step;
233 for (i = 0; i < pitch_overlap && i < len; i++)
234 {
235 amp[i] = fsaturate(old_weight*s->history[PLC_HISTORY_LEN - 1 - i] + new_weight*s->pitchbuf[i]);
236 new_weight += new_step;
237 old_weight -= old_step;
238 if (old_weight < 0.0)
239 old_weight = 0.0;
240 }
241 s->pitch_offset = i;
242 }
243 else
244 {
245 gain = 1.0 - s->missing_samples*ATTENUATION_INCREMENT;
246 i = 0;
247 }
248 for ( ; gain > 0.0 && i < len; i++)
249 {
250 amp[i] = s->pitchbuf[s->pitch_offset]*gain;
251 gain -= ATTENUATION_INCREMENT;
252 if (++s->pitch_offset >= s->pitch)
253 s->pitch_offset = 0;
254 }
255 for ( ; i < len; i++)
256 amp[i] = 0;
257 s->missing_samples += orig_len;
258 save_history(s, amp, len);
259 return len;
260 }
261 /*- End of function --------------------------------------------------------*/
262
263 plc_state_t *plc_init(plc_state_t *s)
264 {
265 memset(s, 0, sizeof(*s));
266 return s;
267 }
268 /*- End of function --------------------------------------------------------*/
269 /*- End of file ------------------------------------------------------------*/