Thu Apr 28 2011 17:14:00

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

Definition at line 42 of file plc.c.

#define INT16_MAX   (32767)

Definition at line 49 of file plc.c.

Referenced by fsaturate().

#define INT16_MIN   (-32767-1)

Definition at line 50 of file plc.c.

Referenced by fsaturate().

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

Definition at line 56 of file plc.c.

#define TRUE   (!FALSE)

Definition at line 45 of file plc.c.


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.

References len().

Referenced by plc_fillin().

{
   int i;
   int j;
   int acc;
   int min_acc;
   int pitch;

   pitch = min_pitch;
   min_acc = INT_MAX;
   for (i = max_pitch; i <= min_pitch; i++) {
      acc = 0;
      for (j = 0; j < len; j++)
         acc += abs(amp[i + j] - amp[j]);
      if (acc < min_acc) {
         min_acc = acc;
         pitch = i;
      }
   }
   return pitch;
}
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().

{
   if (damp > 32767.0)
      return  INT16_MAX;
   if (damp < -32768.0)
      return  INT16_MIN;
   return (int16_t) rint(damp);
}
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().

{
   int16_t tmp[PLC_HISTORY_LEN];

   if (s->buf_ptr == 0)
      return;
   memcpy(tmp, s->history, sizeof(int16_t)*s->buf_ptr);
   memcpy(s->history, s->history + s->buf_ptr, sizeof(int16_t) * (PLC_HISTORY_LEN - s->buf_ptr));
   memcpy(s->history + PLC_HISTORY_LEN - s->buf_ptr, tmp, sizeof(int16_t) * s->buf_ptr);
   s->buf_ptr = 0;
}
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:
sThe packet loss concealer context.
ampThe audio sample buffer.
lenThe 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, len(), 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().

{
   int i;
   int pitch_overlap;
   float old_step;
   float new_step;
   float old_weight;
   float new_weight;
   float gain;
   int16_t *orig_amp;
   int orig_len;

   orig_amp = amp;
   orig_len = len;
   if (s->missing_samples == 0) {
      /* As the gap in real speech starts we need to assess the last known pitch,
         and prepare the synthetic data we will use for fill-in */
      normalise_history(s);
      s->pitch = amdf_pitch(PLC_PITCH_MIN, PLC_PITCH_MAX, s->history + PLC_HISTORY_LEN - CORRELATION_SPAN - PLC_PITCH_MIN, CORRELATION_SPAN);
      /* We overlap a 1/4 wavelength */
      pitch_overlap = s->pitch >> 2;
      /* Cook up a single cycle of pitch, using a single of the real signal with 1/4
         cycle OLA'ed to make the ends join up nicely */
      /* The first 3/4 of the cycle is a simple copy */
      for (i = 0;  i < s->pitch - pitch_overlap;  i++)
         s->pitchbuf[i] = s->history[PLC_HISTORY_LEN - s->pitch + i];
      /* The last 1/4 of the cycle is overlapped with the end of the previous cycle */
      new_step = 1.0/pitch_overlap;
      new_weight = new_step;
      for ( ; i < s->pitch; i++) {
         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;
         new_weight += new_step;
      }
      /* We should now be ready to fill in the gap with repeated, decaying cycles
         of what is in pitchbuf */

      /* We need to OLA the first 1/4 wavelength of the synthetic data, to smooth
         it into the previous real data. To avoid the need to introduce a delay
         in the stream, reverse the last 1/4 wavelength, and OLA with that. */
      gain = 1.0;
      new_step = 1.0 / pitch_overlap;
      old_step = new_step;
      new_weight = new_step;
      old_weight = 1.0 - new_step;
      for (i = 0; i < pitch_overlap; i++) {
         amp[i] = fsaturate(old_weight * s->history[PLC_HISTORY_LEN - 1 - i] + new_weight * s->pitchbuf[i]);
         new_weight += new_step;
         old_weight -= old_step;
         if (old_weight < 0.0)
            old_weight = 0.0;
      }
      s->pitch_offset = i;
   } else {
      gain = 1.0 - s->missing_samples*ATTENUATION_INCREMENT;
      i = 0;
   }
   for ( ; gain > 0.0 && i < len; i++) {
      amp[i] = s->pitchbuf[s->pitch_offset] * gain;
      gain -= ATTENUATION_INCREMENT;
      if (++s->pitch_offset >= s->pitch)
         s->pitch_offset = 0;
   }
   for ( ; i < len; i++)
      amp[i] = 0;
   s->missing_samples += orig_len;
   save_history(s, amp, len);
   return len;
}
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:
sThe packet loss concealer context.
Returns:
A pointer to the he packet loss concealer context.

Definition at line 242 of file plc.c.

References s.

{
   memset(s, 0, sizeof(*s));
   return 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.

Parameters:
sThe packet loss concealer context.
ampThe audio sample buffer.
lenThe 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(), len(), 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().

{
   int i;
   int pitch_overlap;
   float old_step;
   float new_step;
   float old_weight;
   float new_weight;
   float gain;
   
   if (s->missing_samples) {
      /* Although we have a real signal, we need to smooth it to fit well
      with the synthetic signal we used for the previous block */

      /* The start of the real data is overlapped with the next 1/4 cycle
         of the synthetic data. */
      pitch_overlap = s->pitch >> 2;
      if (pitch_overlap > len)
         pitch_overlap = len;
      gain = 1.0 - s->missing_samples*ATTENUATION_INCREMENT;
      if (gain < 0.0)
         gain = 0.0;
      new_step = 1.0/pitch_overlap;
      old_step = new_step*gain;
      new_weight = new_step;
      old_weight = (1.0 - new_step)*gain;
      for (i = 0; i < pitch_overlap; i++) {
         amp[i] = fsaturate(old_weight * s->pitchbuf[s->pitch_offset] + new_weight * amp[i]);
         if (++s->pitch_offset >= s->pitch)
            s->pitch_offset = 0;
         new_weight += new_step;
         old_weight -= old_step;
         if (old_weight < 0.0)
            old_weight = 0.0;
      }
      s->missing_samples = 0;
   }
   save_history(s, amp, len);
   return len;
}
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, len(), and PLC_HISTORY_LEN.

Referenced by plc_fillin(), and plc_rx().

{
   if (len >= PLC_HISTORY_LEN) {
      /* Just keep the last part of the new data, starting at the beginning of the buffer */
       memcpy(s->history, buf + len - PLC_HISTORY_LEN, sizeof(int16_t) * PLC_HISTORY_LEN);
      s->buf_ptr = 0;
      return;
   }
   if (s->buf_ptr + len > PLC_HISTORY_LEN) {
      /* Wraps around - must break into two sections */
      memcpy(s->history + s->buf_ptr, buf, sizeof(int16_t) * (PLC_HISTORY_LEN - s->buf_ptr));
      len -= (PLC_HISTORY_LEN - s->buf_ptr);
      memcpy(s->history, buf + (PLC_HISTORY_LEN - s->buf_ptr), sizeof(int16_t)*len);
      s->buf_ptr = len;
      return;
   }
   /* Can use just one section */
   memcpy(s->history + s->buf_ptr, buf, sizeof(int16_t)*len);
   s->buf_ptr += len;
}