home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / z / zfax22.zip / VCNVT.C < prev    next >
Text File  |  1993-01-05  |  11KB  |  301 lines

  1.  
  2. /**************************************************************************
  3.                         VCNVT.C
  4.  
  5.  Note : This file is for the voice file conversion between the ZyXEL 2, 3-bits
  6.         ADPCM format and the Sound Blaster .VOC file format. (9.6k sampling
  7.         uncompressed waveform)
  8.  
  9.  Usage : vcnvt  0  sfile dfile
  10.                 ^    ^     ^
  11.                 |    |     +--- Destination file path and name
  12.                 |    |
  13.                 |    +--- Source file path and name
  14.                 |
  15.                 +----- 0 : convert from .ZyXEL ADPCM to .VOC format
  16.                 +----- 1 : convert from .VOC to ZyXEL 2 bits ADPCM format
  17.                 +----- 2 : convert from .VOC to ZyXEL 3 bits ADPCM format
  18.  
  19.  Created by Lin Chinru, Jan. 5, 1993.  R&D Dep., ZyXEL Comm. Corp.
  20.  Copyright 1993, ZyXEL Communications Corporation
  21.  **************************************************************************/
  22. #include <stdio.h>
  23.  
  24. void ToVoc(int) ;
  25. void ToAdpcm(int) ;
  26. void Quit(int,char *) ;
  27. void Adaptive(char) ;
  28.  
  29.  
  30. char    Rcnt, CompBit ;
  31. FILE    *SourFh, *DestFh ;
  32.  
  33. int     Rdata, EstMax, Delta, BlkCnt, Data, Pack ;
  34. int     MaxTbl[] = { 0x399A, 0x3A9F, 0x4D14, 0x6607 } ;
  35.  
  36. /* ------------------------------------------------- */
  37. /* Reserved buffers for the ZyXEL ADPCM file header  */
  38. /* ------------------------------------------------- */
  39. char    ZheadBuf0[16] ;
  40. char    ZheadBuf1[16] = { 'Z','y','X','E','L',2,0,0,0,0,0,0,0,0,0,0 } ;
  41.  
  42. /* ------------------------------------------ */
  43. /* Reserved buffers for the .VOC file header  */
  44. /* ------------------------------------------ */
  45. char    VheadBuf0[32] ;
  46. char    VheadBuf1[32] = { 'C','r','e','a','t','i','v','e',' ',\
  47.                           'V','o','i','c','e',' ','F','i','l','e',\
  48.                           0x1a,0x1a,0,0x0a,1,0x29,0x11,\
  49.                           1,0x82,0x70,0,0x98,0 } ;
  50. char    VheadBuf2[4]  = { 2, 0x80, 0x70, 0 } ;
  51.  
  52. void main(int argc, char *argv[])
  53. {
  54.         /* ---------- Open file and check for legality -------------- */
  55.         if ( argc!=4 || (*argv[1]!='0' && *argv[1]!='1' && *argv[1]!='2') )
  56.                 Quit(0,"") ;
  57.         if ( (SourFh=fopen(argv[2],"rb")) == NULL )
  58.                 Quit(1,argv[2]) ;
  59.         if ( (DestFh=fopen(argv[3],"wb")) == NULL )
  60.                 Quit(1,argv[3]) ;
  61.  
  62.         /* ------------------ */
  63.         /* Initial parameters */
  64.         /* ------------------ */
  65.         Rdata   = 0 ;
  66.         Rcnt    = 8 ;
  67.         Delta   = 5 ;
  68.         EstMax  = 0 ;
  69.         BlkCnt  = 0x7080 ;
  70.  
  71.         /* -------------------------------- */
  72.         /*  ZyXEL ADPCM -> .VOC conversion  */
  73.         /* -------------------------------- */
  74.         if ( *argv[1]=='0' )       {
  75.                 fread( ZheadBuf0, sizeof(char), 16, SourFh) ;
  76.                 if ( strcmp( ZheadBuf0, ZheadBuf1, 6) || ZheadBuf0[20]==0 )
  77.                         Quit(2,argv[2]) ;
  78.                 fwrite( VheadBuf1, sizeof(char), 32, DestFh) ;
  79.                 CompBit = ZheadBuf0[10] ;
  80.                 if ( CompBit == 1 ) {
  81.                         MaxTbl[0] = 0x3800 ;
  82.                         MaxTbl[1] = 0x5600 ;
  83.                 }
  84.                 while ( (Data=getc(SourFh)) != EOF )    {
  85.                         if ( CompBit == 1 )     {
  86.                                 ToVoc((Data&0xc0)>>6) ; /* XX-- ---- */
  87.                                 ToVoc((Data&0x30)>>4) ; /* --XX ---- */
  88.                                 ToVoc((Data&0x0c)>>2) ; /* ---- XX-- */
  89.                                 ToVoc(Data&0x03) ;      /* ---- --XX */
  90.                         }
  91.                         else    {
  92.                                 ToVoc((Data&0xe0)>>5) ; /* XXX- ---- */
  93.                                 ToVoc((Data&0x1c)>>2) ; /* ---X XX-- */
  94.                                 Pack = (Data&0x03)<<1 ;
  95.                                 Data = getc(SourFh) ;
  96.                                 ToVoc(Pack|((Data&0x80)>>7)) ;
  97.                                 ToVoc((Data&0x70)>>4) ; /* -XXX ---- */
  98.                                 ToVoc((Data&0x0e)>>1) ; /* ---- XXX- */
  99.                                 Pack = (Data&0x01)<<2 ;
  100.                                 Data = getc(SourFh) ;
  101.                                 ToVoc(Pack|((Data&0xc0)>>6)) ;
  102.                                 ToVoc((Data&0x38)>>3) ; /* --XX X--- */
  103.                                 ToVoc(Data&0x07) ;      /* ---- -XXX */
  104.                         }
  105.                 }
  106.                 while ( BlkCnt-- )
  107.                         putc( 0x7f, DestFh) ;
  108.                 putc( 0, DestFh) ;
  109.         }
  110.  
  111.         /* -------------------------------- */
  112.         /*  .VOC -> ZyXEL ADPCM conversion  */
  113.         /* -------------------------------- */
  114.         else    {
  115.                 fread( VheadBuf0, sizeof(char), 26, SourFh) ;
  116.                 if ( strcmp( VheadBuf0, VheadBuf1, 22) )
  117.                         Quit(2,argv[2]) ;
  118.                 CompBit = *argv[1]-'0' ;
  119.                 if ( CompBit == 1 ) {
  120.                         MaxTbl[0] = 0x3800 ;
  121.                         MaxTbl[1] = 0x5600 ;
  122.                 }
  123.                 ZheadBuf1[10] = CompBit ;
  124.                 fwrite( ZheadBuf1, sizeof(char), 16, DestFh) ;
  125.  
  126.                 /* Pack = 0->not accepted, 1->read sampling rate
  127.                           2->read data, others->bypass          */
  128.                 while ( (Pack=getc(SourFh)) && (Pack!=EOF) ) {
  129.                         /* Read the block length */
  130.                         fread( &BlkCnt, sizeof(int), 1, SourFh) ;
  131.                         /* Bypass this byte */
  132.                         getc(SourFh) ;
  133.                         /* Read the sampling rate, 9600 is required */
  134.                         if ( Pack == 1 )        {
  135.                                 if ( (Data=getc(SourFh))<147 ||
  136.                                       Data>157 || getc(SourFh) )
  137.                                         Quit(2,argv[2]) ;
  138.                                 BlkCnt -= 2 ;
  139.                         }
  140.                         while ( BlkCnt-- )      {
  141.                                 Data = getc(SourFh) ;
  142.                                 if ( Pack <= 2 )
  143.                                         ToAdpcm(Data) ;
  144.                         }
  145.                 }
  146.         }
  147.         fclose(SourFh) ;
  148.         fclose(DestFh) ;
  149. }
  150.  
  151.  
  152. /**************************************************************************
  153.         ToVoc()
  154.  
  155.  
  156.  
  157.  Copyright 1992, ZyXEL Communications Corporation
  158.  ************************************************************************/
  159. void ToVoc(int Vdata)
  160. {
  161. int     Wdata ;
  162.  
  163.         Adaptive((char)Vdata) ;
  164.         if ( EstMax > 8191 )
  165.                 Wdata = 8191 ;
  166.         else if ( EstMax < -8192 )
  167.                 Wdata = -8192 ;
  168.         else
  169.                 Wdata = EstMax ;
  170.         putc( ((Wdata>>6)+128)&0xff, DestFh ) ;
  171.         if ( !(--BlkCnt) )      {
  172.                 fwrite( VheadBuf2, sizeof(char), 4, DestFh) ;
  173.                 BlkCnt  = 0x7080 ;
  174.         }
  175. }
  176.  
  177. /**************************************************************************
  178.         ToAdpcm()
  179.  
  180.  
  181.  
  182.  Copyright 1992, ZyXEL Communications Corporation
  183.  ************************************************************************/
  184. void ToAdpcm(int Edata)
  185. {
  186. char    TmpCompBit, DataBit ;
  187.  
  188.  
  189.         DataBit = 0 ;
  190.         Rdata  &= 0xff00 ;
  191.  
  192.         Edata -= 128 ;
  193.         Edata <<= 6 ;
  194.         Edata += 32 ;
  195.         /* Check for the waveform data and quantize this data */
  196.         if ( Edata -= EstMax )  {
  197.                 TmpCompBit = 2*CompBit ;
  198.                 /* ----------------------------------------------------- */
  199.                 /* If the data is negative, set flag and change the sign */
  200.                 /* ----------------------------------------------------- */
  201.                 if ( Edata < 0 ) {
  202.                         Edata = -Edata ;
  203.                         DataBit = TmpCompBit ;
  204.                 }
  205.                 /* --------------------------------------------------- */
  206.                 /* Quantize the waveform data, delta value is adaptive */
  207.                 /* --------------------------------------------------- */
  208.                 while ( ((Edata-=Delta)>0) && --TmpCompBit )
  209.                         DataBit += 1 ;
  210.                 /* ---------------------------- */
  211.                 /* Rdata is the compressed data */
  212.                 /* ---------------------------- */
  213.                 Rdata |= ( DataBit << (7-CompBit) ) ;
  214.         }
  215.         /* ------------------------------------------------------ */
  216.         /* check if the compressed data can be pack into one byte */
  217.         /* ------------------------------------------------------ */
  218.         TmpCompBit = CompBit+1 ;
  219.         while ( TmpCompBit-- )     {
  220.                 Rdata <<= 1 ;
  221.                 if ( !(--Rcnt) )    {
  222.                         putc(Rdata>>8,DestFh) ;
  223.                         Rcnt = 8 ;
  224.                 }
  225.         }
  226.         /* -------------------------------------- */
  227.         /* Adaptive the Delta and Estimat Maximum */
  228.         /* -------------------------------------- */
  229.         Adaptive(DataBit) ;
  230. }
  231.  
  232.  
  233. /**************************************************************************
  234.         Adaptive(DataBit, SignBit)
  235.  
  236.  
  237.  
  238.  Copyright 1992, ZyXEL Communications Corporation
  239.  ************************************************************************/
  240. void Adaptive(char DataBit)
  241. {
  242. int     TmpMax ;
  243. char    TmpData, SignBit ;
  244. long    TmpDelta ;
  245.  
  246.         SignBit = DataBit & (2*CompBit) ;
  247.         DataBit &= ~(2*CompBit) ;
  248.         if ( (Delta&1) && !SignBit )
  249.                 ++EstMax ;
  250.  
  251.         /* ------------------- */
  252.         /* Calculate the Delta */
  253.         /* ------------------- */
  254.         TmpDelta = Delta ;
  255.         TmpDelta *= MaxTbl[DataBit] ;
  256.         TmpDelta += 8192 ;
  257.         TmpDelta >>= 14 ;
  258.  
  259.         /* -------------------- */
  260.         /* Calculate the EstMax */
  261.         /* -------------------- */
  262.         TmpMax  = (Delta>>1) ;
  263.         while ( DataBit-- )
  264.                 TmpMax += Delta ;
  265.         if ( SignBit )
  266.                 EstMax -= TmpMax ;
  267.         else
  268.                 EstMax += TmpMax ;
  269.         Delta = (int)TmpDelta ;
  270. }
  271.  
  272. /**************************************************************************
  273.         Quit()
  274.  
  275.  
  276.  
  277.  Copyright 1992, ZyXEL Communications Corporation
  278.  ************************************************************************/
  279. void Quit(int Num, char *MsgStr)
  280. {
  281.         if ( Num == 0 ) {
  282.                 printf("Usage : vcnvt 0 sfile dfile.\n");
  283.                 printf("              ^   ^     ^\n") ;
  284.                 printf("              │   │     └ Destination file\n");
  285.                 printf("              │   └────── Source file\n") ;
  286.                 printf("              └────────── Convert type : \n") ;
  287.                 printf("    0 : convert from .ZyXEL ADPCM to .VOC format.\n") ;
  288.                 printf("    1 : convert from .VOC to ZyXEL 2 bits ADPCM.\n") ;
  289.                 printf("    2 : convert from .VOC to ZyXEL 3 bits ADPCM.\n") ;
  290.         }
  291.         else if ( Num == 1 )
  292.                 printf("Can't open %s\n", MsgStr) ;
  293.         else if ( Num == 2 )
  294.                 printf("File format error on %s\n", MsgStr) ;
  295.         exit(1) ;
  296. }
  297.  
  298.  
  299.  
  300.  
  301.