2 * Asterisk -- An open source telephony toolkit.
4 * Written by Steve Underwood <steveu@coppice.org>
6 * Copyright (C) 2004 Steve Underwood
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.
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.
20 * This version may be optionally licenced under the GNU LGPL licence.
22 * A license has been granted to Digium (via disclaimer) for the use of
28 * \brief SpanDSP - a series of DSP components for telephony
30 * \author Steve Underwood <steveu@coppice.org>
35 ASTERISK_FILE_VERSION(__FILE__
, "$Revision$")
42 #include "asterisk/plc.h"
51 #if !defined(INT16_MAX)
52 #define INT16_MAX (32767)
53 #define INT16_MIN (-32767-1)
56 /* We do a straight line fade to zero volume in 50ms when we are filling in for missing data. */
57 #define ATTENUATION_INCREMENT 0.0025 /* Attenuation per sample */
59 #define ms_to_samples(t) (((t)*DEFAULT_SAMPLE_RATE)/1000)
61 static inline int16_t fsaturate(double damp
)
67 return (int16_t) rint(damp
);
70 static void save_history(plc_state_t
*s
, int16_t *buf
, int len
)
72 if (len
>= PLC_HISTORY_LEN
) {
73 /* Just keep the last part of the new data, starting at the beginning of the buffer */
74 memcpy(s
->history
, buf
+ len
- PLC_HISTORY_LEN
, sizeof(int16_t)*PLC_HISTORY_LEN
);
78 if (s
->buf_ptr
+ len
> PLC_HISTORY_LEN
) {
79 /* Wraps around - must break into two sections */
80 memcpy(s
->history
+ s
->buf_ptr
, buf
, sizeof(int16_t)*(PLC_HISTORY_LEN
- s
->buf_ptr
));
81 len
-= (PLC_HISTORY_LEN
- s
->buf_ptr
);
82 memcpy(s
->history
, buf
+ (PLC_HISTORY_LEN
- s
->buf_ptr
), sizeof(int16_t)*len
);
86 /* Can use just one section */
87 memcpy(s
->history
+ s
->buf_ptr
, buf
, sizeof(int16_t)*len
);
91 /*- End of function --------------------------------------------------------*/
93 static void normalise_history(plc_state_t
*s
)
95 int16_t tmp
[PLC_HISTORY_LEN
];
99 memcpy(tmp
, s
->history
, sizeof(int16_t)*s
->buf_ptr
);
100 memcpy(s
->history
, s
->history
+ s
->buf_ptr
, sizeof(int16_t)*(PLC_HISTORY_LEN
- s
->buf_ptr
));
101 memcpy(s
->history
+ PLC_HISTORY_LEN
- s
->buf_ptr
, tmp
, sizeof(int16_t)*s
->buf_ptr
);
105 /*- End of function --------------------------------------------------------*/
107 static int __inline__
amdf_pitch(int min_pitch
, int max_pitch
, int16_t amp
[], int len
)
117 for (i
= max_pitch
; i
<= min_pitch
; i
++) {
119 for (j
= 0; j
< len
; j
++)
120 acc
+= abs(amp
[i
+ j
] - amp
[j
]);
129 /*- End of function --------------------------------------------------------*/
131 int plc_rx(plc_state_t
*s
, int16_t amp
[], int len
)
141 if (s
->missing_samples
) {
142 /* Although we have a real signal, we need to smooth it to fit well
143 with the synthetic signal we used for the previous block */
145 /* The start of the real data is overlapped with the next 1/4 cycle
146 of the synthetic data. */
147 pitch_overlap
= s
->pitch
>> 2;
148 if (pitch_overlap
> len
)
150 gain
= 1.0 - s
->missing_samples
*ATTENUATION_INCREMENT
;
153 new_step
= 1.0/pitch_overlap
;
154 old_step
= new_step
*gain
;
155 new_weight
= new_step
;
156 old_weight
= (1.0 - new_step
)*gain
;
157 for (i
= 0; i
< pitch_overlap
; i
++) {
158 amp
[i
] = fsaturate(old_weight
*s
->pitchbuf
[s
->pitch_offset
] + new_weight
*amp
[i
]);
159 if (++s
->pitch_offset
>= s
->pitch
)
161 new_weight
+= new_step
;
162 old_weight
-= old_step
;
163 if (old_weight
< 0.0)
166 s
->missing_samples
= 0;
168 save_history(s
, amp
, len
);
172 /*- End of function --------------------------------------------------------*/
174 int plc_fillin(plc_state_t
*s
, int16_t amp
[], int len
)
188 if (s
->missing_samples
== 0) {
189 /* As the gap in real speech starts we need to assess the last known pitch,
190 and prepare the synthetic data we will use for fill-in */
191 normalise_history(s
);
192 s
->pitch
= amdf_pitch(PLC_PITCH_MIN
, PLC_PITCH_MAX
, s
->history
+ PLC_HISTORY_LEN
- CORRELATION_SPAN
- PLC_PITCH_MIN
, CORRELATION_SPAN
);
193 /* We overlap a 1/4 wavelength */
194 pitch_overlap
= s
->pitch
>> 2;
195 /* Cook up a single cycle of pitch, using a single of the real signal with 1/4
196 cycle OLA'ed to make the ends join up nicely */
197 /* The first 3/4 of the cycle is a simple copy */
198 for (i
= 0; i
< s
->pitch
- pitch_overlap
; i
++)
199 s
->pitchbuf
[i
] = s
->history
[PLC_HISTORY_LEN
- s
->pitch
+ i
];
200 /* The last 1/4 of the cycle is overlapped with the end of the previous cycle */
201 new_step
= 1.0/pitch_overlap
;
202 new_weight
= new_step
;
203 for ( ; i
< s
->pitch
; i
++) {
204 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
;
205 new_weight
+= new_step
;
207 /* We should now be ready to fill in the gap with repeated, decaying cycles
208 of what is in pitchbuf */
210 /* We need to OLA the first 1/4 wavelength of the synthetic data, to smooth
211 it into the previous real data. To avoid the need to introduce a delay
212 in the stream, reverse the last 1/4 wavelength, and OLA with that. */
214 new_step
= 1.0/pitch_overlap
;
216 new_weight
= new_step
;
217 old_weight
= 1.0 - new_step
;
218 for (i
= 0; i
< pitch_overlap
; i
++) {
219 amp
[i
] = fsaturate(old_weight
*s
->history
[PLC_HISTORY_LEN
- 1 - i
] + new_weight
*s
->pitchbuf
[i
]);
220 new_weight
+= new_step
;
221 old_weight
-= old_step
;
222 if (old_weight
< 0.0)
227 gain
= 1.0 - s
->missing_samples
*ATTENUATION_INCREMENT
;
230 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
);
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 ------------------------------------------------------------*/