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

📄 sunplus_cdxafx_enc_2_0_3.cpp

📁 Enc 压 缩 wav文件为 A16格式的 文件
💻 CPP
📖 第 1 页 / 共 2 页
字号:
      int    i;
   
   	/* encoder variable */
      int len;
      int enErr1[CDXA_FRAME], enErr2[CDXA_FRAME];
      int errSum1, errSum2;
      int fourBit[CDXA_FRAME], fourBit1[CDXA_FRAME], fourBit2[CDXA_FRAME];
      int totalPoint, flages;
      int predictNr, shiftFactor;
      int predictNr1, predictNr2, shiftFactor1, shiftFactor2;
      int ps[CDXA_FILTER_MAX_ORDER], ps1[CDXA_FILTER_MAX_ORDER], ps2[CDXA_FILTER_MAX_ORDER], ks[CDXA_FILTER_MAX_ORDER];
      int word, byte;
      short frame[CDXA_FRAME];
   
   	/* I/O index */
      int    x_index = 0;
      int    y_index = 0;
   
   	/* encoder talbe */
      int T_ENCODE[CDXA_FILTER_MAX_SIZE][CDXA_FILTER_MAX_ORDER] = {
         {0, 0}, {60, 0}, {115, -52}, {98, -55}, {122, -60}, 
         {127, -64}, {64, 0}, {-64, 0}, {-75, -36}, {63, -6}, 
         {6, 19}, {54, -12}, {-24, -32}, {73, -21}, {-42, -15}, 
         {65, -3}, {23, -7}, {75, -14}, {10, -30}, {85, -34}, 
         {65, -33}, {97, -47}, {-9, -12}, {86, -26}, {39, 15}, 
         {79, -47}, {41, -27}, {98, -38}, {38, -50}, {111, -52} 
         };
      
      /* initial encoder variable */
      memset(ps, 0, sizeof(ps));
      memset(ps1, 0, sizeof(ps1));
      memset(ps2, 0, sizeof(ps2));
      memset(ks, 0, sizeof(ks));  
   	
   /****************** start encode *******************/
      while (size>0) {
         if ( (size-CDXA_FRAME)>0 ) {
            flages = 0;		/* normal frame */
            totalPoint = CDXA_FRAME - 1;
            for (i=0; i<CDXA_FRAME; ++i) frame[i] = (short)x[x_index++];
         }
         else { /* The last frame */
            flages = 1;
            totalPoint = size - 1;
            i = 0;
            while (size) {
               frame[i++] = (short)x[x_index++];
               size--;
            }
            for (; i<CDXA_FRAME; ++i) frame[i] = 0;
         }
      
         ps1[0] = ps[0];
         ps2[0] = ps[0];
         ps1[1] = ps[1];
         ps2[1] = ps[1];
         
      	/* noise sharpping */
         findPredict1(frame, enErr1, &predictNr1, &shiftFactor1, T_ENCODE, ps1);
         pack1(enErr1, &errSum1, fourBit1, predictNr1, shiftFactor1, T_ENCODE, ks);
         /* full search */
         findPredict2(frame, &predictNr2, shiftFactor1, &shiftFactor2, T_ENCODE, ps2);
         pack2(frame, enErr2, &errSum2, fourBit2, predictNr2, shiftFactor2 ,T_ENCODE, ps2);
      	
      	/* decision noise sharpping or full search */
         if (errSum1<errSum2) { /* noise sharpping */
            predictNr = predictNr1;
            shiftFactor = shiftFactor1;
            for (i=0; i<CDXA_FRAME; ++i)
               fourBit[i] = fourBit1[i] >> (16-CDXA_RESOLUTION);
            ps[0] = ps1[0];
            ps[1] = ps1[1];
         }
         else { /* full search */
            predictNr = predictNr2;
            shiftFactor = shiftFactor2;
            for (i=0; i<CDXA_FRAME; ++i)
               fourBit[i] = fourBit2[i];
            ps[0] = ps2[0];
            ps[1] = ps2[1];
         }
      	
      	/* generate bit stream */
         word = 0x00008000 + (flages*16384) + (totalPoint*512) + (predictNr*16) + shiftFactor;
         y[y_index++] = (double)(word & 0x000000FF);
         y[y_index++] = (double)(word >> 8);
      	
         len = (totalPoint+1) / 2;
         if (totalPoint%2 == 0) len++;
         for (i=0; i<len; ++i) {
            byte = fourBit[2*i];
            if ( (2*i+1)<=totalPoint )
               byte = fourBit[2*i+1]*16 + byte;
            y[y_index++] = (double)byte;
         }
      
         size -= CDXA_FRAME;	/* Next frame */
      }
      
      return y_index;
   }










#ifdef TEST

#include <math.h>

#include "wavestream.h"

   enum{
   BLOCK_SIZE = 10*1024,
   OUT_BUF_SIZE = BLOCK_SIZE+256
   };

    const char *get_outname(const char *srcpath)
   {
      char *p;
   
      p = strrchr(srcpath, '.');
      *p = '.';
      *(p+1) = 'v';
      *(p+2) = 'a';
      *(p+3) = 'g';
      *(p+4) = '\0';
   
      return srcpath;
   }

    int main(int argc, char *argv[])
   {
   /* file property */
      char fileout_name[64], out_buf[OUT_BUF_SIZE];
      int i, block_len, size;
      short pcm;
      FILE *fp;
   /* bitsream buffer */
      int T_ENCODE[CDXA_FILTER_MAX_SIZE][CDXA_FILTER_MAX_ORDER] = {
         {0, 0}, {60, 0}, {115, -52}, {98, -55}, {122, -60}, 
         {127, -64}, {64, 0}, {-64, 0}, {-75, -36}, {63, -6}, 
         {6, 19}, {54, -12}, {-24, -32}, {73, -21}, {-42, -15}, 
         {65, -3}, {23, -7}, {75, -14}, {10, -30}, {85, -34}, 
         {65, -33}, {97, -47}, {-9, -12}, {86, -26}, {39, 15}, 
         {79, -47}, {41, -27}, {98, -38}, {38, -50}, {111, -52} 
         };
      int len;			
      int ps[CDXA_FILTER_MAX_ORDER], ps1[CDXA_FILTER_MAX_ORDER], ps2[CDXA_FILTER_MAX_ORDER], ks[CDXA_FILTER_MAX_ORDER];
      short frame[CDXA_FRAME];
      int enErr1[CDXA_FRAME], enErr2[CDXA_FRAME];
      int errSum1, errSum2;
      int fourBit[CDXA_FRAME], fourBit1[CDXA_FRAME], fourBit2[CDXA_FRAME];
      int totalPoint, flages;
      int predictNr, shiftFactor;
      int predictNr1, predictNr2, shiftFactor1, shiftFactor2;
      int word, byte;
   
   
      if (argc > 2) {
         printf("Usage: %s [wavfile]\n",argv[0]);
         return 1;
      }
      strcpy(fileout_name, argv[1]);
      fp = fopen( get_outname(fileout_name) ,"wb");
   
   /* Read Wavefile */
      wavestream ip(argv[1]);
      ip.start_read();
      size = ip.getSpeechLength();
   /* Initial ADPCM36 Encoder */   
      /* initial encoder variable */
      memset(ps, 0, sizeof(ps));
      memset(ps1, 0, sizeof(ps1));
      memset(ps2, 0, sizeof(ps2));
      memset(ks, 0, sizeof(ks));
      block_len = 0;
   
      while (size>0) {
      
         if ( (size-CDXA_FRAME)>0 ) {
            flages = 0;		/* normal frame */
            totalPoint = CDXA_FRAME - 1;
            ip.read(frame, CDXA_FRAME);
         }
         else { /* The last frame */
            flages = 1;
            totalPoint = size - 1;
            i = 0;
            while (size) {
               ip.read(&pcm, 1);
               frame[i++] = (int)(pcm);
               size--;
            }
            for (; i<CDXA_FRAME; ++i) frame[i] = 0;
         }
      
         ps1[0] = ps[0];
         ps2[0] = ps[0];
         ps1[1] = ps[1];
         ps2[1] = ps[1];
         
      	/* noise sharpping */
         findPredict1(frame, enErr1, &predictNr1, &shiftFactor1, T_ENCODE, ps1);
         pack1(enErr1, &errSum1, fourBit1, predictNr1, shiftFactor1, T_ENCODE, ks);
         /* full search */
         findPredict2(frame, &predictNr2, shiftFactor1, &shiftFactor2, T_ENCODE, ps2);
         pack2(frame, enErr2, &errSum2, fourBit2, predictNr2, shiftFactor2 ,T_ENCODE, ps2);
      	
      	/* decision noise sharpping or full search */
         if (errSum1<errSum2) { /* noise sharpping */
            predictNr = predictNr1;
            shiftFactor = shiftFactor1;
            for (i=0; i<CDXA_FRAME; ++i)
               fourBit[i] = fourBit1[i] >> (16-CDXA_RESOLUTION);
            ps[0] = ps1[0];
            ps[1] = ps1[1];
         }
         else { /* full search */
            predictNr = predictNr2;
            shiftFactor = shiftFactor2;
            for (i=0; i<CDXA_FRAME; ++i)
               fourBit[i] = fourBit2[i];
            ps[0] = ps2[0];
            ps[1] = ps2[1];
         }
      	
      	/* generate bit stream */
         word = 0x00008000 + (flages*16384) + (totalPoint*512) + (predictNr*16) + shiftFactor;
         out_buf[block_len+0] = (unsigned char)(word & 0x000000FF);
         out_buf[block_len+1] = (unsigned char)(word >> 8);
      	
         len = (totalPoint+1) / 2;
         if (totalPoint%2 == 0) len++;
         for (i=0; i<len; ++i) {
            byte = fourBit[2*i];
            if ( (2*i+1)<=totalPoint )
               byte = fourBit[2*i+1]*16 + byte;
            out_buf[block_len+2+i] = (unsigned char)byte;
         }
         block_len += (len+2);
      
        /* save block to file */
         if (block_len > BLOCK_SIZE) {
            fwrite(out_buf, 1, block_len, fp);
            block_len = 0;
         }
      
      	/* Next frame */
         size -= CDXA_FRAME;	/* Next frame */
      }
   
   /* save rest of data */
      if (block_len != 0)
         fwrite(out_buf, 1, block_len, fp);
   
   
      fclose(fp);
   
      return 0;
   }

#endif /* TEST */










#ifdef TEST_MATLAB

#include <math.h>

#include "wavestream.h"

   enum {
   MAX_FILE_SIZE = 2*1024*1024,
   T = 10					/* period */
   };

    int main(int argc, char *argv[])
   {
      double *speech;
      double *bitstream;
      int spSize, bitSize;
      int i;
      char byte;
      short pcm;
      FILE *fp;
   
      if (argc > 2) {
         printf("Usage: %s [wavfile]\n",argv[0]);
         return 1;
      }
   
      speech = (double*)malloc( MAX_FILE_SIZE*sizeof(double) );
      bitstream = (double*)malloc( MAX_FILE_SIZE*sizeof(double) );
   
      if (argc==1) {
         /* Fixed Pattern */
         spSize = 256 * T;
         for (i=0; i<spSize; ++i)
            speech[i] = floor(32767.0*sin( 2.0*3.1415926*i/256.0)+0.5);
      } 
      else {
         /* Random Pattern */
         wavestream ip(argv[1]);
         ip.start_read();
         spSize = ip.getSpeechLength();
         for (i=0; i<spSize; ++i) {
            ip.read(&pcm, 1);
            speech[i] = (double)pcm;
         }
      }
   
   	/* encode */
      bitSize = sunplus_cdxaFx_enc_v203(bitstream, speech, spSize);
      if ( bitSize>MAX_FILE_SIZE ){
         printf("Error!! Input file too large.\n");
         return 1;
      }
      fp = fopen("test.vag","wb");
      for (i=0; i<bitSize; ++i) {
         byte = (char)bitstream[i];
         fwrite(&byte, sizeof(char), 1, fp);
      }
      fclose(fp);
   		
      free(speech);
      free(bitstream);
   
      return 0;
   }

#endif /* TEST_MATLAB */

⌨️ 快捷键说明

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