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

📄 cvsd.c

📁 蓝牙技术中最常用的语言编码程序,适合用各种系列的DSP开发CVSD编解码程序.
💻 C
📖 第 1 页 / 共 2 页
字号:
                                   ENC_FILTERLEN);
                /*
                 * encode one bit
                 */
                p->com.overload = (((p->com.overload << 1) |
                                    (inval >  p->c.enc.recon_int)) & 7);
                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;
                if (p->com.mla_int > p->com.v_max)
                        p->com.v_max = p->com.mla_int;
                if (p->com.mla_int < p->com.v_min)
                        p->com.v_min = p->com.mla_int;
                if (p->com.overload & 1) {
                        p->c.enc.recon_int += p->com.mla_int;
                        p->bit.shreg |= p->bit.mask;
                } else
                        p->c.enc.recon_int -= p->com.mla_int;
                if ((++(p->bit.cnt)) >= 8) {
                        sox_writeb(ft, p->bit.shreg);
                        p->bytes_written++;
                        p->bit.shreg = p->bit.cnt = 0;
                        p->bit.mask = 1;
                } else
                        p->bit.mask <<= 1;
                p->com.phase += p->com.phase_inc;
                sox_debug_more("input %d %f\n", debug_count, inval);
                sox_debug_more("recon %d %f\n", debug_count, p->c.enc.recon_int);
                debug_count++;
        }
}

/* ---------------------------------------------------------------------- */
/*
 * DVMS file header
 */
struct dvms_header {
        char          Filename[14];
        unsigned      Id;
        unsigned      State;
        time_t        Unixtime;
        unsigned      Usender;
        unsigned      Ureceiver;
        sox_size_t     Length;
        unsigned      Srate;
        unsigned      Days;
        unsigned      Custom1;
        unsigned      Custom2;
        char          Info[16];
        char          extend[64];
        unsigned      Crc;
};

#define DVMS_HEADER_LEN 120

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

static int dvms_read_header(ft_t ft, struct dvms_header *hdr)
{
        unsigned char hdrbuf[DVMS_HEADER_LEN];
        unsigned char *pch = hdrbuf;
        int i;
        unsigned sum;

        if (sox_readbuf(ft, hdrbuf, sizeof(hdrbuf), 1) != 1)
        {
                return (SOX_EOF);
        }
        for(i = sizeof(hdrbuf), sum = 0; i > /*2*/3; i--) /* Deti bug */
                sum += *pch++;
        pch = hdrbuf;
        memcpy(hdr->Filename, pch, sizeof(hdr->Filename));
        pch += sizeof(hdr->Filename);
        hdr->Id = get16_le(&pch);
        hdr->State = get16_le(&pch);
        hdr->Unixtime = get32_le(&pch);
        hdr->Usender = get16_le(&pch);
        hdr->Ureceiver = get16_le(&pch);
        hdr->Length = get32_le(&pch);
        hdr->Srate = get16_le(&pch);
        hdr->Days = get16_le(&pch);
        hdr->Custom1 = get16_le(&pch);
        hdr->Custom2 = get16_le(&pch);
        memcpy(hdr->Info, pch, sizeof(hdr->Info));
        pch += sizeof(hdr->Info);
        memcpy(hdr->extend, pch, sizeof(hdr->extend));
        pch += sizeof(hdr->extend);
        hdr->Crc = get16_le(&pch);
        if (sum != hdr->Crc) 
        {
                sox_report("DVMS header checksum error, read %u, calculated %u",
                     hdr->Crc, sum);
                return (SOX_EOF);
        }
        return (SOX_SUCCESS);
}

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

/*
 * note! file must be seekable
 */
static int dvms_write_header(ft_t ft, struct dvms_header *hdr)
{
        unsigned char hdrbuf[DVMS_HEADER_LEN];
        unsigned char *pch = hdrbuf;
        unsigned char *pchs = hdrbuf;
        int i;
        unsigned sum;

        memcpy(pch, hdr->Filename, sizeof(hdr->Filename));
        pch += sizeof(hdr->Filename);
        put16_le(&pch, hdr->Id);
        put16_le(&pch, hdr->State);
        put32_le(&pch, (unsigned)hdr->Unixtime);
        put16_le(&pch, hdr->Usender);
        put16_le(&pch, hdr->Ureceiver);
        put32_le(&pch, hdr->Length);
        put16_le(&pch, hdr->Srate);
        put16_le(&pch, hdr->Days);
        put16_le(&pch, hdr->Custom1);
        put16_le(&pch, hdr->Custom2);
        memcpy(pch, hdr->Info, sizeof(hdr->Info));
        pch += sizeof(hdr->Info);
        memcpy(pch, hdr->extend, sizeof(hdr->extend));
        pch += sizeof(hdr->extend);
        for(i = sizeof(hdrbuf), sum = 0; i > /*2*/3; i--) /* Deti bug */
                sum += *pchs++;
        hdr->Crc = sum;
        put16_le(&pch, hdr->Crc);
        if (sox_seeki(ft, 0, SEEK_SET) < 0)
        {
                sox_report("seek failed\n: %s",strerror(errno));
                return (SOX_EOF);
        }
        if (sox_writebuf(ft, hdrbuf, sizeof(hdrbuf), 1) != 1)
        {
                sox_report("%s",strerror(errno));
                return (SOX_EOF);
        }
        return (SOX_SUCCESS);
}

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

static void make_dvms_hdr(ft_t ft, struct dvms_header *hdr)
{
        struct cvsdpriv *p = (struct cvsdpriv *) ft->priv;
        size_t len;

        memset(hdr->Filename, 0, sizeof(hdr->Filename));
        len = strlen(ft->filename);
        if (len >= sizeof(hdr->Filename))
                len = sizeof(hdr->Filename)-1;
        memcpy(hdr->Filename, ft->filename, len);
        hdr->Id = hdr->State = 0;
        hdr->Unixtime = time(NULL);
        hdr->Usender = hdr->Ureceiver = 0;
        hdr->Length = p->bytes_written;
        hdr->Srate = p->cvsd_rate/100;
        hdr->Days = hdr->Custom1 = hdr->Custom2 = 0;
        memset(hdr->Info, 0, sizeof(hdr->Info));
        len = strlen(ft->comment);
        if (len >= sizeof(hdr->Info))
                len = sizeof(hdr->Info)-1;
        memcpy(hdr->Info, ft->comment, len);
        memset(hdr->extend, 0, sizeof(hdr->extend));
}

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

static int sox_dvmsstartread(ft_t ft) 
{
        struct dvms_header hdr;
        int rc;

        rc = dvms_read_header(ft, &hdr);
        if (rc){
            sox_fail_errno(ft,SOX_EHDR,"unable to read DVMS header");
            return rc;
        }

        sox_debug("DVMS header of source file \"%s\":");
        sox_debug("  filename  \"%.14s\"",ft->filename);
        sox_debug("  id        0x%x", hdr.Filename);
        sox_debug("  state     0x%x", hdr.Id, hdr.State);
        sox_debug("  time      %s",ctime(&hdr.Unixtime)); /* ctime generates lf */
        sox_debug("  usender   %u", hdr.Usender);
        sox_debug("  ureceiver %u", hdr.Ureceiver);
        sox_debug("  length    %u", hdr.Length);
        sox_debug("  srate     %u", hdr.Srate);
        sox_debug("  days      %u", hdr.Days);
        sox_debug("  custom1   %u", hdr.Custom1);
        sox_debug("  custom2   %u", hdr.Custom2);
        sox_debug("  info      \"%.16s\"", hdr.Info);
        ft->signal.rate = (hdr.Srate < 240) ? 16000 : 32000;
        sox_debug("DVMS rate %dbit/s using %dbit/s deviation %d%%", 
               hdr.Srate*100, ft->signal.rate, 
               ((ft->signal.rate - hdr.Srate*100) * 100) / ft->signal.rate);
        rc = sox_cvsdstartread(ft);
        if (rc)
            return rc;

        return(SOX_SUCCESS);
}

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

static int sox_dvmsstartwrite(ft_t ft) 
{
        struct dvms_header hdr;
        int rc;
        
        rc = sox_cvsdstartwrite(ft);
        if (rc)
            return rc;

        make_dvms_hdr(ft, &hdr);
        rc = dvms_write_header(ft, &hdr);
        if (rc){
                sox_fail_errno(ft,rc,"cannot write DVMS header");
            return rc;
        }

        if (!ft->seekable)
               sox_warn("Length in output .DVMS header will wrong since can't seek to fix it");

        return(SOX_SUCCESS);
}

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

static int sox_dvmsstopwrite(ft_t ft)
{
        struct dvms_header hdr;
        int rc;
        
        sox_cvsdstopwrite(ft);
        if (!ft->seekable)
        {
            sox_warn("File not seekable");
            return (SOX_EOF);
        }
        if (sox_seeki(ft, 0, 0) != 0)
        {
                sox_fail_errno(ft,errno,"Can't rewind output file to rewrite DVMS header.");
                return(SOX_EOF);
        }
        make_dvms_hdr(ft, &hdr);
        rc = dvms_write_header(ft, &hdr);
        if(rc){
            sox_fail_errno(ft,rc,"cannot write DVMS header");
            return rc;
        }       
        return rc;
}

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

/* Cont. Variable Slope Delta */
static const char *cvsdnames[] = {
  "cvs",
  "cvsd",
  NULL
};

static sox_format_t sox_cvsd_format = {
  cvsdnames,
  NULL,
  0,
  sox_cvsdstartread,
  sox_cvsdread,
  sox_cvsdstopread,
  sox_cvsdstartwrite,
  sox_cvsdwrite,
  sox_cvsdstopwrite,
  sox_format_nothing_seek
};

const sox_format_t *sox_cvsd_format_fn(void)
{
    return &sox_cvsd_format;
}
/* Cont. Variable Solot Delta */
static const char *dvmsnames[] = {
  "vms",
  "dvms",
  NULL
};

static sox_format_t sox_dvms_format = {
  dvmsnames,
  NULL,
  0,
  sox_dvmsstartread,
  sox_cvsdread,
  sox_cvsdstopread,
  sox_dvmsstartwrite,
  sox_cvsdwrite,
  sox_dvmsstopwrite,
  sox_format_nothing_seek
};

const sox_format_t *sox_dvms_format_fn(void)
{
    return &sox_dvms_format;
}

⌨️ 快捷键说明

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