home *** CD-ROM | disk | FTP | other *** search
- #include <stdio.h>
- #include <process.h>
- #include <stdlib.h>
- #include <io.h>
-
- #include "cache.h"
-
- int infile,
- outfile,
- samplerate;
- unsigned char header[44];
-
- void compressing()
- {
- unsigned long up=0,
- tmed=0,
- med=0,
- down=0,
- compcik,
- infilelength,
- pp,
- prp;
- unsigned char top,
- presbuf,
- prevbuf,
- prevfunc=2,
- convbuf,
- headercik;
-
- readablecache cachein(infile);
- writeablecache cacheout(outfile);
- if(cachein.status==BADCACHE||cacheout.status==BADCACHE)
- {
- printf("Not enough memory for caches!!\n\n");
- exit(EXIT_FAILURE);
- }
- if(cachein.status==BADCACHE||cacheout.status==BADCACHE)
- {
- printf("Disk error!!\n\n");
- exit(EXIT_FAILURE);
- }
- infilelength=filelength(infile)-1;
- printf("Please wait...\n");
-
- for(headercik=0;headercik<44;headercik++)
- {
- header[headercik]=cachein.readitem();
- if(cachein.status==BADCACHE)
- {
- printf("Read error in header checking!\n\n");
- exit(EXIT_FAILURE);
- }
- }
- prevbuf=cachein.readitem();
- cacheout.writeitem(prevbuf);
- pp=infilelength/1000;
- prp=pp;
- for(compcik=0;compcik<infilelength;compcik++)
- {
- presbuf=cachein.readitem();
- recucc:
- if(presbuf<prevbuf)
- {
- if(up!=0&&down==0&&med==0)
- {
- med=tmed;
- tmed=0;
- top=prevbuf;
- }
- if(prevfunc==1&&down!=0)
- {
- while(down)
- {
- if(down>63)
- {
- convbuf=127;
- down-=63;
- }
- else
- {
- convbuf=(char) down;
- if(med) convbuf|=128;
- down=0;
- }
- cacheout.writeitem(convbuf);
- }
- cacheout.writeitem(top);
- if(med)
- {
- while(med)
- {
- if(med>63)
- {
- convbuf=127;
- med-=63;
- }
- else
- {
- convbuf=(char) med;
- med=0;
- }
- cacheout.writeitem(convbuf);
- }
- }
- while(up)
- {
- if(up>63)
- {
- convbuf=127;
- up-=63;
- }
- else
- {
- convbuf=(char) up;
- up=0;
- }
- cacheout.writeitem(convbuf);
- }
- cacheout.writeitem(prevbuf);
- goto recucc;
- }
- ++down+=tmed;
- prevfunc=0;
- tmed=0;
- }
- if(presbuf>prevbuf)
- {
- if(down!=0&&up==0&&med==0)
- {
- med=tmed;
- tmed=0;
- top=prevbuf;
- }
- if(prevfunc==0&&up!=0)
- {
- while(up)
- {
- if(up>63)
- {
- convbuf=127;
- up-=63;
- }
- else
- {
- convbuf=(char) up;
- if(med) convbuf|=128;
- up=0;
- }
- cacheout.writeitem(convbuf);
- }
- cacheout.writeitem(top);
- if(med)
- {
- while(med)
- {
- if(med>63)
- {
- convbuf=127;
- med-=63;
- }
- else
- {
- convbuf=(char) med;
- med=0;
- }
- cacheout.writeitem(convbuf);
- }
- }
- while(down)
- {
- if(down>63)
- {
- convbuf=127;
- down-=63;
- }
- else
- {
- convbuf=(char) down;
- down=0;
- }
- cacheout.writeitem(convbuf);
- }
- cacheout.writeitem(prevbuf);
- goto recucc;
- }
- ++up+=tmed;
- prevfunc=1;
- tmed=0;
- }
- if(presbuf==prevbuf) tmed++;
- prevbuf=presbuf;
- prp++;
- if(prp>pp||infilelength-2<compcik)
- {
- prp=0;
- printf("\r%.1f%% compressed.",((double)compcik*100/infilelength));
- }
- }
- cacheout.~writeablecache();
- cachein.~readablecache();
- printf("\nCompressing has successful.\n");
-
- long iflength=filelength(infile),
- oflength=filelength(outfile);
-
- printf("The wav file size:%li bytes\n",iflength);
- printf("The dgm file size:%li bytes\n",oflength);
- printf("Compressed ratio:%f%%",((float)oflength/iflength)*100);
- }
-
- void decompressing()
- {
- long flength,
- filelengthcik,
- extractcount,
- extractcik,
- pp,
- prp,
- exc;
- int data;
- unsigned char equ=0,
- presbuf,
- prevbuf,
- equbuf;
-
- readablecache cachein(infile);
- writeablecache cacheout(outfile);
- if(cachein.status==BADCACHE||cacheout.status==BADCACHE)
- {
- printf("Cache memory error!!\n\n");
- exit(EXIT_FAILURE);
- }
- printf("Please wait...\n");
- prevbuf=cachein.readitem();
- cacheout.writeitem(prevbuf);
- flength=filelength(infile)-1;
- pp=flength/1000;
- prp=pp;
- for(filelengthcik=0;filelengthcik<flength;filelengthcik++)
- {
- extractcount=0;
- rread:
- presbuf=cachein.readitem();
- if((presbuf&64)!=0)
- {
- presbuf-=64;
- extractcount+=(long) presbuf;
- filelengthcik++;
- goto rread;
- }
- if((presbuf&128)!=0)
- {
- equ=1;
- presbuf-=128;
- }
- extractcount+=(long) presbuf;
- presbuf=cachein.readitem();
- filelengthcik++;
- data=presbuf-prevbuf;
- for(extractcik=1;extractcik<=extractcount;extractcik++)
- {
- exc=((long)data*extractcik/extractcount);
- cacheout.writeitem(prevbuf+(char)exc);
- }
- if(equ)
- {
- equ=0;
- extractcount=0;
- rreadd:
- equbuf=cachein.readitem();
- filelengthcik++;
- if((equbuf&64)!=0)
- {
- equbuf-=64;
- extractcount+=(long) equbuf;
- goto rreadd;
- }
- extractcount+=(long) equbuf;
- for(extractcik=1;extractcik<=extractcount;extractcik++) cacheout.writeitem(presbuf);
- }
- prevbuf=presbuf;
- prp++;
- if(pp<=prp||flength-2<filelengthcik)
- {
- prp=0;
- printf("\r%.1f%% decompressed.",((double)filelengthcik*100/flength));
- }
- }
- printf("\nDecompressing has successful.\n");
- cacheout.~writeablecache();
- cachein.~readablecache();
-
- long iflength=filelength(infile),
- oflength=filelength(outfile);
-
- printf("The dgm file size:%li bytes\n",iflength);
- printf("The wav file size:%li bytes",oflength);
- }
-
- void main(int parnum,char *pars[])
- {
- printf("Digi Compresser v1.0ß by Psycho/TSI (C) '93\n\n");
- if(parnum!=4)
- {
- printf("Too ");
- if(parnum<4) printf("few ");
- else printf("much ");
- exiting:
- printf("parameters!!\n");
- exits:
- printf("USAGE: dcomp <command> <infile> <outfile>\n");
- printf(" commands: d - convert wav to dgm\n");
- printf(" w - convert dgm to wav\n\n");
- exit(EXIT_FAILURE);
- foperror:
- printf("File opening error!!\n");
- goto exits;
- }
- if(*pars[1]=='d')
- {
- if((infile=open(pars[2],O_RDONLY|O_BINARY))==-1||(outfile=open(pars[3],O_RDWR|O_BINARY|O_CREAT|O_TRUNC,S_IREAD|S_IWRITE))==-1)
- goto foperror;
- compressing();
- }
- else
- if(*pars[1]!='w')
- {
- printf("Bad ");
- goto exiting;
- }
- else
- {
- if((infile=open(pars[2],O_RDONLY|O_BINARY))==-1||(outfile=open(pars[3],O_RDWR|O_CREAT|O_TRUNC|O_BINARY,S_IREAD|S_IWRITE))==-1) goto foperror;
- decompressing();
- }
- printf("\n\n");
- exit(EXIT_FAILURE);
- }
-