#include "asterisk.h"
#include <math.h>
#include "asterisk/plc.h"
Go to the source code of this file.
Defines | |
#define | ATTENUATION_INCREMENT 0.0025 |
#define | FALSE 0 |
#define | INT16_MAX (32767) |
#define | INT16_MIN (-32767-1) |
#define | ms_to_samples(t) (((t)*DEFAULT_SAMPLE_RATE)/1000) |
#define | TRUE (!FALSE) |
Functions | |
static int __inline__ | amdf_pitch (int min_pitch, int max_pitch, int16_t amp[], int len) |
static int16_t | fsaturate (double damp) |
static void | normalise_history (plc_state_t *s) |
int | plc_fillin (plc_state_t *s, int16_t amp[], int len) |
Fill-in a block of missing audio samples. | |
plc_state_t * | plc_init (plc_state_t *s) |
Process a block of received V.29 modem audio samples. | |
int | plc_rx (plc_state_t *s, int16_t amp[], int len) |
Process a block of received audio samples. | |
static void | save_history (plc_state_t *s, int16_t *buf, int len) |
Definition in file plc.c.
#define ATTENUATION_INCREMENT 0.0025 |
static int __inline__ amdf_pitch | ( | int | min_pitch, | |
int | max_pitch, | |||
int16_t | amp[], | |||
int | len | |||
) | [static] |
Definition at line 108 of file plc.c.
Referenced by plc_fillin().
00109 { 00110 int i; 00111 int j; 00112 int acc; 00113 int min_acc; 00114 int pitch; 00115 00116 pitch = min_pitch; 00117 min_acc = INT_MAX; 00118 for (i = max_pitch; i <= min_pitch; i++) { 00119 acc = 0; 00120 for (j = 0; j < len; j++) 00121 acc += abs(amp[i + j] - amp[j]); 00122 if (acc < min_acc) { 00123 min_acc = acc; 00124 pitch = i; 00125 } 00126 } 00127 return pitch; 00128 }
static int16_t fsaturate | ( | double | damp | ) | [inline, static] |
static void normalise_history | ( | plc_state_t * | s | ) | [static] |
Definition at line 94 of file plc.c.
References plc_state_t::buf_ptr, plc_state_t::history, and PLC_HISTORY_LEN.
Referenced by plc_fillin().
00095 { 00096 int16_t tmp[PLC_HISTORY_LEN]; 00097 00098 if (s->buf_ptr == 0) 00099 return; 00100 memcpy(tmp, s->history, sizeof(int16_t)*s->buf_ptr); 00101 memcpy(s->history, s->history + s->buf_ptr, sizeof(int16_t) * (PLC_HISTORY_LEN - s->buf_ptr)); 00102 memcpy(s->history + PLC_HISTORY_LEN - s->buf_ptr, tmp, sizeof(int16_t) * s->buf_ptr); 00103 s->buf_ptr = 0; 00104 }
int plc_fillin | ( | plc_state_t * | s, | |
int16_t | amp[], | |||
int | len | |||
) |
Fill-in a block of missing audio samples.
Fill-in a block of missing audio samples.
s | The packet loss concealer context. | |
amp | The audio sample buffer. | |
len | The number of samples to be synthesised. |
Definition at line 175 of file plc.c.
References amdf_pitch(), ATTENUATION_INCREMENT, CORRELATION_SPAN, fsaturate(), plc_state_t::history, plc_state_t::missing_samples, normalise_history(), plc_state_t::pitch, plc_state_t::pitch_offset, plc_state_t::pitchbuf, PLC_HISTORY_LEN, PLC_PITCH_MAX, PLC_PITCH_MIN, and save_history().
Referenced by adjust_frame_for_plc().
00176 { 00177 int i; 00178 int pitch_overlap; 00179 float old_step; 00180 float new_step; 00181 float old_weight; 00182 float new_weight; 00183 float gain; 00184 int orig_len; 00185 00186 orig_len = len; 00187 if (s->missing_samples == 0) { 00188 /* As the gap in real speech starts we need to assess the last known pitch, 00189 and prepare the synthetic data we will use for fill-in */ 00190 normalise_history(s); 00191 s->pitch = amdf_pitch(PLC_PITCH_MIN, PLC_PITCH_MAX, s->history + PLC_HISTORY_LEN - CORRELATION_SPAN - PLC_PITCH_MIN, CORRELATION_SPAN); 00192 /* We overlap a 1/4 wavelength */ 00193 pitch_overlap = s->pitch >> 2; 00194 /* Cook up a single cycle of pitch, using a single of the real signal with 1/4 00195 cycle OLA'ed to make the ends join up nicely */ 00196 /* The first 3/4 of the cycle is a simple copy */ 00197 for (i = 0; i < s->pitch - pitch_overlap; i++) 00198 s->pitchbuf[i] = s->history[PLC_HISTORY_LEN - s->pitch + i]; 00199 /* The last 1/4 of the cycle is overlapped with the end of the previous cycle */ 00200 new_step = 1.0/pitch_overlap; 00201 new_weight = new_step; 00202 for ( ; i < s->pitch; i++) { 00203 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; 00204 new_weight += new_step; 00205 } 00206 /* We should now be ready to fill in the gap with repeated, decaying cycles 00207 of what is in pitchbuf */ 00208 00209 /* We need to OLA the first 1/4 wavelength of the synthetic data, to smooth 00210 it into the previous real data. To avoid the need to introduce a delay 00211 in the stream, reverse the last 1/4 wavelength, and OLA with that. */ 00212 gain = 1.0; 00213 new_step = 1.0 / pitch_overlap; 00214 old_step = new_step; 00215 new_weight = new_step; 00216 old_weight = 1.0 - new_step; 00217 for (i = 0; i < pitch_overlap; i++) { 00218 amp[i] = fsaturate(old_weight * s->history[PLC_HISTORY_LEN - 1 - i] + new_weight * s->pitchbuf[i]); 00219 new_weight += new_step; 00220 old_weight -= old_step; 00221 if (old_weight < 0.0) 00222 old_weight = 0.0; 00223 } 00224 s->pitch_offset = i; 00225 } else { 00226 gain = 1.0 - s->missing_samples*ATTENUATION_INCREMENT; 00227 i = 0; 00228 } 00229 for ( ; gain > 0.0 && i < len; i++) { 00230 amp[i] = s->pitchbuf[s->pitch_offset] * gain; 00231 gain -= ATTENUATION_INCREMENT; 00232 if (++s->pitch_offset >= s->pitch) 00233 s->pitch_offset = 0; 00234 } 00235 for ( ; i < len; i++) 00236 amp[i] = 0; 00237 s->missing_samples += orig_len; 00238 save_history(s, amp, len); 00239 return len; 00240 }
plc_state_t* plc_init | ( | plc_state_t * | s | ) |
int plc_rx | ( | plc_state_t * | s, | |
int16_t | amp[], | |||
int | len | |||
) |
Process a block of received audio samples.
Process a block of received audio samples.
s | The packet loss concealer context. | |
amp | The audio sample buffer. | |
len | The number of samples in the buffer. |
Definition at line 132 of file plc.c.
References ATTENUATION_INCREMENT, fsaturate(), plc_state_t::missing_samples, plc_state_t::pitch, plc_state_t::pitch_offset, plc_state_t::pitchbuf, and save_history().
Referenced by adjust_frame_for_plc().
00133 { 00134 int i; 00135 int pitch_overlap; 00136 float old_step; 00137 float new_step; 00138 float old_weight; 00139 float new_weight; 00140 float gain; 00141 00142 if (s->missing_samples) { 00143 /* Although we have a real signal, we need to smooth it to fit well 00144 with the synthetic signal we used for the previous block */ 00145 00146 /* The start of the real data is overlapped with the next 1/4 cycle 00147 of the synthetic data. */ 00148 pitch_overlap = s->pitch >> 2; 00149 if (pitch_overlap > len) 00150 pitch_overlap = len; 00151 gain = 1.0 - s->missing_samples*ATTENUATION_INCREMENT; 00152 if (gain < 0.0) 00153 gain = 0.0; 00154 new_step = 1.0/pitch_overlap; 00155 old_step = new_step*gain; 00156 new_weight = new_step; 00157 old_weight = (1.0 - new_step)*gain; 00158 for (i = 0; i < pitch_overlap; i++) { 00159 amp[i] = fsaturate(old_weight * s->pitchbuf[s->pitch_offset] + new_weight * amp[i]); 00160 if (++s->pitch_offset >= s->pitch) 00161 s->pitch_offset = 0; 00162 new_weight += new_step; 00163 old_weight -= old_step; 00164 if (old_weight < 0.0) 00165 old_weight = 0.0; 00166 } 00167 s->missing_samples = 0; 00168 } 00169 save_history(s, amp, len); 00170 return len; 00171 }
static void save_history | ( | plc_state_t * | s, | |
int16_t * | buf, | |||
int | len | |||
) | [static] |
Definition at line 71 of file plc.c.
References plc_state_t::buf_ptr, plc_state_t::history, and PLC_HISTORY_LEN.
Referenced by plc_fillin(), and plc_rx().
00072 { 00073 if (len >= PLC_HISTORY_LEN) { 00074 /* Just keep the last part of the new data, starting at the beginning of the buffer */ 00075 memcpy(s->history, buf + len - PLC_HISTORY_LEN, sizeof(int16_t) * PLC_HISTORY_LEN); 00076 s->buf_ptr = 0; 00077 return; 00078 } 00079 if (s->buf_ptr + len > PLC_HISTORY_LEN) { 00080 /* Wraps around - must break into two sections */ 00081 memcpy(s->history + s->buf_ptr, buf, sizeof(int16_t) * (PLC_HISTORY_LEN - s->buf_ptr)); 00082 len -= (PLC_HISTORY_LEN - s->buf_ptr); 00083 memcpy(s->history, buf + (PLC_HISTORY_LEN - s->buf_ptr), sizeof(int16_t)*len); 00084 s->buf_ptr = len; 00085 return; 00086 } 00087 /* Can use just one section */ 00088 memcpy(s->history + s->buf_ptr, buf, sizeof(int16_t)*len); 00089 s->buf_ptr += len; 00090 }