Wed Oct 28 11:46:17 2009

Asterisk developer's documentation


plc.c File Reference

SpanDSP - a series of DSP components for telephony. More...

#include "asterisk.h"
#include <math.h>
#include "asterisk/plc.h"

Include dependency graph for plc.c:

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_tplc_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)


Detailed Description

SpanDSP - a series of DSP components for telephony.

Author:
Steve Underwood <steveu@coppice.org>

Definition in file plc.c.


Define Documentation

#define ATTENUATION_INCREMENT   0.0025

Definition at line 54 of file plc.c.

Referenced by plc_fillin(), and plc_rx().

#define FALSE   0

#define INT16_MAX   (32767)

Definition at line 49 of file plc.c.

#define INT16_MIN   (-32767-1)

Definition at line 50 of file plc.c.

#define ms_to_samples (  )     (((t)*DEFAULT_SAMPLE_RATE)/1000)

Definition at line 56 of file plc.c.

#define TRUE   (!FALSE)

Definition at line 45 of file plc.c.

Referenced by __sip_ack(), __sip_autodestruct(), __sip_semi_ack(), _sip_show_peer(), _sip_show_peers(), add_sdp(), ast_tzset(), build_peer(), build_rpid(), build_user(), check_auth(), check_dirpath(), check_pendings(), cli_activate(), dialog_cleanup_and_destroy(), find_account(), find_sdp(), find_user_realtime(), gmtload(), handle_request_invite(), handle_request_notify(), handle_request_refer(), handle_response_invite(), handle_response_peerpoke(), interpret_t38_parameters(), 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_register_contact(), parse_sip_options(), proc_session_timer(), process_sdp(), proxy_update(), rcvfax_exec(), reg_source_db(), reload_config(), 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(), time2sub(), transmit_audio(), transmit_invite(), transmit_register(), transmit_reinvite_with_sdp(), transmit_response_with_sdp(), transmit_t38(), tzload(), tzparse(), and udptl_rx_packet().


Function Documentation

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]

Definition at line 58 of file plc.c.

References INT16_MAX, and INT16_MIN.

Referenced by plc_fillin(), and plc_rx().

00059 {
00060    if (damp > 32767.0)
00061       return  INT16_MAX;
00062    if (damp < -32768.0)
00063       return  INT16_MIN;
00064    return (int16_t) rint(damp);
00065 }

static void normalise_history ( plc_state_t s  )  [static]

Definition at line 90 of file plc.c.

References plc_state_t::buf_ptr, plc_state_t::history, and PLC_HISTORY_LEN.

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.

Parameters:
s The packet loss concealer context.
amp The audio sample buffer.
len The number of samples to be synthesised.
Returns:
The number of samples synthesized.

Definition at line 171 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 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.

Parameters:
s The packet loss concealer context.
Returns:
A pointer to the he packet loss concealer context.

Definition at line 242 of file plc.c.

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.

Parameters:
s The packet loss concealer context.
amp The audio sample buffer.
len The number of samples in the buffer.
Returns:
The number of samples in the buffer.

Definition at line 128 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 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_state_t::buf_ptr, plc_state_t::history, and PLC_HISTORY_LEN.

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 }


Generated on Wed Oct 28 11:46:17 2009 for Asterisk - the Open Source PBX by  doxygen 1.5.6