#include <stdio.h>
#include <stdlib.h>
#include <alloc.h>
#include <mem.h>

#include <sbc.h>
#include <sbcvoice.h>

#include <loaddrv.c>
#include "sb_voice.c"

unsigned char *buffer1,*buffer2;
FILE* fp;


struct HdrStruc {
       unsigned char id[20];
       unsigned int vocoffs;
       unsigned int version;
       unsigned int checkcode;

       unsigned char extblktype;
       unsigned char ext_blklen[3];
       unsigned int  ext_tc;
       unsigned char ext_pack;
       unsigned char ext_mode;

       unsigned char voiceblktype;
       unsigned char voice_blklen[3];
       unsigned char voice_tc;
       unsigned char voice_pack;
};


main(int argc, char **argv)
{
unsigned int samprate,buflen;
unsigned int numread;

       if(argc > 2)
       {
               if(sb_voice_init())
                       exit(1);

               if( (buffer1 = malloc(25000)) == NULL) {
                       printf("Malloc error!\n");
                       exit(1);
               }
               if( (buffer2 = malloc(25000)) == NULL) {
                       printf("Malloc error!\n");
                       exit(1);
               }

               if( (fp = fopen(argv[1],"rb")) == NULL) {
                       printf("File not found!\n");
                       exit(1);
               }

               samprate = atoi(argv[2]);

               make_header(buffer1,samprate);
               make_header(buffer2,samprate);

               ct_voice_status = 0;

               do {
                       numread = fread(&buffer1[40],1,24999-40,fp);
                       while(ct_voice_status != 0)
                               if(kbhit()) quit();

                       ctvm_output((char far*)(buffer1+0x1a));

                       numread = fread(&buffer2[40],1,24999-40,fp);
                       while(ct_voice_status != 0)
                               if(kbhit()) quit();

                       ctvm_output((char far*)(buffer2+0x1a));
               } while(numread == 24999-40);
               quit();
       }
       else
       {
               printf("SIERRA plays long sample files\n");
               printf("usage: sierra <filename> <samprate>\n");
       }
}


make_header(unsigned char *buf,unsigned int samprate)
{
       struct HdrStruc Hdr;

       strcpy(Hdr.id,"Creative Voice File\x0\0x1A") ;

       Hdr.vocoffs = 0x1a;
       Hdr.version = 0x10a;
       Hdr.checkcode = 0x1129;

       Hdr.extblktype = 8;                      /* Extended block */
       Hdr.ext_blklen[0] = 04;
       Hdr.ext_blklen[1] = 00;
       Hdr.ext_blklen[2] = 00;
       Hdr.ext_tc = 65536 - (256000000L / (unsigned long)samprate);
       Hdr.ext_pack = 0;
       Hdr.ext_mode = 0;

       Hdr.voiceblktype = 1;
       Hdr.voice_blklen[0] = (25000-40) & 0xff;
       Hdr.voice_blklen[1] = ((25000-40) & 0xff00) >> 8;
       Hdr.voice_blklen[2] = ((25000-40) & 0xff00) >> 16;
       Hdr.voice_tc = 256 - (1000000L/(unsigned long)samprate);
       Hdr.voice_pack = 0;

       memcpy(buf,&Hdr,40);
       buf[24999] = 0;               /* Terminator */

}


quit()
{
       sb_voice_exit();
       fclose(fp);
       free(buffer1);
       free(buffer2);
}