]> git.mxchange.org Git - flightgear.git/blob - utils/iaxclient/lib/spandsp/plc.c
Fix Windows warning during Windows compilation
[flightgear.git] / utils / iaxclient / lib / spandsp / plc.c
1 /*
2  * SpanDSP - a series of DSP components for telephony
3  *
4  * plc.c
5  *
6  * Written by Steve Underwood <steveu@coppice.org>
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     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);
112     s->buf_ptr = 0;
113 }
114 /*- End of function --------------------------------------------------------*/
115
116 static int inline amdf_pitch(int min_pitch, int max_pitch, int16_t amp[], int len)
117 {
118     int i;
119     int j;
120     int acc;
121     int min_acc;
122     int pitch;
123
124     pitch = min_pitch;
125     min_acc = INT_MAX;
126     for (i = max_pitch;  i <= min_pitch;  i++)
127     {
128         acc = 0;
129         for (j = 0;  j < len;  j++)
130             acc += abs(amp[i + j] - amp[j]);
131         if (acc < min_acc)
132         {
133             min_acc = acc;
134             pitch = i;
135         }
136     }
137     return pitch;
138 }
139 /*- End of function --------------------------------------------------------*/
140
141 int plc_rx(plc_state_t *s, int16_t amp[], int len)
142 {
143     int i;
144     int pitch_overlap;
145     float old_step;
146     float new_step;
147     float old_weight;
148     float new_weight;
149     float gain;
150     
151     if (s->missing_samples)
152     {
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 */
155
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)
160             pitch_overlap = len;
161         gain = 1.0 - s->missing_samples*ATTENUATION_INCREMENT;
162         if (gain < 0.0)
163             gain = 0.0;
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++)
169         {
170             amp[i] = fsaturate(old_weight*s->pitchbuf[s->pitch_offset] + new_weight*amp[i]);
171             if (++s->pitch_offset >= s->pitch)
172                 s->pitch_offset = 0;
173             new_weight += new_step;
174             old_weight -= old_step;
175             if (old_weight < 0.0)
176                 old_weight = 0.0;
177         }
178         s->missing_samples = 0;
179     }
180     save_history(s, amp, len);
181     return len;
182 }
183 /*- End of function --------------------------------------------------------*/
184
185 int plc_fillin(plc_state_t *s, int16_t amp[], int len)
186 {
187     int i;
188     int pitch_overlap;
189     float old_step;
190     float new_step;
191     float old_weight;
192     float new_weight;
193     float gain;
194     //int16_t *orig_amp;
195     int orig_len;
196
197     //orig_amp = amp;
198     orig_len = len;
199     if (s->missing_samples == 0)
200     {
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++)
216         {
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;
219         }
220         /* We should now be ready to fill in the gap with repeated, decaying cycles
221            of what is in pitchbuf */
222
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. */
226         gain = 1.0;
227         new_step = 1.0/pitch_overlap;
228         old_step = new_step;
229         new_weight = new_step;
230         old_weight = 1.0 - new_step;
231         for (i = 0;  i < pitch_overlap && i < len;  i++)
232         {
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)
237                 old_weight = 0.0;
238         }
239         s->pitch_offset = i;
240     }
241     else
242     {
243         gain = 1.0 - s->missing_samples*ATTENUATION_INCREMENT;
244         i = 0;
245     }
246     for (  ;  gain > 0.0  &&  i < len;  i++)
247     {
248         amp[i] = s->pitchbuf[s->pitch_offset]*gain;
249         gain -= ATTENUATION_INCREMENT;
250         if (++s->pitch_offset >= s->pitch)
251             s->pitch_offset = 0;
252     }
253     for (  ;  i < len;  i++)
254         amp[i] = 0;
255     s->missing_samples += orig_len;
256     save_history(s, amp, len);
257     return len;
258 }
259 /*- End of function --------------------------------------------------------*/
260
261 plc_state_t *plc_init(plc_state_t *s)
262 {
263     memset(s, 0, sizeof(*s));
264     return s;
265 }
266 /*- End of function --------------------------------------------------------*/
267 /*- End of file ------------------------------------------------------------*/