⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cvsd.c

📁 蓝牙技术中最常用的语言编码程序,适合用各种系列的DSP开发CVSD编解码程序.
💻 C
📖 第 1 页 / 共 2 页
字号:
/*
 *      CVSD (Continuously Variable Slope Delta modulation)
 *      conversion routines
 *
 *      The CVSD format is described in the MIL Std 188 113, which is
 *      available from http://bbs.itsi.disa.mil:5580/T3564
 *
 *      Copyright (C) 1996  
 *      Thomas Sailer (sailer@ife.ee.ethz.ch) (HB9JNX/AE4WA)
 *      Swiss Federal Institute of Technology, Electronics Lab
 *
 *   This library is free software; you can redistribute it and/or
 *   modify it under the terms of the GNU Lesser General Public
 *   License as published by the Free Software Foundation; either
 *   version 2 of the License, or (at your option) any later version.
 *
 *   This library is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *   Lesser General Public License for more details.
 *
 *   You should have received a copy of the GNU Lesser General Public
 *   License along with this library; if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Change History:
 *
 * June 1, 1998 - Chris Bagwell (cbagwell@sprynet.com)
 *   Fixed compile warnings reported by Kjetil Torgrim Homme
 *   <kjetilho@ifi.uio.no>
 *
 *
 */

/* ---------------------------------------------------------------------- */

#include "sox_i.h"

#include <math.h>
#include <string.h>
#include <time.h>
#include <stdio.h>
#include <errno.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>     /* For SEEK_* defines if not found in stdio */
#endif

#include "cvsdfilt.h"

/* ---------------------------------------------------------------------- */
/*
 * private data structures
 */

struct cvsd_common_state {
        unsigned overload;
        float mla_int;
        float mla_tc0;
        float mla_tc1;
        unsigned phase;
        unsigned phase_inc;
        float v_min, v_max;
};

struct cvsd_decode_state {
        float output_filter[DEC_FILTERLEN];
};

struct cvsd_encode_state {
        float recon_int;
        float input_filter[ENC_FILTERLEN];
};

struct cvsdpriv {
        struct cvsd_common_state com;
        union {
                struct cvsd_decode_state dec;
                struct cvsd_encode_state enc;
        } c;
        struct {
                unsigned char shreg;
                unsigned mask;
                unsigned cnt;
        } bit;
        unsigned bytes_written;
        unsigned cvsd_rate;
};

static int debug_count = 0;

/* ---------------------------------------------------------------------- */

static float float_conv(float *fp1, float *fp2,int n)
{
        float res = 0;
        for(; n > 0; n--)
                res += (*fp1++) * (*fp2++);
        return res;
}

/* ---------------------------------------------------------------------- */
/*
 * some remarks about the implementation of the CVSD decoder
 * the principal integrator is integrated into the output filter
 * to achieve this, the coefficients of the output filter are multiplied
 * with (1/(1-1/z)) in the initialisation code.
 * the output filter must have a sharp zero at f=0 (i.e. the sum of the
 * filter parameters must be zero). This prevents an accumulation of
 * DC voltage at the principal integration.
 */
/* ---------------------------------------------------------------------- */

static void cvsdstartcommon(ft_t ft)
{
        struct cvsdpriv *p = (struct cvsdpriv *) ft->priv;
        
        p->cvsd_rate = (ft->signal.rate <= 24000) ? 16000 : 32000;
        ft->signal.rate = 8000;
        ft->signal.channels = 1;
        ft->signal.size = SOX_SIZE_16BIT; /* make output format default to words */
        ft->signal.encoding = SOX_ENCODING_SIGN2;
        /*
         * initialize the decoder
         */
        p->com.overload = 0x5;
        p->com.mla_int = 0;
        /*
         * timeconst = (1/e)^(200 / SR) = exp(-200/SR)
         * SR is the sampling rate
         */
        p->com.mla_tc0 = exp((-200.0)/((float)(p->cvsd_rate)));
        /*
         * phase_inc = 32000 / SR
         */
        p->com.phase_inc = 32000 / p->cvsd_rate;
        /*
         * initialize bit shift register
         */
        p->bit.shreg = p->bit.cnt = 0;
        p->bit.mask = 1;
        /*
         * count the bytes written
         */
        p->bytes_written = 0;
        p->com.v_min = 1;
        p->com.v_max = -1;
        sox_report("cvsd: bit rate %dbit/s, bits from %s", p->cvsd_rate,
               ft->signal.reverse_bits ? "msb to lsb" : "lsb to msb");
}

/* ---------------------------------------------------------------------- */

static int sox_cvsdstartread(ft_t ft) 
{
        struct cvsdpriv *p = (struct cvsdpriv *) ft->priv;
        float *fp1;
        int i;

        cvsdstartcommon(ft);

        p->com.mla_tc1 = 0.1 * (1 - p->com.mla_tc0);
        p->com.phase = 0;
        /*
         * initialize the output filter coeffs (i.e. multiply
         * the coeffs with (1/(1-1/z)) to achieve integration
         * this is now done in the filter parameter generation utility
         */
        /*
         * zero the filter 
         */
        for(fp1 = p->c.dec.output_filter, i = DEC_FILTERLEN; i > 0; i--)
                *fp1++ = 0;

        return (SOX_SUCCESS);
}

/* ---------------------------------------------------------------------- */

static int sox_cvsdstartwrite(ft_t ft) 
{
        struct cvsdpriv *p = (struct cvsdpriv *) ft->priv;
        float *fp1;
        int i;

        cvsdstartcommon(ft);

        p->com.mla_tc1 = 0.1 * (1 - p->com.mla_tc0);
        p->com.phase = 4;
        /*
         * zero the filter 
         */
        for(fp1 = p->c.enc.input_filter, i = ENC_FILTERLEN; i > 0; i--)
                *fp1++ = 0;
        p->c.enc.recon_int = 0;

        return(SOX_SUCCESS);
}

/* ---------------------------------------------------------------------- */

static int sox_cvsdstopwrite(ft_t ft)
{
        struct cvsdpriv *p = (struct cvsdpriv *) ft->priv;

        if (p->bit.cnt) {
                sox_writeb(ft, p->bit.shreg);
                p->bytes_written++;
        }
        sox_debug("cvsd: min slope %f, max slope %f", 
               p->com.v_min, p->com.v_max);     

        return (SOX_SUCCESS);
}

/* ---------------------------------------------------------------------- */

static int sox_cvsdstopread(ft_t ft)
{
        struct cvsdpriv *p = (struct cvsdpriv *) ft->priv;

        sox_debug("cvsd: min value %f, max value %f", 
               p->com.v_min, p->com.v_max);

        return(SOX_SUCCESS);
}

/* ---------------------------------------------------------------------- */

static sox_size_t sox_cvsdread(ft_t ft, sox_sample_t *buf, sox_size_t nsamp) 
{
        struct cvsdpriv *p = (struct cvsdpriv *) ft->priv;
        sox_size_t done = 0;
        float oval;
        
        while (done < nsamp) {
                if (!p->bit.cnt) {
                        if (sox_readb(ft, &(p->bit.shreg)) == SOX_EOF)
                                return done;
                        p->bit.cnt = 8;
                        p->bit.mask = 1;
                }
                /*
                 * handle one bit
                 */
                p->bit.cnt--;
                p->com.overload = ((p->com.overload << 1) | 
                                   (!!(p->bit.shreg & p->bit.mask))) & 7;
                p->bit.mask <<= 1;
                p->com.mla_int *= p->com.mla_tc0;
                if ((p->com.overload == 0) || (p->com.overload == 7))
                        p->com.mla_int += p->com.mla_tc1;
                memmove(p->c.dec.output_filter+1, p->c.dec.output_filter,
                        sizeof(p->c.dec.output_filter)-sizeof(float));
                if (p->com.overload & 1)
                        p->c.dec.output_filter[0] = p->com.mla_int;
                else
                        p->c.dec.output_filter[0] = -p->com.mla_int;
                /*
                 * check if the next output is due
                 */
                p->com.phase += p->com.phase_inc;
                if (p->com.phase >= 4) {
                        oval = float_conv(p->c.dec.output_filter, 
                                          (p->cvsd_rate < 24000) ? 
                                          dec_filter_16 : dec_filter_32, 
                                          DEC_FILTERLEN);
                        sox_debug_more("input %d %f\n", debug_count, p->com.mla_int);
                        sox_debug_more("recon %d %f\n", debug_count, oval);
                        debug_count++;

                        if (oval > p->com.v_max)
                                p->com.v_max = oval;
                        if (oval < p->com.v_min)
                                p->com.v_min = oval;
                        *buf++ = (oval * ((float)SOX_SAMPLE_MAX));
                        done++;
                }
                p->com.phase &= 3;
        }
        return done;
}

/* ---------------------------------------------------------------------- */

static sox_size_t sox_cvsdwrite(ft_t ft, const sox_sample_t *buf, sox_size_t nsamp) 
{
        struct cvsdpriv *p = (struct cvsdpriv *) ft->priv;
        sox_size_t done = 0;
        float inval;

        for(;;) {
                /*
                 * check if the next input is due
                 */
                if (p->com.phase >= 4) {
                        if (done >= nsamp)
                                return done;
                        memmove(p->c.enc.input_filter+1, p->c.enc.input_filter,
                                sizeof(p->c.enc.input_filter)-sizeof(float));
                        p->c.enc.input_filter[0] = (*buf++) / 
                                ((float)SOX_SAMPLE_MAX);
                        done++;
                }
                p->com.phase &= 3;
                /* insert input filter here! */
                inval = float_conv(p->c.enc.input_filter, 
                                   (p->cvsd_rate < 24000) ? 
                                   (enc_filter_16[(p->com.phase >= 2)]) : 
                                   (enc_filter_32[p->com.phase]), 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -