home *** CD-ROM | disk | FTP | other *** search
/ 8bitfiles.net/archives / archives.tar / archives / genie-commodore-file-library / C128CPM / ZBRAW11.ARC / ZBRAW.C < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-16  |  4.1 KB  |  187 lines

  1. /*
  2. Z Blaster RAW player 1.1
  3.  
  4. (C) 1994 Steve Goldsmith
  5. All Rights Reserved
  6.  
  7. Play 4 bit PCM files with nibble swapping feature.
  8.  
  9. Compiled with HI-TECH C 3.09 (CP/M-80).
  10.  
  11. To compile with HI-TECH C and SG C Tools source on same disk use:
  12. C ZBRAW.C -LC128
  13. */
  14.  
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <stat.h>
  18. #include <string.h>
  19. #include <cpm.h>
  20. #include <hitech.h>
  21. #include <cia.h>
  22. #include <sid.h>
  23.  
  24. #define appMaxSize 65535
  25. #define appMinHz   3999
  26. #define appMaxHz   15001
  27.  
  28. void disphelp(void);
  29. void disptime(void);
  30. void starttimer(ulong Hz);
  31. void play(void);
  32. void swap4bit(void);
  33. long getfilesize(char *FileName);
  34. void load(char *FileName);
  35.  
  36. uchar *appBufPtr;           /* buffer to hold sample */
  37. FILE  *appRawFile;          /* raw file pointer */
  38. ulong appHz, appSize;
  39. static ciaTODRec appTODZero = {0,0,0,0};
  40. ciaTODRec appTODTime;
  41.  
  42. /*
  43. Main program parses command line, convert params, loads and plays RAW file
  44. through SID.
  45. */
  46.  
  47. main(int argc, char *argv[])
  48. {
  49.   appBufPtr = NULL; /* set buffer to nil */
  50.   puts("\nZ Blaster Raw Player 1.1 05/15/94 (C) 1994 Steve Goldsmith");
  51.   if (argc > 2)     /* make sure we have 3 or more params */
  52.   {
  53.     sscanf(argv[2],"%ld",&appHz);             /* convert hz param to long */
  54.     if (appHz > appMinHz && appHz < appMaxHz) /* check hz range */
  55.     {
  56.       bdos(45,0x0FE);                     /* bdos return and display error */
  57.       appSize = getfilesize(argv[1]);          /* get raw file size*/
  58.       if (appSize > 0 && appSize < appMaxSize) /* check file size */
  59.       {
  60.         appBufPtr = (uchar *) malloc(appSize); /* alloc buffer */
  61.         if (appBufPtr != NULL)
  62.         {
  63.           load(argv[1]);
  64.           if (strcmp(argv[3],"SN") == 0)
  65.           {
  66.             printf("Swapping nibbles, ");
  67.             settodcia(cia2,appTODZero);
  68.             swap4bit();
  69.             disptime();
  70.           }
  71.           printf("Playing, ");
  72.           settodcia(cia2,appTODZero);
  73.           play();
  74.           disptime();
  75.           free(appBufPtr);
  76.         }
  77.         else
  78.           puts("\nUnable to allocate memory.");
  79.       }
  80.       else
  81.         if (appSize > 0)
  82.           puts("\nFile too large.");
  83.         else
  84.           puts("\nUnable to open file.");
  85.     }
  86.     else
  87.       puts("\nHz value must be >= 4000 and <= 15000.");
  88.   }
  89.   else
  90.     disphelp();
  91. }
  92.  
  93. /*
  94. Display program help.
  95. */
  96.  
  97. void disphelp(void)
  98. {
  99.   puts("\nZBRAW {U:D:}filespec hertz {sn}");
  100.   puts("\nZBRAW FILENAME.RAW 8000 SN (swap nibbles before playing)");
  101.   puts("ZBRAW FILENAME.RAW 15000   (no nibble swap)");
  102. }
  103.  
  104. /*
  105. Display current time in HH:MM:SS format using CIA 2's TOD clock.
  106. */
  107.  
  108. void disptime(void)
  109. {
  110.   ciaTODStr TODStr;
  111.  
  112.   gettodcia(cia2,appTODTime);   /* get tod time */
  113.   todstrcia(appTODTime,TODStr); /* convert bcd time to string */
  114.   TODStr[8] = 0;                /* drop 1/10th seconds */
  115.   printf("%s\n",TODStr);        /* output time */
  116. }
  117.  
  118. /*
  119. Clear ICR and start timer A in continuous mode using Hz value.
  120. */
  121.  
  122. void starttimer(ulong Hz)
  123. {
  124.   setintctrlcia(cia2,ciaClearIcr);
  125.   settimeracia(cia2,timervalcia(Hz),ciaCPUCont);
  126. }
  127.  
  128. /*
  129. Play sample from buffer.
  130. */
  131.  
  132. void play(void)
  133. {
  134. #asm
  135.   di
  136. #endasm
  137.   starttimer(appHz);               /* start hz timer */
  138.   playzb4sid(appBufPtr,appSize);   /* play sample */
  139. #asm
  140.   ei
  141. #endasm
  142. }
  143.  
  144. /*
  145. Swap nibbles in buffer for inverted RAWs.
  146. */
  147.  
  148. void swap4bit(void)
  149. {
  150.   register ushort I;
  151.  
  152.   for(I = 0;  I < appSize; I++)
  153.     appBufPtr[I] = (appBufPtr[I] << 4) | (appBufPtr[I] >> 4);
  154. }
  155.  
  156. /*
  157. Return file size or 0 for error.
  158. */
  159.  
  160. long getfilesize(char *FileName)
  161. {
  162.   struct stat  StatRec;
  163.  
  164.   if (stat(FileName,&StatRec) == 0)
  165.     return(StatRec.st_size);
  166.   else
  167.     return(0);
  168. }
  169.  
  170. /*
  171. Load file into buffer and display load time.
  172. */
  173.  
  174. void load(char *FileName)
  175. {
  176.   if ((appRawFile = fopen(FileName,"rb")) != NULL)
  177.   {
  178.     printf("\nReading %s, %ld bytes, ",FileName,appSize);
  179.     settodcia(cia2,appTODZero);
  180.     fread(appBufPtr,sizeof(uchar),appSize,appRawFile);
  181.     fclose(appRawFile);
  182.     disptime();
  183.   }
  184.   else
  185.     puts("\nUnable to open file.");
  186. }
  187.