Mon Mar 20 08:20:13 2006

Asterisk developer's documentation


Main Page | Modules | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals | Related Pages

plc.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Written by Steve Underwood <steveu@coppice.org>
00005  *
00006  * Copyright (C) 2004 Steve Underwood
00007  *
00008  * All rights reserved.
00009  *
00010  * See http://www.asterisk.org for more information about
00011  * the Asterisk project. Please do not directly contact
00012  * any of the maintainers of this project for assistance;
00013  * the project provides a web site, mailing lists and IRC
00014  * channels for your use.
00015  *
00016  * This program is free software, distributed under the terms of
00017  * the GNU General Public License Version 2. See the LICENSE file
00018  * at the top of the source tree.
00019  *
00020  * This version may be optionally licenced under the GNU LGPL licence.
00021  *
00022  * This version is disclaimed to DIGIUM for inclusion in the Asterisk project.
00023  */
00024 
00025 /*! \file
00026  *
00027  * \brief SpanDSP - a series of DSP components for telephony
00028  *
00029  */
00030 
00031 #include <stdio.h>
00032 #include <stdlib.h>
00033 #include <string.h>
00034 #include <math.h>
00035 #include <limits.h>
00036 
00037 #include "asterisk.h"
00038 
00039 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 7221 $")
00040 
00041 #include "asterisk/plc.h"
00042 
00043 #if !defined(FALSE)
00044 #define FALSE 0
00045 #endif
00046 #if !defined(TRUE)
00047 #define TRUE (!FALSE)
00048 #endif
00049 
00050 #if !defined(INT16_MAX)
00051 #define INT16_MAX (32767)
00052 #define INT16_MIN (-32767-1)
00053 #endif
00054 
00055 /* We do a straight line fade to zero volume in 50ms when we are filling in for missing data. */
00056 #define ATTENUATION_INCREMENT       0.0025               /* Attenuation per sample */
00057 
00058 #define ms_to_samples(t)       (((t)*SAMPLE_RATE)/1000)
00059 
00060 static inline int16_t fsaturate(double damp)
00061 {
00062    if (damp > 32767.0)
00063       return  INT16_MAX;
00064    if (damp < -32768.0)
00065       return  INT16_MIN;
00066    return (int16_t) rint(damp);
00067 }
00068 
00069 static void save_history(plc_state_t *s, int16_t *buf, int len)
00070 {
00071    if (len >= PLC_HISTORY_LEN) {
00072       /* Just keep the last part of the new data, starting at the beginning of the buffer */
00073        memcpy(s->history, buf + len - PLC_HISTORY_LEN, sizeof(int16_t)*PLC_HISTORY_LEN);
00074       s->buf_ptr = 0;
00075       return;
00076    }
00077    if (s->buf_ptr + len > PLC_HISTORY_LEN) {
00078       /* Wraps around - must break into two sections */
00079       memcpy(s->history + s->buf_ptr, buf, sizeof(int16_t)*(PLC_HISTORY_LEN - s->buf_ptr));
00080       len -= (PLC_HISTORY_LEN - s->buf_ptr);
00081       memcpy(s->history, buf + (PLC_HISTORY_LEN - s->buf_ptr), sizeof(int16_t)*len);
00082       s->buf_ptr = len;
00083       return;
00084    }
00085    /* Can use just one section */
00086    memcpy(s->history + s->buf_ptr, buf, sizeof(int16_t)*len);
00087    s->buf_ptr += len;
00088 }
00089 
00090 /*- End of function --------------------------------------------------------*/
00091 
00092 static void normalise_history(plc_state_t *s)
00093 {
00094    int16_t tmp[PLC_HISTORY_LEN];
00095 
00096    if (s->buf_ptr == 0)
00097       return;
00098    memcpy(tmp, s->history, sizeof(int16_t)*s->buf_ptr);
00099    memcpy(s->history, s->history + s->buf_ptr, sizeof(int16_t)*(PLC_HISTORY_LEN - s->buf_ptr));
00100    memcpy(s->history + PLC_HISTORY_LEN - s->buf_ptr, tmp, sizeof(int16_t)*s->buf_ptr);
00101    s->buf_ptr = 0;
00102 }
00103 
00104 /*- End of function --------------------------------------------------------*/
00105 
00106 static int __inline__ amdf_pitch(int min_pitch, int max_pitch, int16_t amp[], int len)
00107 {
00108    int i;
00109    int j;
00110    int acc;
00111    int min_acc;
00112    int pitch;
00113 
00114    pitch = min_pitch;
00115    min_acc = INT_MAX;
00116    for (i = max_pitch;  i <= min_pitch;  i++) {
00117       acc = 0;
00118       for (j = 0;  j < len;  j++)
00119          acc += abs(amp[i + j] - amp[j]);
00120       if (acc < min_acc) {
00121          min_acc = acc;
00122          pitch = i;
00123       }
00124    }
00125    return pitch;
00126 }
00127 
00128 /*- End of function --------------------------------------------------------*/
00129 
00130 int plc_rx(plc_state_t *s, int16_t amp[], int len)
00131 {
00132    int i;
00133    int pitch_overlap;
00134    float old_step;
00135    float new_step;
00136    float old_weight;
00137    float new_weight;
00138    float gain;
00139    
00140    if (s->missing_samples) {
00141       /* Although we have a real signal, we need to smooth it to fit well
00142       with the synthetic signal we used for the previous block */
00143 
00144       /* The start of the real data is overlapped with the next 1/4 cycle
00145          of the synthetic data. */
00146       pitch_overlap = s->pitch >> 2;
00147       if (pitch_overlap > len)
00148          pitch_overlap = len;
00149       gain = 1.0 - s->missing_samples*ATTENUATION_INCREMENT;
00150       if (gain < 0.0)
00151          gain = 0.0;
00152       new_step = 1.0/pitch_overlap;
00153       old_step = new_step*gain;
00154       new_weight = new_step;
00155       old_weight = (1.0 - new_step)*gain;
00156       for (i = 0;  i < pitch_overlap;  i++) {
00157          amp[i] = fsaturate(old_weight*s->pitchbuf[s->pitch_offset] + new_weight*amp[i]);
00158          if (++s->pitch_offset >= s->pitch)
00159             s->pitch_offset = 0;
00160          new_weight += new_step;
00161          old_weight -= old_step;
00162          if (old_weight < 0.0)
00163             old_weight = 0.0;
00164       }
00165       s->missing_samples = 0;
00166    }
00167    save_history(s, amp, len);
00168    return len;
00169 }
00170 
00171 /*- End of function --------------------------------------------------------*/
00172 
00173 int plc_fillin(plc_state_t *s, int16_t amp[], int len)
00174 {
00175    int i;
00176    int pitch_overlap;
00177    float old_step;
00178    float new_step;
00179    float old_weight;
00180    float new_weight;
00181    float gain;
00182    int16_t *orig_amp;
00183    int orig_len;
00184 
00185    orig_amp = amp;
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 }
00241 
00242 /*- End of function --------------------------------------------------------*/
00243 
00244 plc_state_t *plc_init(plc_state_t *s)
00245 {
00246    memset(s, 0, sizeof(*s));
00247    return s;
00248 }
00249 /*- End of function --------------------------------------------------------*/
00250 /*- End of file ------------------------------------------------------------*/

Generated on Mon Mar 20 08:20:13 2006 for Asterisk - the Open Source PBX by  doxygen 1.3.9.1