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.
38 #include <asterisk/plc.h>
47 /* We do a straight line fade to zero volume in 50ms when we are filling in for missing data. */
48 #define ATTENUATION_INCREMENT 0.0025 /* Attenuation per sample */
50 #define ms_to_samples(t) (((t)*SAMPLE_RATE)/1000)
52 static inline int16_t fsaturate(double damp)
58 return (int16_t) rint(damp);
61 static void save_history(plc_state_t *s, int16_t *buf, int len)
63 if (len >= PLC_HISTORY_LEN)
65 /* Just keep the last part of the new data, starting at the beginning of the buffer */
66 memcpy(s->history, buf + len - PLC_HISTORY_LEN, sizeof(int16_t)*PLC_HISTORY_LEN);
70 if (s->buf_ptr + len > PLC_HISTORY_LEN)
72 /* Wraps around - must break into two sections */
73 memcpy(s->history + s->buf_ptr, buf, sizeof(int16_t)*(PLC_HISTORY_LEN - s->buf_ptr));
74 len -= (PLC_HISTORY_LEN - s->buf_ptr);
75 memcpy(s->history, buf + (PLC_HISTORY_LEN - s->buf_ptr), sizeof(int16_t)*len);
79 /* Can use just one section */
80 memcpy(s->history + s->buf_ptr, buf, sizeof(int16_t)*len);
83 /*- End of function --------------------------------------------------------*/
85 static void normalise_history(plc_state_t *s)
87 int16_t tmp[PLC_HISTORY_LEN];
91 memcpy(tmp, s->history, sizeof(int16_t)*s->buf_ptr);
92 memcpy(s->history, s->history + s->buf_ptr, sizeof(int16_t)*(PLC_HISTORY_LEN - s->buf_ptr));
93 memcpy(s->history + PLC_HISTORY_LEN - s->buf_ptr, tmp, sizeof(int16_t)*s->buf_ptr);
96 /*- End of function --------------------------------------------------------*/
98 static int __inline__ amdf_pitch(int min_pitch, int max_pitch, int16_t amp[], int len)
108 for (i = max_pitch; i <= min_pitch; i++)
111 for (j = 0; j < len; j++)
112 acc += abs(amp[i + j] - amp[j]);
121 /*- End of function --------------------------------------------------------*/
123 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)
170 int16_t tmp[PLC_PITCH_OVERLAP_MAX];
183 if (s->missing_samples == 0)
185 /* As the gap in real speech starts we need to assess the last known pitch,
186 and prepare the synthetic data we will use for fill-in */
187 normalise_history(s);
188 s->pitch = amdf_pitch(PLC_PITCH_MIN, PLC_PITCH_MAX, s->history + PLC_HISTORY_LEN - CORRELATION_SPAN - PLC_PITCH_MIN, CORRELATION_SPAN);
189 /* We overlap a 1/4 wavelength */
190 pitch_overlap = s->pitch >> 2;
191 /* Cook up a single cycle of pitch, using a single of the real signal with 1/4
192 cycle OLA'ed to make the ends join up nicely */
193 /* The first 3/4 of the cycle is a simple copy */
194 for (i = 0; i < s->pitch - pitch_overlap; i++)
195 s->pitchbuf[i] = s->history[PLC_HISTORY_LEN - s->pitch + i];
196 /* The last 1/4 of the cycle is overlapped with the end of the previous cycle */
197 new_step = 1.0/pitch_overlap;
198 new_weight = new_step;
199 for ( ; i < s->pitch; i++)
201 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;
202 new_weight += new_step;
204 /* We should now be ready to fill in the gap with repeated, decaying cycles
205 of what is in pitchbuf */
207 /* We need to OLA the first 1/4 wavelength of the synthetic data, to smooth
208 it into the previous real data. To avoid the need to introduce a delay
209 in the stream, reverse the last 1/4 wavelength, and OLA with that. */
211 new_step = 1.0/pitch_overlap;
213 new_weight = new_step;
214 old_weight = 1.0 - new_step;
215 for (i = 0; i < pitch_overlap; i++)
217 amp[i] = fsaturate(old_weight*s->history[PLC_HISTORY_LEN - 1 - i] + new_weight*s->pitchbuf[i]);
218 new_weight += new_step;
219 old_weight -= old_step;
220 if (old_weight < 0.0)
227 gain = 1.0 - s->missing_samples*ATTENUATION_INCREMENT;
230 for ( ; gain > 0.0 && i < len; i++)
232 amp[i] = s->pitchbuf[s->pitch_offset]*gain;
233 gain -= ATTENUATION_INCREMENT;
234 if (++s->pitch_offset >= s->pitch)
237 for ( ; i < len; i++)
239 s->missing_samples += orig_len;
240 save_history(s, amp, len);
243 /*- End of function --------------------------------------------------------*/
245 plc_state_t *plc_init(plc_state_t *s)
247 memset(s, 0, sizeof(*s));
250 /*- End of function --------------------------------------------------------*/
251 /*- End of file ------------------------------------------------------------*/