2 * SpanDSP - a series of DSP components for telephony
6 * Written by Steve Underwood <steveu@coppice.org>
8 * Copyright (C) 2004 Steve Underwood
10 * All rights reserved.
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.
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.
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.
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.
39 #include <asterisk/plc.h>
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 */
51 #define ms_to_samples(t) (((t)*SAMPLE_RATE)/1000)
53 static inline int16_t fsaturate(double damp)
59 return (int16_t) rint(damp);
62 static void save_history(plc_state_t *s, int16_t *buf, int len)
64 if (len >= PLC_HISTORY_LEN)
66 /* Just keep the last part of the new data, starting at the beginning of the buffer */
67 memcpy(s->history, buf + len - PLC_HISTORY_LEN, sizeof(int16_t)*PLC_HISTORY_LEN);
71 if (s->buf_ptr + len > PLC_HISTORY_LEN)
73 /* Wraps around - must break into two sections */
74 memcpy(s->history + s->buf_ptr, buf, sizeof(int16_t)*(PLC_HISTORY_LEN - s->buf_ptr));
75 len -= (PLC_HISTORY_LEN - s->buf_ptr);
76 memcpy(s->history, buf + (PLC_HISTORY_LEN - s->buf_ptr), sizeof(int16_t)*len);
80 /* Can use just one section */
81 memcpy(s->history + s->buf_ptr, buf, sizeof(int16_t)*len);
84 /*- End of function --------------------------------------------------------*/
86 static void normalise_history(plc_state_t *s)
88 int16_t tmp[PLC_HISTORY_LEN];
92 memcpy(tmp, s->history, sizeof(int16_t)*s->buf_ptr);
93 memcpy(s->history, s->history + s->buf_ptr, sizeof(int16_t)*(PLC_HISTORY_LEN - s->buf_ptr));
94 memcpy(s->history + PLC_HISTORY_LEN - s->buf_ptr, tmp, sizeof(int16_t)*s->buf_ptr);
97 /*- End of function --------------------------------------------------------*/
99 static int __inline__ amdf_pitch(int min_pitch, int max_pitch, int16_t amp[], int len)
109 for (i = max_pitch; i <= min_pitch; i++)
112 for (j = 0; j < len; j++)
113 acc += abs(amp[i + j] - amp[j]);
122 /*- End of function --------------------------------------------------------*/
124 int plc_rx(plc_state_t *s, int16_t amp[], int len)
134 if (s->missing_samples)
136 /* Although we have a real signal, we need to smooth it to fit well
137 with the synthetic signal we used for the previous block */
139 /* The start of the real data is overlapped with the next 1/4 cycle
140 of the synthetic data. */
141 pitch_overlap = s->pitch >> 2;
142 if (pitch_overlap > len)
144 gain = 1.0 - s->missing_samples*ATTENUATION_INCREMENT;
147 new_step = 1.0/pitch_overlap;
148 old_step = new_step*gain;
149 new_weight = new_step;
150 old_weight = (1.0 - new_step)*gain;
151 for (i = 0; i < pitch_overlap; i++)
153 amp[i] = fsaturate(old_weight*s->pitchbuf[s->pitch_offset] + new_weight*amp[i]);
154 if (++s->pitch_offset >= s->pitch)
156 new_weight += new_step;
157 old_weight -= old_step;
158 if (old_weight < 0.0)
161 s->missing_samples = 0;
163 save_history(s, amp, len);
166 /*- End of function --------------------------------------------------------*/
168 int plc_fillin(plc_state_t *s, int16_t amp[], int len)
182 if (s->missing_samples == 0)
184 /* As the gap in real speech starts we need to assess the last known pitch,
185 and prepare the synthetic data we will use for fill-in */
186 normalise_history(s);
187 s->pitch = amdf_pitch(PLC_PITCH_MIN, PLC_PITCH_MAX, s->history + PLC_HISTORY_LEN - CORRELATION_SPAN - PLC_PITCH_MIN, CORRELATION_SPAN);
188 /* We overlap a 1/4 wavelength */
189 pitch_overlap = s->pitch >> 2;
190 /* Cook up a single cycle of pitch, using a single of the real signal with 1/4
191 cycle OLA'ed to make the ends join up nicely */
192 /* The first 3/4 of the cycle is a simple copy */
193 for (i = 0; i < s->pitch - pitch_overlap; i++)
194 s->pitchbuf[i] = s->history[PLC_HISTORY_LEN - s->pitch + i];
195 /* The last 1/4 of the cycle is overlapped with the end of the previous cycle */
196 new_step = 1.0/pitch_overlap;
197 new_weight = new_step;
198 for ( ; i < s->pitch; i++)
200 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;
201 new_weight += new_step;
203 /* We should now be ready to fill in the gap with repeated, decaying cycles
204 of what is in pitchbuf */
206 /* We need to OLA the first 1/4 wavelength of the synthetic data, to smooth
207 it into the previous real data. To avoid the need to introduce a delay
208 in the stream, reverse the last 1/4 wavelength, and OLA with that. */
210 new_step = 1.0/pitch_overlap;
212 new_weight = new_step;
213 old_weight = 1.0 - new_step;
214 for (i = 0; i < pitch_overlap; i++)
216 amp[i] = fsaturate(old_weight*s->history[PLC_HISTORY_LEN - 1 - i] + new_weight*s->pitchbuf[i]);
217 new_weight += new_step;
218 old_weight -= old_step;
219 if (old_weight < 0.0)
226 gain = 1.0 - s->missing_samples*ATTENUATION_INCREMENT;
229 for ( ; gain > 0.0 && i < len; i++)
231 amp[i] = s->pitchbuf[s->pitch_offset]*gain;
232 gain -= ATTENUATION_INCREMENT;
233 if (++s->pitch_offset >= s->pitch)
236 for ( ; i < len; i++)
238 s->missing_samples += orig_len;
239 save_history(s, amp, len);
242 /*- End of function --------------------------------------------------------*/
244 plc_state_t *plc_init(plc_state_t *s)
246 memset(s, 0, sizeof(*s));
249 /*- End of function --------------------------------------------------------*/
250 /*- End of file ------------------------------------------------------------*/