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.
50 #if !defined(INT16_MAX)
51 #define INT16_MAX (32767)
52 #define INT16_MIN (-32767-1)
55 /* msvc doesn't know rint() */
56 #if defined(WIN32) && defined(_MSC_VER)
57 #define rint(x) floor((x) + 0.5)
59 #define inline __inline
61 typedef short int16_t;
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 */
68 #define ms_to_samples(t) (((t)*SAMPLE_RATE)/1000)
70 static inline int16_t fsaturate(double damp)
76 return (int16_t) rint(damp);
79 static void save_history(plc_state_t *s, int16_t *buf, int len)
81 if (len >= PLC_HISTORY_LEN)
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);
88 if (s->buf_ptr + len > PLC_HISTORY_LEN)
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);
97 /* Can use just one section */
98 memcpy(s->history + s->buf_ptr, buf, sizeof(int16_t)*len);
101 /*- End of function --------------------------------------------------------*/
103 static void normalise_history(plc_state_t *s)
105 int16_t tmp[PLC_HISTORY_LEN];
109 memcpy(tmp, s->history, sizeof(int16_t)*s->buf_ptr);
110 memcpy(s->history, s->history + s->buf_ptr, sizeof(int16_t)*(PLC_HISTORY_LEN - s->buf_ptr));
111 memcpy(s->history + PLC_HISTORY_LEN - s->buf_ptr, tmp, sizeof(int16_t)*s->buf_ptr);
114 /*- End of function --------------------------------------------------------*/
116 static int inline amdf_pitch(int min_pitch, int max_pitch, int16_t amp[], int len)
126 for (i = max_pitch; i <= min_pitch; i++)
129 for (j = 0; j < len; j++)
130 acc += abs(amp[i + j] - amp[j]);
139 /*- End of function --------------------------------------------------------*/
141 int plc_rx(plc_state_t *s, int16_t amp[], int len)
151 if (s->missing_samples)
153 /* Although we have a real signal, we need to smooth it to fit well
154 with the synthetic signal we used for the previous block */
156 /* The start of the real data is overlapped with the next 1/4 cycle
157 of the synthetic data. */
158 pitch_overlap = s->pitch >> 2;
159 if (pitch_overlap > len)
161 gain = 1.0 - s->missing_samples*ATTENUATION_INCREMENT;
164 new_step = 1.0/pitch_overlap;
165 old_step = new_step*gain;
166 new_weight = new_step;
167 old_weight = (1.0 - new_step)*gain;
168 for (i = 0; i < pitch_overlap; i++)
170 amp[i] = fsaturate(old_weight*s->pitchbuf[s->pitch_offset] + new_weight*amp[i]);
171 if (++s->pitch_offset >= s->pitch)
173 new_weight += new_step;
174 old_weight -= old_step;
175 if (old_weight < 0.0)
178 s->missing_samples = 0;
180 save_history(s, amp, len);
183 /*- End of function --------------------------------------------------------*/
185 int plc_fillin(plc_state_t *s, int16_t amp[], int len)
199 if (s->missing_samples == 0)
201 /* As the gap in real speech starts we need to assess the last known pitch,
202 and prepare the synthetic data we will use for fill-in */
203 normalise_history(s);
204 s->pitch = amdf_pitch(PLC_PITCH_MIN, PLC_PITCH_MAX, s->history + PLC_HISTORY_LEN - CORRELATION_SPAN - PLC_PITCH_MIN, CORRELATION_SPAN);
205 /* We overlap a 1/4 wavelength */
206 pitch_overlap = s->pitch >> 2;
207 /* Cook up a single cycle of pitch, using a single of the real signal with 1/4
208 cycle OLA'ed to make the ends join up nicely */
209 /* The first 3/4 of the cycle is a simple copy */
210 for (i = 0; i < s->pitch - pitch_overlap; i++)
211 s->pitchbuf[i] = s->history[PLC_HISTORY_LEN - s->pitch + i];
212 /* The last 1/4 of the cycle is overlapped with the end of the previous cycle */
213 new_step = 1.0/pitch_overlap;
214 new_weight = new_step;
215 for ( ; i < s->pitch; i++)
217 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;
218 new_weight += new_step;
220 /* We should now be ready to fill in the gap with repeated, decaying cycles
221 of what is in pitchbuf */
223 /* We need to OLA the first 1/4 wavelength of the synthetic data, to smooth
224 it into the previous real data. To avoid the need to introduce a delay
225 in the stream, reverse the last 1/4 wavelength, and OLA with that. */
227 new_step = 1.0/pitch_overlap;
229 new_weight = new_step;
230 old_weight = 1.0 - new_step;
231 for (i = 0; i < pitch_overlap && i < len; i++)
233 amp[i] = fsaturate(old_weight*s->history[PLC_HISTORY_LEN - 1 - i] + new_weight*s->pitchbuf[i]);
234 new_weight += new_step;
235 old_weight -= old_step;
236 if (old_weight < 0.0)
243 gain = 1.0 - s->missing_samples*ATTENUATION_INCREMENT;
246 for ( ; gain > 0.0 && i < len; i++)
248 amp[i] = s->pitchbuf[s->pitch_offset]*gain;
249 gain -= ATTENUATION_INCREMENT;
250 if (++s->pitch_offset >= s->pitch)
253 for ( ; i < len; i++)
255 s->missing_samples += orig_len;
256 save_history(s, amp, len);
259 /*- End of function --------------------------------------------------------*/
261 plc_state_t *plc_init(plc_state_t *s)
263 memset(s, 0, sizeof(*s));
266 /*- End of function --------------------------------------------------------*/
267 /*- End of file ------------------------------------------------------------*/