#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 |
#define FALSE 0 |
Definition at line 42 of file plc.c.
Referenced by _sip_show_peer(), _sip_show_peers(), add_sdp(), ast_tzset(), build_peer(), build_rpid(), build_user(), cb_extensionstate(), check_auth(), check_dirpath(), check_pendings(), do_monitor(), expire_register(), find_call(), find_sdp(), handle_invite_replaces(), handle_request_invite(), handle_request_notify(), handle_request_refer(), handle_request_subscribe(), handle_response(), handle_response_invite(), invent_message(), load_config(), minivm_accmess_exec(), minivm_counter_func_read(), minivm_counter_func_write(), parse_register_contact(), parse_sip_options(), proc_session_timer(), process_sdp(), proxy_update(), rcvfax_exec(), reload_config(), reqprep(), show_console(), sip_addheader(), sip_answer(), sip_destroy_peer(), sip_do_history_deprecated(), sip_hangup(), sip_indicate(), sip_poke_noanswer(), sip_prune_realtime(), sip_read(), sip_register(), sip_sendhtml(), sip_set_history(), sip_set_rtp_peer(), sip_set_udptl_peer(), sip_show_inuse(), sip_show_settings(), sip_show_user(), sip_show_users(), sip_write(), sndfax_exec(), stop_session_timer(), time1(), time2(), time2sub(), transmit_invite(), transmit_reinvite_with_sdp(), transmit_response_with_sdp(), tzload(), tzparse(), and update_call_counter().
#define TRUE (!FALSE) |
Definition at line 45 of file plc.c.
Referenced by __sip_autodestruct(), __sip_destroy(), _sip_show_peer(), _sip_show_peers(), add_sdp(), ast_tzset(), build_peer(), build_rpid(), build_user(), check_auth(), check_dirpath(), check_pendings(), cli_activate(), do_monitor(), find_account(), find_sdp(), find_user_realtime(), gmtload(), handle_request_invite(), handle_request_notify(), handle_request_refer(), handle_response_invite(), handle_response_peerpoke(), leave_voicemail(), load_config(), local_attended_transfer(), message_template_create(), minivm_accmess_exec(), minivm_account_func_read(), minivm_greet_exec(), minivm_notify_exec(), parse_ok_contact(), parse_sip_options(), proc_session_timer(), process_sdp(), proxy_update(), rcvfax_exec(), reg_source_db(), reqprep(), respprep(), restart_session_timer(), show_console(), sip_addheader(), sip_alloc(), sip_destroy(), sip_do_history_deprecated(), sip_hangup(), sip_indicate(), sip_park_thread(), sip_prune_realtime(), sip_read(), sip_reload(), sip_request_call(), sip_scheddestroy(), sip_set_history(), sip_set_udptl_peer(), sip_show_channel(), sip_show_inuse(), sip_show_user(), sip_show_users(), sip_write(), sndfax_exec(), st_get_se(), stop_session_timer(), temp_peer(), time1(), time2(), transmit_audio(), transmit_invite(), transmit_register(), transmit_reinvite_with_sdp(), transmit_response_with_sdp(), transmit_t38(), tzload(), and unload_module().
static int __inline__ amdf_pitch | ( | int | min_pitch, | |
int | max_pitch, | |||
int16_t | amp[], | |||
int | len | |||
) | [static] |
Definition at line 104 of file plc.c.
Referenced by plc_fillin().
00105 { 00106 int i; 00107 int j; 00108 int acc; 00109 int min_acc; 00110 int pitch; 00111 00112 pitch = min_pitch; 00113 min_acc = INT_MAX; 00114 for (i = max_pitch; i <= min_pitch; i++) { 00115 acc = 0; 00116 for (j = 0; j < len; j++) 00117 acc += abs(amp[i + j] - amp[j]); 00118 if (acc < min_acc) { 00119 min_acc = acc; 00120 pitch = i; 00121 } 00122 } 00123 return pitch; 00124 }
static int16_t fsaturate | ( | double | damp | ) | [inline, static] |
static void normalise_history | ( | plc_state_t * | s | ) | [static] |
Definition at line 90 of file plc.c.
References PLC_HISTORY_LEN, and s.
Referenced by plc_fillin().
00091 { 00092 int16_t tmp[PLC_HISTORY_LEN]; 00093 00094 if (s->buf_ptr == 0) 00095 return; 00096 memcpy(tmp, s->history, sizeof(int16_t)*s->buf_ptr); 00097 memcpy(s->history, s->history + s->buf_ptr, sizeof(int16_t) * (PLC_HISTORY_LEN - s->buf_ptr)); 00098 memcpy(s->history + PLC_HISTORY_LEN - s->buf_ptr, tmp, sizeof(int16_t) * s->buf_ptr); 00099 s->buf_ptr = 0; 00100 }
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 171 of file plc.c.
References amdf_pitch(), ATTENUATION_INCREMENT, CORRELATION_SPAN, fsaturate(), normalise_history(), PLC_HISTORY_LEN, PLC_PITCH_MAX, PLC_PITCH_MIN, s, and save_history().
Referenced by framein().
00172 { 00173 int i; 00174 int pitch_overlap; 00175 float old_step; 00176 float new_step; 00177 float old_weight; 00178 float new_weight; 00179 float gain; 00180 int16_t *orig_amp; 00181 int orig_len; 00182 00183 orig_amp = amp; 00184 orig_len = len; 00185 if (s->missing_samples == 0) { 00186 /* As the gap in real speech starts we need to assess the last known pitch, 00187 and prepare the synthetic data we will use for fill-in */ 00188 normalise_history(s); 00189 s->pitch = amdf_pitch(PLC_PITCH_MIN, PLC_PITCH_MAX, s->history + PLC_HISTORY_LEN - CORRELATION_SPAN - PLC_PITCH_MIN, CORRELATION_SPAN); 00190 /* We overlap a 1/4 wavelength */ 00191 pitch_overlap = s->pitch >> 2; 00192 /* Cook up a single cycle of pitch, using a single of the real signal with 1/4 00193 cycle OLA'ed to make the ends join up nicely */ 00194 /* The first 3/4 of the cycle is a simple copy */ 00195 for (i = 0; i < s->pitch - pitch_overlap; i++) 00196 s->pitchbuf[i] = s->history[PLC_HISTORY_LEN - s->pitch + i]; 00197 /* The last 1/4 of the cycle is overlapped with the end of the previous cycle */ 00198 new_step = 1.0/pitch_overlap; 00199 new_weight = new_step; 00200 for ( ; i < s->pitch; i++) { 00201 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; 00202 new_weight += new_step; 00203 } 00204 /* We should now be ready to fill in the gap with repeated, decaying cycles 00205 of what is in pitchbuf */ 00206 00207 /* We need to OLA the first 1/4 wavelength of the synthetic data, to smooth 00208 it into the previous real data. To avoid the need to introduce a delay 00209 in the stream, reverse the last 1/4 wavelength, and OLA with that. */ 00210 gain = 1.0; 00211 new_step = 1.0 / pitch_overlap; 00212 old_step = new_step; 00213 new_weight = new_step; 00214 old_weight = 1.0 - new_step; 00215 for (i = 0; i < pitch_overlap; i++) { 00216 amp[i] = fsaturate(old_weight * s->history[PLC_HISTORY_LEN - 1 - i] + new_weight * s->pitchbuf[i]); 00217 new_weight += new_step; 00218 old_weight -= old_step; 00219 if (old_weight < 0.0) 00220 old_weight = 0.0; 00221 } 00222 s->pitch_offset = i; 00223 } else { 00224 gain = 1.0 - s->missing_samples*ATTENUATION_INCREMENT; 00225 i = 0; 00226 } 00227 for ( ; gain > 0.0 && i < len; i++) { 00228 amp[i] = s->pitchbuf[s->pitch_offset] * gain; 00229 gain -= ATTENUATION_INCREMENT; 00230 if (++s->pitch_offset >= s->pitch) 00231 s->pitch_offset = 0; 00232 } 00233 for ( ; i < len; i++) 00234 amp[i] = 0; 00235 s->missing_samples += orig_len; 00236 save_history(s, amp, len); 00237 return len; 00238 }
plc_state_t* plc_init | ( | plc_state_t * | s | ) |
Process a block of received V.29 modem audio samples.
Process a block of received V.29 modem audio samples.
s | The packet loss concealer context. |
Definition at line 242 of file plc.c.
References s.
00243 { 00244 memset(s, 0, sizeof(*s)); 00245 return s; 00246 }
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 128 of file plc.c.
References ATTENUATION_INCREMENT, fsaturate(), s, and save_history().
Referenced by framein().
00129 { 00130 int i; 00131 int pitch_overlap; 00132 float old_step; 00133 float new_step; 00134 float old_weight; 00135 float new_weight; 00136 float gain; 00137 00138 if (s->missing_samples) { 00139 /* Although we have a real signal, we need to smooth it to fit well 00140 with the synthetic signal we used for the previous block */ 00141 00142 /* The start of the real data is overlapped with the next 1/4 cycle 00143 of the synthetic data. */ 00144 pitch_overlap = s->pitch >> 2; 00145 if (pitch_overlap > len) 00146 pitch_overlap = len; 00147 gain = 1.0 - s->missing_samples*ATTENUATION_INCREMENT; 00148 if (gain < 0.0) 00149 gain = 0.0; 00150 new_step = 1.0/pitch_overlap; 00151 old_step = new_step*gain; 00152 new_weight = new_step; 00153 old_weight = (1.0 - new_step)*gain; 00154 for (i = 0; i < pitch_overlap; i++) { 00155 amp[i] = fsaturate(old_weight * s->pitchbuf[s->pitch_offset] + new_weight * amp[i]); 00156 if (++s->pitch_offset >= s->pitch) 00157 s->pitch_offset = 0; 00158 new_weight += new_step; 00159 old_weight -= old_step; 00160 if (old_weight < 0.0) 00161 old_weight = 0.0; 00162 } 00163 s->missing_samples = 0; 00164 } 00165 save_history(s, amp, len); 00166 return len; 00167 }
static void save_history | ( | plc_state_t * | s, | |
int16_t * | buf, | |||
int | len | |||
) | [static] |
Definition at line 67 of file plc.c.
References PLC_HISTORY_LEN, and s.
Referenced by plc_fillin(), and plc_rx().
00068 { 00069 if (len >= PLC_HISTORY_LEN) { 00070 /* Just keep the last part of the new data, starting at the beginning of the buffer */ 00071 memcpy(s->history, buf + len - PLC_HISTORY_LEN, sizeof(int16_t) * PLC_HISTORY_LEN); 00072 s->buf_ptr = 0; 00073 return; 00074 } 00075 if (s->buf_ptr + len > PLC_HISTORY_LEN) { 00076 /* Wraps around - must break into two sections */ 00077 memcpy(s->history + s->buf_ptr, buf, sizeof(int16_t) * (PLC_HISTORY_LEN - s->buf_ptr)); 00078 len -= (PLC_HISTORY_LEN - s->buf_ptr); 00079 memcpy(s->history, buf + (PLC_HISTORY_LEN - s->buf_ptr), sizeof(int16_t)*len); 00080 s->buf_ptr = len; 00081 return; 00082 } 00083 /* Can use just one section */ 00084 memcpy(s->history + s->buf_ptr, buf, sizeof(int16_t)*len); 00085 s->buf_ptr += len; 00086 }